@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.
Files changed (86) hide show
  1. package/CHANGELOG.md +70 -0
  2. package/README.md +17 -3
  3. package/dist/command-shortcut.d.ts +23 -0
  4. package/dist/command-shortcut.d.ts.map +1 -0
  5. package/dist/command-shortcut.js +47 -0
  6. package/dist/command-shortcut.js.map +1 -0
  7. package/dist/command-shortcut.native.d.ts +11 -0
  8. package/dist/command-shortcut.native.d.ts.map +1 -0
  9. package/dist/command-shortcut.native.js +11 -0
  10. package/dist/command-shortcut.native.js.map +1 -0
  11. package/dist/drag-1d-session.d.ts +26 -0
  12. package/dist/drag-1d-session.d.ts.map +1 -0
  13. package/dist/drag-1d-session.js +65 -0
  14. package/dist/drag-1d-session.js.map +1 -0
  15. package/dist/drag-1d.d.ts +14 -0
  16. package/dist/drag-1d.d.ts.map +1 -0
  17. package/dist/drag-1d.js +93 -0
  18. package/dist/drag-1d.js.map +1 -0
  19. package/dist/drag-1d.native.d.ts +8 -0
  20. package/dist/drag-1d.native.d.ts.map +1 -0
  21. package/dist/drag-1d.native.js +52 -0
  22. package/dist/drag-1d.native.js.map +1 -0
  23. package/dist/floating-geometry.d.ts +19 -0
  24. package/dist/floating-geometry.d.ts.map +1 -1
  25. package/dist/floating-geometry.js +19 -0
  26. package/dist/floating-geometry.js.map +1 -1
  27. package/dist/floating-positioner.d.ts +4 -1
  28. package/dist/floating-positioner.d.ts.map +1 -1
  29. package/dist/floating-positioner.js +42 -4
  30. package/dist/floating-positioner.js.map +1 -1
  31. package/dist/floating-positioner.native.d.ts +5 -0
  32. package/dist/floating-positioner.native.d.ts.map +1 -1
  33. package/dist/floating-positioner.native.js +26 -8
  34. package/dist/floating-positioner.native.js.map +1 -1
  35. package/dist/i18n.d.ts +154 -0
  36. package/dist/i18n.d.ts.map +1 -0
  37. package/dist/i18n.js +158 -0
  38. package/dist/i18n.js.map +1 -0
  39. package/dist/index.d.ts +3 -0
  40. package/dist/index.d.ts.map +1 -1
  41. package/dist/index.js +3 -0
  42. package/dist/index.js.map +1 -1
  43. package/dist/index.native.d.ts +3 -0
  44. package/dist/index.native.d.ts.map +1 -1
  45. package/dist/index.native.js +3 -0
  46. package/dist/index.native.js.map +1 -1
  47. package/dist/native.d.ts +2 -0
  48. package/dist/native.d.ts.map +1 -1
  49. package/dist/native.js +2 -0
  50. package/dist/native.js.map +1 -1
  51. package/dist/portal.native.d.ts.map +1 -1
  52. package/dist/portal.native.js +24 -21
  53. package/dist/portal.native.js.map +1 -1
  54. package/dist/presence.d.ts.map +1 -1
  55. package/dist/presence.js +8 -1
  56. package/dist/presence.js.map +1 -1
  57. package/dist/pressable-interaction.d.ts +41 -0
  58. package/dist/pressable-interaction.d.ts.map +1 -0
  59. package/dist/pressable-interaction.js +72 -0
  60. package/dist/pressable-interaction.js.map +1 -0
  61. package/dist/text-child.d.ts +1 -1
  62. package/dist/text-child.d.ts.map +1 -1
  63. package/dist/text-child.js +1 -1
  64. package/dist/text-child.js.map +1 -1
  65. package/dist/text-child.native.d.ts +7 -1
  66. package/dist/text-child.native.d.ts.map +1 -1
  67. package/dist/text-child.native.js +17 -3
  68. package/dist/text-child.native.js.map +1 -1
  69. package/package.json +7 -3
  70. package/src/command-shortcut.native.ts +15 -0
  71. package/src/command-shortcut.ts +51 -0
  72. package/src/drag-1d-session.ts +77 -0
  73. package/src/drag-1d.native.ts +65 -0
  74. package/src/drag-1d.ts +108 -0
  75. package/src/floating-geometry.ts +24 -0
  76. package/src/floating-positioner.native.tsx +27 -7
  77. package/src/floating-positioner.tsx +48 -2
  78. package/src/i18n.ts +212 -0
  79. package/src/index.native.ts +20 -0
  80. package/src/index.ts +20 -0
  81. package/src/native.ts +15 -0
  82. package/src/portal.native.tsx +30 -22
  83. package/src/presence.ts +7 -1
  84. package/src/pressable-interaction.ts +92 -0
  85. package/src/text-child.native.tsx +18 -6
  86. package/src/text-child.ts +1 -1
