html icon indicating copy to clipboard operation
html copied to clipboard

Customizable `<select>` element

Open josepharhar opened this issue 2 years ago • 68 comments

This proposal includes parser, styling, and rendering changes to make the <select> element fully customizable by authors.

Explainer: https://open-ui.org/components/customizableselect

Spec PR: https://github.com/whatwg/html/pull/10548

Spec PRs (old):

  • https://github.com/whatwg/html/pull/10557
  • https://github.com/whatwg/html/pull/10586
  • https://github.com/whatwg/html/pull/10629
  • https://github.com/whatwg/html/pull/10633
  • https://github.com/whatwg/html/pull/10670

CSS spec PRs:

  • https://github.com/w3c/csswg-drafts/pull/10691
  • https://github.com/w3c/csswg-drafts/pull/10865
  • https://github.com/w3c/csswg-drafts/pull/10936
  • https://github.com/w3c/csswg-drafts/pull/10986

Accessibility PRs:

  • https://github.com/w3c/aria/pull/2369
  • https://github.com/w3c/aria/pull/2344
  • https://github.com/w3c/html-aria/pull/528
  • https://github.com/w3c/aria/pull/2360

Related HTML issues:

  • https://github.com/whatwg/html/issues/10520
  • https://github.com/whatwg/html/issues/10755
  • https://github.com/whatwg/html/issues/10762
  • https://github.com/whatwg/html/issues/10604
  • https://github.com/whatwg/html/issues/10876
  • https://github.com/whatwg/html/issues/10317
  • https://github.com/whatwg/html/issues/10310

Related CSS issues:

  • https://github.com/w3c/csswg-drafts/issues/10028
  • https://github.com/w3c/csswg-drafts/issues/10242
  • https://github.com/w3c/csswg-drafts/issues/10333
  • https://github.com/w3c/csswg-drafts/issues/10380
  • https://github.com/w3c/csswg-drafts/issues/10462
  • https://github.com/w3c/csswg-drafts/issues/10717
  • https://github.com/w3c/csswg-drafts/issues/10758
  • https://github.com/w3c/csswg-drafts/issues/10775
  • https://github.com/w3c/csswg-drafts/issues/10857
  • https://github.com/w3c/csswg-drafts/issues/10908
  • https://github.com/w3c/csswg-drafts/issues/10909
  • https://github.com/w3c/csswg-drafts/issues/11039
  • https://github.com/w3c/csswg-drafts/issues/11185

Standards positions:

  • https://github.com/mozilla/standards-positions/issues/1142
  • https://github.com/mozilla/standards-positions/issues/1060
  • https://github.com/WebKit/standards-positions/issues/414
  • https://github.com/mozilla/standards-positions/issues/1086
  • https://github.com/WebKit/standards-positions/issues/386

josepharhar avatar Sep 26 '23 22:09 josepharhar

@whatwg/forms

zcorpan avatar Sep 27 '23 07:09 zcorpan

I'd like to work on this issue @zcorpan

rohankaushal123 avatar Oct 18 '23 05:10 rohankaushal123

Thank you for bringing this proposal to the WHATWG Joey! I thought this would be a good opportunity to outline how colleagues and I feel about extending HTML in this area. In particular, we feel that new and existing form controls:

  • Should look the same as the operating system controls by default.
  • Should be fully styleable by web developers.
  • Should generally attempt to follow existing HTML element patterns.
  • Should not be redundant with existing HTML form controls.
  • Should work on a wide variety of platforms, including those with very small screens, no mouse or touch, etc.
  • Should be fully accessible.
  • Should not have any l10n or i18n shortcomings.

We understand that the select element can’t address a variety of scenarios due to parser limitations, but the select element could address them in combination with the datalist element. One of our big worries with complete duplication is that we end up not solving the problems with the existing controls and that the duplicated controls will have a variety of shortcomings the older controls did not have.

