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

fix: setting styles with attribute for svg elements #565

Merged
merged 5 commits into from
Jan 15, 2022

Conversation

Confidenceman02
Copy link
Contributor

@Confidenceman02 Confidenceman02 commented Jan 12, 2022

fixes #564

Context

17.0.4 fixed a bug that was causing DOM exceptions by changing the way we were updating styles to use VirtualDom.attribute "class" ... instead of VirtualDom.property "className" ....

This was due to svg elements not liking the property "className" method which is a known phenomenon in the html spec.

Some random stack overflow post that is probably true: https://stackoverflow.com/questions/43750395/how-to-add-a-class-in-a-svg-element-circle-using-javascript

However, whilst the pesky exception was fixed there were some unintended side effects you can read about in this issue #564.

Work completed

  • Revert the extractUnstyledAttribute function to behave as it used to for html elements.
  • Create an extractUnstyledAttributeNS function to set styles for svg nodes using the attribute method.

The above changes focus on only fixing what was breaking, rather than changing the implementation for all elements.

Styled examples showing svg and html elements with their respective class properties.

NS

Jaime Terreu and others added 3 commits January 13, 2022 01:46
NodeNS elements require a slightly different method to consistently
update styles than Html elements.

Specifically, NodeNS elements will require the atttribute property with
the "class" string.
Copy link
Contributor

@tesk9 tesk9 left a comment

Choose a reason for hiding this comment

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

I made a PR adding a couple of tests against this PR -- hope that's alright with you!

This approach looks reasonable to me as long as:

  • we're sure that namespaced nodes will only ever be SVG nodes
  • we are fine with a runtime exception occurring if a user every accidentally uses Html.Styled.Attributes.class with an Svg.Styled node (Using Html.Styled.Attributes's css breaks Svg.Styled at runtime #503 doesn't want to get closed, I guess!)
  • we are fine with elm-test's html helpers not being able to test SVG classes (for this one, I think we either need to fix it in elm-test, or we have to be fine with it)

, btn [ onClick DoSomething ] [ text "Click me!" ]
, StyledSvg.svg [ SvgAttribs.css [], SvgAttribs.width "100", SvgAttribs.height "100" ] [ StyledSvg.circle [ SvgAttribs.cx "50", SvgAttribs.cy "50", SvgAttribs.r "40" ] [] ]
, StyledSvg.svg [ SvgAttribs.class "some-whatever-NS-class", SvgAttribs.css [ opacity (num 0.5) ], SvgAttribs.width "100", SvgAttribs.height "100" ] [ StyledSvg.circle [ SvgAttribs.cx "50", SvgAttribs.cy "50", SvgAttribs.r "40" ] [] ]
Copy link
Contributor

Choose a reason for hiding this comment

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

The circle is a bit low-contrast against the background.

How would you feel about using bright red instead?

Suggested change
, StyledSvg.svg [ SvgAttribs.class "some-whatever-NS-class", SvgAttribs.css [ opacity (num 0.5) ], SvgAttribs.width "100", SvgAttribs.height "100" ] [ StyledSvg.circle [ SvgAttribs.cx "50", SvgAttribs.cy "50", SvgAttribs.r "40" ] [] ]
, StyledSvg.svg
[ class "some-whatever-NS-class"
, SvgAttribs.css [ color (rgb 256 0 0) ]
, SvgAttribs.width "100"
, SvgAttribs.height "100"
]
[ StyledSvg.circle
[ SvgAttribs.cx "50"
, SvgAttribs.cy "50"
, SvgAttribs.r "40"
, SvgAttribs.fill "currentcolor"
]
[]
]

Copy link
Contributor Author

@Confidenceman02 Confidenceman02 Jan 13, 2022

Choose a reason for hiding this comment

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

This is fantastic, many thanks for opening the PR I shall review and merge post haste.

On your points -

  • The changes I made definitely only ever deal with NodeNS and KeyedNodeNS types so I think we are good there.
    We are limited in testing this in the package with elm-test but I don't see why we couldn't test with something like playwright. I did this for elm-select and it works great. Perhaps a little overkill but we could raise an issue to explore how to test the svg class behaviour.

  • The current runtime exception regarding the svg styles already exists so whilst we haven't fixed the issue, we haven't made it worse.

  • I think it makes sense to patch elm-test to handle svg classes. Was it ever actually working correctly? Patching elm-test does address the testing situation in the first point however so win win.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yay, glad you like it!

  • I haven't ever used playwright -- looks great! Maybe Percy could be neat to have in place too?

  • True! 🙈

  • No, as far as I can tell, I don't think elm-test with SVG classes was ever working 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Cool! Changed to a high contrast colour as per your request, I guess we just wait for @rtfeldman to have a look.
I'll open an issue in elm-test to patch svg testing. Never looked at that project so should be fun!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Looks like an issue in elm-test exists elm-explorations/test#134.

Had a super quick look at the project and I think a fix is relatively straightforwards. (I will regret saying that)

Copy link
Owner

@rtfeldman rtfeldman left a comment

Choose a reason for hiding this comment

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

This looks good to me! Thanks for the review and the extra improvements @tesk9!

@Confidenceman02 are you ready for merge and release, or want to make any final changes?

@Confidenceman02
Copy link
Contributor Author

Happy to merge and release @rtfeldman.

@rtfeldman rtfeldman merged commit 818ffaf into rtfeldman:master Jan 15, 2022
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.

17.0.4 generated class name is not added when there's also a set class name
3 participants