@@ -1,7 +1,13 @@
1
+ export {
2
+ type CommandShortcutOptions,
3
+ isApplePlatform,
4
+ useCommandShortcut,
5
+ } from './command-shortcut.native.ts'
1
6
  export {
2
7
  DismissableLayer,
3
8
  type DismissableLayerProps,
4
9
  } from './dismissable-layer.native.tsx'
10
+ export { type Drag1DDetails, type Drag1DOptions, useDrag1D } from './drag-1d.native.ts'
5
11
  export {
6
12
  type Alignment,
7
13
  type BasePlacement,
@@ -44,6 +50,20 @@ export {
44
50
  useHoverTrigger,
45
51
  useTooltipGroup,
46
52
  } from './hover-trigger.native.tsx'
53
+ export {
54
+ fromI18next,
55
+ fromReactIntl,
56
+ type HozoI18n,
57
+ HozoI18nProvider,
58
+ type HozoI18nProviderProps,
59
+ type HozoMessageKey,
60
+ type HozoMessageParams,
61
+ hozoMessages,
62
+ type I18nextLike,
63
+ type ReactIntlLike,
64
+ useHozoI18n,
65
+ useHozoMessage,
66
+ } from './i18n.ts'
47
67
  export {
48
68
  LiveRegion,
49
69
  type LiveRegionMode,
package/src/index.ts CHANGED
@@ -1,7 +1,13 @@
1
+ export {
2
+ type CommandShortcutOptions,
3
+ isApplePlatform,
4
+ useCommandShortcut,
5
+ } from './command-shortcut.ts'
1
6
  export {
2
7
  DismissableLayer,
3
8
  type DismissableLayerProps,
4
9
  } from './dismissable-layer.tsx'
10
+ export { type Drag1DDetails, type Drag1DOptions, useDrag1D } from './drag-1d.ts'
5
11
  export {
6
12
  type Alignment,
7
13
  type BasePlacement,
@@ -44,6 +50,20 @@ export {
44
50
  useHoverTrigger,
45
51
  useTooltipGroup,
46
52
  } from './hover-trigger.tsx'
53
+ export {
54
+ fromI18next,
55
+ fromReactIntl,
56
+ type HozoI18n,
57
+ HozoI18nProvider,
58
+ type HozoI18nProviderProps,
59
+ type HozoMessageKey,
60
+ type HozoMessageParams,
61
+ hozoMessages,
62
+ type I18nextLike,
63
+ type ReactIntlLike,
64
+ useHozoI18n,
65
+ useHozoMessage,
66
+ } from './i18n.ts'
47
67
  export {
48
68
  LiveRegion,
49
69
  type LiveRegionMode,
package/src/native.ts CHANGED
@@ -5,6 +5,7 @@ export {
5
5
  moveAccessibilityFocus,
6
6
  setAccessibilityFocusMover,
7
7
  } from './accessibility-focus.native.ts'
8
+ export { type Drag1DOptions, useDrag1D } from './drag-1d.native.ts'
8
9
  export type { Placement } from './floating-geometry.ts'
9
10
  export {
10
11
  FloatingPositioner,
@@ -19,5 +20,19 @@ export {
19
20
  type UseHoverTriggerReturn,
20
21
  useHoverTrigger,
21
22
  } from './hover-trigger.native.tsx'
23
+ export {
24
+ fromI18next,
25
+ fromReactIntl,
26
+ type HozoI18n,
27
+ HozoI18nProvider,
28
+ type HozoI18nProviderProps,
29
+ type HozoMessageKey,
30
+ type HozoMessageParams,
31
+ hozoMessages,
32
+ type I18nextLike,
33
+ type ReactIntlLike,
34
+ useHozoI18n,
35
+ useHozoMessage,
36
+ } from './i18n.ts'
22
37
  export { Portal, PortalHost, type PortalProps, PortalProvider } from './portal.native.tsx'
23
38
  export { type PresenceBinding, type PresenceState, usePresence } from './presence.native.ts'
@@ -1,4 +1,12 @@
1
- import React, { createContext, type ReactNode, useContext, useEffect, useId, useState } from 'react'
1
+ import React, {
2
+ createContext,
3
+ type ReactNode,
4
+ useContext,
5
+ useEffect,
6
+ useId,
7
+ useMemo,
8
+ useState,
9
+ } from 'react'
2
10
  import { View } from 'react-native'
3
11
 
4
12
  export interface PortalProps {
@@ -18,7 +26,6 @@ export interface PortalProps {
18
26
  }
19
27
 
20
28
  type PortalMethods = {
21
- mount: (id: string, children: ReactNode) => void
22
29
  update: (id: string, children: ReactNode) => void
23
30
  unmount: (id: string) => void
24
31
  }
@@ -32,29 +39,29 @@ const PortalContext = createContext<PortalMethods | null>(null)
32
39
  export function PortalProvider({ children }: { children: ReactNode }) {
33
40
  const [items, setItems] = useState<Map<string, ReactNode>>(() => new Map())
34
41
 
35
- const methods: PortalMethods = {
36
- mount: (id, node) => {
42
+ // Portal effects depend on this API, not the item snapshot. Recreating it
43
+ // after a registration makes that effect unregister/register forever.
44
+ const methods = useMemo<PortalMethods>(() => {
45
+ const put: PortalMethods['update'] = (id, node) => {
37
46
  setItems((prev) => {
47
+ if (prev.has(id) && prev.get(id) === node) return prev
38
48
  const next = new Map(prev)
39
49
  next.set(id, node)
40
50
  return next
41
51
  })
42
- },
43
- update: (id, node) => {
44
- setItems((prev) => {
45
- const next = new Map(prev)
46
- next.set(id, node)
47
- return next
48
- })
49
- },
50
- unmount: (id) => {
51
- setItems((prev) => {
52
- const next = new Map(prev)
53
- next.delete(id)
54
- return next
55
- })
56
- },
57
- }
52
+ }
53
+ return {
54
+ update: put,
55
+ unmount: (id) => {
56
+ setItems((prev) => {
57
+ if (!prev.has(id)) return prev
58
+ const next = new Map(prev)
59
+ next.delete(id)
60
+ return next
61
+ })
62
+ },
63
+ }
64
+ }, [])
58
65
 
59
66
  return (
60
67
  <PortalContext.Provider value={methods}>
@@ -111,11 +118,12 @@ export function Portal({ children, disabled = false }: PortalProps) {
111
118
 
112
119
  useEffect(() => {
113
120
  if (disabled || !portal) return
114
- portal.mount(id, children)
121
+ // A changed child is an in-place item update, not an unregister/register:
122
+ // deleting/readding the same key would move it after unaffected siblings.
115
123
  return () => {
116
124
  portal.unmount(id)
117
125
  }
118
- }, [disabled, portal, id, children])
126
+ }, [disabled, portal, id])
119
127
 
120
128
  useEffect(() => {
121
129
  if (disabled || !portal) return
package/src/presence.ts CHANGED
@@ -53,7 +53,13 @@ function longest(durations: string, delays: string): number {
53
53
  function exitDuration(element: Element): number {
54
54
  const style = getComputedStyle(element)
55
55
  return Math.max(
56
- longest(style.transitionDuration, style.transitionDelay),
56
+ // `transition-none` -- and so `motion-reduce:transition-none` -- sets
57
+ // the property list to `none` and leaves the duration where the other
58
+ // classes put it. Nothing transitions then, and no `transitionend` will
59
+ // come to end the exit early.
60
+ style.transitionProperty === 'none'
61
+ ? 0
62
+ : longest(style.transitionDuration, style.transitionDelay),
57
63
  style.animationName === 'none' ? 0 : longest(style.animationDuration, style.animationDelay),
58
64
  )
59
65
  }
@@ -0,0 +1,92 @@
1
+ import { createContext, 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
+ /**
11
+ * The state a Native pressable shares with the Text inside it: a `HozoText`
12
+ * anywhere below reads it to apply its `hover:` and `active:` looks (#876).
13
+ * Here rather than in `@hozo/primitives` so `@hozo/patterns`' pressable
14
+ * provides the same one, and a Text a caller nests in a pattern reads it too
15
+ * (#974). `transition` is the primitive's own, for its colour motion.
16
+ */
17
+ export const HozoInteractionContext = createContext<
18
+ (HozoPressableInteractionState & { transition?: unknown }) | null
19
+ >(null)
20
+
21
+ const HOVERED = 1
22
+ const FOCUSED = 2
23
+ const PRESSED = 4
24
+ const FOCUS_VISIBLE = 8
25
+ const read = (mask: number): HozoPressableInteractionState => ({
26
+ pressed: (mask & PRESSED) !== 0,
27
+ hovered: (mask & HOVERED) !== 0,
28
+ focused: (mask & FOCUSED) !== 0,
29
+ focusVisible: (mask & FOCUS_VISIBLE) !== 0,
30
+ })
31
+
32
+ /**
33
+ * The Native pressable's event-driven facts, shared with patterns (#897).
34
+ * No subscriptions, animation nodes or global modality listeners. Hosts own
35
+ * event delivery and compose their caller's handlers after these callbacks.
36
+ * onChange runs synchronously for the existing native animation driver;
37
+ * React state drives the ordinary style/children callbacks.
38
+ */
39
+ export function usePressableInteraction({
40
+ focusVisible = false,
41
+ onChange,
42
+ }: {
43
+ /** Text fields indicate focus even after a pointer brings up their keyboard. */
44
+ focusVisible?: boolean | 'keyboard-input'
45
+ onChange?: (state: HozoPressableInteractionState) => void
46
+ } = {}) {
47
+ const [mask, setMask] = useState(0)
48
+ const live = useRef(0)
49
+ // Buttons use the existing modality policy: a pointer/press clears visible
50
+ // focus, a key restores it. Keyboard-input fields instead keep it until blur.
51
+ const modality = useRef<'keyboard' | 'pointer'>('keyboard')
52
+ const set = useCallback(
53
+ (flag: number, active: boolean) => {
54
+ const next = active ? live.current | flag : live.current & ~flag
55
+ live.current = next
56
+ onChange?.(read(next))
57
+ setMask(next)
58
+ },
59
+ [onChange],
60
+ )
61
+ const handlers = useMemo(
62
+ () => ({
63
+ hoverIn: () => set(HOVERED, true),
64
+ hoverOut: () => set(HOVERED, false),
65
+ focus: () => {
66
+ set(FOCUSED, true)
67
+ if (focusVisible)
68
+ set(FOCUS_VISIBLE, focusVisible === 'keyboard-input' || modality.current === 'keyboard')
69
+ },
70
+ blur: () => {
71
+ set(FOCUSED, false)
72
+ if (focusVisible) set(FOCUS_VISIBLE, false)
73
+ },
74
+ pointerDown: () => {
75
+ modality.current = 'pointer'
76
+ if (focusVisible === true) set(FOCUS_VISIBLE, false)
77
+ },
78
+ keyDown: () => {
79
+ modality.current = 'keyboard'
80
+ if (focusVisible && (live.current & FOCUSED) !== 0) set(FOCUS_VISIBLE, true)
81
+ },
82
+ pressIn: () => {
83
+ modality.current = 'pointer'
84
+ if (focusVisible === true) set(FOCUS_VISIBLE, false)
85
+ set(PRESSED, true)
86
+ },
87
+ pressOut: () => set(PRESSED, false),
88
+ }),
89
+ [focusVisible, set],
90
+ )
91
+ return { state: useMemo(() => read(mask), [mask]), handlers }
92
+ }
@@ -23,8 +23,8 @@
23
23
  // simply available. A fallback being right about a case the compiler has
24
24
  // to leave is not a divergence worth removing.
25
25
 
26
- import { Children, type ReactNode } from 'react'
27
- import { Text } from 'react-native'
26
+ import { Children, Fragment, isValidElement, type ReactNode } from 'react'
27
+ import { type StyleProp, Text, type TextStyle } from 'react-native'
28
28
 
29
29
  /**
30
30
  * Children with every text one inside a `Text`, and nothing else touched.
@@ -36,9 +36,21 @@ import { Text } from 'react-native'
36
36
  *
37
37
  * Numbers as well as strings. React renders both as text nodes, and React
38
38
  * Native rejects both for the same reason.
39
+ *
40
+ * `style` is for a component whose box carries text classes -- a panel's
41
+ * colour, a tooltip's size -- which a `View` drops and the Web would have
42
+ * let its text inherit. A pattern that puts a caller's `ReactNode` into a
43
+ * `View` passes it through here, with the box's text half (#1205).
39
44
  */
40
- export function hozoTextChildren(children: ReactNode): ReactNode {
41
- return Children.map(children, (child) =>
42
- typeof child === 'string' || typeof child === 'number' ? <Text>{child}</Text> : child,
43
- )
45
+ export function hozoTextChildren(children: ReactNode, style?: StyleProp<TextStyle>): ReactNode {
46
+ return Children.map(children, (child) => {
47
+ if (typeof child === 'string' || typeof child === 'number')
48
+ return <Text style={style}>{child}</Text>
49
+ // A Fragment is a transparent container, not a native text boundary.
50
+ // Preserve its key while normalizing its children; never inspect or
51
+ // execute arbitrary component elements to guess what they render.
52
+ if (isValidElement<{ children?: ReactNode }>(child) && child.type === Fragment)
53
+ return <Fragment key={child.key}>{hozoTextChildren(child.props.children, style)}</Fragment>
54
+ return child
55
+ })
44
56
  }
package/src/text-child.ts CHANGED
@@ -14,6 +14,6 @@
14
14
  import type { ReactNode } from 'react'
15
15
 
16
16
  /** Identity. See above, and `text-child.native.tsx` for the real one. */
17
- export function hozoTextChildren(children: ReactNode): ReactNode {
17
+ export function hozoTextChildren(children: ReactNode, _style?: unknown): ReactNode {
18
18
  return children
19
19
  }