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

refactor: Restrict aria roles by element type #4607

Open
wants to merge 11 commits into
base: main
Choose a base branch
from

Conversation

rschristian
Copy link
Member

@rschristian rschristian commented Dec 13, 2024

Starting to take a look at #3853, seems role restriction should be simple enough for most elements. Spec Doc

Edit: Unfortunately I've been told these roles are a bit iffy in reality... there's situations in which the spec should be ignored. This 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.


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 ("If size attribute is greater than 1"), but this does get most elements/roles.

With this I think I'm all typed out for the foreseeable future 😅

@coveralls
Copy link

coveralls commented Dec 13, 2024

Coverage Status

coverage: 99.615%. remained the same
when pulling f66acff on refactor/aria-role-subset
into bb68456 on main.

@rschristian rschristian force-pushed the refactor/aria-role-subset branch from 3bb1113 to 4fb26f6 Compare December 15, 2024 21:23
@rschristian rschristian changed the title refactor: Restrict aria roles by element type refactor: Restrict aria roles by element type (simple cases only) Dec 16, 2024
@rschristian rschristian marked this pull request as ready for review December 16, 2024 01:56
@rschristian rschristian force-pushed the refactor/aria-role-subset branch from e52b2b0 to 29a30e0 Compare December 16, 2024 02:04
@rschristian rschristian marked this pull request as draft December 16, 2024 21:44
alt?: Signalish<string | undefined>;
coords?: Signalish<string | undefined>;
download?: Signalish<any>;
href?: Signalish<string | undefined>;
Copy link
Member Author

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> {
Copy link
Member Author

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.

Comment on lines +2509 to +2511
// 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;
Copy link
Member Author

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 rschristian changed the title refactor: Restrict aria roles by element type (simple cases only) refactor: Restrict aria roles by element type Dec 17, 2024
@rschristian rschristian marked this pull request as ready for review December 17, 2024 05:10
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.

2 participants