whatwg / html

HTML Standard
https://html.spec.whatwg.org/multipage/
Other
8.12k stars 2.67k forks source link

Add an imperative way to set popover invoker relationships #10442

Open mfreed7 opened 4 months ago

mfreed7 commented 4 months ago

What is the issue with the HTML Standard?

The popovertarget attribute "connects" a button to a popover, in three key ways:

It seems like there should be an imperative way to create that relationship. This was agreed in OpenUI in https://github.com/openui/open-ui/issues/1024#issuecomment-2037882914, and at the WHATWG/CSSWG/OpenUI task force in https://github.com/whatwg/html/pull/9144#issuecomment-2195095228.

This issue proposes a simple addition to the three popover APIs:

popover.togglePopover({invoker: element});
popover.showPopover({invoker: element});
popover.hidePopover({invoker: element});

Some questions:

scottaohara commented 4 months ago

can element be any element?

while exposing an aria-expanded state won't work on just any element unless it has an implicit or explicit ARIA role that allows for that state to be exposed - maybe this is a way to allow those who have been asking to use popover with their custom button elements the ability to identify their invoker, and if 'any element' is allowed, then `div[role=button]' would be too.

are there a11y implications of only creating the ARIA relationships at invocation time? The declarative API can set those up even before the button is clicked.

depends on exactly what you mean by this. but for instance, if you just have a button in a web page, and an aria-expanded property is only added to it once clicked, then the state change is generally not exposed to screen readers by browsers. from what i remember, there is no signal for the AT to listen for simply by the addition of the expanded property to a button - but rather, only if the expanded property exists and its state changes. again, that's at least what i recall - would have to do some testing to determine if that's changed at all.

mfreed7 commented 4 months ago

@scottaohara and I just discussed this, and I broadly agree with the points made above. One interesting conclusion:

Perhaps that's as it should be for an imperative API - we can try to fix things up as best as possible, but typically the author will need to make sure they're doing things correctly. E.g. manually setting role=button for a button-like custom element.

lukewarlow commented 4 months ago

This either needs element to be a HTMLElement (or child of), or some of the spec will need updating as currently popover invoker is an HTMLElement (see https://html.spec.whatwg.org/#popover-invoker)

togglePopover isn't quite a simple addition because of the optional force boolean argument. We'd have to either take the compat hit of changing the argument type, or do the slightly unfortunate thing of allowing an argument of type boolean or dict where the dict has invoker and force as optional members.

mfreed7 commented 1 month ago

Note https://github.com/whatwg/html/pull/9144#issuecomment-2338853174

lukewarlow commented 1 month ago

Out of curiosity what's the API design looking like specifically for togglePopover given it already has the optional Boolean argument?

mfreed7 commented 1 month ago

Out of curiosity what's the API design looking like specifically for togglePopover given it already has the optional Boolean argument?

I'm open to whatever makes the most sense. I implemented (in Chrome) the easiest/most compatible thing:

boolean togglePopover(optional (TogglePopoverOptions or boolean) options);

i.e. either the boolean or the options bag (which contains the boolean).

mfreed7 commented 1 week ago

I put up a spec PR containing the proposal detailed above: https://github.com/whatwg/html/pull/10728