Liquid Glass for the web: the rim refraction, light and motion of iOS 27 glass on any rounded element. React bindings and pill tabs. MIT.
| 4 |
| /* A hover pill for the rows of a glass menu or list: a well in the glass that |
| 5 | slides to the row under the pointer or with focus. Each edge rides its own |
| 6 | spring: the leading edge runs ahead and the trailing one catches up, so the pill |
| 7 | stretches toward the row it is heading for and gathers back up as it lands. */ |
| 8 |
| 9 | export type LiquidGlassHighlightOptions = { |
| 10 | /** Selector for the rows. Defaults to menu items and options. */ |
| 11 | items?: string | undefined; |
| 12 | }; |
| 13 |
| 14 | export type LiquidGlassHighlight = { |
| 15 | /** Places the pill again, for rows that moved without the pointer or focus moving. */ |
| 16 | update(): void; |
| 17 | destroy(): void; |
| 18 | }; |
| 19 |
| 20 | // Framer Motion's stiffness 700, damping 44 and stiffness 320, damping 30. |
| 21 | const LEAD: SpringConfig = { omega: 26.46, zeta: 0.83 }; |
| 22 | const TRAIL: SpringConfig = { omega: 17.89, zeta: 0.84 }; |
| 23 | // Leaving and coming back within this keeps the glide, so a brief overshoot off the |
| 24 | // menu doesn't make the pill jump when the pointer comes back. |
| 25 | const REAPPEAR_MS = 50; |
| 26 |
| 27 | type Edges = { left: number; top: number; right: number; bottom: number }; |
| 28 |
| 29 | /** |
| 30 | * Gives a glass surface's rows a sliding hover pill. The surface is the positioned |
| 31 | * element the glass is in. |
| 32 | * |
| 33 | * ```ts |
| 34 | * attachLiquidGlassHighlight(menu, { items: '[role="menuitem"]' }); |
| 35 | * ``` |
| 36 | */ |
| 37 | export function attachLiquidGlassHighlight(surface: HTMLElement, options: LiquidGlassHighlightOptions = {}): LiquidGlassHighlight { |
| 38 | const selector = options.items ?? '[role="menuitem"], [role="menuitemcheckbox"], [role="menuitemradio"], [role="option"]'; |
| 39 | const reduced = matchMedia("(prefers-reduced-motion: reduce)"); |
| 40 | const pill = document.createElement("span"); |
| 41 | pill.className = "liquid-glass-well liquid-glass-highlight"; |
| 42 | pill.setAttribute("aria-hidden", "true"); |
| 43 | // Over the glass, under the rows. |
| 44 | const glass = surface.querySelector(":scope > .liquid-glass"); |
| 45 | if (glass) glass.after(pill); |
| 46 | else surface.prepend(pill); |
| 47 |
| 48 | const edges = { left: spring(), top: spring(), right: spring(), bottom: spring() }; |
| 49 | let target: Edges | null = null; |
| 50 | let current: HTMLElement | null = null; |
| 51 | let hovered: HTMLElement | null = null; |
| 52 | let shown = false; |
| 53 | let hiddenAt = -Infinity; |
| 54 | let frame = 0; |
| 55 | let last = 0; |
| 56 | let carry = 0; |
| 57 | // Which edges lead, by the way the pill last moved. |
| 58 | let down = true; |
| 59 | let right = true; |
| 60 |
| 61 | const place = (item: HTMLElement): Edges => { |
| 62 | const frameBox = surface.getBoundingClientRect(); |
| 63 | const box = item.getBoundingClientRect(); |
| 64 | const left = box.left - frameBox.left - surface.clientLeft + surface.scrollLeft; |
| 65 | const top = box.top - frameBox.top - surface.clientTop + surface.scrollTop; |
| 66 | return { left, top, right: left + box.width, bottom: top + box.height }; |
| 67 | }; |
| 68 |
| 69 | const write = () => { |
| 70 | const { left, top, right: r, bottom } = edges; |
| 71 | pill.style.transform = `translate(${left.value.toFixed(2)}px, ${top.value.toFixed(2)}px)`; |
| 72 | pill.style.width = `${Math.max(0, r.value - left.value).toFixed(2)}px`; |
| 73 | pill.style.height = `${Math.max(0, bottom.value - top.value).toFixed(2)}px`; |
| 74 | }; |
| 75 |
| 76 | const jump = (to: Edges) => { |
| 77 | for (const side of ["left", "top", "right", "bottom"] as const) { |
| 78 | edges[side].value = to[side]; |
| 79 | edges[side].velocity = 0; |
| 80 | } |
| 81 | write(); |
| 82 | }; |
| 83 |
| 84 | const near = (edge: Spring, to: number) => Math.abs(edge.value - to) < 0.05 && Math.abs(edge.velocity) < 1; |
| 85 |
| 86 | const tick = (now: number) => { |
| 87 | carry += Math.min(Math.max(now - last, 0) / 1000, 0.05); |
| 88 | last = now; |
| 89 | const to = target; |
| 90 | if (!to) { |
| 91 | frame = 0; |
| 92 | return; |
| 93 | } |
| 94 | for (; carry >= STEP; carry -= STEP) { |
| 95 | stepSpring(edges.top, to.top, down ? TRAIL : LEAD, STEP); |
| 96 | stepSpring(edges.bottom, to.bottom, down ? LEAD : TRAIL, STEP); |
| 97 | stepSpring(edges.left, to.left, right ? TRAIL : LEAD, STEP); |
| 98 | stepSpring(edges.right, to.right, right ? LEAD : TRAIL, STEP); |
| 99 | } |
| 100 | write(); |
| 101 | if (near(edges.top, to.top) && near(edges.bottom, to.bottom) && near(edges.left, to.left) && near(edges.right, to.right)) { |
| 102 | jump(to); |
| 103 | frame = 0; |
| 104 | return; |
| 105 | } |
| 106 | frame = requestAnimationFrame(tick); |
| 107 | }; |
| 108 |
| 109 | const show = (item: HTMLElement) => { |
| 110 | const to = place(item); |
| 111 | current = item; |
| 112 | pill.style.borderRadius = getComputedStyle(item).borderRadius; |
| 113 | // Appearing from hidden snaps; moving between rows glides. |
| 114 | const glide = shown || performance.now() - hiddenAt < REAPPEAR_MS; |
| 115 | if (!glide || reduced.matches || !target) { |
| 116 | if (frame) cancelAnimationFrame(frame); |
| 117 | frame = 0; |
| 118 | target = to; |
| 119 | jump(to); |
| 120 | } else { |
| 121 | down = to.top >= target.top; |
| 122 | right = to.left >= target.left; |
| 123 | target = to; |
| 124 | if (!frame) { |
| 125 | last = performance.now(); |
| 126 | carry = 0; |
| 127 | frame = requestAnimationFrame(tick); |
| 128 | } |
| 129 | } |
| 130 | shown = true; |
| 131 | pill.toggleAttribute("data-shown", true); |
| 132 | }; |
| 133 |
| 134 | const hide = () => { |
| 135 | if (!shown) return; |
| 136 | shown = false; |
| 137 | current = null; |
| 138 | hiddenAt = performance.now(); |
| 139 | pill.removeAttribute("data-shown"); |
| 140 | }; |
| 141 |
| 142 | const itemOf = (node: EventTarget | null) => { |
| 143 | const item = node instanceof Element ? node.closest<HTMLElement>(selector) : null; |
| 144 | if (!item || !surface.contains(item)) return null; |
| 145 | if (item.hasAttribute("data-disabled") || item.getAttribute("aria-disabled") === "true" || item.hasAttribute("disabled")) return null; |
| 146 | return item; |
| 147 | }; |
| 148 |
| 149 | const onPointerMove = (event: PointerEvent) => { |
| 150 | const item = itemOf(event.target); |
| 151 | if (item === hovered) return; |
| 152 | hovered = item; |
| 153 | if (item) show(item); |
| 154 | }; |
| 155 | const onPointerLeave = () => { |
| 156 | hovered = null; |
| 157 | // Keyboard focus keeps its row. |
| 158 | const focused = itemOf(document.activeElement); |
| 159 | if (focused && focused.matches(":focus-visible")) show(focused); |
| 160 | else hide(); |
| 161 | }; |
| 162 | const onFocusIn = (event: FocusEvent) => { |
| 163 | const item = itemOf(event.target); |
| 164 | if (item && item !== current) show(item); |
| 165 | }; |
| 166 | const onFocusOut = (event: FocusEvent) => { |
| 167 | if (!hovered && !itemOf(event.relatedTarget)) hide(); |
| 168 | }; |
| 169 | // Rows measured while a menu poured out were scaled and off their place. |
| 170 | const onLanded = (event: Event) => { |
| 171 | if (event.target !== surface || !current?.isConnected || !target) return; |
| 172 | target = place(current); |
| 173 | jump(target); |
| 174 | }; |
| 175 |
| 176 | surface.addEventListener("pointermove", onPointerMove); |
| 177 | surface.addEventListener("pointerleave", onPointerLeave); |
| 178 | surface.addEventListener("focusin", onFocusIn); |
| 179 | surface.addEventListener("focusout", onFocusOut); |
| 180 | surface.addEventListener(GLASS_LANDED, onLanded); |
| 181 |
| 182 | return { |
| 183 | update() { |
| 184 | if (current?.isConnected) show(current); |
| 185 | else hide(); |
| 186 | }, |
| 187 | destroy() { |
| 188 | if (frame) cancelAnimationFrame(frame); |
| 189 | surface.removeEventListener("pointermove", onPointerMove); |
| 190 | surface.removeEventListener("pointerleave", onPointerLeave); |
| 191 | surface.removeEventListener("focusin", onFocusIn); |
| 192 | surface.removeEventListener("focusout", onFocusOut); |
| 193 | surface.removeEventListener(GLASS_LANDED, onLanded); |
| 194 | pill.remove(); |
| 195 | }, |
| 196 | }; |
| 197 | } |
| 198 |