Plan step 36 (actions). <x-rich-tooltip> renders data-md-rich-tooltip with the bubble's data-md-rich-tooltip-bubble and data-md-side, and data-md-rich-tooltip-title/-text/-actions on its plain text; no class list. rich-tooltip.css draws RichTooltipTokens' surface-container, medium corner, elevation 2, 312px bubble, its corner-to-corner anchor positioning and its fade, the same shape as tooltip.css. rich-tooltip.js's leave grace goes from 200ms to M3's 1.5s (ACT-25), matching tooltip.js. RichTooltipTest is rewritten on the hooks and ComponentStylesheet. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Qwx5USif3wFFmxtHg5U1g9
85 lines
3.4 KiB
JavaScript
85 lines
3.4 KiB
JavaScript
/**
|
|
* `materialRichTooltip`: shows an `<x-rich-tooltip>`.
|
|
*
|
|
* Transient (the default): like a plain tooltip — after a short hover on a pointer that can hover,
|
|
* at once on keyboard focus, standing for M3's 1.5s once the pointer or focus leaves (ACT-25) so
|
|
* the words can still be read while it moves on, and it stays while the pointer moves onto the
|
|
* bubble to reach its actions. Persistent: a press on the trigger opens it as a light-dismiss
|
|
* popover.
|
|
*
|
|
* The trigger is pointed at the bubble with `aria-describedby`, so the explanation — the whole
|
|
* point of a rich tooltip — is read out with the control rather than never at all; a persistent
|
|
* one also says `aria-haspopup="dialog"` and whether it is open. The server cannot write any of
|
|
* it: the trigger comes from a slot, and only script knows which element in it takes the focus.
|
|
* 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.
|
|
*/
|
|
const HOVER_DELAY_MS = 500
|
|
const LEAVE_GRACE_MS = 1500
|
|
|
|
document.addEventListener('alpine:init', () => {
|
|
window.Alpine.data('materialRichTooltip', (persistent = false) => ({
|
|
timer: null,
|
|
|
|
init() {
|
|
const bubble = this.$refs.bubble
|
|
const wrapper = this.$el
|
|
const open = () => bubble.matches(':popover-open')
|
|
|
|
const control = () => [...wrapper.querySelectorAll('button, a[href], [tabindex]')].find((element) => !bubble.contains(element))
|
|
|
|
const describe = () => {
|
|
const trigger = control()
|
|
|
|
if (!trigger) {
|
|
return
|
|
}
|
|
|
|
trigger.setAttribute('aria-describedby', bubble.id)
|
|
|
|
if (persistent) {
|
|
trigger.setAttribute('aria-haspopup', 'dialog')
|
|
trigger.setAttribute('aria-expanded', String(open()))
|
|
}
|
|
}
|
|
|
|
describe()
|
|
bubble.addEventListener('toggle', describe)
|
|
wrapper.addEventListener('focusin', describe)
|
|
wrapper.addEventListener('pointerenter', describe)
|
|
|
|
if (persistent) {
|
|
wrapper.addEventListener('click', (event) => {
|
|
if (bubble.contains(event.target)) {
|
|
return
|
|
}
|
|
|
|
open() ? bubble.hidePopover() : bubble.showPopover()
|
|
})
|
|
|
|
return
|
|
}
|
|
|
|
const show = (delay) => {
|
|
clearTimeout(this.timer)
|
|
this.timer = setTimeout(() => !open() && bubble.showPopover(), delay)
|
|
}
|
|
|
|
const hide = (delay = 0) => {
|
|
clearTimeout(this.timer)
|
|
this.timer = setTimeout(() => open() && bubble.hidePopover(), delay)
|
|
}
|
|
|
|
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('focusout', (event) => !wrapper.contains(event.relatedTarget) && hide())
|
|
document.addEventListener('keydown', (event) => event.key === 'Escape' && hide())
|
|
},
|
|
|
|
destroy() {
|
|
clearTimeout(this.timer)
|
|
},
|
|
}))
|
|
})
|