gooee 0.50.0 → 0.51.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +17 -4
- package/package.json +5 -3
- package/scripts/add.js +28 -6
- package/src/components/Accordion/Accordion.vue +62 -0
- package/src/components/Accordion/AccordionItem.vue +45 -0
- package/src/components/Accordion/accordionContext.ts +6 -0
- package/src/components/Alert/Alert.tsx +7 -27
- package/src/components/Alert/Alert.vue +59 -0
- package/src/components/AlertDialog/AlertDialog.vue +126 -0
- package/src/components/AnimatedNumber/AnimatedNumber.vue +36 -0
- package/src/components/AspectRatio/AspectRatio.vue +17 -0
- package/src/components/Avatar/Avatar.vue +56 -0
- package/src/components/AvatarGroup/AvatarGroup.vue +42 -0
- package/src/components/Badge/Badge.vue +32 -0
- package/src/components/Breadcrumb/Breadcrumb.vue +58 -0
- package/src/components/Button/Button.vue +130 -0
- package/src/components/ButtonGroup/ButtonGroup.tsx +4 -21
- package/src/components/ButtonGroup/ButtonGroup.vue +33 -0
- package/src/components/Card/Card.vue +34 -0
- package/src/components/Center/Center.vue +32 -0
- package/src/components/Checkbox/Checkbox.tsx +8 -25
- package/src/components/Checkbox/Checkbox.vue +108 -0
- package/src/components/CheckboxGroup/CheckboxGroup.vue +76 -0
- package/src/components/Cluster/Cluster.vue +34 -0
- package/src/components/CodeBlock/CodeBlock.vue +45 -0
- package/src/components/Collapsible/Collapsible.vue +59 -0
- package/src/components/Container/Container.vue +22 -0
- package/src/components/CopyButton/CopyButton.tsx +3 -49
- package/src/components/CopyButton/CopyButton.vue +116 -0
- package/src/components/Dial/Dial.tsx +12 -120
- package/src/components/Dial/Dial.vue +228 -0
- package/src/components/Dialog/Dialog.vue +139 -0
- package/src/components/Divider/Divider.vue +27 -0
- package/src/components/DropdownMenu/DropdownMenu.tsx +11 -46
- package/src/components/DropdownMenu/DropdownMenu.vue +185 -0
- package/src/components/EmptyState/EmptyState.vue +33 -0
- package/src/components/Fab/Fab.tsx +4 -14
- package/src/components/Fab/Fab.vue +115 -0
- package/src/components/Fieldset/Fieldset.vue +42 -0
- package/src/components/FormField/FormField.tsx +2 -17
- package/src/components/FormField/FormField.vue +64 -0
- package/src/components/Grid/Grid.vue +43 -0
- package/src/components/HoldToConfirm/HoldToConfirm.tsx +13 -143
- package/src/components/HoldToConfirm/HoldToConfirm.vue +281 -0
- package/src/components/IconButton/IconButton.vue +25 -0
- package/src/components/Input/Input.vue +112 -0
- package/src/components/InputGroup/InputGroup.vue +102 -0
- package/src/components/Kbd/Kbd.tsx +2 -12
- package/src/components/Kbd/Kbd.vue +21 -0
- package/src/components/Link/Link.vue +38 -0
- package/src/components/LivePill/LivePill.tsx +11 -39
- package/src/components/LivePill/LivePill.vue +152 -0
- package/src/components/Meter/Meter.vue +88 -0
- package/src/components/MorphButton/MorphButton.tsx +11 -67
- package/src/components/MorphButton/MorphButton.vue +120 -0
- package/src/components/NumberInput/NumberInput.tsx +11 -48
- package/src/components/NumberInput/NumberInput.vue +184 -0
- package/src/components/Pagination/Pagination.tsx +2 -15
- package/src/components/Pagination/Pagination.vue +87 -0
- package/src/components/Pagination/range.ts +15 -0
- package/src/components/PasswordInput/PasswordInput.vue +53 -0
- package/src/components/PickerWheel/DateWheel.vue +48 -0
- package/src/components/PickerWheel/PickerWheel.tsx +15 -129
- package/src/components/PickerWheel/PickerWheel.vue +77 -0
- package/src/components/PickerWheel/TimeWheel.vue +50 -0
- package/src/components/PickerWheel/WheelColumn.vue +70 -0
- package/src/components/Popover/Popover.vue +108 -0
- package/src/components/ProgressBar/ProgressBar.vue +48 -0
- package/src/components/Prose/Prose.vue +18 -0
- package/src/components/Radio/Radio.vue +49 -0
- package/src/components/Radio/RadioGroup.vue +69 -0
- package/src/components/Retime/Retime.tsx +7 -40
- package/src/components/Retime/Retime.vue +64 -0
- package/src/components/RulerScrubber/RulerScrubber.tsx +9 -38
- package/src/components/RulerScrubber/RulerScrubber.vue +166 -0
- package/src/components/ScrollArea/ScrollArea.tsx +17 -129
- package/src/components/ScrollArea/ScrollArea.vue +58 -0
- package/src/components/SearchInput/SearchInput.tsx +2 -15
- package/src/components/SearchInput/SearchInput.vue +70 -0
- package/src/components/SegmentedControl/SegmentedControl.tsx +7 -51
- package/src/components/SegmentedControl/SegmentedControl.vue +111 -0
- package/src/components/Select/Select.tsx +16 -47
- package/src/components/Select/Select.vue +236 -0
- package/src/components/Sheet/Sheet.tsx +52 -231
- package/src/components/Sheet/Sheet.vue +249 -0
- package/src/components/Skeleton/Skeleton.vue +38 -0
- package/src/components/Slider/Slider.tsx +25 -84
- package/src/components/Slider/Slider.vue +113 -0
- package/src/components/Spinner/Spinner.vue +30 -0
- package/src/components/Split/Split.vue +34 -0
- package/src/components/SplitFlap/SplitFlap.tsx +11 -175
- package/src/components/SplitFlap/SplitFlap.vue +52 -0
- package/src/components/Stack/Stack.vue +22 -0
- package/src/components/SwipeDeck/SwipeDeck.tsx +30 -194
- package/src/components/SwipeDeck/SwipeDeck.vue +168 -0
- package/src/components/Switch/Switch.tsx +6 -75
- package/src/components/Switch/Switch.vue +66 -0
- package/src/components/Table/Table.module.css +12 -3
- package/src/components/Table/Table.tsx +8 -59
- package/src/components/Table/Table.vue +274 -0
- package/src/components/TableOfContents/TableOfContents.vue +90 -0
- package/src/components/Tabs/Tab.vue +37 -0
- package/src/components/Tabs/TabList.vue +47 -0
- package/src/components/Tabs/TabPanel.vue +26 -0
- package/src/components/Tabs/Tabs.module.css +2 -2
- package/src/components/Tabs/Tabs.tsx +11 -84
- package/src/components/Tabs/Tabs.vue +33 -0
- package/src/components/Tabs/tabsContext.ts +15 -0
- package/src/components/Tag/Tag.tsx +6 -26
- package/src/components/Tag/Tag.vue +63 -0
- package/src/components/Textarea/Textarea.tsx +2 -22
- package/src/components/Textarea/Textarea.vue +120 -0
- package/src/components/ThemeToggle/ThemeToggle.tsx +10 -84
- package/src/components/ThemeToggle/ThemeToggle.vue +90 -0
- package/src/components/Toast/Toast.tsx +34 -264
- package/src/components/Toast/Toast.vue +138 -0
- package/src/components/Toast/ToastItem.vue +108 -0
- package/src/components/Toggle/Toggle.vue +52 -0
- package/src/components/Tooltip/Tooltip.tsx +17 -48
- package/src/components/Tooltip/Tooltip.vue +95 -0
- package/src/components/VisuallyHidden/VisuallyHidden.vue +17 -0
- package/src/internal/FieldMessage.vue +41 -0
- package/src/internal/Roll.vue +20 -0
- package/src/internal/focus.ts +4 -47
- package/src/internal/icons.module.css +39 -27
- package/src/internal/layers.ts +7 -61
- package/src/internal/motion/alert.ts +17 -0
- package/src/internal/motion/buttonGroup.ts +21 -0
- package/src/internal/motion/checkbox.ts +39 -0
- package/src/internal/motion/collapseOut.ts +26 -0
- package/src/internal/motion/copyButton.ts +48 -0
- package/src/internal/motion/dial.ts +137 -0
- package/src/internal/motion/fab.ts +14 -0
- package/src/internal/motion/floating.ts +58 -0
- package/src/internal/motion/focus.ts +53 -0
- package/src/internal/motion/formField.ts +19 -0
- package/src/internal/motion/holdToConfirm.ts +147 -0
- package/src/internal/motion/keys.ts +42 -0
- package/src/internal/motion/layers.ts +78 -0
- package/src/internal/motion/listbox.ts +69 -0
- package/src/internal/motion/livePill.ts +71 -0
- package/src/internal/motion/menuKeys.ts +58 -0
- package/src/internal/motion/morphButton.ts +71 -0
- package/src/internal/motion/numberInput.ts +57 -0
- package/src/internal/motion/pickerWheel.ts +166 -0
- package/src/internal/motion/retime.ts +45 -0
- package/src/internal/motion/ruler.ts +57 -0
- package/src/internal/motion/scrollArea.ts +133 -0
- package/src/internal/motion/scrubLook.ts +15 -0
- package/src/internal/motion/segmented.ts +63 -0
- package/src/internal/motion/sheetDetents.ts +271 -0
- package/src/internal/motion/slider.ts +108 -0
- package/src/internal/motion/splitFlap.ts +205 -0
- package/src/internal/motion/swipeDeck.ts +236 -0
- package/src/internal/motion/switchThumb.ts +83 -0
- package/src/internal/motion/table.ts +77 -0
- package/src/internal/motion/tabs.ts +105 -0
- package/src/internal/motion/textarea.ts +22 -0
- package/src/internal/motion/themeIcon.ts +91 -0
- package/src/internal/motion/themeReveal.ts +71 -0
- package/src/internal/motion/toast.ts +291 -0
- package/src/internal/motion/tooltip.ts +58 -0
- package/src/internal/themeReveal.ts +3 -61
- package/src/internal/useFloating.ts +9 -49
- package/src/internal/useScrub.ts +1 -13
- package/src/internal/vue/trigger.ts +18 -0
- package/src/internal/vue/useFloating.ts +38 -0
- package/src/internal/vue/usePresence.ts +21 -0
- package/src/internal/vue/useScrub.ts +40 -0
- package/src/internal/vueIcons.ts +134 -0
- package/src/tokens.css +5 -0
- package/src/internal/relativeTime.ts +0 -26
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { pour, type Pour } from './liquidMore'
|
|
2
|
+
|
|
3
|
+
export type CheckState = 'off' | 'on' | 'mixed'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* A checkbox's cup fill: ink pours in from the side that was pressed (a key fills it evenly from below),
|
|
7
|
+
* levels, and then the mark draws. Clearing drains it the same way. Call `press` on a pointer down,
|
|
8
|
+
* `key` on a key down, and `set` with the state each time it may have changed.
|
|
9
|
+
*/
|
|
10
|
+
export function createCheckboxPour(box: () => HTMLElement | null, cls: string, state: CheckState) {
|
|
11
|
+
let pressedAt: { x: number; t: number } | null = null
|
|
12
|
+
let run: Pour | null = null
|
|
13
|
+
let was = state
|
|
14
|
+
return {
|
|
15
|
+
press(x: number) {
|
|
16
|
+
pressedAt = { x, t: performance.now() }
|
|
17
|
+
},
|
|
18
|
+
key() {
|
|
19
|
+
pressedAt = null
|
|
20
|
+
},
|
|
21
|
+
set(state: CheckState) {
|
|
22
|
+
const prev = was
|
|
23
|
+
was = state
|
|
24
|
+
const filled = state !== 'off'
|
|
25
|
+
if ((prev !== 'off') === filled) return
|
|
26
|
+
const el = box()
|
|
27
|
+
const at = pressedAt
|
|
28
|
+
pressedAt = null
|
|
29
|
+
if (!el) return
|
|
30
|
+
const r = el.getBoundingClientRect()
|
|
31
|
+
const side = at && performance.now() - at.t < 1000 ? Math.max(-1, Math.min(1, (at.x - r.left - r.width / 2) / (r.width / 2))) : 0
|
|
32
|
+
const mark = el.querySelector<SVGPathElement>(prev === 'mixed' || state === 'mixed' ? '[data-mark="dash"]' : '[data-mark="tick"]')
|
|
33
|
+
run = pour(el, mark, filled ? 1 : 0, side, cls, run)
|
|
34
|
+
},
|
|
35
|
+
destroy() {
|
|
36
|
+
run?.stop()
|
|
37
|
+
},
|
|
38
|
+
}
|
|
39
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { easing, motionMs } from '../motion'
|
|
2
|
+
import { focusables } from './focus'
|
|
3
|
+
|
|
4
|
+
// A part that is about to unmount: focus moves to the next control (or the one before) instead of the body.
|
|
5
|
+
export function focusPast(el: HTMLElement) {
|
|
6
|
+
if (!el.contains(document.activeElement)) return
|
|
7
|
+
const others = focusables(document.body).filter((n) => !el.contains(n))
|
|
8
|
+
const next = others.find((n) => el.compareDocumentPosition(n) & Node.DOCUMENT_POSITION_FOLLOWING)
|
|
9
|
+
const prev = [...others].reverse().find((n) => el.compareDocumentPosition(n) & Node.DOCUMENT_POSITION_PRECEDING)
|
|
10
|
+
;(next ?? prev)?.focus({ preventScroll: true })
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
// Folds an inline element's width away so its neighbours slide in, then calls done.
|
|
14
|
+
export function collapseOut(el: HTMLElement, done: () => void) {
|
|
15
|
+
const ms = motionMs('base')
|
|
16
|
+
if (ms === 0 || !el.animate) return done()
|
|
17
|
+
const w = el.offsetWidth
|
|
18
|
+
const anim = el.animate(
|
|
19
|
+
[
|
|
20
|
+
{ width: `${w}px`, opacity: 1, transform: 'scale(1)' },
|
|
21
|
+
{ width: '0px', opacity: 0, transform: 'scale(0.8)', marginInlineEnd: '0px', paddingInline: '0px', borderWidth: '0px' },
|
|
22
|
+
],
|
|
23
|
+
{ duration: ms, easing: easing('morph'), fill: 'forwards' },
|
|
24
|
+
)
|
|
25
|
+
anim.onfinish = done
|
|
26
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
// CopyButton's browser work with no framework: the clipboard write and the icon placed on the label's capitals.
|
|
2
|
+
|
|
3
|
+
export async function writeText(text: string) {
|
|
4
|
+
try {
|
|
5
|
+
await navigator.clipboard.writeText(text)
|
|
6
|
+
return true
|
|
7
|
+
} catch {
|
|
8
|
+
// Fallback for http pages and older browsers.
|
|
9
|
+
const ta = document.createElement('textarea')
|
|
10
|
+
ta.value = text
|
|
11
|
+
ta.setAttribute('readonly', '')
|
|
12
|
+
ta.style.position = 'fixed'
|
|
13
|
+
ta.style.opacity = '0'
|
|
14
|
+
const prev = document.activeElement as HTMLElement | null
|
|
15
|
+
document.body.appendChild(ta)
|
|
16
|
+
ta.select()
|
|
17
|
+
const ok = document.execCommand('copy')
|
|
18
|
+
ta.remove()
|
|
19
|
+
prev?.focus()
|
|
20
|
+
return ok
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** With a label, the icon sits on the middle of the label's capital letters, not of its line. Returns a cleanup. */
|
|
25
|
+
export function capShift(root: HTMLElement | null, icons: HTMLElement | null, text: Node | null | undefined) {
|
|
26
|
+
root?.style.removeProperty('--cap-shift')
|
|
27
|
+
if (!root || !icons || !text) return () => {}
|
|
28
|
+
const place = () => {
|
|
29
|
+
root.style.removeProperty('--cap-shift')
|
|
30
|
+
const range = document.createRange()
|
|
31
|
+
range.selectNodeContents(text)
|
|
32
|
+
const box = range.getBoundingClientRect()
|
|
33
|
+
const look = getComputedStyle(root)
|
|
34
|
+
const pen = document.createElement('canvas').getContext('2d')
|
|
35
|
+
if (!pen || !box.height) return
|
|
36
|
+
pen.font = `${look.fontWeight} ${look.fontSize} ${look.fontFamily}`
|
|
37
|
+
const m = pen.measureText('H')
|
|
38
|
+
const cap = box.top + m.fontBoundingBoxAscent - m.actualBoundingBoxAscent / 2
|
|
39
|
+
const r = icons.getBoundingClientRect()
|
|
40
|
+
root.style.setProperty('--cap-shift', `${Math.round((cap - r.top - r.height / 2) * 100) / 100}px`)
|
|
41
|
+
}
|
|
42
|
+
place()
|
|
43
|
+
let live = true
|
|
44
|
+
document.fonts?.ready.then(() => live && place())
|
|
45
|
+
return () => {
|
|
46
|
+
live = false
|
|
47
|
+
}
|
|
48
|
+
}
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
import { clamp, lastStep, roundTo, stepKey, steps, type wheelSteps } from '../feel'
|
|
2
|
+
import type { PointerLike, ScrubberOptions } from './scrubber'
|
|
3
|
+
import { sideways } from './scrubLook'
|
|
4
|
+
|
|
5
|
+
// The Dial's scale, drag, tap and wheel, for the React and Vue Dial. The motion is createScrubber's.
|
|
6
|
+
|
|
7
|
+
// The printed and mercury dials sweep 270 degrees, with the dead part at the bottom.
|
|
8
|
+
export const SWEEP = 270
|
|
9
|
+
|
|
10
|
+
// Mercury is drawn in this box (SVG units): knob, bump and level dots.
|
|
11
|
+
export const W = 280
|
|
12
|
+
export const H = 252
|
|
13
|
+
export const CX = W / 2
|
|
14
|
+
export const CY = 128
|
|
15
|
+
export const KNOB = 58
|
|
16
|
+
export const DOT = 84
|
|
17
|
+
export const polar = (deg: number, r: number) => [CX + r * Math.sin((deg * Math.PI) / 180), CY - r * Math.cos((deg * Math.PI) / 180)] as const
|
|
18
|
+
|
|
19
|
+
type DialPointer = PointerLike & { shiftKey: boolean }
|
|
20
|
+
type Centre = (r: DOMRect) => { x: number; y: number }
|
|
21
|
+
|
|
22
|
+
/** A drag around a centre: the change in angle since the last move, never the angle itself, so a press never jumps. */
|
|
23
|
+
function turn(unitsPerDeg: number, centre: Centre) {
|
|
24
|
+
return (e: DialPointer, last: { x: number; y: number }, el: HTMLElement) => {
|
|
25
|
+
const c = centre(el.getBoundingClientRect())
|
|
26
|
+
// Near the centre the angle swings wildly, so it does not count.
|
|
27
|
+
if (Math.hypot(e.clientX - c.x, e.clientY - c.y) < 16) return 0
|
|
28
|
+
let d = ((Math.atan2(e.clientY - c.y, e.clientX - c.x) - Math.atan2(last.y - c.y, last.x - c.x)) * 180) / Math.PI
|
|
29
|
+
if (d > 180) d -= 360
|
|
30
|
+
if (d < -180) d += 360
|
|
31
|
+
return d * unitsPerDeg * (e.shiftKey ? 0.25 : 1)
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** Where a tap sits around a centre, in degrees clockwise from the top, or null too near the centre to tell. */
|
|
36
|
+
function angleAt(e: DialPointer, centre: Centre) {
|
|
37
|
+
const c = centre((e.currentTarget as HTMLElement).getBoundingClientRect())
|
|
38
|
+
if (Math.hypot(e.clientX - c.x, e.clientY - c.y) < 16) return null
|
|
39
|
+
return (Math.atan2(e.clientX - c.x, c.y - e.clientY) * 180) / Math.PI
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const mercuryCentre: Centre = (r) => ({ x: r.left + r.width / 2, y: r.top + (r.height * CY) / H })
|
|
43
|
+
const knobCentre: Centre = (r) => ({ x: r.left + r.width / 2, y: r.top + r.height / 2 })
|
|
44
|
+
|
|
45
|
+
export type DialScale = { min: number; max: number; step: number; marks?: number[]; variant: 'printed' | 'mercury' | 'arc' }
|
|
46
|
+
export type Rim = { r: number; gap: number }
|
|
47
|
+
|
|
48
|
+
/** The steps of a dial: the last one, every one, the labelled ones, and how a value lands on one. */
|
|
49
|
+
export function dialScale({ min, max, step, marks }: DialScale) {
|
|
50
|
+
const top = lastStep(min, max, step)
|
|
51
|
+
const all = steps(min, max, step)
|
|
52
|
+
return {
|
|
53
|
+
top,
|
|
54
|
+
all,
|
|
55
|
+
labelled: (marks ?? [min, max]).map((v) => roundTo(v, step, min)),
|
|
56
|
+
land: (p: number) => clamp(roundTo(p, step, min), min, top),
|
|
57
|
+
stepDeg: SWEEP / Math.max(1, all.length - 1),
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** The scrubber options for a dial: how far a drag turns it, where keys, a tap and the wheel take it. */
|
|
62
|
+
export function dialScrub(p: DialScale, rim: Rim | null, wheel: ReturnType<typeof wheelSteps>): Pick<ScrubberOptions<DialPointer>, 'min' | 'max' | 'edge' | 'land' | 'coast' | 'maxSpeed' | 'move' | 'key' | 'tap' | 'wheel'> {
|
|
63
|
+
const { min, max, step, variant } = p
|
|
64
|
+
const { top, labelled, land, stepDeg } = dialScale(p)
|
|
65
|
+
return {
|
|
66
|
+
min,
|
|
67
|
+
max: top,
|
|
68
|
+
edge: step * (variant === 'arc' ? 0.8 : variant === 'mercury' ? 0.12 : 0.6),
|
|
69
|
+
land,
|
|
70
|
+
coast: variant === 'arc' ? 0.2 : 0,
|
|
71
|
+
maxSpeed: 160 * step,
|
|
72
|
+
move: variant === 'arc' ? (e, last) => (-(e.clientX - last.x) / (rim?.gap || 1)) * step * (e.shiftKey ? 0.25 : 1) : variant === 'mercury' ? turn(step / stepDeg, mercuryCentre) : turn((max - min) / SWEEP, knobCentre),
|
|
73
|
+
key: (k, t) => stepKey(k, t, { min, max, step, marks: labelled }),
|
|
74
|
+
// A tap without a drag goes to the mark under it, so the dial works with one plain press.
|
|
75
|
+
tap: (e, at) => {
|
|
76
|
+
if (variant === 'arc') {
|
|
77
|
+
if (!rim?.gap || !rim.r) return null
|
|
78
|
+
const r = (e.currentTarget as HTMLElement).getBoundingClientRect()
|
|
79
|
+
// The rim curves, so the tap's sideways distance becomes a length along the arc.
|
|
80
|
+
const along = rim.r * Math.asin(clamp((e.clientX - (r.left + r.width / 2)) / rim.r, -1, 1))
|
|
81
|
+
return land(at + (along / rim.gap) * step)
|
|
82
|
+
}
|
|
83
|
+
if (variant === 'mercury') {
|
|
84
|
+
const a = angleAt(e, mercuryCentre)
|
|
85
|
+
// Past either end of the sweep, land clamps to the nearer end (the dead part is split at the bottom).
|
|
86
|
+
return a === null ? null : land(min + ((a + 135) / stepDeg) * step)
|
|
87
|
+
}
|
|
88
|
+
const a = angleAt(e, knobCentre)
|
|
89
|
+
if (a === null) return null
|
|
90
|
+
// The printed face turns, so the tap is read on the face: min at 0 and max 270 degrees back from it.
|
|
91
|
+
const back = (((((at - min) / (max - min || 1)) * SWEEP - a) % 360) + 360) % 360
|
|
92
|
+
if (back > SWEEP) return back < SWEEP + (360 - SWEEP) / 2 ? top : min
|
|
93
|
+
return land(min + (back / SWEEP) * (max - min))
|
|
94
|
+
},
|
|
95
|
+
wheel:
|
|
96
|
+
variant === 'arc'
|
|
97
|
+
? (e, t) => {
|
|
98
|
+
const dx = sideways(e)
|
|
99
|
+
if (dx === null || !rim?.gap) return null
|
|
100
|
+
const n = wheel(dx, rim.gap, e.timeStamp)
|
|
101
|
+
const to = clamp(roundTo(t + n * step, step, min), min, top)
|
|
102
|
+
return to === t && (t === min || t === top) ? null : to
|
|
103
|
+
}
|
|
104
|
+
: undefined,
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* The arc lets an up-and-down swipe scroll the page, but a finger following the curve also moves up or
|
|
110
|
+
* down. Once a touch moves more sideways than half its rise, it is a turn: the page stops taking it.
|
|
111
|
+
* Returns a function that stops.
|
|
112
|
+
*/
|
|
113
|
+
export function arcTouch(el: HTMLElement) {
|
|
114
|
+
let from: { x: number; y: number } | null = null
|
|
115
|
+
let turning = false
|
|
116
|
+
const start = (e: TouchEvent) => {
|
|
117
|
+
from = { x: e.touches[0].clientX, y: e.touches[0].clientY }
|
|
118
|
+
turning = false
|
|
119
|
+
}
|
|
120
|
+
const move = (e: TouchEvent) => {
|
|
121
|
+
if (!from) return
|
|
122
|
+
if (!turning) {
|
|
123
|
+
const dx = Math.abs(e.touches[0].clientX - from.x)
|
|
124
|
+
const dy = Math.abs(e.touches[0].clientY - from.y)
|
|
125
|
+
if (dx + dy < 2) return
|
|
126
|
+
turning = dx > dy / 2
|
|
127
|
+
if (!turning) from = null
|
|
128
|
+
}
|
|
129
|
+
if (turning && e.cancelable) e.preventDefault()
|
|
130
|
+
}
|
|
131
|
+
el.addEventListener('touchstart', start, { passive: true })
|
|
132
|
+
el.addEventListener('touchmove', move, { passive: false })
|
|
133
|
+
return () => {
|
|
134
|
+
el.removeEventListener('touchstart', start)
|
|
135
|
+
el.removeEventListener('touchmove', move)
|
|
136
|
+
}
|
|
137
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { ScrollReader } from './scrollFrame'
|
|
2
|
+
|
|
3
|
+
/** Extended: down the page it folds to the icon, back up it opens again. It reads the scroll, never a timer. */
|
|
4
|
+
export function foldOnScroll(setCompact: (compact: boolean) => void): ScrollReader {
|
|
5
|
+
let last: number | null = null
|
|
6
|
+
return () => {
|
|
7
|
+
const y = window.scrollY
|
|
8
|
+
const prev = last
|
|
9
|
+
last = y
|
|
10
|
+
if (prev === null || Math.abs(y - prev) < 4) return
|
|
11
|
+
const next = y > prev && y > 80
|
|
12
|
+
return () => setCompact(next)
|
|
13
|
+
}
|
|
14
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
// Places a floating layer (a menu, popover or tooltip) next to its anchor, flipping to the other side when
|
|
2
|
+
// it would leave the screen and keeping a margin from the screen's edges. The same in every framework.
|
|
3
|
+
|
|
4
|
+
export type Side = 'top' | 'bottom' | 'left' | 'right'
|
|
5
|
+
export type Align = 'start' | 'center' | 'end'
|
|
6
|
+
export type FloatingOptions = { side?: Side; align?: Align; offset?: number; matchWidth?: boolean }
|
|
7
|
+
export type Place = { top: number; left: number; side: Side; width?: number }
|
|
8
|
+
|
|
9
|
+
const MARGIN = 8
|
|
10
|
+
|
|
11
|
+
/** Where the layer goes now, or null while either element is missing. */
|
|
12
|
+
export function place(anchor: HTMLElement | null, floating: HTMLElement | null, { side = 'bottom', align = 'center', offset = 8, matchWidth = false }: FloatingOptions = {}): Place | null {
|
|
13
|
+
if (!anchor || !floating) return null
|
|
14
|
+
const r = anchor.getBoundingClientRect()
|
|
15
|
+
const width = matchWidth ? r.width : undefined
|
|
16
|
+
const fw = width ?? floating.offsetWidth
|
|
17
|
+
const fh = floating.offsetHeight
|
|
18
|
+
const vw = window.innerWidth
|
|
19
|
+
const vh = window.innerHeight
|
|
20
|
+
|
|
21
|
+
let s = side
|
|
22
|
+
if (s === 'bottom' && r.bottom + offset + fh > vh - MARGIN && r.top - offset - fh > MARGIN) s = 'top'
|
|
23
|
+
else if (s === 'top' && r.top - offset - fh < MARGIN && r.bottom + offset + fh < vh - MARGIN) s = 'bottom'
|
|
24
|
+
else if (s === 'right' && r.right + offset + fw > vw - MARGIN && r.left - offset - fw > MARGIN) s = 'left'
|
|
25
|
+
else if (s === 'left' && r.left - offset - fw < MARGIN && r.right + offset + fw < vw - MARGIN) s = 'right'
|
|
26
|
+
|
|
27
|
+
let top = 0
|
|
28
|
+
let left = 0
|
|
29
|
+
if (s === 'bottom' || s === 'top') {
|
|
30
|
+
top = s === 'bottom' ? r.bottom + offset : r.top - offset - fh
|
|
31
|
+
left = align === 'start' ? r.left : align === 'end' ? r.right - fw : r.left + r.width / 2 - fw / 2
|
|
32
|
+
} else {
|
|
33
|
+
left = s === 'right' ? r.right + offset : r.left - offset - fw
|
|
34
|
+
top = align === 'start' ? r.top : align === 'end' ? r.bottom - fh : r.top + r.height / 2 - fh / 2
|
|
35
|
+
}
|
|
36
|
+
left = Math.min(Math.max(MARGIN, left), Math.max(MARGIN, vw - fw - MARGIN))
|
|
37
|
+
top = Math.min(Math.max(MARGIN, top), Math.max(MARGIN, vh - fh - MARGIN))
|
|
38
|
+
return { top, left, side: s, width }
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** Calls `update` now and whenever either element resizes or the page scrolls or resizes. Returns a stop. */
|
|
42
|
+
export function follow(anchor: HTMLElement | null, floating: HTMLElement | null, update: () => void) {
|
|
43
|
+
update()
|
|
44
|
+
const ro = new ResizeObserver(update)
|
|
45
|
+
if (floating) ro.observe(floating)
|
|
46
|
+
if (anchor) ro.observe(anchor)
|
|
47
|
+
window.addEventListener('scroll', update, true)
|
|
48
|
+
window.addEventListener('resize', update)
|
|
49
|
+
return () => {
|
|
50
|
+
ro.disconnect()
|
|
51
|
+
window.removeEventListener('scroll', update, true)
|
|
52
|
+
window.removeEventListener('resize', update)
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** The layer's style: fixed in its place, or see-through (not hidden, so focus can still move in) before the first measure. */
|
|
57
|
+
export const floatingStyle = (at: Place | null) =>
|
|
58
|
+
at ? { position: 'fixed' as const, top: at.top, left: at.left, width: at.width } : { position: 'fixed' as const, top: 0, left: 0, opacity: 0, pointerEvents: 'none' as const }
|
|
@@ -33,3 +33,56 @@ export function returnTarget(): HTMLElement | null {
|
|
|
33
33
|
if (pressed && (!active || active === document.body || (active !== pressed && active.contains(pressed)))) return pressed
|
|
34
34
|
return active
|
|
35
35
|
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Keeps Tab inside `root` until the returned function is called, starting on `initialFocus` or the first
|
|
39
|
+
* focusable (close buttons opt out with data-skip-initial-focus). On release, focus goes back to where it
|
|
40
|
+
* came from, if it is still inside the layer.
|
|
41
|
+
*/
|
|
42
|
+
export function trapFocus(root: HTMLElement, { initialFocus, restore = true }: { initialFocus?: HTMLElement | null; restore?: boolean } = {}) {
|
|
43
|
+
const previous = returnTarget()
|
|
44
|
+
// Close buttons opt out of first focus, so focus lands on the content or the safe action.
|
|
45
|
+
const list = focusables(root)
|
|
46
|
+
const first = initialFocus ?? list.find((el) => !el.hasAttribute('data-skip-initial-focus')) ?? list[0] ?? root
|
|
47
|
+
first.focus({ preventScroll: true })
|
|
48
|
+
|
|
49
|
+
// Safari's plain Tab skips buttons, so it can jump from the middle of the layer to the page behind,
|
|
50
|
+
// or off the page. A Tab that lands outside goes back to the end it wrapped past.
|
|
51
|
+
let check = 0
|
|
52
|
+
const onKey = (e: KeyboardEvent) => {
|
|
53
|
+
if (e.key !== 'Tab') return
|
|
54
|
+
const items = focusables(root)
|
|
55
|
+
if (items.length === 0) {
|
|
56
|
+
e.preventDefault()
|
|
57
|
+
root.focus()
|
|
58
|
+
return
|
|
59
|
+
}
|
|
60
|
+
const firstEl = items[0]
|
|
61
|
+
const lastEl = items[items.length - 1]
|
|
62
|
+
const current = document.activeElement
|
|
63
|
+
if (e.shiftKey && (current === firstEl || current === root)) {
|
|
64
|
+
e.preventDefault()
|
|
65
|
+
lastEl.focus()
|
|
66
|
+
} else if (!e.shiftKey && current === lastEl) {
|
|
67
|
+
e.preventDefault()
|
|
68
|
+
firstEl.focus()
|
|
69
|
+
} else {
|
|
70
|
+
const back = e.shiftKey
|
|
71
|
+
clearTimeout(check)
|
|
72
|
+
check = window.setTimeout(() => {
|
|
73
|
+
if (root.contains(document.activeElement)) return
|
|
74
|
+
const now = focusables(root)
|
|
75
|
+
now[back ? now.length - 1 : 0]?.focus({ preventScroll: true })
|
|
76
|
+
})
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
root.addEventListener('keydown', onKey)
|
|
80
|
+
return () => {
|
|
81
|
+
clearTimeout(check)
|
|
82
|
+
root.removeEventListener('keydown', onKey)
|
|
83
|
+
// Give focus back only if it is still in the layer, so a click elsewhere keeps its own focus.
|
|
84
|
+
const now = document.activeElement
|
|
85
|
+
const stillInside = !now || now === document.body || root.contains(now)
|
|
86
|
+
if (restore && stillInside && previous && document.contains(previous)) previous.focus({ preventScroll: true })
|
|
87
|
+
}
|
|
88
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
// FormField's control wiring and text, shared by React and Vue.
|
|
2
|
+
|
|
3
|
+
/** Props to spread on the control, so its label, help and error are wired up. */
|
|
4
|
+
export type FormFieldControlProps = {
|
|
5
|
+
/** Id the label points to with htmlFor. */
|
|
6
|
+
id: string
|
|
7
|
+
/** Id of the help or error text, whichever is showing. */
|
|
8
|
+
'aria-describedby'?: string
|
|
9
|
+
/** Set when the field has an error. */
|
|
10
|
+
'aria-invalid'?: true
|
|
11
|
+
/** Set when the field is required. */
|
|
12
|
+
'aria-required'?: true
|
|
13
|
+
/** Set when the field is disabled. */
|
|
14
|
+
disabled?: boolean
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export type FormFieldLabels = {
|
|
18
|
+
optional: string
|
|
19
|
+
}
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import { bezier } from '../feel'
|
|
2
|
+
import { body, boxPath, lerp, loopsPath, pincher, seg, trace, type Box } from '../liquidPath'
|
|
3
|
+
import { morphCurve, readMotionMs } from '../motion'
|
|
4
|
+
import { frames, layer, morphEase, motionFor } from './liquidMatter'
|
|
5
|
+
|
|
6
|
+
// The hold to confirm button's motion: the burning line on its edge and the cancel drop that buds out of it.
|
|
7
|
+
|
|
8
|
+
// A press shorter than this is a tap: it opens the confirm step, like one key press does.
|
|
9
|
+
export const TAP = 250
|
|
10
|
+
// Held this long, the hint changes to "Keep holding".
|
|
11
|
+
export const HINT_AFTER = 300
|
|
12
|
+
// A finger that slides this far off the button lets go.
|
|
13
|
+
export const LEAVE = 16
|
|
14
|
+
// How long the done state shows before the button can be used again.
|
|
15
|
+
export const DONE_FOR = 2400
|
|
16
|
+
// A click this soon after the step opens is the rest of a double-click, not a choice.
|
|
17
|
+
export const GUARD = 500
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* The cancel drop buds out of the button's right edge, its neck stretches and breaks, and it grows into
|
|
21
|
+
* the round cancel button (`in`), or flows back into the button (`out`). The button keeps its own look:
|
|
22
|
+
* the drawing is cut out where it is. `cls` is the drawing's class. Returns a stop, or null when motion is reduced.
|
|
23
|
+
*/
|
|
24
|
+
export function bud(row: HTMLElement, shell: HTMLElement, x: HTMLElement, way: 'in' | 'out', cls: string) {
|
|
25
|
+
// 400ms both ways: the drop is small, and at the morph's 600ms the cancel button felt late to arrive.
|
|
26
|
+
const ms = motionFor('reveal')
|
|
27
|
+
if (!ms) return null
|
|
28
|
+
const box = (el: HTMLElement): Box => {
|
|
29
|
+
const r = el.getBoundingClientRect(),
|
|
30
|
+
o = row.getBoundingClientRect()
|
|
31
|
+
return { cx: r.left - o.left + r.width / 2, cy: r.top - o.top + r.height / 2, hw: r.width / 2, hh: r.height / 2, r: r.height / 2 }
|
|
32
|
+
}
|
|
33
|
+
const O = box(shell)
|
|
34
|
+
const look = getComputedStyle(x)
|
|
35
|
+
const line = parseFloat(look.getPropertyValue('--gooee-line-thick')) || 1.5
|
|
36
|
+
const T = box(x)
|
|
37
|
+
const T2: Box = { ...T, hw: T.hw - line / 2, hh: T.hh - line / 2, r: T.r - line / 2 }
|
|
38
|
+
const { svg, paths } = layer(row, cls, null)
|
|
39
|
+
const path = paths[0]
|
|
40
|
+
path.style.fill = look.backgroundColor
|
|
41
|
+
path.style.stroke = look.getPropertyValue('--gooee-color-border-strong')
|
|
42
|
+
path.style.strokeWidth = `${line}px`
|
|
43
|
+
const clip = svg.appendChild(document.createElementNS('http://www.w3.org/2000/svg', 'clipPath'))
|
|
44
|
+
clip.id = `gooee-hold-${Math.random().toString(36).slice(2)}`
|
|
45
|
+
const hole = clip.appendChild(document.createElementNS('http://www.w3.org/2000/svg', 'path'))
|
|
46
|
+
hole.setAttribute('clip-rule', 'evenodd')
|
|
47
|
+
hole.setAttribute('d', `M-4000 -4000H4000V4000H-4000Z${boxPath({ ...O, hw: O.hw + 0.5, hh: O.hh + 0.5, r: O.r + 0.5 })}`)
|
|
48
|
+
path.setAttribute('clip-path', `url(#${clip.id})`)
|
|
49
|
+
const e = morphEase()
|
|
50
|
+
const pin = pincher(e, motionFor('fast'))
|
|
51
|
+
const edge = O.cx + O.hw - O.hh
|
|
52
|
+
const r0 = O.hh * 0.45
|
|
53
|
+
x.style.opacity = '0'
|
|
54
|
+
const stop = frames(
|
|
55
|
+
ms,
|
|
56
|
+
(k, now) => {
|
|
57
|
+
const u = way === 'in' ? k : 1 - k
|
|
58
|
+
const f = e(u)
|
|
59
|
+
const r = lerp(r0, T2.hh, e(seg(u, 0.25, 1)))
|
|
60
|
+
const D: Box = { cx: lerp(O.cx + O.hw - r0, T2.cx, f), cy: O.cy, hw: r, hh: r, r }
|
|
61
|
+
const p = pin(body(O, [edge, O.cy]), body(D), now)
|
|
62
|
+
path.setAttribute(
|
|
63
|
+
'd',
|
|
64
|
+
loopsPath(
|
|
65
|
+
trace(p.sdf, [
|
|
66
|
+
[edge, O.cy, 0, 1],
|
|
67
|
+
[D.cx, D.cy, 0, 1],
|
|
68
|
+
]),
|
|
69
|
+
),
|
|
70
|
+
)
|
|
71
|
+
},
|
|
72
|
+
() => {
|
|
73
|
+
svg.remove()
|
|
74
|
+
x.style.opacity = ''
|
|
75
|
+
},
|
|
76
|
+
)
|
|
77
|
+
return () => {
|
|
78
|
+
stop()
|
|
79
|
+
svg.remove()
|
|
80
|
+
x.style.opacity = ''
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// The pill's outline as one closed path that starts on the bottom edge at x and runs left first. The
|
|
85
|
+
// line is drawn at both ends of the path at once, so it burns out both ways from x and meets opposite.
|
|
86
|
+
export function outline(w: number, h: number, inset: number, x: number) {
|
|
87
|
+
const r = h / 2 - inset
|
|
88
|
+
const t = inset
|
|
89
|
+
const b = h - inset
|
|
90
|
+
const l = h / 2
|
|
91
|
+
const rr = w - h / 2
|
|
92
|
+
const px = Math.max(l, Math.min(rr, x))
|
|
93
|
+
return `M${px},${b} H${l} A${r},${r} 0 0 1 ${l},${t} H${rr} A${r},${r} 0 0 1 ${rr},${b} Z`
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export const cssMs = (el: Element | null, name: string, fallback: number) => {
|
|
97
|
+
const raw = el ? getComputedStyle(el).getPropertyValue(name).trim() : ''
|
|
98
|
+
const v = parseFloat(raw)
|
|
99
|
+
if (!Number.isFinite(v)) return fallback
|
|
100
|
+
return raw.endsWith('ms') ? v : raw.endsWith('s') ? v * 1000 : v
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* The burning line on the button's edge, drawn on `run` (a path with pathLength 1). `forward` burns at an
|
|
105
|
+
* even speed, since it is a clock, and calls `tick` each frame and `done` at the end. `rewind` runs back
|
|
106
|
+
* fast on the morph curve: a rubber band, not a clock.
|
|
107
|
+
*/
|
|
108
|
+
export function createFuse(run: () => SVGPathElement | null) {
|
|
109
|
+
let p = 0
|
|
110
|
+
let raf = 0
|
|
111
|
+
const paint = (v: number) => {
|
|
112
|
+
p = v
|
|
113
|
+
const el = run()
|
|
114
|
+
if (!el) return
|
|
115
|
+
el.style.strokeDasharray = `${v / 2} ${1 - v} ${v / 2} 0`
|
|
116
|
+
el.style.opacity = v > 0 ? '1' : '0'
|
|
117
|
+
}
|
|
118
|
+
const stop = () => cancelAnimationFrame(raf)
|
|
119
|
+
const forward = (total: number, tick: (now: number) => void, done: () => void) => {
|
|
120
|
+
stop()
|
|
121
|
+
let last = performance.now()
|
|
122
|
+
const step = (now: number) => {
|
|
123
|
+
const next = Math.min(1, p + (now - last) / total)
|
|
124
|
+
last = now
|
|
125
|
+
paint(next)
|
|
126
|
+
tick(now)
|
|
127
|
+
if (next >= 1) return done()
|
|
128
|
+
raf = requestAnimationFrame(step)
|
|
129
|
+
}
|
|
130
|
+
raf = requestAnimationFrame(step)
|
|
131
|
+
}
|
|
132
|
+
const rewind = () => {
|
|
133
|
+
stop()
|
|
134
|
+
const from = p
|
|
135
|
+
const length = readMotionMs('fast')
|
|
136
|
+
if (!length || !from) return paint(0)
|
|
137
|
+
const ease = bezier(morphCurve())
|
|
138
|
+
const t0 = performance.now()
|
|
139
|
+
const step = (now: number) => {
|
|
140
|
+
const t = Math.min(1, (now - t0) / length)
|
|
141
|
+
paint(from * (1 - ease(t)))
|
|
142
|
+
if (t < 1) raf = requestAnimationFrame(step)
|
|
143
|
+
}
|
|
144
|
+
raf = requestAnimationFrame(step)
|
|
145
|
+
}
|
|
146
|
+
return { paint, forward, rewind, stop }
|
|
147
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
// Page-wide key watchers shared by the React and Vue parts. Each returns a function that stops it.
|
|
2
|
+
|
|
3
|
+
// Calls down(true) while the key is held and down(false) when it is let go or the window loses focus.
|
|
4
|
+
export function holdKey(key: string, down: (held: boolean) => void) {
|
|
5
|
+
const match = (e: KeyboardEvent) => e.key.toLowerCase() === key.toLowerCase()
|
|
6
|
+
const onDown = (e: KeyboardEvent) => match(e) && down(true)
|
|
7
|
+
const onUp = (e: KeyboardEvent) => match(e) && down(false)
|
|
8
|
+
const reset = () => down(false)
|
|
9
|
+
window.addEventListener('keydown', onDown)
|
|
10
|
+
window.addEventListener('keyup', onUp)
|
|
11
|
+
window.addEventListener('blur', reset)
|
|
12
|
+
return () => {
|
|
13
|
+
window.removeEventListener('keydown', onDown)
|
|
14
|
+
window.removeEventListener('keyup', onUp)
|
|
15
|
+
window.removeEventListener('blur', reset)
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const isTyping = (el: Element | null) =>
|
|
20
|
+
!!el && (el instanceof HTMLInputElement || el instanceof HTMLTextAreaElement || el instanceof HTMLSelectElement || (el as HTMLElement).isContentEditable)
|
|
21
|
+
|
|
22
|
+
// Pressing the key anywhere focuses and selects the field. Ignored while another field has focus, so it
|
|
23
|
+
// never steals typed text, and while a modal the field is not in is open, so focus never leaves the modal.
|
|
24
|
+
export function shortcutFocus(key: string, field: () => HTMLInputElement | null) {
|
|
25
|
+
const onKey = (e: KeyboardEvent) => {
|
|
26
|
+
if (e.key !== key || e.metaKey || e.ctrlKey || e.altKey || isTyping(document.activeElement)) return
|
|
27
|
+
const modals = document.querySelectorAll('[aria-modal="true"]')
|
|
28
|
+
if (modals.length && !modals[modals.length - 1].contains(field())) return
|
|
29
|
+
e.preventDefault()
|
|
30
|
+
field()?.focus()
|
|
31
|
+
field()?.select()
|
|
32
|
+
}
|
|
33
|
+
window.addEventListener('keydown', onKey)
|
|
34
|
+
return () => window.removeEventListener('keydown', onKey)
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// A textarea grows with its text.
|
|
38
|
+
export function fitHeight(el: HTMLTextAreaElement | null) {
|
|
39
|
+
if (!el) return
|
|
40
|
+
el.style.height = 'auto'
|
|
41
|
+
el.style.height = `${el.scrollHeight}px`
|
|
42
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
// A stack of open layers, so Esc and outside clicks only close the top one.
|
|
2
|
+
const stack: Array<{ id: symbol; hint: boolean }> = []
|
|
3
|
+
|
|
4
|
+
export type DismissOptions = {
|
|
5
|
+
/** A press outside counts. On by default. */
|
|
6
|
+
outside?: boolean
|
|
7
|
+
/** A tooltip-like layer: it takes Esc first, but clicks pass to the layer below it. */
|
|
8
|
+
hint?: boolean
|
|
9
|
+
/** Focus moving outside it, while no layer is open above it, counts as outside too. */
|
|
10
|
+
focusOut?: boolean
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Joins the layer stack until the returned function is called. Esc, and a press outside the elements
|
|
15
|
+
* `inside` returns (or focus leaving them, with focusOut), call `onDismiss` while this is the top layer.
|
|
16
|
+
*/
|
|
17
|
+
export function dismissable(inside: () => Array<HTMLElement | null>, onDismiss: (reason: 'escape' | 'outside', event: Event) => void, { outside = true, hint = false, focusOut = false }: DismissOptions = {}) {
|
|
18
|
+
const id = Symbol('layer')
|
|
19
|
+
stack.push({ id, hint })
|
|
20
|
+
const isTop = () => stack[stack.length - 1]?.id === id
|
|
21
|
+
const isTopForPointer = () => stack.filter((l) => !l.hint).pop()?.id === id
|
|
22
|
+
const within = (target: Node) => inside().some((el) => el?.contains(target))
|
|
23
|
+
|
|
24
|
+
const onKey = (e: KeyboardEvent) => {
|
|
25
|
+
if (e.key === 'Escape' && isTop()) {
|
|
26
|
+
e.stopPropagation()
|
|
27
|
+
onDismiss('escape', e)
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
const onPointer = (e: PointerEvent) => {
|
|
31
|
+
if (!outside || !isTopForPointer()) return
|
|
32
|
+
const target = e.target as Node
|
|
33
|
+
if (within(target)) return
|
|
34
|
+
// Things like the toast stack sit above every layer and never count as outside.
|
|
35
|
+
if (target instanceof Element && target.closest('[data-gooee-layer-ignore]')) return
|
|
36
|
+
onDismiss('outside', e)
|
|
37
|
+
}
|
|
38
|
+
const onFocus = (e: FocusEvent) => {
|
|
39
|
+
if (!focusOut || !isTopForPointer()) return
|
|
40
|
+
if (within(e.target as Node)) return
|
|
41
|
+
onDismiss('outside', e)
|
|
42
|
+
}
|
|
43
|
+
document.addEventListener('keydown', onKey)
|
|
44
|
+
document.addEventListener('pointerdown', onPointer)
|
|
45
|
+
document.addEventListener('focusin', onFocus)
|
|
46
|
+
return () => {
|
|
47
|
+
document.removeEventListener('keydown', onKey)
|
|
48
|
+
document.removeEventListener('pointerdown', onPointer)
|
|
49
|
+
document.removeEventListener('focusin', onFocus)
|
|
50
|
+
const i = stack.findIndex((l) => l.id === id)
|
|
51
|
+
if (i >= 0) stack.splice(i, 1)
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
let lockCount = 0
|
|
56
|
+
let saved = ''
|
|
57
|
+
let savedPadding = ''
|
|
58
|
+
|
|
59
|
+
/** Stops the page from scrolling until the returned function is called. Locks can nest. */
|
|
60
|
+
export function lockScroll() {
|
|
61
|
+
if (lockCount === 0) {
|
|
62
|
+
const body = document.body
|
|
63
|
+
// Fills the space the scrollbar leaves, so the page does not shift sideways.
|
|
64
|
+
const bar = window.innerWidth - document.documentElement.clientWidth
|
|
65
|
+
saved = body.style.overflow
|
|
66
|
+
savedPadding = body.style.paddingRight
|
|
67
|
+
if (bar > 0) body.style.paddingRight = `calc(${getComputedStyle(body).paddingRight} + ${bar}px)`
|
|
68
|
+
body.style.overflow = 'hidden'
|
|
69
|
+
}
|
|
70
|
+
lockCount++
|
|
71
|
+
return () => {
|
|
72
|
+
lockCount--
|
|
73
|
+
if (lockCount === 0) {
|
|
74
|
+
document.body.style.overflow = saved
|
|
75
|
+
document.body.style.paddingRight = savedPadding
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
}
|