-
Notifications
You must be signed in to change notification settings - Fork 31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Svg and Canvas attribute control #71
Comments
Hey @maxblee Yea good idea. I like the second approach since it covers future use cases. And probably just |
Here's an example. Seems to work: https://svelte.dev/repl/6bb9adafd7f545f181335b680e3b0409?version=3.48.0 |
@mhkeller Sorry, I meant to respond/get to this sooner but got lost in other things. I've had a chance to take an initial stab at this. Similar to you, my first thought was to use spread props. But for some reason, they seem to create some sort of conflict with the scaleCanvas utility. I saw this when I built the static SvelteKit site and viewed the Voronoi example. When I didn't include the spread attributes <canvas class="layercake-layout-canvas" style="width: 770px; height: 170px; position: absolute; inset: 10px 5px 20px 25px;" width="1540" height="340"></canvas> But with the spread attributes, the width and height stayed at 100%, even though <canvas class="layercake-layout-canvas" style="width: 100%; height: 100%; position: absolute; inset: 10px 5px 20px 25px;" width="1540" height="340"></canvas> I think the underlying issue has to do something with how Svelte tries to handle spread attributes — there's a seemingly similar open issue on Svelte — but I'm not familiar enough with the internals of Svelte to know for sure. The workaround I wound up going with was to use the element bindings: export let attrs = {};
$: if (element) {
Object.entries(attrs).forEach(d => element.setAttribute(...d));
} This doesn't break any of the existing visuals and seems to work on a minimal example. I'm adding a pull request now using that syntax. Not sure if it's the best way to go, but hopefully it's helpful. |
Thanks for doing the research. Maybe set ‘attrs’ to null at first and add ‘attrs’ in the if statement so that doesn’t run if unset and also maybe gives svelte more of a hint to run that block when ‘attrs’ changes? It may also be good to add a check to see if the attributes value differs from what exists. That way if you change the prop, it won’t have the chance to trigger any unnecessary repaints. I don’t have a specific use case but i feel like those bugs are subtle and this may be a good way to avoid unnecessary code execution. |
Makes sense. So something like this?: export let attrs = null;
$: if (element && attrs) {
Object.entries(attrs)
// hasAttribute check may be necessary because getAttribute can return an empty string in some browsers
// and some attributes (e.g. hidden) may be set to empty strings
// https://developer.mozilla.org/en-US/docs/Web/API/Element/getAttribute#non-existing_attributes
.filter(d => !element.hasAttribute(d[0]) || element.getAttribute(d[0]) !== d[1])
.forEach(d => element.setAttribute(...d))
} |
I would avoid the extra filter loop and include an |
That sounds good. I've added a pull request with these changes. Let me know if you need any changes. |
I might have overlooked the reason, but why not pass |
@techniq would that fix this issue? |
@mhkeller It does not. I have not ran into this issue myself (surprisingly) and have used The example REPL uses <img src="//unsplash.it/600/400" width="100%" alt="Alt text" {...{}} > Sorry for the noise and I see why |
Hi! I'm wondering, what is the status on this issue? I am also running up against a case where I want to add aria-label/ role/tabindex attributes to my SVG, and also a title (this goes inside the SVG) for screen reader accessibility. (Would you recommend creating a fork of the repo to do this in the meantime?) |
I think you don't have to go full-fork actually. You should be able to grab import { Svg } from 'layercake'; ...you can simply import it like a normal component import Svg from './Svg.svelte'; In terms of supporting this inside of the library, we have this PR but there was concern that allowing arbitrary props would break some of Svelte's optimizations. This use case is really helpful to know, though, and I think doing a PR for the most common attributes would be a good replacement. You're using these?
|
@maxblee what do you think about scaling down that PR to just the common accessibility labels instead of allowing arbitrary ones? |
(Perfect! That's what I ended up doing, but I wasn't sure if that was the best way.) Yes, exactly. And |
I see from the original comment there is |
I'm not sure it's possible to have My last message was a little unclear, though-- I meant |
Got it. Okay that should be easy, we'll do it as a separate slot like |
Hello, I was wondering if it might be possible to add attribute-level controls on the
<svg>
and<canvas>
elements (soSvg
,ScaledSvg
,Canvas
, andWebgl
). My most common use-case for this comes in wanting to add descriptions for svgs. In order to do this in HTML, I would write something along the lines ofCurrently, I should be able to add the
role
andaria-labelledby
attributes on theSvg
component by runningsetAttribute()
on the element bindings, but that feels a little bit clunky, especially for something I use fairly regularly. I'd prefer being able to write something along the lines ofor
If you'd be interested in this, let me know; I'd be happy to submit a pull request. Thanks!
The text was updated successfully, but these errors were encountered: