gooee 0.48.0 → 0.51.0
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/package.json +4 -2
- 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 -142
- 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 +12 -40
- 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/fling.ts +29 -106
- package/src/internal/focus.ts +4 -79
- package/src/internal/icons.module.css +39 -27
- package/src/internal/layers.ts +7 -61
- package/src/internal/liquid.ts +26 -213
- package/src/internal/liquidMatter.ts +9 -181
- package/src/internal/liquidMore.ts +6 -669
- 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/fling.ts +147 -0
- package/src/internal/motion/floating.ts +58 -0
- package/src/internal/motion/focus.ts +88 -0
- package/src/internal/motion/formField.ts +19 -0
- package/src/internal/motion/holdToConfirm.ts +147 -0
- package/src/internal/motion/index.ts +20 -0
- package/src/internal/motion/keys.ts +42 -0
- package/src/internal/motion/layers.ts +78 -0
- package/src/internal/motion/liquid.ts +241 -0
- package/src/internal/motion/liquidMatter.ts +198 -0
- package/src/internal/motion/liquidMore.ts +676 -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/scrollFrame.ts +152 -0
- package/src/internal/motion/scrubLook.ts +15 -0
- package/src/internal/motion/scrubber.ts +154 -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/softPress.ts +334 -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/scrollFrame.ts +4 -145
- package/src/internal/softPress.ts +27 -289
- package/src/internal/themeReveal.ts +3 -61
- package/src/internal/useFloating.ts +9 -49
- package/src/internal/useScrub.ts +15 -120
- 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,17 @@
|
|
|
1
|
+
import { easing, motionMs } from '../motion'
|
|
2
|
+
|
|
3
|
+
// Folds an alert's height to zero, so content below slides up instead of jumping, then calls done.
|
|
4
|
+
export function foldAway(el: HTMLElement, done: () => void) {
|
|
5
|
+
const ms = motionMs('slow')
|
|
6
|
+
if (ms === 0 || !el.animate) return done()
|
|
7
|
+
const h = el.offsetHeight
|
|
8
|
+
const anim = el.animate(
|
|
9
|
+
[
|
|
10
|
+
{ height: `${h}px`, opacity: 1, transform: 'scale(1)' },
|
|
11
|
+
{ height: `${h}px`, opacity: 0, transform: 'scale(0.98)', offset: 0.45 },
|
|
12
|
+
{ height: '0px', opacity: 0, transform: 'scale(0.98)', paddingTop: '0px', paddingBottom: '0px', marginTop: '0px', marginBottom: '0px', borderWidth: '0px' },
|
|
13
|
+
],
|
|
14
|
+
{ duration: ms, easing: easing('morph'), fill: 'forwards' },
|
|
15
|
+
)
|
|
16
|
+
anim.onfinish = done
|
|
17
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
// A row with no room wraps onto more rows. Each row stays joined, with round ends of its own, so the
|
|
2
|
+
// buttons that start and end a row are marked as they fall. Returns the function that stops watching.
|
|
3
|
+
export function markRows(root: HTMLElement) {
|
|
4
|
+
const mark = () => {
|
|
5
|
+
const kids = [...root.children] as HTMLElement[]
|
|
6
|
+
kids.forEach((k, i) => {
|
|
7
|
+
const prev = kids[i - 1], next = kids[i + 1]
|
|
8
|
+
k.toggleAttribute('data-row-start', !prev || prev.offsetTop !== k.offsetTop)
|
|
9
|
+
k.toggleAttribute('data-row-end', !next || next.offsetTop !== k.offsetTop)
|
|
10
|
+
})
|
|
11
|
+
}
|
|
12
|
+
mark()
|
|
13
|
+
const ro = new ResizeObserver(mark)
|
|
14
|
+
ro.observe(root)
|
|
15
|
+
const mo = new MutationObserver(mark)
|
|
16
|
+
mo.observe(root, { childList: true })
|
|
17
|
+
return () => {
|
|
18
|
+
ro.disconnect()
|
|
19
|
+
mo.disconnect()
|
|
20
|
+
}
|
|
21
|
+
}
|
|
@@ -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,147 @@
|
|
|
1
|
+
import { morphTo, rubber, speedTracker } from '../feel'
|
|
2
|
+
import { morphCurve, readMotionMs } from '../motion'
|
|
3
|
+
|
|
4
|
+
/** An element, or a function that returns it, for frameworks that hand it over later (a React ref). */
|
|
5
|
+
export type Target = HTMLElement | (() => HTMLElement | null)
|
|
6
|
+
const resolve = (t: Target) => (typeof t === 'function' ? t() : t)
|
|
7
|
+
|
|
8
|
+
/** What a drag needs from a pointer event: native and React events both have these. */
|
|
9
|
+
export type PointerLike = Pick<PointerEvent, 'clientY' | 'pointerId' | 'pointerType' | 'button' | 'type'> & { target: EventTarget | null; currentTarget: EventTarget | null }
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* A let-go: runs from `from` to `to` on the morph curve, starting at `velocity` (units per second), so
|
|
13
|
+
* it carries on at the hand's speed. From rest it takes --gooee-motion-morph-out: Retime slows it,
|
|
14
|
+
* reduced motion (0ms) jumps. Returns a stop function.
|
|
15
|
+
*/
|
|
16
|
+
export function release(from: number, to: number, velocity: number, onFrame: (value: number) => void, onDone?: () => void) {
|
|
17
|
+
return morphTo({ from, to, velocity, duration: readMotionMs('morph-out'), curve: morphCurve(), onFrame, onDone })
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* A panel that the hand drags along one axis and lets go of. `fling` glides it to a place at the hand's
|
|
22
|
+
* speed. While it runs, data-fluid on the panel turns its CSS motion off; once it closes on the way out,
|
|
23
|
+
* the CSS close keyframes stay off too, so they do not start over. Call `opened` and `closed` as the
|
|
24
|
+
* panel opens and closes.
|
|
25
|
+
*/
|
|
26
|
+
export function createFling(target: Target, axis: 'x' | 'y') {
|
|
27
|
+
let at = 0
|
|
28
|
+
let to = 0
|
|
29
|
+
let stop = () => {}
|
|
30
|
+
|
|
31
|
+
const paint = (v: number) => {
|
|
32
|
+
at = v
|
|
33
|
+
const el = resolve(target)
|
|
34
|
+
if (el) el.style.transform = v ? `translate${axis === 'x' ? 'X' : 'Y'}(${v}px)` : ''
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
return {
|
|
38
|
+
get at() {
|
|
39
|
+
return at
|
|
40
|
+
},
|
|
41
|
+
paint,
|
|
42
|
+
fling(place: number, speed: number) {
|
|
43
|
+
const el = resolve(target)
|
|
44
|
+
if (!el) return
|
|
45
|
+
stop()
|
|
46
|
+
to = place
|
|
47
|
+
el.setAttribute('data-fluid', '')
|
|
48
|
+
stop = release(at, place, speed, paint, () => {
|
|
49
|
+
if (place === 0) el.removeAttribute('data-fluid')
|
|
50
|
+
})
|
|
51
|
+
},
|
|
52
|
+
stop: () => stop(),
|
|
53
|
+
// Opened again, even mid-close on the same panel: it starts at rest, not where the old glide was going.
|
|
54
|
+
opened() {
|
|
55
|
+
stop()
|
|
56
|
+
to = 0
|
|
57
|
+
paint(0)
|
|
58
|
+
resolve(target)?.removeAttribute('data-fluid')
|
|
59
|
+
},
|
|
60
|
+
// Closed by Esc or a tap while gliding back: the CSS close takes over from where it is.
|
|
61
|
+
closed() {
|
|
62
|
+
if (to !== 0) return
|
|
63
|
+
stop()
|
|
64
|
+
resolve(target)?.removeAttribute('data-fluid')
|
|
65
|
+
},
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export type Fling = ReturnType<typeof createFling>
|
|
70
|
+
|
|
71
|
+
// Past this share of its height, or flicked down this fast (px per second), a let-go closes the sheet.
|
|
72
|
+
const CLOSE_RATIO = 0.3
|
|
73
|
+
const CLOSE_SPEED = 500
|
|
74
|
+
|
|
75
|
+
export type DragCloseOptions = {
|
|
76
|
+
/** Asked to close. If the panel is still open a frame later, it glides back. */
|
|
77
|
+
onClose: () => void
|
|
78
|
+
isOpen: () => boolean
|
|
79
|
+
/** Asked at each press, so a part can take hold only in its phone layout, or only by its header. */
|
|
80
|
+
can?: (e: PointerLike) => boolean
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* A bottom sheet the hand drags down to close. Feed it the pointer events of the part it is held by, or
|
|
85
|
+
* call `listen` on that part. A let-go past a third of its height, or a flick down, closes it; any other
|
|
86
|
+
* let-go glides it back.
|
|
87
|
+
*/
|
|
88
|
+
export function createDragClose(target: Target, { onClose, isOpen, can = () => true }: DragCloseOptions) {
|
|
89
|
+
const fling = createFling(target, 'y')
|
|
90
|
+
let drag: { y: number; dy: number; pointer: number; speed: ReturnType<typeof speedTracker> } | null = null
|
|
91
|
+
|
|
92
|
+
const down = (e: PointerLike) => {
|
|
93
|
+
if ((e.target as HTMLElement).closest('button') || !can(e)) return
|
|
94
|
+
if (e.pointerType === 'mouse' && e.button !== 0) return
|
|
95
|
+
fling.stop()
|
|
96
|
+
// Caught mid-glide, it carries on from where it is.
|
|
97
|
+
drag = { y: e.clientY - fling.at, dy: fling.at, pointer: e.pointerId, speed: speedTracker() }
|
|
98
|
+
drag.speed.add(fling.at)
|
|
99
|
+
;(e.currentTarget as HTMLElement).setPointerCapture(e.pointerId)
|
|
100
|
+
resolve(target)?.setAttribute('data-dragging', '')
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const move = (e: PointerLike) => {
|
|
104
|
+
const panel = resolve(target)
|
|
105
|
+
if (!drag || !panel || drag.pointer !== e.pointerId) return
|
|
106
|
+
const raw = e.clientY - drag.y
|
|
107
|
+
drag.dy = raw >= 0 ? raw : rubber(raw, panel.offsetHeight)
|
|
108
|
+
drag.speed.add(drag.dy)
|
|
109
|
+
fling.paint(drag.dy)
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
const up = (e: PointerLike) => {
|
|
113
|
+
const d = drag
|
|
114
|
+
const panel = resolve(target)
|
|
115
|
+
drag = null
|
|
116
|
+
if (!d || !panel) return
|
|
117
|
+
;(e.currentTarget as HTMLElement).releasePointerCapture(d.pointer)
|
|
118
|
+
panel.removeAttribute('data-dragging')
|
|
119
|
+
const cancelled = e.type === 'pointercancel'
|
|
120
|
+
const speed = cancelled ? 0 : d.speed.speed()
|
|
121
|
+
const h = panel.offsetHeight
|
|
122
|
+
if (!cancelled && (d.dy > h * CLOSE_RATIO || (speed > CLOSE_SPEED && d.dy > 24))) {
|
|
123
|
+
fling.fling(h, speed)
|
|
124
|
+
onClose()
|
|
125
|
+
requestAnimationFrame(() => {
|
|
126
|
+
if (isOpen()) fling.fling(0, 0)
|
|
127
|
+
})
|
|
128
|
+
} else fling.fling(0, speed)
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
return {
|
|
132
|
+
fling,
|
|
133
|
+
down,
|
|
134
|
+
move,
|
|
135
|
+
up,
|
|
136
|
+
/** Listens on the part it is held by. Returns a function that stops listening. */
|
|
137
|
+
listen(handle: HTMLElement) {
|
|
138
|
+
const ac = new AbortController()
|
|
139
|
+
const opts = { signal: ac.signal }
|
|
140
|
+
handle.addEventListener('pointerdown', down, opts)
|
|
141
|
+
handle.addEventListener('pointermove', move, opts)
|
|
142
|
+
handle.addEventListener('pointerup', up, opts)
|
|
143
|
+
handle.addEventListener('pointercancel', up, opts)
|
|
144
|
+
return () => ac.abort()
|
|
145
|
+
},
|
|
146
|
+
}
|
|
147
|
+
}
|
|
@@ -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 }
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { watchKeyboard } from '../keyboard'
|
|
2
|
+
|
|
3
|
+
const FOCUSABLE =
|
|
4
|
+
'a[href], button:not([disabled]), input:not([disabled]), select:not([disabled]), textarea:not([disabled]), [tabindex]:not([tabindex="-1"]), [contenteditable="true"]'
|
|
5
|
+
|
|
6
|
+
export function focusables(root: HTMLElement): HTMLElement[] {
|
|
7
|
+
return Array.from(root.querySelectorAll<HTMLElement>(FOCUSABLE)).filter(
|
|
8
|
+
(el) => !el.hasAttribute('inert') && el.getClientRects().length > 0,
|
|
9
|
+
)
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
// Safari, on iPhone and on Mac, does not focus a button when it is tapped or clicked. So the control
|
|
13
|
+
// that opened a layer is often not the focused element; remember the last one pressed instead.
|
|
14
|
+
// Watched from the moment this file loads, since a layer only mounts after the press that opens it.
|
|
15
|
+
let lastPressed: HTMLElement | null = null
|
|
16
|
+
if (typeof document !== 'undefined') {
|
|
17
|
+
document.addEventListener(
|
|
18
|
+
'pointerdown',
|
|
19
|
+
(e) => {
|
|
20
|
+
lastPressed = (e.target instanceof Element ? e.target.closest<HTMLElement>(FOCUSABLE) : null) ?? null
|
|
21
|
+
},
|
|
22
|
+
true,
|
|
23
|
+
)
|
|
24
|
+
// Focus moved from code stays above a phone keyboard.
|
|
25
|
+
watchKeyboard()
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// Where focus should go back to: the focused element, unless it is the page or a box around the
|
|
29
|
+
// control that was just pressed. Then that control.
|
|
30
|
+
export function returnTarget(): HTMLElement | null {
|
|
31
|
+
const active = document.activeElement as HTMLElement | null
|
|
32
|
+
const pressed = lastPressed && lastPressed.isConnected ? lastPressed : null
|
|
33
|
+
if (pressed && (!active || active === document.body || (active !== pressed && active.contains(pressed)))) return pressed
|
|
34
|
+
return active
|
|
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
|
+
}
|