-
-
Notifications
You must be signed in to change notification settings - Fork 2k
refactor: Restrict aria roles by element type #4607
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
Conversation
3bb1113 to
4fb26f6
Compare
e52b2b0 to
29a30e0
Compare
| alt?: Signalish<string | undefined>; | ||
| coords?: Signalish<string | undefined>; | ||
| download?: Signalish<any>; | ||
| href?: Signalish<string | undefined>; |
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.
When an incorrect role is applied, if we're restricting via more complex intersection types, our Signalish helper seems to cause error messages to blow up if the particular intersection key can be found on the base interface.
This isn't an issue in all cases, but to be somewhat consistent, I've stripped these from the base interfaces. Take the following example:
<input type="checkbox" role="slider" /> // I'm invalid!With type?: Signalish<HTMLInputTypeAttribute | undefined> set, you get this error:
Type '{ type: "checkbox"; role: "slider"; }' is not assignable to type 'InputHTMLAttributes<HTMLInputElement>'.
Types of property 'type' are incompatible.
Type '"checkbox"' is not assignable to type '(Signalish<HTMLInputTypeAttribute | undefined> & Signalish<"button">) | (Signalish<HTMLInputTypeAttribute | undefined> & Signalish<...>) | (Signalish<...> & Signalish<...>) | (Signalish<...> & Signalish<...>) | (Signalish<...> & Signalish<...>)'. (tsserver 2322)
Without, you get this:
Type '{ type: "checkbox"; role: "slider"; }' is not assignable to type 'InputHTMLAttributes<HTMLInputElement>'.
Types of property 'type' are incompatible.
Type '"checkbox"' is not assignable to type 'Signalish<"button"> | Signalish<"image"> | Signalish<"range"> | Signalish<"reset"> | Signalish<"submit">'. (tsserver 2322)
Not an expert here, but the latter is a lot better.
src/jsx.d.ts
Outdated
| > & | ||
| AnchorAriaRoles; | ||
|
|
||
| interface PartialAreaHTMLAttributes<T> extends HTMLAttributes<T> { |
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.
Partial... might be a poor name, very much open to suggestions.
src/jsx.d.ts
Outdated
| // Spec states this branch is limited to "no `multiple` attribute AND no `size` attribute greater than 1". | ||
| // We can't really express that in TS though so we'll just ignore `size` for now. | ||
| //size?: never; |
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.
Worth calling out, we can't quite adhere to this spec but might be better than nothing. Can also just revert too, treat it like others we can't support via types and just allow any aria role.
|
@rschristian Let's target this one at |
|
Worried it might be breaking? I have no strong opinion either way, though with how verbose this is, it might make v11 look a bit messier. |
f66acff to
eef3c0d
Compare
baa98ab to
7d16078
Compare
JoviDeCroock
left a comment
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 should allow us to close out the #3853 issue I reckon
|
Hm, I don't think so. While this does take a big first step, it's limited to aria roles; it does not attempt to provide (restricted) types for any of the myriad of other aria attributes that are available. |
3a33142 to
8f273f2
Compare
8f273f2 to
9013f3b
Compare
d8906e9 to
28038c2
Compare
9013f3b to
1b72ced
Compare
1b72ced to
e1c440f
Compare
fa2875f to
3c4e804
Compare
|
Going to merge, everything seems good! |
* refactor: Restrict aria roles by element type * test: Add simple test for button roles * refactor: Make roles signalish * test: Add tests for `never` roles * refactor: Remove half-implemented select roles * fix: Ensure attributes w/ `never` roles can be constructed * refactor: Restrict more complex aria roles * refactor: Remove unused input type interface, fix select multiple type * fix: Ensure partials extend EventTarget * revert: Bring back input type attribute * test: Fix test typo * refactor: Remove 'generic' aria role from allowed values * refactor: Revise select multiple/size roles
* refactor: Restrict aria roles by element type * test: Add simple test for button roles * refactor: Make roles signalish * test: Add tests for `never` roles * refactor: Remove half-implemented select roles * fix: Ensure attributes w/ `never` roles can be constructed * refactor: Restrict more complex aria roles * refactor: Remove unused input type interface, fix select multiple type * fix: Ensure partials extend EventTarget * revert: Bring back input type attribute * test: Fix test typo * refactor: Remove 'generic' aria role from allowed values * refactor: Revise select multiple/size roles
* refactor: Restrict aria roles by element type * test: Add simple test for button roles * refactor: Make roles signalish * test: Add tests for `never` roles * refactor: Remove half-implemented select roles * fix: Ensure attributes w/ `never` roles can be constructed * refactor: Restrict more complex aria roles * refactor: Remove unused input type interface, fix select multiple type * fix: Ensure partials extend EventTarget * revert: Bring back input type attribute * test: Fix test typo * refactor: Remove 'generic' aria role from allowed values * refactor: Revise select multiple/size roles
* refactor: Restrict aria roles by element type * test: Add simple test for button roles * refactor: Make roles signalish * test: Add tests for `never` roles * refactor: Remove half-implemented select roles * fix: Ensure attributes w/ `never` roles can be constructed * refactor: Restrict more complex aria roles * refactor: Remove unused input type interface, fix select multiple type * fix: Ensure partials extend EventTarget * revert: Bring back input type attribute * test: Fix test typo * refactor: Remove 'generic' aria role from allowed values * refactor: Revise select multiple/size roles
* refactor: Restrict aria roles by element type * test: Add simple test for button roles * refactor: Make roles signalish * test: Add tests for `never` roles * refactor: Remove half-implemented select roles * fix: Ensure attributes w/ `never` roles can be constructed * refactor: Restrict more complex aria roles * refactor: Remove unused input type interface, fix select multiple type * fix: Ensure partials extend EventTarget * revert: Bring back input type attribute * test: Fix test typo * refactor: Remove 'generic' aria role from allowed values * refactor: Revise select multiple/size roles
* refactor: Restrict aria roles by element type * test: Add simple test for button roles * refactor: Make roles signalish * test: Add tests for `never` roles * refactor: Remove half-implemented select roles * fix: Ensure attributes w/ `never` roles can be constructed * refactor: Restrict more complex aria roles * refactor: Remove unused input type interface, fix select multiple type * fix: Ensure partials extend EventTarget * revert: Bring back input type attribute * test: Fix test typo * refactor: Remove 'generic' aria role from allowed values * refactor: Revise select multiple/size roles
* refactor: Restrict aria roles by element type * test: Add simple test for button roles * refactor: Make roles signalish * test: Add tests for `never` roles * refactor: Remove half-implemented select roles * fix: Ensure attributes w/ `never` roles can be constructed * refactor: Restrict more complex aria roles * refactor: Remove unused input type interface, fix select multiple type * fix: Ensure partials extend EventTarget * revert: Bring back input type attribute * test: Fix test typo * refactor: Remove 'generic' aria role from allowed values * refactor: Revise select multiple/size roles
Starting to take a look at #3853, seems role restriction should be simple enough for most elements. Spec Doc
Edit: Unfortunately there's some rare situations in which the spec should be ignored which presents an issue here: adding stricter types might dissuade people against using the correct role on some elements because the spec (and therefore our types) don't allow it. I'd like to guess that this occurs far less often than a user simply reaching for the wrong roles, however, hence this PR.
There are some roles that we can't limit via types, mainly due to descendant restrictions ("If not a descendant of
<article>,<aside>,<main>, ..., etc.) or specific attribute values ("Ifsizeattribute is greater than 1"), but this does get most elements/roles.With this I think I'm all typed out for the foreseeable future 😅