-
-
Notifications
You must be signed in to change notification settings - Fork 635
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
Account for polymorphic components in getElementType
#945
Conversation
I'd be open to being convinced, but currently I think polymorphic components are a bad practice that makes code harder to maintain and understand, and I'm not sure it's a good idea to enable that by supporting it here. |
I agree that polymorphic components can make code harder to follow but I like that they allow teams to utilize design system components without having to compromise on the semantics for their specific use case. If a design system has a component that is almost perfect but needs a small semantic change to improve accessibility, should we make the team re-write the component? Or make the team request/implement a design system change? -- Requesting a change could take a long time and re-writing the component adds tech debt. Though, it might be nice to implement the change on the design system side polymorphic components give teams an opportunity to make quick semantic change to a component that could prevent issues for many users. |
I guess I'd expect each component in the design system to be for a single semantic use case - and if someone wants a new one, the design system could provide a new wrapping component for it (with a refactor so that both components wrapped an internal one with a prop to control the semantics). That would allow you to lint for, and clearly convey, that each component has a single semantic use. Is that approach not workable? |
My other thought... This pattern is so widely used: that it feels essential to account for polymorphic components. I think even if the setting was defaulted off, allowing for polymorphic support would benefit a wide range of jsx-a11y users. |
TODO:
|
Those are indeed widely-used systems - although I remain convinced that polymorphic components are a harmful pattern, being able to lint for their usage reduces harm, so let's go forward with this as an option, defaulted to false, that somehow notes in the readme that polymorphic components are a pattern to be avoided. |
getElementType
getElementType
I tested |
@ljharb Do you know when the test fixes will be merged? |
9d12654
to
8e935a9
Compare
They're now merged, and I rebased this PR and cleaned up the read me a bit. Taking another look. |
8e935a9
to
9eeebaf
Compare
Codecov Report
@@ Coverage Diff @@
## main #945 +/- ##
=======================================
Coverage 99.02% 99.02%
=======================================
Files 105 105
Lines 1640 1642 +2
Branches 576 578 +2
=======================================
+ Hits 1624 1626 +2
Misses 16 16
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks good; let's also add some tests in various rules that guarantee we're exercising getElementType indirectly?
98776d9
to
377026a
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM overall!
@ljharb sorry to re-ping you! I just wanted to check in and see if we can move forward on this PR. |
0799c59
to
fffb05b
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks for the ping, sorry for the delay
What
Currently,
getElementType
does not have a great way for testing the semantics of polymorphic components. This PR is intended to fix that.By default, the
getElementType
will search for a node's attributes for the propas
. Ifas
is setgetElementType
will return the value ofas
.flexibility
Some libraries do not use
as
to set the semantics of their polymorphic components (e.g. perhaps they usehtml
, orasChild
). In order to allow for flexibility, I introduced a new settingpolymorphicPropName
that allows users to configure the name of the polymorphic prop.Questions
A couple of thoughts came up when making this pr: