{"id":1334,"date":"2024-09-21T22:20:15","date_gmt":"2024-09-21T22:20:15","guid":{"rendered":"https:\/\/html5accessibility.com\/stuff\/?p=1334"},"modified":"2024-10-01T12:11:08","modified_gmt":"2024-10-01T12:11:08","slug":"drugs-button-popover","status":"publish","type":"post","link":"https:\/\/html5accessibility.com\/stuff\/2024\/09\/21\/drugs-button-popover\/","title":{"rendered":"drugs button popover"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1338 size-large\" src=\"https:\/\/html5accessibility.com\/stuff\/wp-content\/uploads\/2024\/09\/Design-1024x426.jpeg\" alt=\"David, Patrick, Jonny and I, anxiously await for the birth of the little king (David and Sarah's son).\" width=\"580\" height=\"241\" srcset=\"https:\/\/html5accessibility.com\/stuff\/wp-content\/uploads\/2024\/09\/Design-1024x426.jpeg 1024w, https:\/\/html5accessibility.com\/stuff\/wp-content\/uploads\/2024\/09\/Design-300x125.jpeg 300w, https:\/\/html5accessibility.com\/stuff\/wp-content\/uploads\/2024\/09\/Design-768x319.jpeg 768w, https:\/\/html5accessibility.com\/stuff\/wp-content\/uploads\/2024\/09\/Design-1536x639.jpeg 1536w, https:\/\/html5accessibility.com\/stuff\/wp-content\/uploads\/2024\/09\/Design-1200x499.jpeg 1200w, https:\/\/html5accessibility.com\/stuff\/wp-content\/uploads\/2024\/09\/Design.jpeg 1914w\" sizes=\"auto, (max-width: 580px) 100vw, 580px\" \/><\/p>\n<p><strong>Update: 01\/10\/2024<\/strong><\/p>\n<p class=\"note\">Several <a href=\"https:\/\/mastodon.world\/@siblingpastry\/113226467371684445\">people<\/a> have questioned my reasoning for writing about the use case of <code>popover<\/code> as a tooltip, <em>no<\/em> I was not on drugs, at the time of writing I noticed that <a href=\"https:\/\/github.com\/\">GitHub<\/a> was using <code>popover<\/code> as a tooltip:<a href=\"https:\/\/mastodon.social\/@SteveFaulkner\/113227306247321811\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1375\" src=\"https:\/\/html5accessibility.com\/stuff\/wp-content\/uploads\/2024\/09\/fe10ded4ef76b0a4.png\" alt=\"pull request icon button on GitHub with popover text acting as the accessible name\" width=\"140\" height=\"113\" \/><\/a><\/p>\n<p>During preparation for my talk at <a href=\"https:\/\/2024.stateofthebrowser.com\/\">State of the Browser 2024<\/a> I started to look into the <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/HTML\/Global_attributes\/popover\"><code>popover<\/code><\/a> attribute. One of the things I noticed is that you have to hook up the popover content (by a means other than <code>popovertarget<\/code>) to the triggering element if you want the content to act as the <a href=\"https:\/\/w3c.github.io\/accname\/#dfn-accessible-name\">accessible name<\/a> for s<em>aid<\/em> trigger.<\/p>\n<p><strong>proto popover code example:<\/strong><\/p>\n<pre>&lt;button <strong>popovertarget<\/strong>=\"my-popover\" type=\"button\"&gt;\ud83d\udc8a&lt;\/button&gt;\r\n&lt;div <strong>popover<\/strong> id=\"my-popover\"&gt;Psychadelic drugs&lt;\/div&gt;<\/pre>\n<p>The accessible name for the <code>button<\/code> in the preceding example will be \ud83d\udc8a, when in this case I want it to be <em>Psychadelic drugs\u00a0<\/em>(the text content of the <code>popover<\/code>)<\/p>\n<p>I thought, hey I can use a <code>label<\/code> element to provide the accessible name for the <code>button<\/code> (as it&#8217;s a <a href=\"https:\/\/html.spec.whatwg.org\/multipage\/forms.html#category-label\">labelable element<\/a>) like this:<\/p>\n<pre>&lt;!-- DOES NOT WORK --&gt;\r\n&lt;button popovertarget=\"my-popover\" \r\ntype=\"button\" <strong>id=\"pill\"<\/strong>&gt;\ud83d\udc8a&lt;\/button&gt;\r\n&lt;div popover id=\"my-popover\"&gt;\r\n<strong>&lt;label for=\"pill\"&gt;Psychadelic drugs&lt;\/label&gt;<\/strong>\r\n&lt;\/div&gt;\r\n<\/pre>\n<p>Problem is that the <code>label<\/code> only provides the accessible name when the <em>PopOver<\/em> is visible. So unless the <code>button<\/code> is clicked\/pressed the <code>button<\/code> has no accessible name.<\/p>\n<p>I then remembered that unlike <code>aria-labelledby<\/code> the <code>for\/id<\/code> relationship does not work when the <code>&lt;label&gt;<\/code> element is <code>display:none<\/code>. Which kinda sucks. I mean I could use <code>aria-labelledby<\/code> like this:<\/p>\n<pre>&lt;!-- WORKS --&gt;\r\n&lt;button popovertarget=\"my-popover\" type=\"button\" \r\n<strong>aria-labelledby=\"my-popover\"<\/strong>&gt;\ud83d\udc8a&lt;\/button&gt; \r\n&lt;div popover <strong>id=\"my-popover\"<\/strong>&gt; \r\nPsychadelic drugs\r\n&lt;\/div&gt;<\/pre>\n<p class=\"note\">But I prefer to follow the <a href=\"https:\/\/w3c.github.io\/using-aria\/#rule1\">First Rule of ARIA<\/a>, when possible. So how do I use a HTML <code>label<\/code> element rather than <code>aria-labelledby<\/code> in this case?<\/p>\n<h3>YOU DON&#8217;T<\/h3>\n<p><del>A solution that popped into my head that appears to work is moving the <code>label<\/code> element to contain the popover.<\/del>\u00a0 Thought it worked (probably due to a fever dream), but after further testing, turned out it doesn&#8217;t:<\/p>\n<pre>&lt;!-- DOES NOT WORK --&gt;\r\n&lt;button popovertarget=\"my-popover\" \r\ntype=\"button\" <strong>id=\"pill\"<\/strong>&gt;\ud83d\udc8a&lt;\/button&gt; \r\n<strong style=\"letter-spacing: -0.315px;\">&lt;label for=\"pill\"&gt;\r\n<\/strong>&lt;div popover id=\"my-popover\"&gt; \r\ndrugs&lt;\/div&gt;\r\n<strong style=\"letter-spacing: -0.315px;\">&lt;\/label&gt;<\/strong><\/pre>\n<h2>Viable options<\/h2>\n<p>Use <code>aria-labelledby<\/code>:<\/p>\n<pre>&lt;!-- WORKS --&gt;\r\n&lt;button popovertarget=\"my-popover\" type=\"button\" \r\n<strong>aria-labelledby=\"my-popover\"<\/strong>&gt;\ud83d\udc8a&lt;\/button&gt; \r\n&lt;div popover <strong>id=\"my-popover\"<\/strong>&gt; \r\nPsychadelic drugs\r\n&lt;\/div&gt;<\/pre>\n<p>Use <code>aria-label<\/code> on the <code>button<\/code>:<\/p>\n<pre>&lt;!-- WORKS --&gt;\r\n&lt;button popovertarget=\"my-popover\" type=\"button\" \r\n<strong>aria-label=\"Psychadelic drugs\"<\/strong>&gt;\ud83d\udc8a&lt;\/button&gt; \r\n&lt;div popover id=\"my-popover\"&gt; \r\nPsychadelic drugs\r\n&lt;\/div&gt;<\/pre>\n<h2>Try it for yourself<\/h2>\n<p class=\"codepen\" style=\"height: 600px; box-sizing: border-box; display: flex; align-items: center; justify-content: center; border: 2px solid; margin: 1em 0; padding: 1em;\" data-height=\"600\" data-default-tab=\"result\" data-slug-hash=\"dyBELQm\" data-pen-title=\"drugs\" data-user=\"stevef\">See the Pen <a href=\"https:\/\/codepen.io\/stevef\/pen\/dyBELQm\"><br \/>\ndrugs<\/a> by steve faulkner (<a href=\"https:\/\/codepen.io\/stevef\">@stevef<\/a>)<br \/>\non <a href=\"https:\/\/codepen.io\">CodePen<\/a>.<\/p>\n<p><script async src=\"https:\/\/cpwebassets.codepen.io\/assets\/embed\/ei.js\"><\/script><\/p>\n<h2>Notes:<\/h2>\n<p>This only works accessibly on pointer\/keyboard devices. On touch only devices the <em>only<\/em> way, I can work out, to display the popover is by activating the button,\u00a0 which sorta defeats the purpose of having a descriptive visible label via <code>popover<\/code> as people need to know what it does before activating the button.<\/p>\n<p>I used the following CSS to display the\u00a0 <code>popover<\/code> on focus\/hover:<\/p>\n<pre>\/* Show popover on button focus *\/\r\n#popoverButton:focus + #my-popover {\r\n  display: block;\r\n}\r\n\r\n\/* Show popover on button hover *\/\r\n#popoverButton:hover + #my-popover {\r\n  display: block;\r\n}\r\n<\/pre>\n<p>The only reasonable, accessible method I can find to provide a visible label for a button when the meaning of iconogrophy is obscure is to use an <strong>always visible label<\/strong>, in this case <strong>you can<\/strong> also use the <strong><code>label<\/code><\/strong> element to provide the accessible name:<\/p>\n<p class=\"codepen\" style=\"height: 540px; box-sizing: border-box; display: flex; align-items: center; justify-content: center; border: 2px solid; margin: 1em 0; padding: 1em;\" data-height=\"540\" data-default-tab=\"result\" data-slug-hash=\"gONEoJp\" data-pen-title=\"persistent visual label\" data-user=\"stevef\">See the Pen <a href=\"https:\/\/codepen.io\/stevef\/pen\/gONEoJp\"><br \/>\npersistent visual label<\/a> by steve faulkner (<a href=\"https:\/\/codepen.io\/stevef\">@stevef<\/a>)<br \/>\non <a href=\"https:\/\/codepen.io\">CodePen<\/a>.<\/p>\n<p><script async src=\"https:\/\/cpwebassets.codepen.io\/assets\/embed\/ei.js\"><\/script><\/p>\n<hr \/>\n<h2>The Fall &#8211; Mr Pharmacist<\/h2>\n<p><iframe loading=\"lazy\" title=\"YouTube video player\" src=\"https:\/\/www.youtube.com\/embed\/56dxJjXbnjg?si=QA1tsQxlr5hMeDiX\" width=\"560\" height=\"315\" frameborder=\"0\" allowfullscreen=\"allowfullscreen\"><\/iframe><\/p>\n<details>\n<summary>Lyrics<\/summary>\n<pre>Mr. Pharmacist\r\nCan you help me out today\r\nIn your usual lovely way\r\nOh Mr. Pharmacist I insist\r\nThat you give me some of that vitamin see\r\nMr. Pharmacist\r\nDear Pharmacist won't you please\r\nGive me some energy\r\nMr. Pharmacist\r\nHey Mr. Pharmacist\r\nI'll recommend you to my friends\r\nThey'll be happy in the end\r\nMr. Pharmacist can you help\r\nSend me on a 'delic kick\r\nMr. Pharmacist\r\nDear Pharmacist use your mind\r\nYou better stock me up for the wintertime\r\nMr. Pharmacist\r\nHey Mr. Pharmacist\r\nWords cannot express\r\nFeeling I suggest\r\nOh Mr. Pharmacist I can plead\r\nGimme some of that powder I need\r\nMr. Pharmacist\r\nDear Pharmacist I'll be back\r\nWith a handful of empty sack\r\nMr. Pharmacist\r\n<\/pre>\n<\/details>\n","protected":false},"excerpt":{"rendered":"<p>Update: 01\/10\/2024 Several people have questioned my reasoning for writing about the use case of popover as a tooltip, no I was not on drugs, at the time of writing I noticed that GitHub was using popover as a tooltip: During preparation for my talk at State of the Browser 2024 I started to look [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1334","post","type-post","status-publish","format-standard","hentry","category-htmlaccessibility"],"_links":{"self":[{"href":"https:\/\/html5accessibility.com\/stuff\/wp-json\/wp\/v2\/posts\/1334","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/html5accessibility.com\/stuff\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/html5accessibility.com\/stuff\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/html5accessibility.com\/stuff\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/html5accessibility.com\/stuff\/wp-json\/wp\/v2\/comments?post=1334"}],"version-history":[{"count":34,"href":"https:\/\/html5accessibility.com\/stuff\/wp-json\/wp\/v2\/posts\/1334\/revisions"}],"predecessor-version":[{"id":1376,"href":"https:\/\/html5accessibility.com\/stuff\/wp-json\/wp\/v2\/posts\/1334\/revisions\/1376"}],"wp:attachment":[{"href":"https:\/\/html5accessibility.com\/stuff\/wp-json\/wp\/v2\/media?parent=1334"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/html5accessibility.com\/stuff\/wp-json\/wp\/v2\/categories?post=1334"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/html5accessibility.com\/stuff\/wp-json\/wp\/v2\/tags?post=1334"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}