TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

How should the new <selectedoption> element work?

65 点作者 jaffathecake7 个月前

18 条评论

gabesullice7 个月前
IMO, this whole cloning idea is overcomplicated. Let the duplication of markup happen server side or in React (or whatever).<p>If the developer wants a &lt;selectedoption&gt;, they should fill it with &lt;option&gt; elements that correspond to the &lt;option&gt; elements that appear in the dropdown. Each option could reuse the &lt;label for=&quot;&quot;&gt; pattern to associate them with their counterpart.<p>Then, when an option is selected by the end user, the corresponding option inside the &lt;selectedoption&gt; would get a `selected` attribute.<p>The default CSS would be:<p>selectedoption &gt; option { display: none; } selectedoption &gt; option[selected] { display: block; }<p>This gives complete control to the developer, works fine without JS, and wouldn&#x27;t have shocking side effects.<p>As they say, &quot;duplication is cheaper than the wrong abstraction&quot;.
lifthrasiir7 个月前
I think it is worthwhile to revisit the starting motivation of customizing `&lt;select&gt;`. There would be generally two camps:<p>1. Declarative camp, where people expect two differently stylable copies of `&lt;option&gt;` somewhere in `&lt;select&gt;`. I don&#x27;t think the exact copying mechanism and timing is very important here, as long as it can be safely done and works without JS.<p>2. Programmable camp, where people want the full control over appearance and behavior. We can reasonably assume that JS is required for this camp, and they generally have better understanding of what gets rendered or not in the first place.<p>Given both requirements, it seems wise to couple the clone timing with the event processing, which most cases in the second camp would have to do anyway. In the other words, the `input` event would have the default behavior of full cloning, and that default behavior should be prevented via `e.preventDefault()`. The second camp will almost always use this option and can use their own `MutationObserver` or similar to implement edge cases described in the OP. The timing and extent for the default behavior can be then optimized without affecting the second camp. It is even possible to distinguish two camps via different values of the `appearance` CSS property.<p>Sidetrack: It seems that `&lt;selectedoption&gt;` indeed exists mainly to distinguish two camps? But that leaves many edge cases like multiple `&lt;selectedoption&gt;` elements I believe. Maybe something like `&lt;select reflect=&quot;foo&quot;&gt;&lt;button&gt;&lt;option id=&quot;foo&quot;&gt;...&lt;&#x2F;option&gt;&lt;&#x2F;button&gt;...&lt;&#x2F;select&gt;` might be more appropriate. (`&lt;output&gt;` was my initial thought, but `&lt;option&gt;` would be more regular since it can&#x27;t nest.)
评论 #41912426 未加载
2freedi7 个月前
Correct if I&#x27;m wrong, but this still doesn&#x27;t allow us to define the dropdown content and selected content for each option separately? Maybe something like this:<p><pre><code> &lt;select name=&quot;commenters&quot;&gt; &lt;option value=&quot;annevk&quot;&gt;Anne van Kesteren&lt;&#x2F;option&gt; &lt;option value=&quot;jakearchibald&quot;&gt; &lt;label&gt;Jake Archibald&lt;&#x2F;label&gt; &lt;selectedcontent&gt; &lt;div&gt; &lt;img src=&quot;profile.avif&quot; alt=&quot;Jake&#x27;s photo&quot;&gt; &lt;div&gt;Jake Archibald&lt;br&gt;&lt;small&gt;@jakearchibald&lt;&#x2F;small&gt;&lt;&#x2F;div&gt; &lt;&#x2F;div&gt; &lt;&#x2F;selectedcontent&gt; &lt;&#x2F;option&gt; &lt;option value=&quot;sorvell&quot;&gt;Steve Orvell&lt;&#x2F;option&gt; &lt;&#x2F;select&gt; </code></pre> The value attribute would be required when using these new sub elements. This structure feels familiar and progressive to me.<p>Naively, I would imagine that the following JavaScript would cause 1 DOM update, 1 redraw of the option if the dropdown is open, and 1 redraw of the selected option:<p><pre><code> document.querySelector(&#x27;option:selected selectedcontent&#x27;).innerHTML = &#x27;Jake Archibald&#x27;; </code></pre> Obviously, things are different when using multiple. Maybe a `select &gt; selectedcontent` element containing a copy of each `option &gt; selectedcontent` element that is updated on change events.
评论 #41913141 未加载
dfabulich7 个月前
I think it should be Option 2, &quot;Automatically reset the content when anything in the selected &lt;option&gt; changes.&quot;<p>But furthermore, I think it should be possible to turn &lt;selectedoption&gt; mirroring entirely off, e.g. with an attribute like &lt;selectedoption mirroring=&quot;none&quot;&gt;, and I think most developers using reactive frameworks should prefer to do that.<p>If I&#x27;m using any reactive framework designed to do targeted DOM updates, I want my framework to be in complete control. When an option is selected, I&#x27;ll populate &lt;selectedoption&gt; myself. When an &lt;option&gt; is modified, I modified it, and I know which &lt;option&gt; is selected, so I&#x27;ll perform a targeted DOM update of &lt;selectedoption&gt; as well.<p>You had a whole separate podcast episode about how&#x2F;why having the browser itself do targeted DOM updates is an enormous can of worms, mostly for attribute vs. property reasons. <a href="https:&#x2F;&#x2F;offthemainthread.tech&#x2F;episode&#x2F;putting-react-in-the-browser&#x2F;" rel="nofollow">https:&#x2F;&#x2F;offthemainthread.tech&#x2F;episode&#x2F;putting-react-in-the-b...</a> And anyway, every framework wants to handle mutations differently, and framework designers are in consensus that we haven&#x27;t picked a clear winner yet. So, as nice as it would be if we had a replaceHTML API that everybody loved, we don&#x27;t have that now, and we shouldn&#x27;t hold up customizable &lt;select&gt; for this.<p>&lt;selectedoption&gt; mirroring is for folks who don&#x27;t want to use any JavaScript (and I think that&#x27;s a good thing). In that case, mirroring all updates automatically, synchronously, is the simplest thing that can possibly work.<p>Developers who want optimal performance want full control, and they just want to turn mirroring entirely off.
评论 #41880380 未加载
mminer2377 个月前
Why should styling even have a separate element? And duplicated? I thought the whole point of CSS was to not clutter HTML with styling.<p>This shouldn&#x27;t be a literal new element. It should be a psuedo-element like other component parts like `::-[engine]-slider-thumb` are. You can then style `select::selected-option` and not have to mangle your HTML or worry about mirroring HTML.
评论 #41914915 未加载
PoignardAzur7 个月前
I would lean towards option 1, because it&#x27;s the only one which doesn&#x27;t lock the other options out.<p>You can just not provide selectedoption or provide it in a limited way, and see what patterns emerge in how people use it in practice. Maybe it&#x27;ll turn out that people want something completely different from selectedoption&#x27;s current design. Maybe it&#x27;ll turn out that people often want to display something different between the select menu and the select button and selectedoption doesn&#x27;t actually bring much to the table.
TheFlyingFish7 个月前
My feeling is that the most important situation to consider here is where the updating happens from third-party JS. If you&#x27;re writing your own JS, it isn&#x27;t that much harder to just target the selectedoption as well as the original one, and if you&#x27;re using a full-fat framework like React or whatever it&#x27;s downright easy. So I think the benefit of providing an explicit API to trigger a clone is limited.<p>So that leaves the various automatic options, either synchronous, debounced, or the fancy targeted version. This seems like a pretty straightforward complexity&#x2F;performance tradeoff to me, with the synchronous version being the simplest (both to implement and to understand) and going up from there.<p>With that in mind, I&#x27;m inclined toward the middle option (changes are synced automatically, but batched in the next microtask) since it feels like the best balance of complexity&#x2F;usability. Seems like it would eliminate some of the biggest performance footguns, without being too much of a bear to implement or too difficult to understand.<p>On the other hand, I would personally also be ok with no syncing at all, just the initial clone when an option is selected, if it would mean we get this feature sooner. Really looking forward to not having to roll my own dropdowns.
评论 #41912435 未加载
butchler7 个月前
If you&#x27;re using a framework like React to manage updates to the DOM, then you probably don&#x27;t want to use selectedoption in the first place because it&#x27;s just as easy and more flexible&#x2F;predictable to use the framework&#x27;s existing patterns for keeping different parts of the DOM in sync.<p>So the main target audience for selectedoption is probably people not using a framework, and using no or little JavaScript. That audience probably would want selectedoption to &quot;just work&quot; without having to manually call a function to reset things. So while option 1 seems like a reasonable option to me personally as someone who mainly works with React, it&#x27;s probably not what most people using selectedoption would want.<p>Option 2 just has too much potential for introducing hard to debug and hard to fix performance issues, so I feel like it&#x27;s automatically not the best choice. Its main advantage is that it&#x27;s probably the most straightforward to understand&#x2F;explain.<p>Option 4 would help with the performance issues, but it sounds like it would be a nightmare for browsers to implement. It would also be a bit difficult to explain to devs how exactly it works and what edge cases need to be considered (like mutating the selectedoption &quot;fork&quot; potentially causing issues).<p>So by process of elimination option 3 seems like the best to me. It solves the performance issues but it&#x27;s still pretty easy to understand&#x2F;explain. It&#x27;s main disadvantage seems to be that from the perspective of code running synchronously it&#x27;s possible for the selectedoption and option to get out of sync, but 1) it&#x27;s a bit hard to imagine a practical use case for needing to do this kind of comparison and 2) simply waiting until the next microtask before doing the comparison would probably be an easy fix even if this is a problem in some cases.<p>For all of these options, it might be useful for there to be some way to intercept the default behavior. For example maybe there could be some event such that calling .preventDefault() inside an event handler has the side effect of preventing the selectedoption from being reset, if resetting it is not desired. Of course you don&#x27;t need to use selectedoption at all if the automatic resetting is not desired, but maybe there will be some cases where you only want to make an exception in a few cases but generally do want the selectedoption default behavior.
_heimdall7 个月前
&gt; Option: Nothing by default, but provide a way to trigger an update<p>I would strongly suggest this option unless there&#x27;s a strong reason the browser <i>must</i> keep the elements in sync automatically.<p>Unless I&#x27;m mistaken, the other options could all be done in userland with a MutationObserver, a debounce method wrapping a timeout, and custom logic optimizing updates for specific attributes or changes.<p>I&#x27;m particularly worried about how automatic updates&#x2F;cloning will work across different frontend frameworks. Today&#x27;s more popular frameworks already diverge quite a bit with how they handle rerendering and DOM updates, and the next big framework to come along could take a more radical approach to rendering and interactivity.<p>In my opinion, anything that can reasonably be done in user code should be done there - at least until it causes enough problems that it needs to be solidified into browser specs. I&#x27;m always worried that the any clever built-in magic may work today but cause problems later, and because removing the spec is difficult most users would just skip the new &lt;selectedoption&gt; element all together.
wffurr7 个月前
Is this a path towards a fully style-able select element that still operates correctly with keyboard and accessibility features?<p>It never fails to amaze me how many bad semi functional “select” controls I encounter on a regular basis. I must be part of the small minority that fills out forms entirely with the keyboard using the tab key and prefix matching.
评论 #41913003 未加载
Y-bar7 个月前
Is &lt;button&gt; a valid child of &lt;select&gt; now? How do we deal with an &quot;interact:able&quot; element nested inside another such element gracefully? (i.e. not pushing a lot of preventDefaults and stopping event bubbling here and there).
评论 #41912854 未加载
londons_explore7 个月前
IMO, the extra functionality this gives does not warrant the complexity, extra cognitive load and learning curve for new developers.<p>Existing solutions solve enough use cases, and the remaining use cases can use javascript to build whatever they like.
jmull7 个月前
It looks like the proposal is to build a little single purpose reactive UI engine into &lt;select&gt;&lt;button&gt;&lt;selectedcontent&gt;, and we&#x27;re talking over the details.<p>To me it&#x27;s a bad idea from the top.<p>Either leave reactive updates to frameworks, or develop a general built-in reactive framework. Building a one-off that introduces an all-new pattern into the browser seems like a perpetual headache.
评论 #41913428 未加载
hlandau7 个月前
This seems overcomplicated. Maybe I&#x27;m missing something, but is there some reason not just to define a psuedoelement on the current option?<p><pre><code> option::picker-current { ... }</code></pre>
评论 #41912515 未加载
parasti7 个月前
First option is clearly best just because it lets developers decide. Every select library should have a method like this and I breathe a sigh of relief every time I find one.
lakomen7 个月前
Better add a &lt;optiontitle&gt; element
donatj7 个月前
Someone tell me I&#x27;m wrong and why, but this whole thing seems like unnecessary complication? We have JavaScript for bespoke select tags.
评论 #41913347 未加载
评论 #41913330 未加载
评论 #41913339 未加载
transfire7 个月前
Who is in charge of these HTML changes? This is horrible. It’s bad enough that for the last three decades we’ve have to use the `selected` attribute, now we are going to have a `button` in `select` with a `selectedoption` tag?<p>Please, just No!<p>The CSS for the style of the element belongs with the `select` tag. And all that should be needed to demark the selected option is a matching `value` attribute.<p>I get the sense that the people currently working on HTML and CSS are front-end designers and not programmers. We need functional design, polymorphism and simplicity, not just more kitchen sinks.
评论 #41912476 未加载
评论 #41912353 未加载