@hozo/behaviors 0.1.0 → 0.3.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/CHANGELOG.md +49 -0
- package/README.md +16 -2
- package/dist/accessibility-focus.native.d.ts +23 -0
- package/dist/accessibility-focus.native.d.ts.map +1 -0
- package/dist/accessibility-focus.native.js +81 -0
- package/dist/accessibility-focus.native.js.map +1 -0
- package/dist/command-shortcut.d.ts +23 -0
- package/dist/command-shortcut.d.ts.map +1 -0
- package/dist/command-shortcut.js +47 -0
- package/dist/command-shortcut.js.map +1 -0
- package/dist/command-shortcut.native.d.ts +11 -0
- package/dist/command-shortcut.native.d.ts.map +1 -0
- package/dist/command-shortcut.native.js +11 -0
- package/dist/command-shortcut.native.js.map +1 -0
- package/dist/focus-scope.d.ts +9 -1
- package/dist/focus-scope.d.ts.map +1 -1
- package/dist/focus-scope.js +43 -11
- package/dist/focus-scope.js.map +1 -1
- package/dist/i18n.d.ts +140 -0
- package/dist/i18n.d.ts.map +1 -0
- package/dist/i18n.js +144 -0
- package/dist/i18n.js.map +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -1
- package/dist/index.native.d.ts +3 -0
- package/dist/index.native.d.ts.map +1 -1
- package/dist/index.native.js +3 -0
- package/dist/index.native.js.map +1 -1
- package/dist/native.d.ts +3 -0
- package/dist/native.d.ts.map +1 -1
- package/dist/native.js +3 -0
- package/dist/native.js.map +1 -1
- package/dist/presence.d.ts +19 -0
- package/dist/presence.d.ts.map +1 -0
- package/dist/presence.js +96 -0
- package/dist/presence.js.map +1 -0
- package/dist/presence.native.d.ts +10 -0
- package/dist/presence.native.d.ts.map +1 -0
- package/dist/presence.native.js +18 -0
- package/dist/presence.native.js.map +1 -0
- package/dist/pressable-interaction.d.ts +30 -0
- package/dist/pressable-interaction.d.ts.map +1 -0
- package/dist/pressable-interaction.js +64 -0
- package/dist/pressable-interaction.js.map +1 -0
- package/package.json +7 -2
- package/src/accessibility-focus.native.ts +88 -0
- package/src/command-shortcut.native.ts +15 -0
- package/src/command-shortcut.ts +51 -0
- package/src/focus-scope.tsx +49 -8
- package/src/i18n.ts +198 -0
- package/src/index.native.ts +20 -0
- package/src/index.ts +24 -0
- package/src/native.ts +22 -0
- package/src/presence.native.ts +27 -0
- package/src/presence.ts +118 -0
- package/src/pressable-interaction.ts +79 -0
package/src/presence.ts
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
// Keeping an element mounted while it animates out, on the Web
|
|
2
|
+
// (decision 007, slice 2).
|
|
3
|
+
//
|
|
4
|
+
// React removes an element the moment it stops being rendered, so nothing
|
|
5
|
+
// can animate after that. This holds on to it after `show` turns false,
|
|
6
|
+
// reports `state: 'closed'` for the caller to put on the element as
|
|
7
|
+
// `data-state`, and lets it go when the element's transition or animation
|
|
8
|
+
// ends. The exit itself is CSS -- a `data-[state=closed]:` class and a
|
|
9
|
+
// `transition-*` one -- so it costs no script beyond this.
|
|
10
|
+
//
|
|
11
|
+
// A hook rather than only a component because Hozo's overlays cannot wrap
|
|
12
|
+
// their panel in one: the panel sits inside a positioner, a dismiss layer
|
|
13
|
+
// and a focus scope, and it is the panel that leaves. `Presence` in
|
|
14
|
+
// `@hozo/primitives` is this hook around one child.
|
|
15
|
+
|
|
16
|
+
import { useCallback, useEffect, useRef, useState } from 'react'
|
|
17
|
+
|
|
18
|
+
export type PresenceState = 'open' | 'closed'
|
|
19
|
+
|
|
20
|
+
export interface PresenceBinding {
|
|
21
|
+
/** Whether to render the element at all. */
|
|
22
|
+
mounted: boolean
|
|
23
|
+
/** For the element's `data-state`. */
|
|
24
|
+
state: PresenceState
|
|
25
|
+
/** The element whose transition is the exit. */
|
|
26
|
+
ref: (node: Element | null) => void
|
|
27
|
+
onTransitionEnd: (event: { target: EventTarget; currentTarget: EventTarget }) => void
|
|
28
|
+
onAnimationEnd: (event: { target: EventTarget; currentTarget: EventTarget }) => void
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** How long past the computed end to wait for an event that never fires. */
|
|
32
|
+
const GRACE_MS = 100
|
|
33
|
+
|
|
34
|
+
/** The longest `duration + delay` across a comma-separated pair of lists. */
|
|
35
|
+
function longest(durations: string, delays: string): number {
|
|
36
|
+
const ms = (value: string) => {
|
|
37
|
+
const number = Number.parseFloat(value)
|
|
38
|
+
if (Number.isNaN(number)) return 0
|
|
39
|
+
return value.trim().endsWith('ms') ? number : number * 1000
|
|
40
|
+
}
|
|
41
|
+
const d = durations.split(',')
|
|
42
|
+
const l = delays.split(',')
|
|
43
|
+
// CSS repeats the shorter list to the length of the longer one.
|
|
44
|
+
const length = Math.max(d.length, l.length)
|
|
45
|
+
let out = 0
|
|
46
|
+
for (let i = 0; i < length; i++) {
|
|
47
|
+
out = Math.max(out, ms(d[i % d.length] ?? '0s') + ms(l[i % l.length] ?? '0s'))
|
|
48
|
+
}
|
|
49
|
+
return out
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** How long the element's exit runs, from its computed style. */
|
|
53
|
+
function exitDuration(element: Element): number {
|
|
54
|
+
const style = getComputedStyle(element)
|
|
55
|
+
return Math.max(
|
|
56
|
+
longest(style.transitionDuration, style.transitionDelay),
|
|
57
|
+
style.animationName === 'none' ? 0 : longest(style.animationDuration, style.animationDelay),
|
|
58
|
+
)
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export function usePresence(show: boolean): PresenceBinding {
|
|
62
|
+
const [mounted, setMounted] = useState(show)
|
|
63
|
+
// Set during render rather than in an effect, so an element that is shown
|
|
64
|
+
// again appears in the same commit instead of one frame late.
|
|
65
|
+
if (show && !mounted) setMounted(true)
|
|
66
|
+
|
|
67
|
+
const element = useRef<Element | null>(null)
|
|
68
|
+
const ending = useRef<{ at: number; timer: ReturnType<typeof setTimeout> } | null>(null)
|
|
69
|
+
|
|
70
|
+
const finish = useCallback(() => {
|
|
71
|
+
if (ending.current) clearTimeout(ending.current.timer)
|
|
72
|
+
ending.current = null
|
|
73
|
+
setMounted(false)
|
|
74
|
+
}, [])
|
|
75
|
+
|
|
76
|
+
useEffect(() => {
|
|
77
|
+
if (show || !mounted) return
|
|
78
|
+
// Read after the commit that set `data-state="closed"`, so the
|
|
79
|
+
// computed style is the one the element is leaving with -- including
|
|
80
|
+
// `motion-reduce:transition-none`, which makes this zero.
|
|
81
|
+
const target = element.current
|
|
82
|
+
const duration = target ? exitDuration(target) : 0
|
|
83
|
+
if (duration === 0) {
|
|
84
|
+
finish()
|
|
85
|
+
return
|
|
86
|
+
}
|
|
87
|
+
const at = performance.now() + duration
|
|
88
|
+
ending.current = { at, timer: setTimeout(finish, duration + GRACE_MS) }
|
|
89
|
+
return () => {
|
|
90
|
+
if (ending.current) clearTimeout(ending.current.timer)
|
|
91
|
+
ending.current = null
|
|
92
|
+
}
|
|
93
|
+
}, [show, mounted, finish])
|
|
94
|
+
|
|
95
|
+
// `transitionend` fires once per property, and the properties may end
|
|
96
|
+
// at different times. Only the one that ends last finishes the exit.
|
|
97
|
+
const onEnd = useCallback(
|
|
98
|
+
(event: { target: EventTarget; currentTarget: EventTarget }) => {
|
|
99
|
+
if (event.target !== event.currentTarget) return
|
|
100
|
+
const pending = ending.current
|
|
101
|
+
if (!pending || performance.now() < pending.at - 20) return
|
|
102
|
+
finish()
|
|
103
|
+
},
|
|
104
|
+
[finish],
|
|
105
|
+
)
|
|
106
|
+
|
|
107
|
+
const ref = useCallback((node: Element | null) => {
|
|
108
|
+
element.current = node
|
|
109
|
+
}, [])
|
|
110
|
+
|
|
111
|
+
return {
|
|
112
|
+
mounted,
|
|
113
|
+
state: show ? 'open' : 'closed',
|
|
114
|
+
ref,
|
|
115
|
+
onTransitionEnd: onEnd,
|
|
116
|
+
onAnimationEnd: onEnd,
|
|
117
|
+
}
|
|
118
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { useCallback, useMemo, useRef, useState } from 'react'
|
|
2
|
+
|
|
3
|
+
export interface HozoPressableInteractionState {
|
|
4
|
+
pressed: boolean
|
|
5
|
+
hovered: boolean
|
|
6
|
+
focused: boolean
|
|
7
|
+
focusVisible: boolean
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
const HOVERED = 1
|
|
11
|
+
const FOCUSED = 2
|
|
12
|
+
const PRESSED = 4
|
|
13
|
+
const FOCUS_VISIBLE = 8
|
|
14
|
+
const read = (mask: number): HozoPressableInteractionState => ({
|
|
15
|
+
pressed: (mask & PRESSED) !== 0,
|
|
16
|
+
hovered: (mask & HOVERED) !== 0,
|
|
17
|
+
focused: (mask & FOCUSED) !== 0,
|
|
18
|
+
focusVisible: (mask & FOCUS_VISIBLE) !== 0,
|
|
19
|
+
})
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* The Native pressable's event-driven facts, shared with patterns (#897).
|
|
23
|
+
* No subscriptions, animation nodes or global modality listeners. Hosts own
|
|
24
|
+
* event delivery and compose their caller's handlers after these callbacks.
|
|
25
|
+
* onChange runs synchronously for the existing native animation driver;
|
|
26
|
+
* React state drives the ordinary style/children callbacks.
|
|
27
|
+
*/
|
|
28
|
+
export function usePressableInteraction({
|
|
29
|
+
focusVisible = false,
|
|
30
|
+
onChange,
|
|
31
|
+
}: {
|
|
32
|
+
focusVisible?: boolean
|
|
33
|
+
onChange?: (state: HozoPressableInteractionState) => void
|
|
34
|
+
} = {}) {
|
|
35
|
+
const [mask, setMask] = useState(0)
|
|
36
|
+
const live = useRef(0)
|
|
37
|
+
// Matches the primitive's existing policy: initial focus is keyboard-like;
|
|
38
|
+
// a pointer/press clears it, and a key makes a currently focused host visible.
|
|
39
|
+
const modality = useRef<'keyboard' | 'pointer'>('keyboard')
|
|
40
|
+
const set = useCallback(
|
|
41
|
+
(flag: number, active: boolean) => {
|
|
42
|
+
const next = active ? live.current | flag : live.current & ~flag
|
|
43
|
+
live.current = next
|
|
44
|
+
onChange?.(read(next))
|
|
45
|
+
setMask(next)
|
|
46
|
+
},
|
|
47
|
+
[onChange],
|
|
48
|
+
)
|
|
49
|
+
const handlers = useMemo(
|
|
50
|
+
() => ({
|
|
51
|
+
hoverIn: () => set(HOVERED, true),
|
|
52
|
+
hoverOut: () => set(HOVERED, false),
|
|
53
|
+
focus: () => {
|
|
54
|
+
set(FOCUSED, true)
|
|
55
|
+
if (focusVisible) set(FOCUS_VISIBLE, modality.current === 'keyboard')
|
|
56
|
+
},
|
|
57
|
+
blur: () => {
|
|
58
|
+
set(FOCUSED, false)
|
|
59
|
+
if (focusVisible) set(FOCUS_VISIBLE, false)
|
|
60
|
+
},
|
|
61
|
+
pointerDown: () => {
|
|
62
|
+
modality.current = 'pointer'
|
|
63
|
+
if (focusVisible) set(FOCUS_VISIBLE, false)
|
|
64
|
+
},
|
|
65
|
+
keyDown: () => {
|
|
66
|
+
modality.current = 'keyboard'
|
|
67
|
+
if (focusVisible && (live.current & FOCUSED) !== 0) set(FOCUS_VISIBLE, true)
|
|
68
|
+
},
|
|
69
|
+
pressIn: () => {
|
|
70
|
+
modality.current = 'pointer'
|
|
71
|
+
if (focusVisible) set(FOCUS_VISIBLE, false)
|
|
72
|
+
set(PRESSED, true)
|
|
73
|
+
},
|
|
74
|
+
pressOut: () => set(PRESSED, false),
|
|
75
|
+
}),
|
|
76
|
+
[focusVisible, set],
|
|
77
|
+
)
|
|
78
|
+
return { state: useMemo(() => read(mask), [mask]), handlers }
|
|
79
|
+
}
|