TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

The <Dialog> Element

339 pointsby cardamomoover 2 years ago

18 comments

justsomeuserover 2 years ago
My notes on this:<p>- &lt;dialog&gt;.showModal() is an indirect API to `top-layer`.<p>- `top-layer` is kind of like a sibling to the root &lt;html&gt;, elements can be placed into the top-layer <i>from any position in the existing DOM</i> (it is like they have two positions). This allows co-locating your &lt;dialog&gt; next to the relevant elements in the tree.<p>- There is only one `top-layer` but it can have many children. Last opened === current element on top.<p>- Z-index has no effect in the top-layer. No need to compete for a higher z-index.<p>- ::backdrop is a pseudo element that you can style behind the &lt;dialog&gt;. It is always below the last &lt;dialog&gt; opened.<p>- Not supported in Safari &lt;= 15.3
评论 #34765033 未加载
rockwotjover 2 years ago
Wish I could use this, but Safari only started supporting this as of 15.4, which was released less than a year ago. I&#x27;ll likely have to wait another 2-3 years until our site stops seeing these older Safari browsers.<p>EDIT: looks like Firefox released support around the same time, but my company&#x27;s Firefox users seem to be all be ok more recent versions.
评论 #34759991 未加载
评论 #34760988 未加载
评论 #34764014 未加载
评论 #34761014 未加载
评论 #34760329 未加载
zelphirkaltover 2 years ago
&gt; The native HTML &lt;dialog&gt; element should be used in creating modal dialogs as it provides usability and accessibility features that must be replicated if using other elements for a similar purpose. Use the appropriate .showModal() or .show() method to render dialogs. If creating a custom dialog implementation, ensure all expected default behaviors are supported and proper labeling recommendations are followed.<p>Any bets how long it will take JS frameworks to catch up to default behavior?<p>Many websites still do not manage to properly make the back and forward button work, even after years.<p>I am glad, that more semantic elements made it into HTML, reducing the amount of stuff needed to make simple things. I only wish, that we used the standard more.
评论 #34763538 未加载
评论 #34770408 未加载
评论 #34765108 未加载
评论 #34779089 未加载
djbeadleover 2 years ago
Supported on all major browsers and 93% of total users according to caniuse.com<p><a href="https:&#x2F;&#x2F;caniuse.com&#x2F;?search=dialog" rel="nofollow">https:&#x2F;&#x2F;caniuse.com&#x2F;?search=dialog</a>
评论 #34760505 未加载
评论 #34765145 未加载
mgover 2 years ago
It doesn&#x27;t seem to prevent scrolling?<p><a href="https:&#x2F;&#x2F;jsfiddle.net&#x2F;46nha7v3&#x2F;" rel="nofollow">https:&#x2F;&#x2F;jsfiddle.net&#x2F;46nha7v3&#x2F;</a><p>I enjoy native elements when they are there. You can do really nice interactive trees with the &lt;details&gt; element for example:<p><a href="https:&#x2F;&#x2F;twitter.com&#x2F;marekgibney&#x2F;status&#x2F;1593950777739218947" rel="nofollow">https:&#x2F;&#x2F;twitter.com&#x2F;marekgibney&#x2F;status&#x2F;1593950777739218947</a><p>But I am not sure if it is a good idea to bake more and more of them into the browsers. Couldn&#x27;t well made JavaScript modules provide the same functionality without making browsers more and more complex?
评论 #34760221 未加载
评论 #34760814 未加载
评论 #34760213 未加载
评论 #34761541 未加载
评论 #34760976 未加载
评论 #34764989 未加载
评论 #34760940 未加载
评论 #34762902 未加载
userbinatorover 2 years ago
At least this should theoretically make it easier to block those horrible popup modals, if they&#x27;re all grouped under one element:<p><pre><code> dialog {display:none}</code></pre>
评论 #34760118 未加载
评论 #34763051 未加载
davchanaover 2 years ago
I came across this element few weeks ago.<p>Before this,I used to use a div with stuff, and javascript to show hide that div when required. E.g. Spanner icon in this page, lower left corner: <a href="https:&#x2F;&#x2F;spa.bydav.in&#x2F;tsDMV&#x2F;tsVerif.html" rel="nofollow">https:&#x2F;&#x2F;spa.bydav.in&#x2F;tsDMV&#x2F;tsVerif.html</a><p>Now, recently I used this dialog element with javascript &amp; buttons to show hide div. E.g like Settings button at <a href="https:&#x2F;&#x2F;spa.bydav.in&#x2F;weather&#x2F;" rel="nofollow">https:&#x2F;&#x2F;spa.bydav.in&#x2F;weather&#x2F;</a><p>I like that it comes on top, stays in center, no checkbook &amp; css stuff.
modelessover 2 years ago
The interesting thing about &lt;dialog&gt; is that it uses the &quot;top layer&quot; which always appears above everything else on the page regardless of z-index. So you can delete hacks like z-index:2147483647.
评论 #34760879 未加载
评论 #34759828 未加载
评论 #34759922 未加载
Matheus28over 2 years ago
&gt; Warning: The tabindex attribute must not be used on the &lt;dialog&gt; element.<p>This really needs an explanation
评论 #34759929 未加载
transfireover 2 years ago
Masters of convolution.<p>&gt; the returnValue property gets set to the value of the button that was used to save the form&#x27;s state.<p>So a more complex form will have to shove all its data into a single attribute for retrieval? Notice the example provide returns the favorite animal as “cancel” if you hit cancel.
评论 #34760079 未加载
werdnapkover 2 years ago
I replaced all uses of 3rd party dialogs with this native version in all my client projects (who&#x27;s browsers support this feature) last year. Works well and that extra code is now gone.<p>More advanced &lt;select&gt; elements are what I&#x27;d like to see support for next.
评论 #34764928 未加载
wizofausover 2 years ago
Anyone seeing that the topic tag for comments on this thread is truncated after the &quot;The&quot;, presumably something to do with the &lt; character?
评论 #34760037 未加载
评论 #34760625 未加载
aliqotover 2 years ago
Makes me wonder, the more JS functionality creeps into html and css, the less we need JS. I&#x27;m curious if that&#x27;s an acknowledged future that these consortiums and working groups are headed toward, even if unspoken.
评论 #34775139 未加载
radoover 2 years ago
Tried to make it useful here: <a href="https:&#x2F;&#x2F;radogado.github.io&#x2F;n-modal&#x2F;" rel="nofollow">https:&#x2F;&#x2F;radogado.github.io&#x2F;n-modal&#x2F;</a>
vxNsrover 2 years ago
&gt; <i>It is important to provide a mechanism to close a dialog within the dialog element. For instance, the Esc key does not close non-modal dialogs by default, nor can one assume that a user will even have access to a physical keyboard (e.g., someone using a touch screen device without access to a keyboard).</i><p>Well this is annoying as a user. Not being able to click away or use escape to close it feels like built in pop up that can’t be ignored.
评论 #34761436 未加载
评论 #34760846 未加载
ofirgover 2 years ago
Maybe someone can explain the reason why the imperative and the declarative way of controlling the open state works differently.<p>I run into the modal&#x2F;no modal thing just a day ago.
sam_goodyover 2 years ago
How long has this been available (in Chrome&#x2F;FF on desktop)?
wg0over 2 years ago
It seems like the &lt;font&gt; tag all over to me. No?
评论 #34761403 未加载