openui / open-ui

Maintain an open standard for UI and promote its adherence and adoption.
https://open-ui.org
Other
3.59k stars 191 forks source link

Extend popovertarget to support custom elements #1088

Open alexkeng opened 2 months ago

alexkeng commented 2 months ago

We (Edge) are interested in exploring how we can extend popover API to support custom elements.

Two questions:

  1. Is there any concern blocking us from extending popovertarget to support custom elements? The extension has been discussed in several forums (eg., #9110, #9109 in whatwg, and #302 in openui), so far no conclusion has been reached yet, but from what I can find, it seem the community in general believes that the API would be cleaner and easier to use if we could use popover/popovertarget declaratively cross root.
  2. If there is no major concern, with the new spec command/commmandfor anticipated to eventually replace popovertarget, do we still want to invest in adding new capabilities to popovertarget? or should we focus on pushing command/commandfor?

@mfreed7 @keithamus @sanketj

keithamus commented 2 months ago

Could you explain more about what you intend to extend? Is the proposal to allow custom elements to have the popovertarget or commandfor attributes? Or is it to extend the value of those attributes to penetrate shadow boundaries or something different?

My opinion is that commandfor has supportive positions from three major browsers and as such I think investment should go into that attribute rather than popovertarget. While we have yet to seek official positions on the deprecation of popovertarget (it would be a little early given commandfor hasn’t shipped) I remain confident that we will pursue deprecation soon.

alexkeng commented 2 months ago

The main goal is to make this example work:

<custom-button popovertarget="foo">Show Popover</custom-button>
<div id="foo" popover>This is a popover</div>

And if we think focusing on commandfor is the preferable direction, then the goal would be to make this work (while leave popovertarget unchanged):

<custom-button commandfor="foo" command="toggle-popover">Open Popover</custom-button>
<div id="foo" popover>Hello world</div>
sanketj commented 2 months ago

@gregwhitworth Could we add this issue to the agenda for the 9/12 meeting? I don't have permissions to add labels.

gregwhitworth commented 2 months ago

@sanketj done, also updated your privileges so that you should be able to modify the labels

css-meeting-bot commented 2 months ago

The Open UI Community Group just discussed Extend popovertarget to support custom elements.

The full IRC log of that discussion <bkardell_> masonf: There are two directions for the interest target api - a central one, or 'don't do that' - just provide the lower based API that make each thing possible.. Is it better to have a bunch of lower level APIs or a higher level API? If it is lower level it seems like, harder to get the accessibility right? wdyt?
<bkardell_> alexking: I am a web platform developer from Microsoft. The issue is about two things: it seems it has been a pinpoint for developers that they can't use a popover (or invoker) on a custom element...
<keithamus> q+
<gregwhitworth> q+
<gregwhitworth> ack keithamus
<masonf> q+
<bkardell_> keithamus: the reason we picked buttons is for accessibility - they already have the right interaction model, they are exposed the right way, they are the semantically relevant element - none of that is true for custom elements out of the box
<bkardell_> q+
<masonf> q+ scotto
<bkardell_> keithamus: if it gets commandfor does it automatically get all oft hose things
<gregwhitworth> ack masonf
<gregwhitworth> q--
<gregwhitworth> q-- gregwhitworth
<keithamus> q+
<masonf> https://github.com/whatwg/html/issues/9110#issuecomment-1516468633
<bkardell_> masonf: there's a couple of threads you linked to about this. my pref would be to solve the more general "how do you make a custom element that works like a regular button" - there is a suggestion from annevk that says you can put an element internals that just does all that. That makes sense to me. I think it would be better to solve generally and then use that here
<gregwhitworth> ack bkardell_
<gregwhitworth> ack scotto
<sanketj_> q+
<bkardell_> scotto: to echo mason, I understand the use case on custom elements - why use a button when I could use a button that smells like me, my own button. We need it to be a button - exposed as a button... you need to get the a11y semantics and so on. You can use JS right now to make it do all of the things, but you have to do a lot of work to make it accessible. Just allowing this on custom elements without getting the promise that what they
<bkardell_> are putting this on is a button means it won't work the majority of the time
<bkardell_> scotto: so that is the blocker. It should work on a button, but without that author requirement it is going to do harm and people are going to abuse this in ways that won't work for people
<gregwhitworth> ack keithamus
<bkardell_> alexking: We can agree with that, it makes sense to me
<bkardell_> keithamus: There are broader discussion since we don't have is="..", how do we make it into a button? It's not just buttons tho - those are common, so you wind up talking about it, but there are things like tables and lists where you can't have a custom <tr> or <li> because they can't be parsed that way
<bkardell_> keithamus: maybe this is too abstract but maybe we need to look at how to open the parser to how we can interleave them, or maybe make some kind of behavior you can mix in... button has the behavior of my button - they are compositional - we discussed some of these ideas at TPAC last year... the internals, I feel like that's ok, but we're going to wind up with a list of those
<gregwhitworth> ack sanketj_
<masonf> q+
<bkardell_> sanketj_: this is fantastic feedback - the element internals thing is definitely something to go back and look at. This whole discussion is about button and then making the custom element act like a button, but is it specifically the point that it is button and button like, or is it... like, hypothetically if you wanted to use input type checkbox as a popover host - are there things you could to do enable that while still supporting all of
<bkardell_> the rest?
<gregwhitworth> ack masonf
<bkardell_> masonf: are you saying it's not just custom elements, could we make even input type checkbox as a thing that does a popover... we talked about this while designing it. Everything else that isn't a button is different, that's why we didn't do that - they would have other issues
<bkardell_> masonf: there is one implementer who is against builtin custom elements but I agree that it is the better solution
<bkardell_> masonf: some browsers ship this - just one doesn't.
<bkardell_> sanketj_: you mean like extends? That too had it's limitation, you wouldn't be able to attach a shadow Dom, it still doesn't work
<bkardell_> masonf: I'm not saying it fixes all problems, I'm just saying a more general thing - a way that makes it behave as much like a built in would be ideal
chrisdholt commented 2 months ago

The Open UI Community Group just discussed Extend popovertarget to support custom elements.

The full IRC log of that discussion

masonf: There are two directions for the interest target api - a central one, or 'don't do that' - just provide the lower based API that make each thing possible.. Is it better to have a bunch of lower level APIs or a higher level API? If it is lower level it seems like, harder to get the accessibility right? wdyt? alexking: I am a web platform developer from Microsoft. The issue is about two things: it seems it has been a pinpoint for developers that they can't use a popover (or invoker) on a custom element... q+ q+ ack keithamus q+ keithamus: the reason we picked buttons is for accessibility - they already have the right interaction model, they are exposed the right way, they are the semantically relevant element - none of that is true for custom elements out of the box q+ q+ scotto keithamus: if it gets commandfor does it automatically get all oft hose things ack masonf q-- q-- gregwhitworth q+ https://github.com/[whatwg/html/issues/9110](https://github.com/whatwg/html/issues/9110)#issuecomment-1516468633 masonf: there's a couple of threads you linked to about this. my pref would be to solve the more general "how do you make a custom element that works like a regular button" - there is a suggestion from annevk that says you can put an element internals that just does all that. That makes sense to me. I think it would be better to solve generally and then use that here ack bkardell_ ack scotto q+ scotto: to echo mason, I understand the use case on custom elements - why use a button when I could use a button that smells like me, my own button. We need it to be a button - exposed as a button... you need to get the a11y semantics and so on. You can use JS right now to make it do all of the things, but you have to do a lot of work to make it accessible. Just allowing this on custom elements without getting the promise that what they are putting this on is a button means it won't work the majority of the time scotto: so that is the blocker. It should work on a button, but without that author requirement it is going to do harm and people are going to abuse this in ways that won't work for people ack keithamus alexking: We can agree with that, it makes sense to me keithamus: There are broader discussion since we don't have is="..", how do we make it into a button? It's not just buttons tho - those are common, so you wind up talking about it, but there are things like tables and lists where you can't have a custom or
  • because they can't be parsed that way keithamus: maybe this is too abstract but maybe we need to look at how to open the parser to how we can interleave them, or maybe make some kind of behavior you can mix in... button has the behavior of my button - they are compositional - we discussed some of these ideas at TPAC last year... the internals, I feel like that's ok, but we're going to wind up with a list of those ack sanketj_ q+ sanketj_: this is fantastic feedback - the element internals thing is definitely something to go back and look at. This whole discussion is about button and then making the custom element act like a button, but is it specifically the point that it is button and button like, or is it... like, hypothetically if you wanted to use input type checkbox as a popover host - are there things you could to do enable that while still supporting all of the rest? ack masonf masonf: are you saying it's not just custom elements, could we make even input type checkbox as a thing that does a popover... we talked about this while designing it. Everything else that isn't a button is different, that's why we didn't do that - they would have other issues masonf: there is one implementer who is against builtin custom elements but I agree that it is the better solution masonf: some browsers ship this - just one doesn't. sanketj_: you mean like extends? That too had it's limitation, you wouldn't be able to attach a shadow Dom, it still doesn't work masonf: I'm not saying it fixes all problems, I'm just saying a more general thing - a way that makes it behave as much like a built in would be ideal
  • This is a great conversation and I was sorry to yet again miss - Thursdays are brutal for meetings these days :).

    In terms of built-ins, that could be an option, but given that there is an implementor who is fundamentally opposed, it's not something that can scale well as a solution. The lack of a shadow root aside, if we had broad agreement from implementors, I would be much more open to this as a direction.

    While the initial conversation is around popover, that's more a singular instance of the overall problem IMO. Consider the <label></label> element - there is literally no way to get label semantics outside of using that element. If we stick it in a shadow root, we've got cross-root aria problems (solved but not scaled/shipped). You also have the reality of creating layers of elements (element w/ shadow root => semantic element) where one would suffice...at scale and as a convention, this creates increased cost from a performance standpoint. Built-ins help solve this as well, but again, there is an implementor that has signaled they are fundamentally opposed and will not implement.

    In terms of using element internals, both of these use cases (and more) could be solved if there were a way to inherit the behaviors and semantics of a native element as part of that API. It seems like that's what was potentially proposed, and if it's feasible, could be a good way to ensure that something signaling as a button performs as a button. It's possible that could help solve the long overdue issue where custom element buttons can't act as submit buttons: https://github.com/WICG/webcomponents/issues/814.

    elementInternals.extends = "button" or elementInternals.implements = "label" or whatever is the best semantic would be a welcome addition.

    Ultimately, my growing concern is that while the declarative API's that are being iterated on are helpful for part of the web platform, they leave out a first-class citizen in Web Components, which is the web platforms design for componentization - a central delivery method of most design systems these days. If our goal is to advance the web platform, it seems like we need to keep an eye on how these solutions scale to it's own component model. Without that as a core consideration, it feels as though we're not delivering on a holistic solution.

    alexkeng commented 2 months ago

    adding more details for the TPAC discussion: (a full explainer will be published soon)

    Goal To enable the following example:

    <custom-button popovertarget="foo">Show Popover</custom-button>
    <div id="foo" popover>This is a popover</div>

    Proposed Solution Introduce a new attribute type to ElementInternals. The example below shows how an autonomous custom element can define itself as a button using ElementInternals.type = "button", which enables it to have popovertarget/popovertargetaction capability (i.e., when the custom element is clicked, the associated popover element will perform the action defined by popovertargetaction):

    class CustomButton extends HTMLElement {
      #internals;
      constructor() {
        super();
        this.#internals = this.attachInternals();
        this.#internals.type = "button";
      }
    }

    ElementInternals.type Values ElementInternals.type will support only button for now. Future expansions to include additional types, such as submit, label, or types derived from other form elements (see the full list at the end) are anticipated, but it'll be discussed/addressed separately.

    Behaviors Accessibility When ElementInternals.type is assigned, the ARIA attributes of ElementInternals will be implicitly assigned accordingly. For example, if ElementInternals.type = "button" and ElementInternals.role is null, the custom element will behave as if ElementInternals.role = "button". If ElementInternals.role is assigned, it will take precedence over the default role derived from ElementInternals.type. If the role attribute on the custom element is assigned, it’ll take precedence over ElementInternals.role.

    Focus When ElementInternals.type = "button", the custom element becomes focusable.

    PreventDefault The popover behavior can be suppressed if the author calls preventDefault() in the click event handler of the custom element. This aligns with how <button popovertarget> handles preventDefault().

    formAssociated The attribute can be either true or false.

    ElementInternals.type is one type and the custom built-in element extends another This case is not possible at the moment as ElementInternals does not support custom built-in elements.

    Alternative solutions ElementInternals.button = button Pros: TBD Cons: the extensibility is limited

    <custom-button role=button> Pros: declarative Cons: backward compatibility. The attribute is designed/used with ARIA in mind. The new behavior is not necessarily what the authors want.

    Full list of potential values for ElementInternals.type NOTE: this is NOT directly part of the custom button + popovertarget solution. The list is to give readers a sense of how the .type API could evolve. Each individual element would need separate discussion and our solution to button shouldn't be blocked by the progress of other element.

    Derived from <input type=*>:

    Derived from other form elements:

    scottaohara commented 2 months ago

    If type took precedent over role, that’d be the opposite of how role is supposed to work.

    Role should still be allowed to modify the role of the element, but the element should still have all the behaviors / functionality of the type that was declared.

    chrisdholt commented 2 months ago

    If type took precedent over role, that’d be the opposite of how role is supposed to work.

    Role should still be allowed to modify the role of the element, but the element should still have all the behaviors / functionality of the type that was declared.

    I would expect that element internals would set the default for the element and not take precedence over declarative syntax. Just as native HTML elements have default semantics which can have precedence taken by role, custom elements have this today (constrained) with ElementInternals. If I define the default role as "button" for my custom element with ElementInternals and then in HTML land add role="menuitem", the role is menuitem. I wouldn't expect that to change with "subclassing" here.

    Just a note, but speaking specifically of button, this would solve a very longstanding issue and pain point with form associated buttons in that they cannot be a submit button: https://github.com/WICG/webcomponents/issues/814. Building off the very primitive example provided by Domenic, using elementInternals would take the strawman proposal slightly further but allow reusable, encapsulated, semantically correct, and likely more performant custom elements to be delivered at scale for design systems.

    In the Webkit position on customized built-in elements, they brought up some very fair concerns about customized built-ins that this proposal may be able to address as well: https://github.com/WebKit/standards-positions/issues/97#issuecomment-1328880274

    scottaohara commented 2 months ago

    @chrisdholt yup that's what i'd expect. reading through this again, maybe i misread how the role/type were taking precedent over each other? but sounds like we agree, so if i did read that wrong, never mind me :)

    alexkeng commented 2 months ago

    yes, to clarify, the order of precedence is <custom-button role=*> > ElementInternals.role > ElementInternals.type

    sanketj commented 2 months ago

    Pasting in minutes from the ad-hoc extension of the joint WHATWG-CSSWG-OpenUI CG session:

    Extend popovertarget to support custom elements github: openui/open-ui#1088 (comment)

    sanketj: use case we're looking to enable is popovertarget on custom elements

    sanketj: allow authors using custom element to ?? without any script

    sanketj: we believe the proposal can be extended to other types of form control behavior

    sanketj: proposal leverages element internals

    sanketj: give the cusotm element button-type button behavior, support things like popovertarget/action

    sanketj: also a11y things, if no role defined

    sanketj: easy opt-in to communicate to the browser that this custom element should have button-type behavior on the page

    chrishtr: so if clicked, will open popover?

    sanketj: behavios just like

    EisenbergEffect commented 2 months ago

    Can we get a declarative mechanism for type so that we can use it in combination with server renderered shadow dom? In general, we need declarative mechanisms for the internals APIs.

    sanketj commented 2 months ago

    Can we get a declarative mechanism for type so that we can use it in combination with server renderered shadow dom? In general, we need declarative mechanisms for the internals APIs.

    I think we would need declarative custom elements to unlock those benefits. Right?