@hozo/behaviors 0.2.0 → 0.4.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 +70 -0
- package/README.md +17 -3
- 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/drag-1d-session.d.ts +26 -0
- package/dist/drag-1d-session.d.ts.map +1 -0
- package/dist/drag-1d-session.js +65 -0
- package/dist/drag-1d-session.js.map +1 -0
- package/dist/drag-1d.d.ts +14 -0
- package/dist/drag-1d.d.ts.map +1 -0
- package/dist/drag-1d.js +93 -0
- package/dist/drag-1d.js.map +1 -0
- package/dist/drag-1d.native.d.ts +8 -0
- package/dist/drag-1d.native.d.ts.map +1 -0
- package/dist/drag-1d.native.js +52 -0
- package/dist/drag-1d.native.js.map +1 -0
- package/dist/floating-geometry.d.ts +19 -0
- package/dist/floating-geometry.d.ts.map +1 -1
- package/dist/floating-geometry.js +19 -0
- package/dist/floating-geometry.js.map +1 -1
- package/dist/floating-positioner.d.ts +4 -1
- package/dist/floating-positioner.d.ts.map +1 -1
- package/dist/floating-positioner.js +42 -4
- package/dist/floating-positioner.js.map +1 -1
- package/dist/floating-positioner.native.d.ts +5 -0
- package/dist/floating-positioner.native.d.ts.map +1 -1
- package/dist/floating-positioner.native.js +26 -8
- package/dist/floating-positioner.native.js.map +1 -1
- package/dist/i18n.d.ts +154 -0
- package/dist/i18n.d.ts.map +1 -0
- package/dist/i18n.js +158 -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 +2 -0
- package/dist/native.d.ts.map +1 -1
- package/dist/native.js +2 -0
- package/dist/native.js.map +1 -1
- package/dist/portal.native.d.ts.map +1 -1
- package/dist/portal.native.js +24 -21
- package/dist/portal.native.js.map +1 -1
- package/dist/presence.d.ts.map +1 -1
- package/dist/presence.js +8 -1
- package/dist/presence.js.map +1 -1
- package/dist/pressable-interaction.d.ts +41 -0
- package/dist/pressable-interaction.d.ts.map +1 -0
- package/dist/pressable-interaction.js +72 -0
- package/dist/pressable-interaction.js.map +1 -0
- package/dist/text-child.d.ts +1 -1
- package/dist/text-child.d.ts.map +1 -1
- package/dist/text-child.js +1 -1
- package/dist/text-child.js.map +1 -1
- package/dist/text-child.native.d.ts +7 -1
- package/dist/text-child.native.d.ts.map +1 -1
- package/dist/text-child.native.js +17 -3
- package/dist/text-child.native.js.map +1 -1
- package/package.json +7 -3
- package/src/command-shortcut.native.ts +15 -0
- package/src/command-shortcut.ts +51 -0
- package/src/drag-1d-session.ts +77 -0
- package/src/drag-1d.native.ts +65 -0
- package/src/drag-1d.ts +108 -0
- package/src/floating-geometry.ts +24 -0
- package/src/floating-positioner.native.tsx +27 -7
- package/src/floating-positioner.tsx +48 -2
- package/src/i18n.ts +212 -0
- package/src/index.native.ts +20 -0
- package/src/index.ts +20 -0
- package/src/native.ts +15 -0
- package/src/portal.native.tsx +30 -22
- package/src/presence.ts +7 -1
- package/src/pressable-interaction.ts +92 -0
- package/src/text-child.native.tsx +18 -6
- package/src/text-child.ts +1 -1
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
export interface Drag1DOptions {
|
|
2
|
+
axis: 'x' | 'y'
|
|
3
|
+
disabled?: boolean
|
|
4
|
+
/** Native can wait for movement so a Pressable still receives ordinary taps. */
|
|
5
|
+
nativeActivationDistance?: number
|
|
6
|
+
/** Validate measured geometry before starting a session. */
|
|
7
|
+
canStart?: () => boolean
|
|
8
|
+
/** Horizontal deltas follow the platform's writing direction. */
|
|
9
|
+
onStart?: () => void
|
|
10
|
+
onMove?: (delta: number, details: Drag1DDetails) => void
|
|
11
|
+
onEnd?: (delta: number, details: Drag1DDetails) => void
|
|
12
|
+
onCancel?: () => void
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export interface Drag1DDetails {
|
|
16
|
+
/** Pixels per millisecond along the same logical axis as delta. */
|
|
17
|
+
velocity: number
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** Shared lifecycle: moves are coalesced, but release always flushes its final delta. */
|
|
21
|
+
export function createDrag1DSession(
|
|
22
|
+
options: () => Drag1DOptions,
|
|
23
|
+
schedule: (callback: () => void) => number,
|
|
24
|
+
unschedule: (id: number) => void,
|
|
25
|
+
) {
|
|
26
|
+
let active = false
|
|
27
|
+
let pending: number | undefined
|
|
28
|
+
let delta = 0
|
|
29
|
+
let delivered = 0
|
|
30
|
+
let details: Drag1DDetails = { velocity: 0 }
|
|
31
|
+
const flush = () => {
|
|
32
|
+
if (pending !== undefined) unschedule(pending)
|
|
33
|
+
pending = undefined
|
|
34
|
+
if (active && delta !== delivered) {
|
|
35
|
+
delivered = delta
|
|
36
|
+
options().onMove?.(delta, details)
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
const cancel = () => {
|
|
40
|
+
if (!active) return
|
|
41
|
+
active = false
|
|
42
|
+
if (pending !== undefined) unschedule(pending)
|
|
43
|
+
pending = undefined
|
|
44
|
+
options().onCancel?.()
|
|
45
|
+
}
|
|
46
|
+
return {
|
|
47
|
+
get active() {
|
|
48
|
+
return active
|
|
49
|
+
},
|
|
50
|
+
start() {
|
|
51
|
+
if (active || options().disabled || options().canStart?.() === false) return false
|
|
52
|
+
active = true
|
|
53
|
+
delta = delivered = 0
|
|
54
|
+
details = { velocity: 0 }
|
|
55
|
+
options().onStart?.()
|
|
56
|
+
return active
|
|
57
|
+
},
|
|
58
|
+
move(next: number, sample: Drag1DDetails = { velocity: 0 }) {
|
|
59
|
+
if (!active) return
|
|
60
|
+
if (options().disabled) return cancel()
|
|
61
|
+
delta = next
|
|
62
|
+
details = sample
|
|
63
|
+
if (pending === undefined) pending = schedule(flush)
|
|
64
|
+
},
|
|
65
|
+
end(next: number, sample: Drag1DDetails = details) {
|
|
66
|
+
if (!active) return
|
|
67
|
+
if (options().disabled) return cancel()
|
|
68
|
+
delta = next
|
|
69
|
+
details = sample
|
|
70
|
+
flush()
|
|
71
|
+
if (!active) return
|
|
72
|
+
active = false
|
|
73
|
+
options().onEnd?.(delta, details)
|
|
74
|
+
},
|
|
75
|
+
cancel,
|
|
76
|
+
}
|
|
77
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { useEffect, useMemo, useRef, useState } from 'react'
|
|
2
|
+
import { I18nManager, PanResponder, type PanResponderInstance } from 'react-native'
|
|
3
|
+
import { createDrag1DSession, type Drag1DOptions } from './drag-1d-session.ts'
|
|
4
|
+
|
|
5
|
+
export type { Drag1DDetails, Drag1DOptions } from './drag-1d-session.ts'
|
|
6
|
+
|
|
7
|
+
export function useDrag1D(options: Drag1DOptions): {
|
|
8
|
+
dragging: boolean
|
|
9
|
+
handlers: PanResponderInstance['panHandlers']
|
|
10
|
+
} {
|
|
11
|
+
const live = useRef(options)
|
|
12
|
+
live.current = options
|
|
13
|
+
const [dragging, setDragging] = useState(false)
|
|
14
|
+
const session = useMemo(
|
|
15
|
+
() =>
|
|
16
|
+
createDrag1DSession(
|
|
17
|
+
() => live.current,
|
|
18
|
+
(callback) => requestAnimationFrame(callback),
|
|
19
|
+
(id) => cancelAnimationFrame(id),
|
|
20
|
+
),
|
|
21
|
+
[],
|
|
22
|
+
)
|
|
23
|
+
const responder = useMemo(() => {
|
|
24
|
+
let axis: 'x' | 'y' = 'x'
|
|
25
|
+
let sign = 1
|
|
26
|
+
const delta = (dx: number, dy: number) => (axis === 'x' ? dx : dy) * sign
|
|
27
|
+
return PanResponder.create({
|
|
28
|
+
onStartShouldSetPanResponder: () =>
|
|
29
|
+
!live.current.disabled && (live.current.nativeActivationDistance ?? 0) <= 0,
|
|
30
|
+
onMoveShouldSetPanResponder: (_, state) => {
|
|
31
|
+
if (live.current.disabled) return false
|
|
32
|
+
const distance = live.current.nativeActivationDistance ?? 0
|
|
33
|
+
return distance <= 0 || Math.abs(live.current.axis === 'x' ? state.dx : state.dy) > distance
|
|
34
|
+
},
|
|
35
|
+
onPanResponderGrant: () => {
|
|
36
|
+
axis = live.current.axis
|
|
37
|
+
sign = axis === 'x' && I18nManager.isRTL ? -1 : 1
|
|
38
|
+
if (session.start()) setDragging(true)
|
|
39
|
+
},
|
|
40
|
+
onPanResponderMove: (_, state) =>
|
|
41
|
+
session.move(delta(state.dx, state.dy), {
|
|
42
|
+
velocity: ((axis === 'x' ? state.vx : state.vy) ?? 0) * sign,
|
|
43
|
+
}),
|
|
44
|
+
onPanResponderRelease: (_, state) => {
|
|
45
|
+
session.end(delta(state.dx, state.dy), {
|
|
46
|
+
velocity: ((axis === 'x' ? state.vx : state.vy) ?? 0) * sign,
|
|
47
|
+
})
|
|
48
|
+
setDragging(false)
|
|
49
|
+
},
|
|
50
|
+
onPanResponderTerminate: () => {
|
|
51
|
+
session.cancel()
|
|
52
|
+
setDragging(false)
|
|
53
|
+
},
|
|
54
|
+
onPanResponderTerminationRequest: () => true,
|
|
55
|
+
})
|
|
56
|
+
}, [session])
|
|
57
|
+
useEffect(() => {
|
|
58
|
+
if (options.disabled) {
|
|
59
|
+
session.cancel()
|
|
60
|
+
setDragging(false)
|
|
61
|
+
}
|
|
62
|
+
}, [options.disabled, session])
|
|
63
|
+
useEffect(() => () => session.cancel(), [session])
|
|
64
|
+
return { dragging, handlers: responder.panHandlers }
|
|
65
|
+
}
|
package/src/drag-1d.ts
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { type PointerEvent, useCallback, useEffect, useMemo, useRef, useState } from 'react'
|
|
2
|
+
import { createDrag1DSession, type Drag1DOptions } from './drag-1d-session.ts'
|
|
3
|
+
|
|
4
|
+
export type { Drag1DDetails, Drag1DOptions } from './drag-1d-session.ts'
|
|
5
|
+
|
|
6
|
+
export function useDrag1D(options: Drag1DOptions) {
|
|
7
|
+
const live = useRef(options)
|
|
8
|
+
live.current = options
|
|
9
|
+
const [dragging, setDragging] = useState(false)
|
|
10
|
+
const gesture = useRef<{
|
|
11
|
+
id: number
|
|
12
|
+
start: number
|
|
13
|
+
sign: number
|
|
14
|
+
axis: 'x' | 'y'
|
|
15
|
+
host: HTMLElement
|
|
16
|
+
last: number
|
|
17
|
+
lastAt: number
|
|
18
|
+
velocity: number
|
|
19
|
+
} | null>(null)
|
|
20
|
+
const session = useMemo(
|
|
21
|
+
() =>
|
|
22
|
+
createDrag1DSession(
|
|
23
|
+
() => live.current,
|
|
24
|
+
(callback) => requestAnimationFrame(callback),
|
|
25
|
+
(id) => cancelAnimationFrame(id),
|
|
26
|
+
),
|
|
27
|
+
[],
|
|
28
|
+
)
|
|
29
|
+
const release = useCallback(() => {
|
|
30
|
+
const current = gesture.current
|
|
31
|
+
gesture.current = null
|
|
32
|
+
if (current?.host.hasPointerCapture(current.id)) current.host.releasePointerCapture(current.id)
|
|
33
|
+
}, [])
|
|
34
|
+
const cancel = useCallback(() => {
|
|
35
|
+
session.cancel()
|
|
36
|
+
release()
|
|
37
|
+
setDragging(false)
|
|
38
|
+
}, [session, release])
|
|
39
|
+
useEffect(() => {
|
|
40
|
+
if (options.disabled) cancel()
|
|
41
|
+
}, [options.disabled, cancel])
|
|
42
|
+
useEffect(
|
|
43
|
+
() => () => {
|
|
44
|
+
session.cancel()
|
|
45
|
+
release()
|
|
46
|
+
},
|
|
47
|
+
[session, release],
|
|
48
|
+
)
|
|
49
|
+
const delta = (event: PointerEvent<HTMLElement>) => {
|
|
50
|
+
const at = gesture.current!
|
|
51
|
+
return ((at.axis === 'x' ? event.clientX : event.clientY) - at.start) * at.sign
|
|
52
|
+
}
|
|
53
|
+
const matches = (event: PointerEvent<HTMLElement>) => gesture.current?.id === event.pointerId
|
|
54
|
+
const sample = (event: PointerEvent<HTMLElement>, retainVelocity = false) => {
|
|
55
|
+
const at = gesture.current!
|
|
56
|
+
const position = at.axis === 'x' ? event.clientX : event.clientY
|
|
57
|
+
if (position !== at.last || !retainVelocity) {
|
|
58
|
+
at.velocity = ((position - at.last) * at.sign) / Math.max(1, event.timeStamp - at.lastAt)
|
|
59
|
+
}
|
|
60
|
+
at.last = position
|
|
61
|
+
at.lastAt = event.timeStamp
|
|
62
|
+
return { velocity: at.velocity }
|
|
63
|
+
}
|
|
64
|
+
return {
|
|
65
|
+
dragging,
|
|
66
|
+
handlers: {
|
|
67
|
+
onPointerDown(event: PointerEvent<HTMLElement>) {
|
|
68
|
+
if (gesture.current || event.button !== 0 || !event.isPrimary || options.disabled) return
|
|
69
|
+
const host = event.currentTarget
|
|
70
|
+
const axis = options.axis
|
|
71
|
+
const sign = axis === 'x' && getComputedStyle(host).direction === 'rtl' ? -1 : 1
|
|
72
|
+
gesture.current = {
|
|
73
|
+
id: event.pointerId,
|
|
74
|
+
host,
|
|
75
|
+
axis,
|
|
76
|
+
sign,
|
|
77
|
+
start: axis === 'x' ? event.clientX : event.clientY,
|
|
78
|
+
last: axis === 'x' ? event.clientX : event.clientY,
|
|
79
|
+
lastAt: event.timeStamp,
|
|
80
|
+
velocity: 0,
|
|
81
|
+
}
|
|
82
|
+
if (!session.start()) {
|
|
83
|
+
gesture.current = null
|
|
84
|
+
return
|
|
85
|
+
}
|
|
86
|
+
host.setPointerCapture(event.pointerId)
|
|
87
|
+
host.focus()
|
|
88
|
+
event.preventDefault()
|
|
89
|
+
setDragging(true)
|
|
90
|
+
},
|
|
91
|
+
onPointerMove(event: PointerEvent<HTMLElement>) {
|
|
92
|
+
if (matches(event)) session.move(delta(event), sample(event))
|
|
93
|
+
},
|
|
94
|
+
onPointerUp(event: PointerEvent<HTMLElement>) {
|
|
95
|
+
if (!matches(event)) return
|
|
96
|
+
session.end(delta(event), sample(event, true))
|
|
97
|
+
release()
|
|
98
|
+
setDragging(false)
|
|
99
|
+
},
|
|
100
|
+
onPointerCancel(event: PointerEvent<HTMLElement>) {
|
|
101
|
+
if (matches(event)) cancel()
|
|
102
|
+
},
|
|
103
|
+
onLostPointerCapture(event: PointerEvent<HTMLElement>) {
|
|
104
|
+
if (matches(event)) cancel()
|
|
105
|
+
},
|
|
106
|
+
},
|
|
107
|
+
}
|
|
108
|
+
}
|
package/src/floating-geometry.ts
CHANGED
|
@@ -304,3 +304,27 @@ export function computePosition(
|
|
|
304
304
|
arrow,
|
|
305
305
|
}
|
|
306
306
|
}
|
|
307
|
+
|
|
308
|
+
/**
|
|
309
|
+
* A position computed in window coordinates, moved into the coordinates of the
|
|
310
|
+
* box the floating element is laid out in.
|
|
311
|
+
*
|
|
312
|
+
* React Native measures in the window but places `position: absolute` against
|
|
313
|
+
* the parent, so the two agree only when the parent sits at the window's
|
|
314
|
+
* origin. Without a `PortalProvider` the panel renders where it was written,
|
|
315
|
+
* inside its own pattern's padding, and landed that far from its anchor
|
|
316
|
+
* (#1326). The parent's origin is not measurable from the floating element
|
|
317
|
+
* directly, but it is where the element is in the window less where it was
|
|
318
|
+
* placed: `floatingInWindow - placed`.
|
|
319
|
+
*/
|
|
320
|
+
export function toContainingBlock(
|
|
321
|
+
position: PositionResult,
|
|
322
|
+
floatingInWindow: { x: number; y: number },
|
|
323
|
+
placed: { x: number; y: number },
|
|
324
|
+
): PositionResult {
|
|
325
|
+
return {
|
|
326
|
+
...position,
|
|
327
|
+
x: position.x - (floatingInWindow.x - placed.x),
|
|
328
|
+
y: position.y - (floatingInWindow.y - placed.y),
|
|
329
|
+
}
|
|
330
|
+
}
|
|
@@ -7,13 +7,17 @@ import React, {
|
|
|
7
7
|
useRef,
|
|
8
8
|
useState,
|
|
9
9
|
} from 'react'
|
|
10
|
-
import { type StyleProp, View, type ViewStyle } from 'react-native'
|
|
10
|
+
import { type StyleProp, useWindowDimensions, View, type ViewStyle } from 'react-native'
|
|
11
11
|
import {
|
|
12
12
|
type ComputePositionOptions,
|
|
13
13
|
computePosition,
|
|
14
14
|
type PositionResult,
|
|
15
|
+
toContainingBlock,
|
|
15
16
|
} from './floating-geometry.ts'
|
|
16
17
|
|
|
18
|
+
/** Where the positioner parks the panel until it has been measured once. */
|
|
19
|
+
const OFFSCREEN = -9999
|
|
20
|
+
|
|
17
21
|
/**
|
|
18
22
|
* What React Native hands back for a `<View>`, rather than a shape of our
|
|
19
23
|
* own that happens to have `measureInWindow` on it.
|
|
@@ -31,6 +35,11 @@ export interface UseFloatingPositionOptions extends ComputePositionOptions {
|
|
|
31
35
|
|
|
32
36
|
/**
|
|
33
37
|
* Universal React hook for floating element positioning on React Native.
|
|
38
|
+
*
|
|
39
|
+
* The result is in the coordinates of the floating element's parent, for its
|
|
40
|
+
* `left`/`top`. That assumes the element sits at `left: -9999, top: -9999`
|
|
41
|
+
* until the first result and at each result after, as `FloatingPositioner`
|
|
42
|
+
* places it: the parent's origin is read back from where that put it.
|
|
34
43
|
*/
|
|
35
44
|
export function useFloatingPosition({
|
|
36
45
|
anchorRef,
|
|
@@ -45,6 +54,14 @@ export function useFloatingPosition({
|
|
|
45
54
|
arrowPadding = 4,
|
|
46
55
|
}: UseFloatingPositionOptions): PositionResult | null {
|
|
47
56
|
const [position, setPosition] = useState<PositionResult | null>(null)
|
|
57
|
+
// The window the anchor was measured in, so flip and shift know where its
|
|
58
|
+
// edges are. A fixed 400x800 flipped panels on a tall phone that had room
|
|
59
|
+
// below, and let them run off a narrower one. Read as a hook so a rotation
|
|
60
|
+
// or a resized window positions the panel again.
|
|
61
|
+
const { width: windowWidth, height: windowHeight } = useWindowDimensions()
|
|
62
|
+
// Where the floating element was last placed, in its parent's coordinates:
|
|
63
|
+
// what turns its window measurement back into the parent's origin.
|
|
64
|
+
const placed = useRef({ x: OFFSCREEN, y: OFFSCREEN })
|
|
48
65
|
|
|
49
66
|
const update = useCallback(() => {
|
|
50
67
|
if (!enabled) return
|
|
@@ -54,9 +71,8 @@ export function useFloatingPosition({
|
|
|
54
71
|
anchor.measureInWindow((ax, ay, aw, ah) => {
|
|
55
72
|
const floating = floatingRef.current
|
|
56
73
|
if (floating?.measureInWindow) {
|
|
57
|
-
floating.measureInWindow((
|
|
58
|
-
|
|
59
|
-
const viewport = { width: 400, height: 800 }
|
|
74
|
+
floating.measureInWindow((fx, fy, fw, fh) => {
|
|
75
|
+
const viewport = { width: windowWidth, height: windowHeight }
|
|
60
76
|
const result = computePosition(
|
|
61
77
|
{ x: ax, y: ay, width: aw, height: ah },
|
|
62
78
|
{ x: 0, y: 0, width: fw, height: fh },
|
|
@@ -71,7 +87,9 @@ export function useFloatingPosition({
|
|
|
71
87
|
arrowPadding,
|
|
72
88
|
},
|
|
73
89
|
)
|
|
74
|
-
|
|
90
|
+
const local = toContainingBlock(result, { x: fx, y: fy }, placed.current)
|
|
91
|
+
placed.current = { x: local.x, y: local.y }
|
|
92
|
+
setPosition(local)
|
|
75
93
|
})
|
|
76
94
|
}
|
|
77
95
|
})
|
|
@@ -86,6 +104,8 @@ export function useFloatingPosition({
|
|
|
86
104
|
shift,
|
|
87
105
|
viewportPadding,
|
|
88
106
|
arrowPadding,
|
|
107
|
+
windowWidth,
|
|
108
|
+
windowHeight,
|
|
89
109
|
])
|
|
90
110
|
|
|
91
111
|
useEffect(() => {
|
|
@@ -139,8 +159,8 @@ export function FloatingPositioner({
|
|
|
139
159
|
}
|
|
140
160
|
: {
|
|
141
161
|
position: 'absolute' as const,
|
|
142
|
-
left:
|
|
143
|
-
top:
|
|
162
|
+
left: OFFSCREEN,
|
|
163
|
+
top: OFFSCREEN,
|
|
144
164
|
opacity: 0,
|
|
145
165
|
}
|
|
146
166
|
|
|
@@ -4,6 +4,7 @@ import {
|
|
|
4
4
|
type RefObject,
|
|
5
5
|
useCallback,
|
|
6
6
|
useEffect,
|
|
7
|
+
useLayoutEffect,
|
|
7
8
|
useRef,
|
|
8
9
|
useState,
|
|
9
10
|
} from 'react'
|
|
@@ -121,6 +122,25 @@ export interface FloatingPositionerProps extends ComputePositionOptions {
|
|
|
121
122
|
anchorRef: RefObject<HTMLElement | null>
|
|
122
123
|
className?: string
|
|
123
124
|
style?: CSSProperties
|
|
125
|
+
/** Opt in to a non-modal top layer without moving the DOM or its CSS ancestry.
|
|
126
|
+
* Engines without showPopover keep inline positioning and warn once. */
|
|
127
|
+
topLayer?: boolean
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
let warnedTopLayerFallback = false
|
|
131
|
+
|
|
132
|
+
// Popover's UA box is a visible panel. Here the same host remains only a
|
|
133
|
+
// transparent positioning box; the caller still owns the panel's appearance.
|
|
134
|
+
const topLayerReset: CSSProperties = {
|
|
135
|
+
inset: 'auto',
|
|
136
|
+
width: 'auto',
|
|
137
|
+
height: 'auto',
|
|
138
|
+
margin: 0,
|
|
139
|
+
padding: 0,
|
|
140
|
+
border: 0,
|
|
141
|
+
overflow: 'visible',
|
|
142
|
+
color: 'inherit',
|
|
143
|
+
background: 'transparent',
|
|
124
144
|
}
|
|
125
145
|
|
|
126
146
|
/**
|
|
@@ -132,9 +152,30 @@ export function FloatingPositioner({
|
|
|
132
152
|
anchorRef,
|
|
133
153
|
className,
|
|
134
154
|
style,
|
|
155
|
+
topLayer = false,
|
|
135
156
|
...options
|
|
136
157
|
}: FloatingPositionerProps) {
|
|
137
158
|
const floatingRef = useRef<HTMLDivElement>(null)
|
|
159
|
+
useLayoutEffect(() => {
|
|
160
|
+
const node = floatingRef.current
|
|
161
|
+
if (!topLayer || !node) return
|
|
162
|
+
if (typeof node.showPopover !== 'function') {
|
|
163
|
+
// Otherwise the browser's [popover]:not(:popover-open) rule can keep
|
|
164
|
+
// an unsupported/polyfilled host hidden rather than restoring inline.
|
|
165
|
+
node.removeAttribute('popover')
|
|
166
|
+
if (!warnedTopLayerFallback) {
|
|
167
|
+
warnedTopLayerFallback = true
|
|
168
|
+
console.warn(
|
|
169
|
+
'[hozo] FloatingPositioner: showPopover is unavailable; inline fallback cannot escape ancestor clipping or stacking contexts.',
|
|
170
|
+
)
|
|
171
|
+
}
|
|
172
|
+
return
|
|
173
|
+
}
|
|
174
|
+
if (!node.matches(':popover-open')) node.showPopover()
|
|
175
|
+
// DOM removal (or removing the attribute) removes it from the top layer.
|
|
176
|
+
// Do not call hidePopover during unmount: Menu owns focus restoration,
|
|
177
|
+
// including the outside-click path which deliberately does not restore.
|
|
178
|
+
}, [topLayer])
|
|
138
179
|
const position = useFloatingPosition({
|
|
139
180
|
anchorRef,
|
|
140
181
|
floatingRef,
|
|
@@ -146,7 +187,11 @@ export function FloatingPositioner({
|
|
|
146
187
|
position: 'fixed',
|
|
147
188
|
left: `${position.x}px`,
|
|
148
189
|
top: `${position.y}px`,
|
|
149
|
-
width: options.matchAnchorWidth
|
|
190
|
+
width: options.matchAnchorWidth
|
|
191
|
+
? `${position.anchorWidth}px`
|
|
192
|
+
: topLayer
|
|
193
|
+
? 'auto'
|
|
194
|
+
: undefined,
|
|
150
195
|
willChange: 'transform',
|
|
151
196
|
display: position.referenceHidden ? 'none' : undefined,
|
|
152
197
|
}
|
|
@@ -160,8 +205,9 @@ export function FloatingPositioner({
|
|
|
160
205
|
return (
|
|
161
206
|
<div
|
|
162
207
|
ref={floatingRef}
|
|
208
|
+
popover={topLayer ? 'manual' : undefined}
|
|
163
209
|
className={className}
|
|
164
|
-
style={{ ...positionStyles, ...style }}
|
|
210
|
+
style={{ ...(topLayer ? topLayerReset : undefined), ...positionStyles, ...style }}
|
|
165
211
|
data-placement={position?.placement}
|
|
166
212
|
data-flipped={position?.flipped ? '' : undefined}
|
|
167
213
|
data-shifted={position?.shifted ? '' : undefined}
|
package/src/i18n.ts
ADDED
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
// How Hozo's own strings and locale reach an application's i18n library
|
|
2
|
+
// (decision 008).
|
|
3
|
+
//
|
|
4
|
+
// Hozo does not translate. Applications already have a library for that --
|
|
5
|
+
// i18next, react-intl, Lingui -- with plural rules, interpolation and
|
|
6
|
+
// catalogues, and Hozo's strings belong in the same catalogue as the rest.
|
|
7
|
+
// So this is only the connection: a context carrying `locale`, `dir` and a
|
|
8
|
+
// `translate` function, the English every component falls back to, and two
|
|
9
|
+
// adapters that are a few lines each and typed structurally, so no
|
|
10
|
+
// `@hozo/*` package depends on any library.
|
|
11
|
+
//
|
|
12
|
+
// Platform-neutral on purpose: one file, imported by both entry points, so a
|
|
13
|
+
// provider on one side is the same object on the other.
|
|
14
|
+
|
|
15
|
+
import { createContext, createElement, type ReactNode, useCallback, useContext } from 'react'
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Every string Hozo's components show or announce, in English, by key.
|
|
19
|
+
*
|
|
20
|
+
* Exported so a project can seed its catalogue from it, and so a test can
|
|
21
|
+
* check none is missing. Parameters are named (`{remaining}`), because every
|
|
22
|
+
* library this connects to interpolates by name.
|
|
23
|
+
*/
|
|
24
|
+
export const hozoMessages = {
|
|
25
|
+
'hozo.code.label': 'Code',
|
|
26
|
+
'hozo.code.contents': 'Code contents: {title}',
|
|
27
|
+
'hozo.code.lines': '{title}, {count} lines',
|
|
28
|
+
'hozo.code.added': 'Added',
|
|
29
|
+
'hozo.code.removed': 'Removed',
|
|
30
|
+
'hozo.code.highlighted': 'Highlighted',
|
|
31
|
+
'hozo.code.copy': 'Copy code',
|
|
32
|
+
'hozo.code.copied': 'Copied',
|
|
33
|
+
'hozo.code.copyFailed': 'Could not copy code',
|
|
34
|
+
'hozo.splitter.label': 'Resize {label}',
|
|
35
|
+
'hozo.splitter.value': '{size} pixels',
|
|
36
|
+
'hozo.splitter.expand': 'Expand',
|
|
37
|
+
'hozo.splitter.collapse': 'Collapse',
|
|
38
|
+
'hozo.avatar.label': '{name}, {status}',
|
|
39
|
+
'hozo.avatar.statusBusy': 'busy',
|
|
40
|
+
'hozo.avatar.statusOffline': 'offline',
|
|
41
|
+
'hozo.avatar.statusOnline': 'online',
|
|
42
|
+
'hozo.badge.overflow': 'more than {max}',
|
|
43
|
+
'hozo.bottomSheet.handle': 'Resize',
|
|
44
|
+
'hozo.calendar.nextMonth': 'Next month',
|
|
45
|
+
'hozo.calendar.previousMonth': 'Previous month',
|
|
46
|
+
'hozo.calendar.rangeEnd': 'end of range',
|
|
47
|
+
'hozo.calendar.rangeStart': 'start of range',
|
|
48
|
+
'hozo.calendar.today': 'today',
|
|
49
|
+
'hozo.chip.remove': 'Remove {label}',
|
|
50
|
+
'hozo.color.black': 'black',
|
|
51
|
+
'hozo.color.blue': 'blue',
|
|
52
|
+
'hozo.color.cyan': 'cyan',
|
|
53
|
+
'hozo.color.dark': 'dark {color}',
|
|
54
|
+
'hozo.color.gray': 'gray',
|
|
55
|
+
'hozo.color.green': 'green',
|
|
56
|
+
'hozo.color.light': 'light {color}',
|
|
57
|
+
'hozo.color.orange': 'orange',
|
|
58
|
+
'hozo.color.pink': 'pink',
|
|
59
|
+
'hozo.color.purple': 'purple',
|
|
60
|
+
'hozo.color.red': 'red',
|
|
61
|
+
'hozo.color.white': 'white',
|
|
62
|
+
'hozo.color.yellow': 'yellow',
|
|
63
|
+
'hozo.colorPicker.alpha': 'Opacity',
|
|
64
|
+
'hozo.colorPicker.alphaValue': '{value}% opaque',
|
|
65
|
+
'hozo.colorPicker.described': '{name}, {hex}',
|
|
66
|
+
'hozo.colorPicker.eyeDropper': 'Pick a colour from the screen',
|
|
67
|
+
'hozo.colorPicker.hex': 'Hex colour',
|
|
68
|
+
'hozo.colorPicker.hue': 'Hue',
|
|
69
|
+
'hozo.colorPicker.hueValue': '{degrees} degrees, {name}',
|
|
70
|
+
'hozo.colorPicker.lightness': 'Lightness',
|
|
71
|
+
'hozo.colorPicker.percent': '{value}%',
|
|
72
|
+
'hozo.colorPicker.presets': 'Presets',
|
|
73
|
+
'hozo.colorPicker.saturation': 'Saturation',
|
|
74
|
+
'hozo.commandPalette.empty': 'No results',
|
|
75
|
+
'hozo.commandPalette.label': 'Command palette',
|
|
76
|
+
'hozo.commandPalette.oneResult': '1 result',
|
|
77
|
+
'hozo.commandPalette.results': '{count} results',
|
|
78
|
+
'hozo.datePicker.dialog': 'Choose a date',
|
|
79
|
+
'hozo.dateRangePicker.dialog': 'Choose a range of dates',
|
|
80
|
+
'hozo.dateTimePicker.dialog': 'Choose a date and time',
|
|
81
|
+
'hozo.dateTimePicker.done': 'Done',
|
|
82
|
+
'hozo.field.required': 'Required',
|
|
83
|
+
'hozo.fileDropzone.selectedMany': '{count} files selected',
|
|
84
|
+
'hozo.fileDropzone.selectedOne': '{name} selected, {size}',
|
|
85
|
+
'hozo.fileDropzone.tooLarge': '{name} is too large: {size}, and the limit is {limit}',
|
|
86
|
+
'hozo.fileDropzone.tooMany': '{name} was not added: one pick takes {max}',
|
|
87
|
+
'hozo.fileDropzone.tooSmall': '{name} is too small: {size}',
|
|
88
|
+
'hozo.fileDropzone.unavailable': 'No file picker is available',
|
|
89
|
+
'hozo.fileDropzone.wrongType': '{name} is not an accepted kind of file',
|
|
90
|
+
'hozo.nativeSelect.cancel': 'Cancel',
|
|
91
|
+
'hozo.otpInput.hint': '{length} characters',
|
|
92
|
+
'hozo.pagination.label': 'Pagination',
|
|
93
|
+
'hozo.pagination.next': 'Next page',
|
|
94
|
+
'hozo.pagination.page': 'Page {page}',
|
|
95
|
+
'hozo.pagination.previous': 'Previous page',
|
|
96
|
+
'hozo.stepper.completed': 'completed',
|
|
97
|
+
'hozo.stepper.current': 'current',
|
|
98
|
+
'hozo.stepper.error': 'has an error',
|
|
99
|
+
'hozo.stepper.label': 'Progress',
|
|
100
|
+
'hozo.stepper.step': 'Step {step} of {count}: {label}, {status}',
|
|
101
|
+
'hozo.stepper.upcoming': 'not started',
|
|
102
|
+
'hozo.textArea.remaining': '{remaining} of {maxLength} characters left',
|
|
103
|
+
'hozo.timePicker.decrease': 'Decrease',
|
|
104
|
+
'hozo.timePicker.empty': '--',
|
|
105
|
+
'hozo.timePicker.hour': 'Hour',
|
|
106
|
+
'hozo.timePicker.increase': 'Increase',
|
|
107
|
+
'hozo.timePicker.minute': 'Minute',
|
|
108
|
+
'hozo.timePicker.period': 'AM or PM',
|
|
109
|
+
} as const
|
|
110
|
+
|
|
111
|
+
export type HozoMessageKey = keyof typeof hozoMessages
|
|
112
|
+
export type HozoMessageParams = Readonly<Record<string, string | number>>
|
|
113
|
+
|
|
114
|
+
export interface HozoI18n {
|
|
115
|
+
/** BCP 47. Used for `Intl`: dates, numbers, the first day of the week. */
|
|
116
|
+
locale?: string
|
|
117
|
+
/**
|
|
118
|
+
* Which way the content runs. Read by behaviours that cannot ask the
|
|
119
|
+
* platform; never applied. The Web's direction is `<html dir>` and
|
|
120
|
+
* Native's is `I18nManager`, and both are the application's to set.
|
|
121
|
+
*/
|
|
122
|
+
dir?: 'ltr' | 'rtl'
|
|
123
|
+
/**
|
|
124
|
+
* The project's translation of `key`. `fallback` is Hozo's English with
|
|
125
|
+
* `params` already filled in, for a library that shows its default when a
|
|
126
|
+
* key is missing.
|
|
127
|
+
*/
|
|
128
|
+
translate?: (key: HozoMessageKey, params: HozoMessageParams, fallback: string) => string
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
const HozoI18nContext = createContext<HozoI18n>({})
|
|
132
|
+
|
|
133
|
+
export interface HozoI18nProviderProps {
|
|
134
|
+
value: HozoI18n
|
|
135
|
+
children?: ReactNode
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Puts a project's i18n where Hozo's components can read it.
|
|
140
|
+
*
|
|
141
|
+
* Give it a new `value` when the language changes -- `useMemo` on the
|
|
142
|
+
* language -- and every Hozo string below re-renders in it.
|
|
143
|
+
*/
|
|
144
|
+
export function HozoI18nProvider({ value, children }: HozoI18nProviderProps) {
|
|
145
|
+
return createElement(HozoI18nContext.Provider, { value }, children)
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
export function useHozoI18n(): HozoI18n {
|
|
149
|
+
return useContext(HozoI18nContext)
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
function interpolate(template: string, params: HozoMessageParams): string {
|
|
153
|
+
return template.replace(/\{(\w+)\}/g, (whole, name: string) =>
|
|
154
|
+
name in params ? String(params[name]) : whole,
|
|
155
|
+
)
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* A function resolving one of Hozo's strings: the component's own prop if
|
|
160
|
+
* there is one, then the project's translation, then Hozo's English.
|
|
161
|
+
*/
|
|
162
|
+
export function useHozoMessage() {
|
|
163
|
+
const { translate } = useHozoI18n()
|
|
164
|
+
return useCallback(
|
|
165
|
+
(key: HozoMessageKey, params: HozoMessageParams = {}, explicit?: string): string => {
|
|
166
|
+
if (explicit !== undefined) return explicit
|
|
167
|
+
const fallback = interpolate(hozoMessages[key], params)
|
|
168
|
+
return translate ? translate(key, params, fallback) : fallback
|
|
169
|
+
},
|
|
170
|
+
[translate],
|
|
171
|
+
)
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/** The part of an i18next instance this reads. */
|
|
175
|
+
export interface I18nextLike {
|
|
176
|
+
t(key: string, options?: Record<string, unknown>): string
|
|
177
|
+
language?: string
|
|
178
|
+
dir?(language?: string): 'ltr' | 'rtl'
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* Hozo's i18n from an i18next instance: Hozo's keys are i18next keys, and
|
|
183
|
+
* Hozo's English is i18next's `defaultValue`.
|
|
184
|
+
*/
|
|
185
|
+
export function fromI18next(i18n: I18nextLike): HozoI18n {
|
|
186
|
+
return {
|
|
187
|
+
locale: i18n.language,
|
|
188
|
+
dir: i18n.dir?.(i18n.language),
|
|
189
|
+
translate: (key, params, fallback) => i18n.t(key, { ...params, defaultValue: fallback }),
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/** The part of a react-intl `IntlShape` this reads. */
|
|
194
|
+
export interface ReactIntlLike {
|
|
195
|
+
formatMessage(
|
|
196
|
+
descriptor: { id: string; defaultMessage?: string },
|
|
197
|
+
values?: Record<string, string | number>,
|
|
198
|
+
): string
|
|
199
|
+
locale?: string
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* Hozo's i18n from react-intl: Hozo's keys are message ids, and Hozo's
|
|
204
|
+
* English is the `defaultMessage`.
|
|
205
|
+
*/
|
|
206
|
+
export function fromReactIntl(intl: ReactIntlLike): HozoI18n {
|
|
207
|
+
return {
|
|
208
|
+
locale: intl.locale,
|
|
209
|
+
translate: (key, params, fallback) =>
|
|
210
|
+
intl.formatMessage({ id: key, defaultMessage: fallback }, params),
|
|
211
|
+
}
|
|
212
|
+
}
|