Show no tooltip on the control a sheet or dialog focuses as it opens

A modal side sheet, bottom sheet and the modal rail move the focus to
their first control as they open (x-trap), and `<x-modal>` does in
`showModal()`. On a page loaded with the sheet already open (a
`wire:model` set from `?workout=` in the URL) the browsers count that
focus as `:focus-visible`, and so they do for a sheet or dialog opened
from the keyboard, so tooltip.js showed the close button's "Close"
tooltip the moment the layer appeared, over its title, with no keyboard
use. Confirmed in Chrome, Firefox and Safari for the deep-linked sheet
and a full-screen dialog opened with Enter; a deep-linked `<x-modal>`'s
own focus was not counted as keyboard focus. M3 lets a tooltip appear
on hover or focus and says nothing that asks for one on a focus the
component moves itself.

layers.js gains `openingFocus(event)`: a focus is a modal layer's own
when the nearest open dialog, `aria-modal` panel or modal rail around
its target is entered from outside it, or from nowhere, and no Tab is
moving it (a Tab moves the focus while its keydown is handled, by the
browser or by focus-trap wrapping round the panel). tooltip.js and
rich-tooltip.js leave such a focus out; focus moved within the layer,
a Tab onto the control and a focus returned to it from a menu inside
still show the tooltip. The docblocks, the skill and UPGRADE.md say so.

A browser test loads a page with a modal sheet open at 393px, opens a
full-screen dialog and a dialog whose first control is a rich tooltip's
trigger from the keyboard, and checks that no tooltip is up on the
focused control, then that Tab wrapping round to the close button shows
its tooltip; it fails without the change in Chrome, Firefox and Safari,
each part on its own.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Andreas Reinhold / reini
2026-09-17 07:16:32 +02:00
co-authored by Claude Opus 5
parent 16c52d46e9
commit b62c83f244
8 changed files with 170 additions and 8 deletions
+43
View File
@@ -29,6 +29,12 @@
* (materialShowModal(), dialog.js); a modal `<dialog>` keeps the rest of the page from assistive
* technology itself, and its own `x-trap` pauses the focus trap of the panel under it, whose Tab
* would otherwise pull the focus back to an inert sheet.
*
* **The focus a layer moves.** A modal panel, `<x-modal>` and the modal rail move the focus to
* their first control as they open (x-trap, `showModal()`), and on a page loaded with the layer
* open, or opened from the keyboard, the browser counts that as `:focus-visible`. `openingFocus()`
* tells that focus apart from a person's, so a tooltip does not stand over a sheet's close button
* the moment the sheet appears (tooltip.js, rich-tooltip.js).
*/
/** Everything that can hold an Escape before the window hears it, nearest first. */
@@ -62,6 +68,43 @@ const isOpen = (layer) => {
return true
}
/** The layers that move the focus into themselves as they open. */
const MODAL_LAYERS = 'dialog, [aria-modal="true"], [data-md-navigation-rail-panel]'
/**
* Whether a Tab is moving the focus: the browser moves it while the keydown is handled, and so does
* focus-trap when it takes a Tab round a panel, so a focus that lands before the next task is the
* Tab's.
*/
let tabbing = false
document.addEventListener('keydown', (event) => {
if (event.key === 'Tab') {
tabbing = true
setTimeout(() => (tabbing = false))
}
}, true)
/**
* Whether a `focusin` is an open modal layer moving the focus into itself (see the header): the
* focus arrives from outside the nearest open layer around its target, or from nowhere, and no Tab
* is moving it. Focus moved within the layer, and a Tab that focus-trap brings back to its first
* control, are a person's.
*/
export const openingFocus = (event) => {
if (tabbing || !(event.target instanceof Element)) {
return false
}
let layer = event.target.closest(MODAL_LAYERS)
while (layer !== null && !isOpen(layer)) {
layer = layer.parentElement?.closest(MODAL_LAYERS) ?? null
}
return layer !== null && !(event.relatedTarget instanceof Node && layer.contains(event.relatedTarget))
}
/** Whether an Escape keydown is this open panel's to act on (see the header). */
const owns = (panel, event) => {
if (event.defaultPrevented) {
+5 -2
View File
@@ -2,7 +2,8 @@
* `materialRichTooltip`: shows an `<x-rich-tooltip>`.
*
* Transient (the default): like a plain tooltip (tooltip.js) — after a short hover on a pointer
* that can hover, at once on keyboard focus, and standing for 1.5s once the pointer or the focus
* that can hover, at once on keyboard focus (not the focus a sheet or dialog moves to it as it
* opens, layers.js), and standing for 1.5s once the pointer or the focus
* leaves, which M3 gives plain and rich tooltips alike (docs/reference/m3/
* components-actions-communication-containment.md § Tooltips, ACT-25). The bubble is inside the
* wrapper, so moving onto it to reach its actions, by pointer or by Tab, is no leave at all; Escape
@@ -15,6 +16,8 @@
* A Livewire morph strips attributes the server did not render and gives the bubble a new id, so
* they are written again whenever the trigger is reached.
*/
import { openingFocus } from './layers.js'
const HOVER_DELAY_MS = 500
const LEAVE_GRACE_MS = 1500
@@ -95,7 +98,7 @@ document.addEventListener('alpine:init', () => {
wrapper.addEventListener('pointerenter', (event) => event.pointerType === 'mouse' && show(HOVER_DELAY_MS))
wrapper.addEventListener('pointerleave', () => hide(LEAVE_GRACE_MS))
wrapper.addEventListener('focusin', (event) => event.target.matches(':focus-visible') && show(0))
wrapper.addEventListener('focusin', (event) => !openingFocus(event) && event.target.matches(':focus-visible') && show(0))
wrapper.addEventListener('focusout', (event) => !wrapper.contains(event.relatedTarget) && hide(LEAVE_GRACE_MS))
document.addEventListener('keydown', (event) => event.key === 'Escape' && hide())
},
+6 -2
View File
@@ -3,11 +3,15 @@
*
* The trigger is the popover's parent element (the button, or the standalone wrapper). Hover
* counts only for a pointer that can hover, after a short delay so a pointer passing over a
* toolbar does not flash every label; keyboard focus shows it at once. A press and Escape hide it
* toolbar does not flash every label; keyboard focus shows it at once — but not the focus a sheet,
* a dialog or the modal rail moves to its first control as it opens (layers.js), which would put
* a tooltip over a sheet's close button the moment the sheet appears. A press and Escape hide it
* at once; leaving and blur let it stand for M3's 1.5s, so the words can still be read while the
* pointer moves on. Only one tooltip is up at a time: showing one takes the last one down, which
* the browser will not do for us because these are `popover="manual"`.
*/
import { openingFocus } from './layers.js'
const HOVER_DELAY_MS = 500
const LEAVE_DELAY_MS = 1500
@@ -56,7 +60,7 @@ document.addEventListener('alpine:init', () => {
this.listen(trigger, 'pointerenter', (event) => event.pointerType === 'mouse' && show(HOVER_DELAY_MS))
this.listen(trigger, 'pointerleave', () => hide(LEAVE_DELAY_MS))
this.listen(trigger, 'pointerdown', () => hide())
this.listen(trigger, 'focusin', () => trigger.matches(':focus-within:has(:focus-visible), :focus-visible') && show(0))
this.listen(trigger, 'focusin', (event) => !openingFocus(event) && trigger.matches(':focus-within:has(:focus-visible), :focus-visible') && show(0))
this.listen(trigger, 'focusout', () => hide(LEAVE_DELAY_MS))
this.listen(document, 'keydown', (event) => event.key === 'Escape' && hide())
},