(Aside: https://github.com/whatwg/html/issues/5791 seems very related to this issue.)

annevk avatar Oct 19 '23 07:10 annevk

I think the explainer doesn't explain the need for a new element in a convincing way, because the multiple examples look addressable in the context of select: flags are addressable by emoji instead of image files, subheadings by the label attribute of optgroup, and color and font-level styling seems feasible without a new element.

(From off-explainer examples, I gather that the ambition level of the feature is much higher than the explainer makes appear.)

hsivonen avatar Oct 23 '23 15:10 hsivonen

Thanks for taking a look and discussing with colleagues Anne!

Should look the same as the operating system controls by default.

If we made appearance:none or a new appearance value like appearance:base as discussed here and here have a standardized style, then the default appearance:auto appearance could look the same as OS controls. That would be up to each UA to decide, as it is currently for all other form controls.

Should be fully styleable by web developers.

Agreed! Styleability is the primary driving use case for the current proposal.

Should generally attempt to follow existing HTML element patterns.

Agreed! Domenic left some feedback in this vein and we completely revamped the proposal to use existing HTML element patterns by using new tag names instead of slot and behavior attributes.

Should work on a wide variety of platforms, including those with very small screens, no mouse or touch, etc.

Agreed! Existing elements conform to this by letting the browser/OS provide a native picker which unfortunately can’t be styled or customized by web developers, right? It was recommended that we let the browser use native pickers for selectlist on mobile here. Ideally developers would be enabled to make their own pickers that work on these platforms when they want to as well.

Should be fully accessible.

Agreed! This is also a core requirement for the proposal. I have been working closely with accessibility experts participating in OpenUI to ensure that the accessibility mappings and keyboard behaviors follow the current best practices in ARIA.

Should not have any l10n or i18n shortcomings.

Agreed! The content is fully customizable/replaceable, so it should be localizable and internationalizable, right? The only thing I can think of is supporting right-to-left and vertical writing modes when rendering where the listbox popup goes, which is something we are doing for other form control elements and we can easily do via Anchor Positioning for selectlist. Is there anything else you had in mind here?

Should not be redundant with existing HTML form controls.

We understand that the select element can’t address a variety of scenarios due to parser limitations, but the select element could address them in combination with the datalist element

It sounds like yall would prefer to reuse the existing <select> element instead of creating a new tag name. Am I correct? As you mentioned, we can’t have the new content model exist inside the <select> element due to parser rules, but we could try to do something with how datalist works by adding attributes to <select> which refer to the other elements we want to use, which would perhaps look like <select list=my-listbox button=my-button>. However, I don’t think this would work well. This works for <input list=datalist> because datalists are just used to host text data without doing actual rendering. Since we are actually trying to render an authored <listbox> or <button> element inside the select/selectlist, we don’t have the machinery in the browser to render them from an arbitrary location in the DOM. ShadowDOM is perfect for this and is how existing builtin elements like details and summary work. It is also easier and more idiomatic (like <option> and <summary>) for the new listbox and button elements to know when they should have special behavior for selectlist because they can look at their parent context rather than having to keep track of which select element has an attribute pointing at them. A preview of the State of HTML survey I’ve seen also lists <datalist> as a top developer pain point, which perhaps alludes that the ergonomics of referring to a separate element aren’t great.

One of our big worries with complete duplication is that we end up not solving the problems with the existing controls and that the duplicated controls will have a variety of shortcomings the older controls did not have.

I agree that duplication should be avoided. Selectlist is not a complete duplicate of select. <select> can draw outside the bounds of the browser window, and <selectlist> has a completely different rendering model that allows full customization. If it were possible to shoehorn into <select> that would be nice, but it looks infeasible.

josepharhar avatar Oct 27 '23 23:10 josepharhar

I think the explainer doesn't explain the need for a new element in a convincing way, because the multiple examples look addressable in the context of select: flags are addressable by emoji instead of image files, subheadings by the label attribute of optgroup, and color and font-level styling seems feasible without a new element.

Thanks for the feedback! I am improving the examples here: https://github.com/openui/open-ui/pull/918

josepharhar avatar Oct 30 '23 18:10 josepharhar

flags are addressable by emoji instead of image files

I just wanted to point out Windows doesn't provide flag emojis so this specific example is a good use case for needing images not emoji.

lukewarlow avatar Oct 30 '23 18:10 lukewarlow

Existing elements conform to this by letting the browser/OS provide a native picker which unfortunately can’t be styled or customized by web developers, right?

If you use appearance:none they can usually be styled. And we should do more work here to add something akin to appearance:base to make that more ergonomic for web developers. I don't think the solution should be adding new elements that don't have platform theming by default as that would make the HTML language inconsistent.

ShadowDOM is perfect for this and is how existing builtin elements like details and summary work.

For <datalist id=options> we'd have to clone the options into the select element's shadow tree for rendering purposes, I think, much in the same way we probably have to clone one of the options as the selected option. As long as this happens in the internal shadow tree it's largely not observable and should be okay.

It would also have the benefit of having a consistent design with <input type=text list=options>, so we'd tackle combo boxes at the same time (or at least make it possible to reuse the design for them). Since the <datalist> continues to mainly serve as a source of data I don't think there's any need to track what element is pointing to it.

(There are some challenges here with how to match these options and their elements from CSS, but that seems like a surmountable problem.)

I haven't looked at survey in detail, but I'd assume that if <datalist> is mentioned as a pain point it would mainly be around not being able to style it, which is something we'd have to tackle for combo boxes and select alike.

annevk avatar Nov 01 '23 15:11 annevk

I’ve been discussing with @annevk and @pxlcoder about how to move forward here, and we have reached agreement on some things. Please correct if I misrepresent anything!

  • Reusing the <select> element will work
  • We can change the parser for <select> to allow particular new child tags like <button> and <datalist>
  • We can work incrementally, first by making these parser changes in the spec etc.
  • Using <datalist> as a child of <select> will work to replace the listbox with custom content

josepharhar avatar Dec 01 '23 00:12 josepharhar

I'm concerned by the implications of the multiple attribute. How does it work with the new parsing?

That being said I am excited by the progressive enhancement possibilities.

lukewarlow avatar Dec 01 '23 00:12 lukewarlow

If you use appearance:none they can usually be styled. And we should do more work here to add something akin to appearance:base to make that more ergonomic for web developers.

@annevk The majority you cannot and it isn't solely about styling but also extending them. While select may be able to be re-used; one of the benefits of introducing the new elements that are interoperable by default and not OS platform styled does make the platform consistent rather than trying to retro-fit all the build-in UI controls. I think we may want to fork this into a more generic issue specific to the introduction of new elements as I'm curious how your assertion holds up for input type=switch that not only allows styling but extensibility without a new element; type=file, type=progress, etc?

Using <datalist> as a child of <select> will work to replace the listbox with custom content This makes sense to me outside of my position that we should introduce a new element, not a new CSS attribute to solve this problem as it's not just about styling

@josepharhar @annevk so will this likewise have the child of <selectedoption> and ability to modify the initial shadow DOM?

gregwhitworth avatar Dec 01 '23 00:12 gregwhitworth

@josepharhar @annevk so will this likewise have the child of <selectedoption> and ability to modify the initial shadow DOM?

The idea (as I see it) is to still have a <selectedoption> element that can be a child of the <button> element we use to replace the <select>'s default button.

The UA shadowroot will certainly adapt if and when needed to support slotting in and replacing the default button and listbox.

Does that answer your question? I'm not sure if I understood it well.

josepharhar avatar Dec 04 '23 18:12 josepharhar

Adding agenda+ to move this to stage 1

josepharhar avatar Jan 09 '24 21:01 josepharhar

I updated the explainer to be <select> instead of <selectlist>: https://open-ui.org/components/selectlist/

josepharhar avatar Jan 10 '24 18:01 josepharhar

I was just talking to @una and she said that using a <datalist> which accepts rich content in <select> would be inconsistent because <datalist> won't accept rich content when used with <input>, so it would be better to create a new element like <listbox> for this. If, however, we eventually make <datalist> with <input> accept rich content, then it would not be inconsistent.

It would also have the benefit of having a consistent design with <input type=text list=options>, so we'd tackle combo boxes at the same time (or at least make it possible to reuse the design for them). Since the <datalist> continues to mainly serve as a source of data I don't think there's any need to track what element is pointing to it.

I believe these positions are aligned and that we should improve <datalist> for <input type=text>.

I don't think that <input type=text> is powerful enough to handle the use cases described here, but hopefully we can still improve <datalist> anyway: https://open-ui.org/components/combobox.explainer/

josepharhar avatar Jan 18 '24 21:01 josepharhar

I'm a big fan of improving <datalist>! I've long asked for it to be styleable so it can match my designs.

See demo here: https://codepen.io/argyleink/pen/GRemrav

  • imagine the datalist was presented as a rich list of contacts with avatars and meta information
  • both the email input and select inputs could use this rich presentation OR fallback for old browsers to just a string from the label (current behavior)

All of a sudden, datalist is a hero and very valuable. Which is not how it's currently regarded today.

The above demo, notice how the rich content of the datalist doesn't break the parser, and the label attribute properly populates both the presentation of the option and the value as the selected value.

If datalist gets the ability to show rich content and can be styled, the work left for <select> is to teach it to how to be a <select list=""> AND teach datalist to support the [multiple] attribute. It might also mean allowing uniquely that <select> can have an implicit datalist (also in the demo), this way older browsers still render select as it always has.

This improves all form inputs with a list attribute! Appeasing old and new browsers, but enabling rich presentation of options for newer parsers.


for combobox, I feel like that needs a new input type, as the value it holds is no longer a string, but an array of objects, both in the value and in the DOM. imagine pointing an <input type=combobox list="contacts" />, and each contact chosen is turned into a little chip inside the input. datalist again being just a presentation of options, styleable by authors, returning values for inputs to use.

argyleink avatar Jan 18 '24 21:01 argyleink

The idea is that the combobox proposal would be that way to do input + datalist properly.

lukewarlow avatar Jan 18 '24 21:01 lukewarlow

@argyleink combobox doesn't have to be an array it has the single Vs multi concept that select has

lukewarlow avatar Jan 18 '24 21:01 lukewarlow

@argyleink combobox doesn't have to be an array it has the single Vs multi concept that select has

sure, but it's contents need to be nodes and not strings tho right? it has children, where input type text doesnt

argyleink avatar Jan 18 '24 21:01 argyleink

I agree that we could make the datalist a bit more lovable.

I do want to note though that one thing I loved about the previous proposal was to style the "listbox" and "button" using a pseudo element.

Reason: as a developer we sometimes need to work with third parties - for example - payment providers. It is common that when working with such external tools, we only get to add some CSS and cannot touch the HTML. Just saying that this is a feature that will be missed and could've been loved by many.

If that idea is dead. I can understand that. But I did want to note the importance of having that capability of pseudo elements.

brechtDR avatar Jan 24 '24 16:01 brechtDR

OpenUI resolved on using an attribute instead of datalist/button to opt in to the new parser behavior: https://github.com/openui/open-ui/issues/985#issuecomment-1922141513

This would mean that the following would parse all elements:

<select bikeshed>
  <option>
    <img src="one.jpg">
    one
  <option>
  <option>
    <img src="two.jpg">
    two
  </option>
</select>

In order to make the element fully featured the same way that the original selectlist element proposal was, this would likely mean that the UA shadowroot will have a default datalist/listbox popup that will be used if the author doesn't provide one. However, if the author does provide one, then it will be used instead. So the following would also be valid and result in a datalist popover:

<select bikeshed>
  <datalist>
    <option>...</option>
    ...
  </datalist>
</select>

Again to be fully featured like the selectlist element, we would likely also add pseudo elements to the select element like ::button and ::listbox or ::datalist to target the the default button if the author doesn't provide one or the author provided button if there is one, and same for the datalist.

OpenUI is working to provide a list of reasons why using an attribute like this is better. I'd be interested to hear thoughts from @domenic @annevk and @smaug----. Adding agenda+ to discuss.

josepharhar avatar Feb 01 '24 20:02 josepharhar

I tried reading through those minutes, but it's not really clear to me why a new attribute is needed. How is

<select bikeshed>
  <option>
    <img src="one.jpg">

different from

<select>
  <datalist>
    <option>
      <img src="one.jpg">

? In fact, allowing img might well be something we can do without bikeshed="" or <datalist>, similar to how we did hr.

annevk avatar Feb 03 '24 09:02 annevk

Switching parsing mode for an element's descendants based on an attribute seems pretty unusual and I don't know if we have any precedent for it. I liked the old proposal in that the parser always behaved the same way.

It's also a bit strange that in the new proposal, it sounds like <select bikeshed><datalist>... stuff ...</datalist></select> is the same as <select bikeshed>... stuff ...</select>. It's not unprecedented (cf. <table> and <tbody>), but I think it's a bit of a confusing experience for developers, and it definitely makes one wonder why we need the bikeshed instead of just always doing <select><datalist>... stuff ...</datalist></select>.

Can you say more on why the group decided to move away from the previous consensus? The thread seems very long so any kind of summarization effort would be helpful.

domenic avatar Feb 03 '24 09:02 domenic

After re-reading the minutes the key reasons come down to these two bullet points:

  • Attribute DX is preferred since it feels similar to shadowrootmode, type on an input to change the expected DOM and behaviors and will be easier to teach
  • Concern about needing specific children may impact future widgets that are more complex such as date, color, switch, and other future widgets?

To @domenic point we discussed table quite a bit as an example and we even discussed making it so that it would go back and do cleanup and @mfreed7 noted that this wasn't a desired path, here are those minutes so there didn't seem much desire to do table type fixup:

flackr: i have a point on that which may be relevant - we could make the construction behavior similar to tables - the browser could make flackr: I was suggesting, if the concern is not knowing whether you have these elements we could make it so that the attribute implictly creates the parts and those are still targetable masonf: that's foster parenting and it's not fun masonf: there are XSS issues, etc luke: it causes hydration mishaps

@annevk said the following:

I tried reading through those minutes, but it's not really clear to me why a new attribute is needed. How is ... code example ... different from ... code example ...

It seems to me as though this debate is six of one, half a dozen of the other at this point. @josepharhar already called out that he doubted WHATWG would be in agreement on this and if you all prefer the children it would be good to understand why you all prefer that path to the attribute.

In fact, allowing img might well be something we can do without bikeshed="" or <datalist>, similar to how we did hr.

This may possibly be an orthogonal issue but my only concern would be back compat.

gregwhitworth avatar Feb 04 '24 20:02 gregwhitworth

@annevk said:

I tried reading through those minutes, but it's not really clear to me why a new attribute is needed.

One thing i'd like to note Is that by using an attribute, the authors could easily just opt in to custom style the datalist or options(to for example: add an image inside of it). They could do this without explicitly having to add the button to trigger the change.

It provides a much better developer experience and ease of tutoring. Changing an HTML element based on it's children is not really common and would also increase the chances for developer error.

brechtDR avatar Feb 05 '24 07:02 brechtDR

Opting into custom styling is to be done through CSS (appearance:base, appearance:base-select, or whatever we decide for that). That doesn't require markup and using markup would bring us back to the days of <font>. Not good. The new markup is only there to allow for richer data structures.

annevk avatar Feb 05 '24 07:02 annevk

I think Brecht means doing select[base] { appearance: base-select } rather than select:has(datalist)

lukewarlow avatar Feb 05 '24 08:02 lukewarlow

I don't understand the use case. Why would you not want to style all your select elements equally? We're not drawing a line in the sand here, we're extending the select element so it can do more things.

annevk avatar Feb 05 '24 08:02 annevk

Why would you not want to style all your select elements equally?

I don't understand why you should be forced to style everything. Maybe you just want to create a little shadow or border on the datalist, but want the default button / marker styling. Or even the other way around, Maybe you want to go the extra mile with the button beyond the capabilities of what you can do with a <select> now, but want to leave other elements as they are.

brechtDR avatar Feb 05 '24 09:02 brechtDR

And why would that not be possible?

annevk avatar Feb 05 '24 09:02 annevk