Skip to content
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

Change classMap to set classes correctly for SVGs #1070

Merged
merged 8 commits into from Jan 24, 2020

Conversation

jridgewell
Copy link
Contributor

This fixes the classMap directive inside SVGs by avoiding className, and avoiding classList which isn't supported in IE11 SVGs.

This is another attempt at #932. I've reused @AdamVig's commit to give him the commit stats.
Fixes #930.

jridgewell and others added 3 commits January 20, 2020 18:48
Co-Authored-By: Adam Vigneaux <adam@adamvig.com>
Co-Authored-By: Adam Vigneaux <adam@adamvig.com>
@googlebot
Copy link

All (the pull request submitter and all commit authors) CLAs are signed, but one or more commits were authored or co-authored by someone other than the pull request submitter.

We need to confirm that all authors are ok with their commits being contributed to this project. Please have them confirm that by leaving a comment that contains only @googlebot I consent. in this pull request.

Note to project maintainer: There may be cases where the author cannot leave a comment, or the comment is not properly detected as consent. In those cases, you can manually confirm consent of the commit author(s), and set the cla label to yes (if enabled on your project).

ℹ️ Googlers: Go here for more info.

@jridgewell
Copy link
Contributor Author

Hi @AdamVig, can you comment "@googlebot I consent" so we can reuse your commit?

@AdamVig
Copy link
Contributor

AdamVig commented Jan 21, 2020

@googlebot I consent

@googlebot
Copy link

CLAs look good, thanks!

ℹ️ Googlers: Go here for more info.

@AdamVig
Copy link
Contributor

AdamVig commented Jan 21, 2020

@jridgewell Thanks for revisiting this, it has been on my list since my original pull request.

Copy link
Collaborator

@justinfagnani justinfagnani left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, thanks a lot for taking this up @jridgewell!

I'm a little bummed about just dropping down to setting the class attribute now, but we never measure the supposed perf benefit of manipulating the classList directly, so it was an unverified claim. There should be some benefit left from the deep dirty-checking on the classInfo object.

Given this is just using setAttribute() I think there's a simplification here, so I'd like to see that tried first.

@@ -23,20 +22,21 @@ export interface ClassInfo {
* Stores the ClassInfo object applied to a given AttributePart.
* Used to unset existing values when a new ClassInfo object is applied.
*/
const previousClassesCache = new WeakMap<Part, Set<string>>();
const previousClassesCache = new WeakMap<Part, Map<string, unknown>>();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this deserves a comment explaining what's happening, since the key in the Map is now either a class name or a static string...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

src/directives/class-map.ts Show resolved Hide resolved
@justinfagnani justinfagnani self-assigned this Jan 21, 2020
const cssInfo = {foo: 0, bar: true, zonk: true};
render(svg`<circle class="${classMap(cssInfo)}"></circle>`, container);
const el = container.firstElementChild!;
assert.isFalse(el.classList.contains('foo'));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

classList needs to be replaced with className.split(' ')... for SVG elements on IE

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

assert.isFalse(el.classList.contains('aa'));
assert.isTrue(el.classList.contains('bb'));
renderClassMapStatic({});
assert.isFalse(el.classList.contains('aa'));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems bad to me, but I guess it's current behavior. I'd much rather have the behavior that the output is the same for the same input, regardless of order of calls (with the exception of out-of-band classList mutations).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. Wanna cut a v2 anytime soon?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not particularly :)

I'd want to move further on SSR and add a whole lot of benchmarks before that at least.

@jridgewell jridgewell merged commit 4c341ae into master Jan 24, 2020
@jridgewell jridgewell deleted the fix-class-map-svg-compat-2 branch January 24, 2020 01:02
@nicolejadeyee nicolejadeyee mentioned this pull request Feb 11, 2020
15 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

classMap directive does not work with SVG
4 participants