Dawnline 0.15.0
Build packages / CI (push) Blocked by required conditions
Build packages / Version (push) Waiting to run
CI / Web (format, types, tests, bundles) (push) Blocked by required conditions
CI / ShellCheck (push) Blocked by required conditions
CI / ucode static checks (push) Blocked by required conditions
CI / ucode compiles and tests pass (OpenWrt 24.10) (push) Blocked by required conditions
CI / ucode compiles and tests pass (OpenWrt 25.12) (push) Blocked by required conditions
Build packages / CI (push) Blocked by required conditions
Build packages / Version (push) Waiting to run
CI / Web (format, types, tests, bundles) (push) Blocked by required conditions
CI / ShellCheck (push) Blocked by required conditions
CI / ucode static checks (push) Blocked by required conditions
CI / ucode compiles and tests pass (OpenWrt 24.10) (push) Blocked by required conditions
CI / ucode compiles and tests pass (OpenWrt 25.12) (push) Blocked by required conditions
- The device menu (⋯) is an iOS-style menu on frosted glass: the copy actions show the address they copy, Rename, Reconnect and Remove sit under them, and one highlight glides from row to row - The language picker shows flags; pick a language with a click, or press the pill and slide onto one. The flag and the code change in place - The page's QR code opens as a small card under its button, with Copy link and Share - All dropdowns look and behave the same: frosted glass, one open at a time, and a press outside, Escape or the button again closes them - Setting a device up: when it can't finish connecting or connects but passes no data, the sheet keeps just the status and what it means. No data now says the router drops its traffic because the address doesn't match, and the device's card says "Connected, but no data" right away instead of "Connected" - Setting a phone up from another device: Download and Copy for the connection sit on its row, next to the arrow - On iPhone and iPad the caret shows as soon as a field is focused, the page no longer zooms on a pinch or a double tap, and sheets and their animations run smoother - A press near the edge of a button or a card always counts, even as it shrinks under the pointer - Animations settle without a jump at the end - New green, orange and red - The focus ring shows only while moving around with the keyboard
This commit is contained in:
1 parent
a1915f070b
commit
ba231e7187
51 files changed
+1713
-1062
No files matched your search
@@ -23,6 +23,11 @@ export interface AccItem {
|
||||
val?: Node | null;
|
||||
/** The body shows the same numbers: the head drops them while open. */
|
||||
valHide?: boolean;
|
||||
/**
|
||||
* Actions on what the row holds, beside the chevron while it is open (gone while it is shut). Beside the head,
|
||||
* not in it: the head is a button itself.
|
||||
*/
|
||||
aside?: Node | null;
|
||||
body: (Node | null | false | undefined)[];
|
||||
}
|
||||
|
||||
@@ -139,20 +144,28 @@ export function accordion(
|
||||
);
|
||||
const r = h(
|
||||
'div',
|
||||
{ class: `acc-row${still ? ' still' : ''}${open ? ' open' : ''}` },
|
||||
{ class: `acc-row${still ? ' still' : ''}${open ? ' open' : ''}${it.aside ? ' has-aside' : ''}` },
|
||||
headEl,
|
||||
it.aside ? h('div', { class: 'acc-aside' }, it.aside) : null,
|
||||
h('div', { class: 'acc-body' }, ...kids),
|
||||
);
|
||||
acc.appendChild(r);
|
||||
return r;
|
||||
});
|
||||
const bodyOf = (r: HTMLElement) => r.querySelector('.acc-body') as HTMLElement;
|
||||
const corners = () =>
|
||||
rows.forEach((r, i) => {
|
||||
/** Corners and edges over the rows that show: a row stepped out (only()) leaves no gap or corner behind. */
|
||||
const corners = () => {
|
||||
const shown = rows.filter((r) => !r.hidden);
|
||||
shown.forEach((r, i) => {
|
||||
const op = r.classList.contains('open');
|
||||
r.classList.toggle('rt', i === 0 || op || rows[i - 1].classList.contains('open'));
|
||||
r.classList.toggle('rb', i === rows.length - 1 || op || rows[i + 1].classList.contains('open'));
|
||||
r.classList.toggle('rt', i === 0 || op || shown[i - 1].classList.contains('open'));
|
||||
r.classList.toggle('rb', i === shown.length - 1 || op || shown[i + 1].classList.contains('open'));
|
||||
});
|
||||
rows.forEach((r) => {
|
||||
r.classList.toggle('first', r === shown[0]);
|
||||
r.classList.toggle('last', r === shown[shown.length - 1]);
|
||||
});
|
||||
};
|
||||
const read = (x: HTMLElement) => {
|
||||
const cs = getComputedStyle(x);
|
||||
const out: Record<string, string> = {};
|
||||
|
||||
+11
-13
@@ -3,10 +3,12 @@
|
||||
// and blinks as macOS draws it; a selection is an accent wash with a handle at
|
||||
// each end, as iOS draws it, growing out of the caret and fading back into it.
|
||||
// The input stays the browser's own (typing, IME, its native selection, made
|
||||
// see-through): this only draws over it. On a touch screen the system draws its
|
||||
// own selection and handles, so ours stays off and the caret steps aside while
|
||||
// one shows. The look is in styles/controls.css; like the rest of the page this
|
||||
// writes styles through `el.style` only (the CSP drops style attributes).
|
||||
// see-through): this only draws over it. With a mouse or trackpad only: on a
|
||||
// touch screen the system's caret stays (in the accent), with its own selection
|
||||
// and handles. iOS doesn't tell a page reliably where its caret went (a tap in
|
||||
// the words, a press-and-drag): a drawn one would wait for the first key. The
|
||||
// look is in styles/controls.css; like the rest of the page this writes styles
|
||||
// through `el.style` only (the CSP drops style attributes).
|
||||
//
|
||||
// Places come from a hidden mirror with the input's own font: the width of the
|
||||
// text up to a position, less how far the input has scrolled. The pure parts
|
||||
@@ -87,15 +89,15 @@ export function places(f: FieldText, measure: (text: string) => number): Places
|
||||
const FONT = ['fontFamily', 'fontSize', 'fontWeight', 'letterSpacing', 'fontFeatureSettings', 'fontVariationSettings'] as const;
|
||||
|
||||
/**
|
||||
* Give the input of `field` (a `.field`) the glide caret and, with a fine pointer, the selection.
|
||||
* Give the input of `field` (a `.field`) the glide caret and the selection, with a mouse or trackpad.
|
||||
* Call it once, as the field is built (it measures only once the field is on the page).
|
||||
*/
|
||||
export function smoothCaret(field: HTMLElement) {
|
||||
const input = field.querySelector('input');
|
||||
if (!input) return;
|
||||
const fine = matchMedia('(pointer: fine)').matches;
|
||||
field.classList.add('own-caret');
|
||||
if (fine) field.classList.add('own-sel');
|
||||
// A touch screen: the system's caret (controls.css gives it the accent).
|
||||
if (!matchMedia('(pointer: fine)').matches) return;
|
||||
field.classList.add('own-caret', 'own-sel');
|
||||
const wash = h('span', { class: 'caret-sel' });
|
||||
const h0 = h('span', { class: 'caret-hd s' });
|
||||
const h1 = h('span', { class: 'caret-hd e' });
|
||||
@@ -187,7 +189,7 @@ export function smoothCaret(field: HTMLElement) {
|
||||
all.forEach((g) => jump(g, p.start));
|
||||
first = false;
|
||||
}
|
||||
if (p.range && fine) {
|
||||
if (p.range) {
|
||||
// Selecting: the wash grows out of where the caret is, and the caret steps aside. A wash still fading from
|
||||
// the last selection turns back instead and glides from its old ends to the new ones.
|
||||
if (!selecting) {
|
||||
@@ -204,10 +206,6 @@ export function smoothCaret(field: HTMLElement) {
|
||||
cur.t = p.caret;
|
||||
field.classList.add('selecting');
|
||||
caret.classList.remove('on', 'blink');
|
||||
} else if (p.range) {
|
||||
// A touch screen: the system's own selection shows.
|
||||
caret.classList.remove('on', 'blink');
|
||||
return;
|
||||
} else {
|
||||
// Collapsed: the wash fades where it stands, and its ends join the caret once it is gone.
|
||||
// A click puts the caret where it was clicked at once, as macOS does; typing and the arrows glide.
|
||||
|
||||
@@ -0,0 +1,84 @@
|
||||
// What every dropdown shares (the ⋯ menu, the pick pills' popups, the page's code): its look (.drop,
|
||||
// styles/drop.css: Liquid Glass, a soft even edge, a soft shadow on light only), one open at a time, the same gap to
|
||||
// its button and margin to the screen's edge, the same way in (a spring out of where it grows from) and out (back
|
||||
// towards it, from where it stands), and the same ways to shut it: a press outside it and its button, Escape (the
|
||||
// sheet under it stays; focus goes back to the button), a resize (at once). Each lays out and fills its own.
|
||||
|
||||
import { EASE_IN, POP, play } from '../motion';
|
||||
|
||||
/** From its button, and from the screen's edge. */
|
||||
export const GAP = 8;
|
||||
export const EDGE = 12;
|
||||
|
||||
let current: ((now?: boolean) => void) | null = null;
|
||||
|
||||
/** A dropdown is open (the page holds its repaints meanwhile). */
|
||||
export const dropOpen = () => !!current;
|
||||
|
||||
/** Shut the one open (`now`: at once: a sheet opening, the page drawn again in another language). */
|
||||
export function closeDrop(now = false) {
|
||||
current?.(now);
|
||||
}
|
||||
|
||||
/** Where an element stands now, mid-motion included. */
|
||||
function standing(el: Element): Keyframe {
|
||||
const s = getComputedStyle(el);
|
||||
return { opacity: s.opacity, transform: s.transform, filter: s.filter === 'none' ? 'blur(0px)' : s.filter };
|
||||
}
|
||||
|
||||
/** In: a spring out of its transform origin. */
|
||||
export function appear(el: Element) {
|
||||
play(
|
||||
el,
|
||||
[
|
||||
{ opacity: 0, transform: 'scale(.5)', filter: 'blur(6px)' },
|
||||
{ opacity: 1, transform: 'none', filter: 'blur(0px)' },
|
||||
],
|
||||
{ duration: POP.duration, easing: POP.easing },
|
||||
);
|
||||
}
|
||||
|
||||
/** Out: back towards its origin from where it stands, then gone. */
|
||||
export function vanish(el: Element) {
|
||||
const from = standing(el);
|
||||
el.getAnimations().forEach((a) => a.cancel());
|
||||
play(el, [from, { opacity: 0, transform: 'scale(.7)', filter: 'blur(4px)' }], {
|
||||
duration: 110,
|
||||
easing: EASE_IN,
|
||||
fill: 'forwards',
|
||||
}).onfinish = () => el.remove();
|
||||
}
|
||||
|
||||
/**
|
||||
* Open `panel` for `button`: another one open shuts at once, and the ways to shut this one are listened for.
|
||||
* `shut(now)` does the closing itself; what this returns is to be called first thing in it (it lets go of the
|
||||
* listeners, says the button is shut, and is false when the dropdown was shut already).
|
||||
*/
|
||||
export function opened(panel: Element, button: HTMLElement, shut: (now?: boolean) => void): () => boolean {
|
||||
if (current) current(true);
|
||||
const outside = (e: PointerEvent) => {
|
||||
const t = e.target as Node;
|
||||
if (!panel.contains(t) && !button.contains(t)) shut();
|
||||
};
|
||||
const esc = (e: KeyboardEvent) => {
|
||||
if (e.key !== 'Escape') return;
|
||||
e.stopPropagation();
|
||||
shut();
|
||||
button.focus({ preventScroll: true });
|
||||
};
|
||||
const resized = () => shut(true);
|
||||
document.addEventListener('pointerdown', outside, true);
|
||||
document.addEventListener('keydown', esc, true);
|
||||
addEventListener('resize', resized);
|
||||
button.setAttribute('aria-expanded', 'true');
|
||||
current = shut;
|
||||
return () => {
|
||||
if (current !== shut) return false;
|
||||
current = null;
|
||||
document.removeEventListener('pointerdown', outside, true);
|
||||
document.removeEventListener('keydown', esc, true);
|
||||
removeEventListener('resize', resized);
|
||||
button.setAttribute('aria-expanded', 'false');
|
||||
return true;
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
// The dropdowns' highlight (ui/drop.ts: the ⋯ menu's rows, a pick pill's segments): one for all of a dropdown's
|
||||
// items, under their icons and words, the glass's solid highlight, the same for every item. It comes in on the item
|
||||
// the pointer (the keyboard, or a finger sliding over from a pick pill) reaches, glides from item to item on GLIDE
|
||||
// taking each one's place, size and corners, presses in with it, and fades where the pointer leaves (or goes back to
|
||||
// the item it rests on: a pick pill's picked segment).
|
||||
|
||||
import { h } from '../dom';
|
||||
|
||||
export interface Glide {
|
||||
/** Light this item (null: none). */
|
||||
light(item: HTMLElement | null): void;
|
||||
}
|
||||
|
||||
/**
|
||||
* `host`: what holds the items and the highlight, positioned. `rest`: the item the highlight rests on (a pick pill's
|
||||
* picked segment): it starts there, and goes back there rather than fading when the pointer leaves.
|
||||
*/
|
||||
export function glide(host: HTMLElement, items: HTMLElement[], rest: HTMLElement | null = null): Glide {
|
||||
const hl = h('span', { class: 'drop-hl', 'aria-hidden': 'true' });
|
||||
host.prepend(hl);
|
||||
let hot: HTMLElement | null = null;
|
||||
const light = (item: HTMLElement | null) => {
|
||||
if (item === hot) return;
|
||||
hot?.classList.remove('hot');
|
||||
hot = item;
|
||||
if (!item) return void (hl.style.opacity = '0');
|
||||
item.classList.add('hot');
|
||||
// Gone altogether, it is placed at once and fades in there; still showing, it glides from where it stands.
|
||||
hl.classList.toggle('moving', +getComputedStyle(hl).opacity > 0.01);
|
||||
Object.assign(hl.style, {
|
||||
translate: `${item.offsetLeft}px ${item.offsetTop}px`,
|
||||
width: `${item.offsetWidth}px`,
|
||||
height: `${item.offsetHeight}px`,
|
||||
borderRadius: getComputedStyle(item).borderRadius,
|
||||
opacity: '1',
|
||||
});
|
||||
};
|
||||
for (const it of items) {
|
||||
it.addEventListener('pointerenter', () => light(it));
|
||||
it.addEventListener('focus', () => it.matches(':focus-visible') && light(it));
|
||||
it.addEventListener('pointerdown', () => {
|
||||
hl.classList.add('down');
|
||||
document.addEventListener('pointerup', () => hl.classList.remove('down'), { once: true, capture: true });
|
||||
});
|
||||
}
|
||||
host.addEventListener('pointerleave', () => light(rest));
|
||||
light(rest);
|
||||
return { light: (item) => light(item ?? rest) };
|
||||
}
|
||||
+92
-101
@@ -1,123 +1,114 @@
|
||||
// The ⋯ menu: a small panel under (or over) its button, items with an icon,
|
||||
// a highlight that glides after the pointer and takes the item's colour
|
||||
// (orange for Reconnect, red for Remove). A choice among options (the
|
||||
// language) marks the current one with a check instead of icons. One at a
|
||||
// time; it closes on a press outside, on scroll, on Escape and when its button
|
||||
// is pressed again.
|
||||
// The ⋯ menu: an iOS 26-style menu under its button (over it where the screen
|
||||
// ends): Liquid Glass (the page blurred behind), a soft even edge.
|
||||
// Laid out as iOS 26's: rows (an icon, the label and a line under it where the
|
||||
// item has one: the address it copies), a thin line between groups; Reconnect
|
||||
// orange, Remove red. The dropdowns' highlight glides from row to row. It springs out from the
|
||||
// button's side and drops back towards it. Shut by a
|
||||
// press outside, Escape, its button again or a choice. One at a time.
|
||||
|
||||
import { h, ic } from '../dom';
|
||||
import { EASE_IN, POP, play } from '../motion';
|
||||
import { appear, closeDrop, EDGE, GAP, opened, vanish } from './drop';
|
||||
import { glide } from './glide';
|
||||
|
||||
export interface MenuItem {
|
||||
label: string;
|
||||
/** An action's icon; an option (`checked` set) shows a check in its place, on the current one only. */
|
||||
icon?: string;
|
||||
checked?: boolean;
|
||||
/** Its line icon. */
|
||||
icon: string;
|
||||
/** Under the label: what it acts on (the address it copies). */
|
||||
sub?: string;
|
||||
tone?: 'warn' | 'bad';
|
||||
run: () => unknown;
|
||||
}
|
||||
|
||||
let menuEl: HTMLElement | null = null;
|
||||
/** The button the open menu belongs to: pressing it again closes the menu. */
|
||||
let anchorEl: HTMLElement | null = null;
|
||||
|
||||
export const menuOpen = () => !!menuEl;
|
||||
|
||||
/** `now`: gone at once (Escape, or another menu opening). */
|
||||
export function closeMenu(now = false) {
|
||||
const m = menuEl;
|
||||
if (!m) return;
|
||||
menuEl = null;
|
||||
anchorEl?.setAttribute('aria-expanded', 'false');
|
||||
anchorEl = null;
|
||||
if (now) {
|
||||
m.remove();
|
||||
return;
|
||||
/** `all`: null between groups (kept only between items); `title`: what it acts on (the device), for a screen reader. */
|
||||
export function openMenu(anchor: HTMLElement, all: (MenuItem | null)[], title: string) {
|
||||
// Its button again: it shuts.
|
||||
if (anchor.getAttribute('aria-expanded') === 'true') return closeDrop();
|
||||
const groups: MenuItem[][] = [[]];
|
||||
for (const it of all) {
|
||||
if (it) groups[groups.length - 1].push(it);
|
||||
else if (groups[groups.length - 1].length) groups.push([]);
|
||||
}
|
||||
// From where it stands: one closed while it still pops in (a scroll, a second press) goes from there.
|
||||
const s = getComputedStyle(m);
|
||||
const a = play(
|
||||
m,
|
||||
[
|
||||
{ opacity: s.opacity, transform: s.transform, filter: s.filter },
|
||||
{ opacity: 0, transform: 'scale(.96)', filter: 'blur(3px)' },
|
||||
],
|
||||
{
|
||||
duration: 120,
|
||||
easing: EASE_IN,
|
||||
fill: 'forwards',
|
||||
},
|
||||
);
|
||||
m.getAnimations().forEach((x) => x !== a && x.cancel());
|
||||
a.onfinish = () => m.remove();
|
||||
}
|
||||
if (!groups[groups.length - 1].length) groups.pop();
|
||||
|
||||
/** `items`: null is a separator, kept only between items. */
|
||||
export function openMenu(anchor: HTMLElement, all: (MenuItem | null)[]) {
|
||||
if (anchorEl === anchor && menuEl) return closeMenu();
|
||||
closeMenu(true);
|
||||
const items = all.filter((it, i) => it || (i > 0 && all[i - 1] && all.slice(i + 1).some(Boolean)));
|
||||
const hl = h('span', { class: 'menu-hl' });
|
||||
const m = h('div', { class: 'menu', role: 'menu' }, hl);
|
||||
for (const it of items) {
|
||||
if (!it) {
|
||||
m.appendChild(h('div', { class: 'menu-sep' }));
|
||||
continue;
|
||||
}
|
||||
const rows: HTMLElement[] = [];
|
||||
const item = (it: MenuItem, cls: string, ...kids: (Node | null)[]) => {
|
||||
const b = h(
|
||||
'button',
|
||||
{
|
||||
type: 'button',
|
||||
role: it.checked === undefined ? 'menuitem' : 'menuitemradio',
|
||||
'aria-checked': it.checked === undefined ? null : String(it.checked),
|
||||
class: `mi ${it.tone ?? ''}`,
|
||||
role: 'menuitem',
|
||||
class: `${cls}${it.tone ? ` ${it.tone}` : ''}`,
|
||||
onclick: () => {
|
||||
closeMenu();
|
||||
close();
|
||||
it.run();
|
||||
},
|
||||
},
|
||||
it.checked === undefined ? ic(it.icon ?? '') : ic('check', it.checked ? 'i' : 'i off'),
|
||||
h('span', {}, it.label),
|
||||
...kids,
|
||||
);
|
||||
// The highlight comes in where the pointer is, then glides from item to item. Whether it shows is read off the
|
||||
// screen: one still fading out (the pointer left and came back) turns back and glides from where it stands;
|
||||
// only one gone altogether is placed at once.
|
||||
b.addEventListener('pointerenter', () => {
|
||||
const seen = +getComputedStyle(hl).opacity > 0.01;
|
||||
hl.className = `menu-hl${seen ? ' moving' : ''} ${it.tone ?? ''}`;
|
||||
hl.style.transform = `translateY(${b.offsetTop - 6}px)`;
|
||||
hl.style.opacity = '1';
|
||||
});
|
||||
m.appendChild(b);
|
||||
}
|
||||
m.addEventListener('pointerleave', () => (hl.style.opacity = '0'));
|
||||
document.body.appendChild(m);
|
||||
const r = anchor.getBoundingClientRect();
|
||||
const w = m.offsetWidth;
|
||||
const mh = m.offsetHeight;
|
||||
// Under the button, or over it where the screen ends: it grows out of the button either way.
|
||||
const below = r.bottom + 8 + mh < innerHeight - 8;
|
||||
m.style.left = `${Math.max(12, Math.min(r.right - w, innerWidth - w - 12))}px`;
|
||||
m.style.top = `${below ? r.bottom + 8 : r.top - 8 - mh}px`;
|
||||
m.style.transformOrigin = `${below ? 'top' : 'bottom'} right`;
|
||||
play(
|
||||
m,
|
||||
[
|
||||
{ opacity: 0, transform: 'scale(.9)', filter: 'blur(4px)' },
|
||||
{ opacity: 1, transform: 'none', filter: 'none' },
|
||||
],
|
||||
{
|
||||
duration: POP.duration,
|
||||
easing: POP.easing,
|
||||
},
|
||||
rows.push(b);
|
||||
return b;
|
||||
};
|
||||
const sections: HTMLElement[] = [
|
||||
...groups.map((g) =>
|
||||
h(
|
||||
'div',
|
||||
{ class: 'pop-grp' },
|
||||
...g.map((it) =>
|
||||
item(
|
||||
it,
|
||||
'mi',
|
||||
ic(it.icon),
|
||||
h('span', { class: 'mi-b' }, h('span', { class: 'mi-l' }, it.label), it.sub ? h('span', { class: 'mi-s' }, it.sub) : null),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
];
|
||||
// A thin line between sections (iOS's, inset; the rows themselves sit in a 16 px inset).
|
||||
const body = h(
|
||||
'div',
|
||||
{ class: 'pop-body' },
|
||||
...sections.flatMap((sec, i) => (i ? [h('div', { class: 'pop-sep', 'aria-hidden': 'true' }, h('span')), sec] : [sec])),
|
||||
);
|
||||
menuEl = m;
|
||||
anchorEl = anchor;
|
||||
anchor.setAttribute('aria-expanded', 'true');
|
||||
}
|
||||
const pop = h('div', { class: 'pop drop', role: 'menu', 'aria-label': title }, body);
|
||||
document.body.append(pop);
|
||||
|
||||
document.addEventListener('pointerdown', (e) => {
|
||||
const t = e.target as Element;
|
||||
if (menuEl && !menuEl.contains(t) && !anchorEl?.contains(t)) closeMenu();
|
||||
});
|
||||
addEventListener('scroll', () => closeMenu(), { passive: true });
|
||||
// Under the button where it fits, else over it; centred on it, inside the screen's edge, growing from its side.
|
||||
const k = anchor.getBoundingClientRect();
|
||||
const w = Math.min(pop.offsetWidth, innerWidth - EDGE * 2);
|
||||
pop.style.width = `${w}px`;
|
||||
const ht = pop.offsetHeight;
|
||||
const mid = k.left + k.width / 2;
|
||||
const left = Math.max(EDGE, Math.min(mid - w / 2, innerWidth - EDGE - w));
|
||||
const below = k.bottom + GAP + ht <= innerHeight - EDGE || k.top - GAP - ht < EDGE;
|
||||
const top = below ? k.bottom + GAP : k.top - GAP - ht;
|
||||
Object.assign(pop.style, {
|
||||
left: `${left + scrollX}px`,
|
||||
top: `${top + scrollY}px`,
|
||||
transformOrigin: `${mid - left}px ${below ? 0 : ht}px`,
|
||||
});
|
||||
|
||||
appear(pop);
|
||||
const release = opened(pop, anchor, close);
|
||||
|
||||
// The dropdowns' one highlight for the rows (ui/glide.ts).
|
||||
glide(body, rows);
|
||||
|
||||
// Arrows move through the rows (the rest of the keys and presses are the dropdown's: ui/drop.ts).
|
||||
const keys = (e: KeyboardEvent) => {
|
||||
if (e.key !== 'ArrowDown' && e.key !== 'ArrowUp') return;
|
||||
e.preventDefault();
|
||||
const i = rows.indexOf(document.activeElement as HTMLElement);
|
||||
const j = i < 0 ? (e.key === 'ArrowDown' ? 0 : rows.length - 1) : i + (e.key === 'ArrowDown' ? 1 : -1);
|
||||
rows[Math.max(0, Math.min(rows.length - 1, j))]?.focus({ preventScroll: true });
|
||||
};
|
||||
document.addEventListener('keydown', keys, true);
|
||||
|
||||
function close(instant = false) {
|
||||
if (!release()) return;
|
||||
document.removeEventListener('keydown', keys, true);
|
||||
if (instant || !anchor.isConnected) pop.remove();
|
||||
else vanish(pop);
|
||||
}
|
||||
}
|
||||
+33
-21
@@ -121,7 +121,7 @@ function finish(el: HTMLElement) {
|
||||
clearTimeout(r.timer);
|
||||
runs.delete(el);
|
||||
el.getAnimations().forEach((a) => a.cancel());
|
||||
el.classList.remove('mt', 'mt-ib');
|
||||
el.classList.remove('mt', 'mt-ib', 'mt-run');
|
||||
el.textContent = r.text;
|
||||
}
|
||||
|
||||
@@ -162,6 +162,33 @@ export interface MorphOpts {
|
||||
color?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* A letter of a morph leaving: it lifts away as it blurs out (`delay`: its place in the sweep; `from`: how it
|
||||
* shows). Also what goes with the words as one more letter (a pick pill's flag), so both move as one.
|
||||
*/
|
||||
export function letterOut(el: Element, delay = 0, from: Look = SHOWN): Animation {
|
||||
return play(
|
||||
el,
|
||||
[
|
||||
{ opacity: from.opacity, filter: from.filter, transform: 'none' },
|
||||
{ opacity: 0, filter: 'blur(4px)', transform: 'translateY(-0.45em)' },
|
||||
],
|
||||
{ duration: 220, delay, easing: EASE_IN, fill: 'forwards' },
|
||||
);
|
||||
}
|
||||
|
||||
/** A letter of a morph coming in: it rises in as it sharpens, after the old ones start to go. */
|
||||
export function letterIn(el: Element, delay = 0): Animation {
|
||||
return play(
|
||||
el,
|
||||
[
|
||||
{ opacity: 0, filter: 'blur(4px)', transform: 'translateY(0.45em)' },
|
||||
{ opacity: 1, filter: 'blur(0px)', transform: 'none' },
|
||||
],
|
||||
{ duration: 380, delay: 120 + delay, easing: EASE_OUT, fill: 'backwards' },
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* `el`'s text turns into `next`, letter by letter: the letters both say stay
|
||||
* and slide to their new places, the others lift away or rise in. `from`: what
|
||||
@@ -196,6 +223,9 @@ export function morphText(el: HTMLElement, next: string, from = textOf(el), o: M
|
||||
const flow = whole && cs.display === 'inline';
|
||||
if (cs.display === 'inline' && !whole) el.classList.add('mt-ib');
|
||||
if (cs.position === 'static') el.classList.add('mt');
|
||||
// A box that clips its text (one line ending in an ellipsis) lets it show whole while it changes: the letters
|
||||
// rise from under the line and blur, and the width glides past the new ones. Clipped again once done.
|
||||
el.classList.add('mt-run');
|
||||
let r0: DOMRect | null = live ? midway : null;
|
||||
let t0 = live?.at ?? at ?? null;
|
||||
let at0: number[] = [];
|
||||
@@ -316,26 +346,8 @@ export function morphText(el: HTMLElement, next: string, from = textOf(el), o: M
|
||||
// A long text keeps the same overall sweep: the steps shorten instead.
|
||||
const so = Math.min(12, 160 / Math.max(1, out.length));
|
||||
const si = Math.min(22, 260 / Math.max(1, inn.length));
|
||||
out.forEach((c, i) =>
|
||||
play(
|
||||
c,
|
||||
[
|
||||
{ opacity: s0.opacity, filter: s0.filter, transform: 'none' },
|
||||
{ opacity: 0, filter: 'blur(4px)', transform: 'translateY(-0.45em)' },
|
||||
],
|
||||
{ duration: 220, delay: i * so, easing: EASE_IN, fill: 'forwards' },
|
||||
),
|
||||
);
|
||||
inn.forEach((c, i) =>
|
||||
play(
|
||||
c,
|
||||
[
|
||||
{ opacity: 0, filter: 'blur(4px)', transform: 'translateY(0.45em)' },
|
||||
{ opacity: 1, filter: 'blur(0px)', transform: 'none' },
|
||||
],
|
||||
{ duration: 380, delay: 120 + i * si, easing: EASE_OUT, fill: 'backwards' },
|
||||
),
|
||||
);
|
||||
out.forEach((c, i) => letterOut(c, i * so, s0));
|
||||
inn.forEach((c, i) => letterIn(c, i * si));
|
||||
const ends =
|
||||
Math.max(220 + out.length * so, inn.length ? 120 + inn.length * si + 380 : 0, glides || pairs.length ? SMOOTH.duration : 0) + 40;
|
||||
runs.set(el, { text: next, timer: window.setTimeout(() => finish(el), ends) });
|
||||
|
||||
@@ -0,0 +1,323 @@
|
||||
// A pill that picks one of a few options (the language in the breadcrumb; the demo's set-up state): what is
|
||||
// picked and a chevron. Its popup, a dropdown as every other (ui/drop.ts: its glass, its gap, its way in and out,
|
||||
// what shuts it), is a row of segments in one pill frame beside it, its tail one shape with it, pointing at the pill. A click opens it and another picks; or press the pill, slide onto an option and let go
|
||||
// there (a quick pick: let go anywhere else after sliding and nothing changes). The chevron turns up as it opens
|
||||
// and on round to down as it shuts; what the pill says morphs into the one picked. Fixed to the screen (it may open
|
||||
// on a sheet), it also shuts on a wheel or a swipe, and on Tab.
|
||||
|
||||
import { h, ic } from '../dom';
|
||||
import { EASE_IN, play } from '../motion';
|
||||
import { appear, EDGE, GAP, opened, vanish } from './drop';
|
||||
import { glide } from './glide';
|
||||
import { letterIn, letterOut, morphText, setText } from './morph';
|
||||
|
||||
export interface PickOption<T> {
|
||||
value: T;
|
||||
label: () => string;
|
||||
/** Before the label, in the popup and on the pill (a dot, a flag): made anew each time it shows. */
|
||||
lead?: () => Element;
|
||||
}
|
||||
|
||||
export interface PickPill<T> {
|
||||
el: HTMLButtonElement;
|
||||
/** Show this value, without motion (it changed elsewhere). */
|
||||
show(v: T): void;
|
||||
close(): void;
|
||||
}
|
||||
|
||||
interface PickOpts<T> {
|
||||
/** The pill's own class (its look). */
|
||||
cls: string;
|
||||
label: () => string;
|
||||
options: PickOption<T>[];
|
||||
value: T;
|
||||
/** What the pill says for a value: the option's label unless given (the language's code). */
|
||||
short?: (v: T) => string;
|
||||
pick: (v: T) => void;
|
||||
/** Where the popup goes first: above the pill (default) or under it; the other side where it does not fit. */
|
||||
prefer?: 'above' | 'below';
|
||||
}
|
||||
|
||||
/** The tail: its height and half its width at the base. */
|
||||
const TAIL = 6;
|
||||
const HALF = 10;
|
||||
|
||||
/**
|
||||
* The popup's outline, `w` × `h`: a pill of radius `r` with its tail at `x` on the top edge (`up`) or the bottom
|
||||
* one, its shoulders leaving the edge level (so it flows out of the frame) and its tip rounded.
|
||||
*/
|
||||
function outline(w: number, ht: number, r: number, x: number, up: boolean): string {
|
||||
const y0 = up ? TAIL : 0;
|
||||
const y1 = up ? ht : ht - TAIL;
|
||||
const tip = up ? 0 : ht;
|
||||
const n = (v: number) => Math.round(v * 100) / 100;
|
||||
const tail = (a: number, b: number, base: number) =>
|
||||
`L ${n(a)} ${base} C ${n(a + (b - a) / 4)} ${base} ${n(x - (b - a) / 8)} ${tip} ${n(x)} ${tip} ` +
|
||||
`C ${n(x + (b - a) / 8)} ${tip} ${n(b - (b - a) / 4)} ${base} ${n(b)} ${base} `;
|
||||
return (
|
||||
`M ${r} ${y0} ${up ? tail(x - HALF, x + HALF, y0) : ''}L ${w - r} ${y0} A ${r} ${r} 0 0 1 ${w - r} ${y1} ` +
|
||||
`${up ? '' : tail(x + HALF, x - HALF, y1)}L ${r} ${y1} A ${r} ${r} 0 0 1 ${r} ${y0} Z`
|
||||
);
|
||||
}
|
||||
|
||||
interface Open {
|
||||
el: HTMLElement;
|
||||
pop: HTMLElement;
|
||||
close(): void;
|
||||
choose(b: HTMLElement): void;
|
||||
/** The dropdowns' highlight on this segment (ui/glide.ts). */
|
||||
light(b: HTMLElement | null): void;
|
||||
/** A press on the pill under way: was the popup open before it, and has it slid since. */
|
||||
press: { wasOpen: boolean; x: number; y: number; moved: boolean } | null;
|
||||
}
|
||||
let open: Open | null = null;
|
||||
|
||||
/** Gone from where it stands, so one leaving while it still comes in turns back from there. */
|
||||
function leave(el: Element, frames: Keyframe, ms: number) {
|
||||
const s = getComputedStyle(el);
|
||||
el.getAnimations().forEach((a) => a.cancel());
|
||||
play(
|
||||
el,
|
||||
[
|
||||
{ opacity: s.opacity, transform: s.transform, filter: s.filter },
|
||||
{ opacity: 0, ...frames },
|
||||
],
|
||||
{
|
||||
duration: ms,
|
||||
easing: EASE_IN,
|
||||
fill: 'forwards',
|
||||
},
|
||||
).onfinish = () => el.remove();
|
||||
}
|
||||
|
||||
const optionAt = (x: number, y: number) =>
|
||||
open ? (document.elementFromPoint(x, y)?.closest<HTMLElement>('.pp-pop button') ?? null) : null;
|
||||
|
||||
let wired = false;
|
||||
function wire() {
|
||||
if (wired) return;
|
||||
wired = true;
|
||||
document.addEventListener(
|
||||
'pointermove',
|
||||
(e) => {
|
||||
const p = open?.press;
|
||||
if (!open || !p) return;
|
||||
if (Math.hypot(e.clientX - p.x, e.clientY - p.y) > 6) p.moved = true;
|
||||
open.light(optionAt(e.clientX, e.clientY));
|
||||
},
|
||||
true,
|
||||
);
|
||||
document.addEventListener(
|
||||
'pointerup',
|
||||
(e) => {
|
||||
const o = open;
|
||||
const p = o?.press;
|
||||
if (!o || !p) return;
|
||||
o.press = null;
|
||||
const at = optionAt(e.clientX, e.clientY);
|
||||
if (at && o.pop.contains(at)) return o.choose(at);
|
||||
const onPill = o.el.contains(document.elementFromPoint(e.clientX, e.clientY));
|
||||
// A click on the pill: it opened and stays; open already, it shuts. Slid off and let go elsewhere: nothing.
|
||||
if (onPill ? p.wasOpen && !p.moved : p.moved) o.close();
|
||||
},
|
||||
true,
|
||||
);
|
||||
document.addEventListener('pointercancel', () => open && (open.press = null), true);
|
||||
document.addEventListener(
|
||||
'keydown',
|
||||
(e) => {
|
||||
if (!open) return;
|
||||
if (e.key === 'Tab') open.close();
|
||||
else if ((e.key === 'ArrowLeft' || e.key === 'ArrowRight') && open.pop.contains(document.activeElement)) {
|
||||
e.preventDefault();
|
||||
const bs = [...open.pop.querySelectorAll<HTMLElement>('button')];
|
||||
const i = bs.indexOf(document.activeElement as HTMLElement) + (e.key === 'ArrowRight' ? 1 : -1);
|
||||
bs[Math.max(0, Math.min(bs.length - 1, i))].focus({ preventScroll: true });
|
||||
}
|
||||
},
|
||||
true,
|
||||
);
|
||||
for (const ev of ['wheel', 'touchmove'])
|
||||
addEventListener(ev, (e) => open && !open.pop.contains(e.target as Node) && !open.el.contains(e.target as Node) && open.close(), {
|
||||
passive: true,
|
||||
capture: true,
|
||||
});
|
||||
}
|
||||
|
||||
export function pickPill<T>(o: PickOpts<T>): PickPill<T> {
|
||||
wire();
|
||||
let value = o.value;
|
||||
const opt = (v: T) => o.options.find((x) => x.value === v) ?? o.options[0];
|
||||
const words = (v: T) => (o.short ? o.short(v) : opt(v).label());
|
||||
const lead = h('span', { class: 'pp-lead' });
|
||||
const lbl = h('span', { class: 'pp-t' });
|
||||
const chev = ic('chevron', 'i pp-chev') as SVGElement;
|
||||
const el = h(
|
||||
'button',
|
||||
{ type: 'button', class: `pp ${o.cls}`, 'aria-haspopup': 'menu', 'aria-expanded': 'false' },
|
||||
lead,
|
||||
lbl,
|
||||
chev,
|
||||
) as HTMLButtonElement;
|
||||
/** What it leads with follows the value; `swap`: the old one blurs away over the new one as that pops in. */
|
||||
const paint = (swap = false) => {
|
||||
const n = opt(value).lead?.();
|
||||
const old = swap ? [...lead.children].filter((c) => !c.classList.contains('pp-gone')) : [];
|
||||
lead.replaceChildren(...old, ...(n ? [n] : []));
|
||||
lead.hidden = !n && !old.length;
|
||||
el.setAttribute('aria-label', o.label());
|
||||
if (!swap) return;
|
||||
// As the first letter of the words beside it: it goes and comes with them (ui/morph.ts).
|
||||
for (const c of old) {
|
||||
c.classList.add('pp-gone');
|
||||
letterOut(c).onfinish = () => c.remove();
|
||||
}
|
||||
if (n) letterIn(n);
|
||||
};
|
||||
setText(lbl, words(value));
|
||||
paint();
|
||||
|
||||
// The chevron turns by its own angle, so shutting goes on round (up, left, down) rather than back; once down it
|
||||
// is 90° again, unseen.
|
||||
chev.addEventListener('transitionend', () => {
|
||||
if (chev.style.rotate !== '-270deg') return;
|
||||
chev.style.transition = 'none';
|
||||
chev.style.rotate = '';
|
||||
chev.getBoundingClientRect();
|
||||
chev.style.transition = '';
|
||||
});
|
||||
|
||||
let me: Open | null = null;
|
||||
let shade: HTMLElement | null = null;
|
||||
/** Lets go of what every dropdown listens for (ui/drop.ts). */
|
||||
let release: () => boolean = () => false;
|
||||
const close = (now = false) => {
|
||||
if (!me || !release()) return;
|
||||
if (now) {
|
||||
me.pop.remove();
|
||||
shade?.remove();
|
||||
} else {
|
||||
vanish(me.pop);
|
||||
if (shade) leave(shade, {}, 110);
|
||||
}
|
||||
shade = null;
|
||||
if (open === me) open = null;
|
||||
me = null;
|
||||
chev.style.rotate = '-270deg';
|
||||
};
|
||||
const choose = (b: HTMLElement) => {
|
||||
const v = o.options[Number(b.dataset.i)].value;
|
||||
close();
|
||||
if (v === value) return;
|
||||
const was = words(value);
|
||||
value = v;
|
||||
// Picking may draw the page again (a language): the pill is the same one, put back; its words morph after.
|
||||
o.pick(v);
|
||||
paint(true);
|
||||
morphText(lbl, words(v), was);
|
||||
};
|
||||
|
||||
const show = (focus: boolean) => {
|
||||
const pop = h(
|
||||
'div',
|
||||
{ class: 'pp-pop drop', role: 'menu', 'aria-label': o.label() },
|
||||
...o.options.map((x, i) =>
|
||||
h(
|
||||
'button',
|
||||
{
|
||||
type: 'button',
|
||||
role: 'menuitemradio',
|
||||
tabindex: '-1',
|
||||
'data-i': String(i),
|
||||
'aria-checked': String(x.value === value),
|
||||
onclick: (e: Event) => choose(e.currentTarget as HTMLElement),
|
||||
},
|
||||
x.lead?.() ?? null,
|
||||
h('span', {}, x.label()),
|
||||
),
|
||||
),
|
||||
);
|
||||
document.body.append(pop);
|
||||
// Centred on the pill, inside its sheet where it fits (else the screen), the tail on the pill's middle and its
|
||||
// tip the dropdowns' gap from it.
|
||||
const r = el.getBoundingClientRect();
|
||||
const box = el.closest('.sheet')?.getBoundingClientRect() ?? { left: 0, right: innerWidth };
|
||||
const w = pop.offsetWidth;
|
||||
const ht = pop.offsetHeight;
|
||||
const mid = r.left + r.width / 2;
|
||||
const lo = Math.max(EDGE, box.left + 10);
|
||||
const hi = Math.min(innerWidth - EDGE, box.right - 10);
|
||||
const left = Math.max(lo, Math.min(mid - w / 2, hi - w));
|
||||
// The tail is part of the glass (one shape with the frame, a clip path): the popup grows by it towards the pill.
|
||||
const all = ht + TAIL;
|
||||
const above = r.top - GAP - all >= EDGE;
|
||||
const under = r.bottom + GAP + all <= innerHeight - EDGE;
|
||||
const below = o.prefer === 'below' ? under || !above : !above;
|
||||
const top = below ? r.bottom + GAP : r.top - GAP - all;
|
||||
pop.classList.add(below ? 'below' : 'above');
|
||||
const x = Math.max(ht / 2 + HALF, Math.min(mid - left, w - ht / 2 - HALF));
|
||||
const shape = outline(w, all, ht / 2, x, below);
|
||||
Object.assign(pop.style, {
|
||||
left: `${left}px`,
|
||||
top: `${top}px`,
|
||||
height: `${all}px`,
|
||||
clipPath: `path('${shape}')`,
|
||||
transformOrigin: `${x}px ${below ? 0 : all}px`,
|
||||
});
|
||||
const ring = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
|
||||
ring.setAttribute('class', 'pp-ring');
|
||||
ring.setAttribute('aria-hidden', 'true');
|
||||
ring.setAttribute('viewBox', `0 0 ${w} ${all}`);
|
||||
const edge = document.createElementNS('http://www.w3.org/2000/svg', 'path');
|
||||
edge.setAttribute('d', shape);
|
||||
ring.append(edge);
|
||||
pop.append(ring);
|
||||
// The glass's shadow (light only, tokens.css): the clip path would cut one off the popup, so a clear pill under
|
||||
// its frame casts it (an outer shadow shows only outside its box).
|
||||
shade = h('div', { class: 'pp-shade', 'aria-hidden': 'true' });
|
||||
Object.assign(shade.style, {
|
||||
left: `${left}px`,
|
||||
top: `${top + (below ? TAIL : 0)}px`,
|
||||
width: `${w}px`,
|
||||
height: `${ht}px`,
|
||||
});
|
||||
pop.before(shade);
|
||||
play(shade, [{ opacity: 0 }, { opacity: 1 }], { duration: 220, easing: 'ease-out' });
|
||||
appear(pop);
|
||||
release = opened(pop, el, close);
|
||||
// The highlight rests on the picked segment: it is what says which one is picked.
|
||||
const { light } = glide(pop, [...pop.querySelectorAll<HTMLElement>('button')], pop.querySelector<HTMLElement>('[aria-checked="true"]'));
|
||||
me = { el, pop, close, choose, light, press: null };
|
||||
open = me;
|
||||
chev.style.rotate = '-90deg';
|
||||
if (focus) pop.querySelector<HTMLElement>('[aria-checked="true"]')?.focus({ preventScroll: true });
|
||||
};
|
||||
|
||||
let pressedAt = 0;
|
||||
el.addEventListener('pointerdown', (e) => {
|
||||
if (e.button !== 0) return;
|
||||
pressedAt = performance.now();
|
||||
const wasOpen = !!me;
|
||||
if (!me) show(false);
|
||||
me!.press = { wasOpen, x: e.clientX, y: e.clientY, moved: false };
|
||||
// A touch keeps to what it pressed: let it go, so what is under the finger can be found as it slides.
|
||||
if (el.hasPointerCapture(e.pointerId)) el.releasePointerCapture(e.pointerId);
|
||||
});
|
||||
// The keyboard's click (Return, Space): a press already did what it does.
|
||||
el.addEventListener('click', () => {
|
||||
if (performance.now() - pressedAt < 800) return;
|
||||
if (me) close();
|
||||
else show(true);
|
||||
});
|
||||
|
||||
return {
|
||||
el,
|
||||
show(v) {
|
||||
if (v === value) return;
|
||||
value = v;
|
||||
setText(lbl, words(v));
|
||||
paint();
|
||||
},
|
||||
close,
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
// A control pressed scales down (its :active look, in the stylesheets): let go near its edge after a moment and
|
||||
// the pointer is outside it by then, so the browser gives the click to what holds both and nothing happens. Here
|
||||
// what was pressed keeps the footprint it had when pressed: let go inside that and it gets the click, the
|
||||
// browser's own (to the ancestor) dropped. A mouse or trackpad only: a touch stays with what it pressed.
|
||||
|
||||
const PRESSABLE = 'button, a[href], [role="button"], label';
|
||||
|
||||
let pressed: { el: HTMLElement; at: DOMRect } | null = null;
|
||||
/** The browser's own click of this press, to the ancestor, is to be dropped. */
|
||||
let swallow = false;
|
||||
|
||||
export function keepPressFootprint() {
|
||||
document.addEventListener(
|
||||
'pointerdown',
|
||||
(e) => {
|
||||
const el = e.pointerType === 'mouse' && e.button === 0 ? (e.target as Element).closest?.<HTMLElement>(PRESSABLE) : null;
|
||||
pressed = el ? { el, at: el.getBoundingClientRect() } : null;
|
||||
},
|
||||
true,
|
||||
);
|
||||
document.addEventListener(
|
||||
'pointerup',
|
||||
(e) => {
|
||||
const p = pressed;
|
||||
pressed = null;
|
||||
if (!p || p.el.contains(e.target as Node)) return;
|
||||
const { at } = p;
|
||||
if (e.clientX < at.left || e.clientX > at.right || e.clientY < at.top || e.clientY > at.bottom) return;
|
||||
swallow = true;
|
||||
// The click, if the browser sends one, comes right after this; anything later is another press.
|
||||
setTimeout(() => (swallow = false));
|
||||
p.el.click();
|
||||
},
|
||||
true,
|
||||
);
|
||||
document.addEventListener(
|
||||
'click',
|
||||
(e) => {
|
||||
if (!swallow || !e.isTrusted) return;
|
||||
swallow = false;
|
||||
e.stopPropagation();
|
||||
e.preventDefault();
|
||||
},
|
||||
true,
|
||||
);
|
||||
}
|
||||
@@ -2,7 +2,10 @@
|
||||
// name. The pill stretches like skiper21's tabs: its leading edge reaches the
|
||||
// new item first and the trailing edge follows, so the name fading in is always
|
||||
// inside it. Items grow by flex-grow (a share of the track, not pixels), so
|
||||
// they stay right while the sheet around them changes width.
|
||||
// they stay right while the sheet around them changes width. At rest the pill
|
||||
// stands where CSS puts it (its item's place among the others: controls.css), so
|
||||
// a sheet changing width, a phone turned, carry it along with nothing to watch;
|
||||
// only a pick moves it from here.
|
||||
|
||||
import { h, ic } from '../dom';
|
||||
import { EASE_OUT, LEAD, SMOOTH, calm, play } from '../motion';
|
||||
@@ -64,11 +67,20 @@ export function iconSeg<T extends string>(items: SegItem<T>[], value: T, onPick:
|
||||
const b = btns.find((x) => x.dataset.v === value);
|
||||
return b && b.offsetWidth ? [b.offsetLeft, el.clientWidth - b.offsetLeft - b.offsetWidth] : null;
|
||||
};
|
||||
/** At rest: its item's place, for CSS to keep (`--i` of `--n`), nothing inline. */
|
||||
const placePill = () => {
|
||||
const e = edges();
|
||||
if (!e) return;
|
||||
pill.style.left = `${e[0]}px`;
|
||||
pill.style.right = `${e[1]}px`;
|
||||
pill.style.setProperty(
|
||||
'--i',
|
||||
String(
|
||||
Math.max(
|
||||
0,
|
||||
btns.findIndex((b) => b.dataset.v === value),
|
||||
),
|
||||
),
|
||||
);
|
||||
pill.style.setProperty('--n', String(btns.length));
|
||||
pill.style.left = '';
|
||||
pill.style.right = '';
|
||||
};
|
||||
|
||||
let raf = 0;
|
||||
@@ -166,9 +178,7 @@ export function iconSeg<T extends string>(items: SegItem<T>[], value: T, onPick:
|
||||
}
|
||||
|
||||
mark();
|
||||
new ResizeObserver(() => {
|
||||
if (!raf) placePill();
|
||||
}).observe(el);
|
||||
placePill();
|
||||
return {
|
||||
el,
|
||||
sync(v) {
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
|
||||
import { h, ic } from '../dom';
|
||||
import { BOUNCY, DRAWER, EASE_IN, EASE_OUT, SMOOTH, SOFT, dismisses, play, rubber, unrubber } from '../motion';
|
||||
import { closeMenu } from './menu';
|
||||
import { closeDrop } from './drop';
|
||||
|
||||
/** A view: its root, and what the sheet reads from it. */
|
||||
export interface View {
|
||||
@@ -134,7 +134,12 @@ function build() {
|
||||
true,
|
||||
);
|
||||
document.body.appendChild(root);
|
||||
new ResizeObserver(() => startWidth(planWidth())).observe(vp);
|
||||
// What changes the sheet's width says so itself (a view, a row opening, a code swapped: planWidth); left is the
|
||||
// window, a phone turned or a desktop window crossing the dialog's width. Once a frame at most.
|
||||
let resizing = 0;
|
||||
addEventListener('resize', () => {
|
||||
if (!resizing) resizing = requestAnimationFrame(() => ((resizing = 0), startWidth(planWidth())));
|
||||
});
|
||||
pullToClose();
|
||||
}
|
||||
|
||||
@@ -158,6 +163,12 @@ export function onLeave(fn: () => void) {
|
||||
left = fn;
|
||||
}
|
||||
|
||||
/** Who is told the sheet changed: a view shown in it, its work done (or not), the sheet gone. */
|
||||
const watchers: (() => void)[] = [];
|
||||
/** Told whenever the sheet changes (the demo's state switch: demo/way-pick.ts). */
|
||||
export const onSheetChange = (fn: () => void) => void watchers.push(fn);
|
||||
const changed = () => watchers.forEach((f) => f());
|
||||
|
||||
/** After this sheet closes (once). */
|
||||
export function onClosed(fn: () => void) {
|
||||
closed = fn;
|
||||
@@ -165,7 +176,7 @@ export function onClosed(fn: () => void) {
|
||||
|
||||
export function openSheet(factory: ViewFactory) {
|
||||
if (!root) build();
|
||||
closeMenu(true);
|
||||
closeDrop(true);
|
||||
if (root.dataset.closing) {
|
||||
// Still on its way out: the new flow opens once it is gone, as it would have a moment later.
|
||||
next = factory;
|
||||
@@ -226,6 +237,7 @@ export function openSheet(factory: ViewFactory) {
|
||||
/* no history */
|
||||
}
|
||||
}
|
||||
changed();
|
||||
}
|
||||
|
||||
/** `now`: gone at once (Escape: what the keyboard does never animates). */
|
||||
@@ -295,6 +307,7 @@ function gone() {
|
||||
const f = closed;
|
||||
closed = null;
|
||||
f?.();
|
||||
changed();
|
||||
const n = next;
|
||||
next = null;
|
||||
if (n) openSheet(n);
|
||||
@@ -419,6 +432,7 @@ function swapView(v: View) {
|
||||
fill: 'backwards',
|
||||
},
|
||||
);
|
||||
changed();
|
||||
}
|
||||
|
||||
/** Further into a flow (Add → Open on the device). */
|
||||
@@ -493,6 +507,7 @@ export function glide(change: () => void, focus: (Element | null | undefined)[]
|
||||
*/
|
||||
export function settle(done = true) {
|
||||
sheetEl?.classList.toggle('arrived', done);
|
||||
changed();
|
||||
}
|
||||
|
||||
/** The sheet scrolls back to the top (a device connected under a scrolled sheet). */
|
||||
|
||||
Reference in new issue
Block a user