@hozo/primitives 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.
Files changed (76) hide show
  1. package/CHANGELOG.md +42 -0
  2. package/README.md +24 -2
  3. package/dist/flat-list.native.d.ts +8 -8
  4. package/dist/flat-list.native.d.ts.map +1 -1
  5. package/dist/flat-list.native.js +55 -25
  6. package/dist/flat-list.native.js.map +1 -1
  7. package/dist/foundation.d.ts +7 -4
  8. package/dist/foundation.d.ts.map +1 -1
  9. package/dist/foundation.js +33 -16
  10. package/dist/foundation.js.map +1 -1
  11. package/dist/foundation.native.d.ts +16 -2
  12. package/dist/foundation.native.d.ts.map +1 -1
  13. package/dist/foundation.native.js.map +1 -1
  14. package/dist/index.d.ts +6 -3
  15. package/dist/index.d.ts.map +1 -1
  16. package/dist/index.js +7 -9
  17. package/dist/index.js.map +1 -1
  18. package/dist/index.native.d.ts +1 -0
  19. package/dist/index.native.d.ts.map +1 -1
  20. package/dist/index.native.js +1 -0
  21. package/dist/index.native.js.map +1 -1
  22. package/dist/link.native.d.ts +16 -7
  23. package/dist/link.native.d.ts.map +1 -1
  24. package/dist/link.native.js +9 -5
  25. package/dist/link.native.js.map +1 -1
  26. package/dist/presence-context.d.ts +15 -0
  27. package/dist/presence-context.d.ts.map +1 -0
  28. package/dist/presence-context.js +11 -0
  29. package/dist/presence-context.js.map +1 -0
  30. package/dist/presence.d.ts +12 -0
  31. package/dist/presence.d.ts.map +1 -0
  32. package/dist/presence.js +45 -0
  33. package/dist/presence.js.map +1 -0
  34. package/dist/presence.native.d.ts +8 -0
  35. package/dist/presence.native.d.ts.map +1 -0
  36. package/dist/presence.native.js +66 -0
  37. package/dist/presence.native.js.map +1 -0
  38. package/dist/pressable.d.ts +9 -2
  39. package/dist/pressable.d.ts.map +1 -1
  40. package/dist/pressable.js +9 -6
  41. package/dist/pressable.js.map +1 -1
  42. package/dist/pressable.native.d.ts.map +1 -1
  43. package/dist/pressable.native.js +181 -195
  44. package/dist/pressable.native.js.map +1 -1
  45. package/dist/runtime-container.native.d.ts +11 -9
  46. package/dist/runtime-container.native.d.ts.map +1 -1
  47. package/dist/runtime-container.native.js +8 -6
  48. package/dist/runtime-container.native.js.map +1 -1
  49. package/dist/runtime-transition.native.d.ts +16 -1
  50. package/dist/runtime-transition.native.d.ts.map +1 -1
  51. package/dist/runtime-transition.native.js +34 -7
  52. package/dist/runtime-transition.native.js.map +1 -1
  53. package/dist/text-input.d.ts +319 -3
  54. package/dist/text-input.d.ts.map +1 -1
  55. package/dist/text-input.js +4 -3
  56. package/dist/text-input.js.map +1 -1
  57. package/dist/view.d.ts +11 -2
  58. package/dist/view.d.ts.map +1 -1
  59. package/dist/view.js +7 -6
  60. package/dist/view.js.map +1 -1
  61. package/package.json +4 -3
  62. package/src/flat-list.native.tsx +69 -47
  63. package/src/foundation.native.tsx +16 -1
  64. package/src/foundation.tsx +66 -25
  65. package/src/index.native.ts +2 -0
  66. package/src/index.ts +209 -219
  67. package/src/link.native.tsx +29 -18
  68. package/src/presence-context.ts +27 -0
  69. package/src/presence.native.tsx +81 -0
  70. package/src/presence.tsx +55 -0
  71. package/src/pressable.native.tsx +251 -243
  72. package/src/pressable.ts +92 -79
  73. package/src/runtime-container.native.tsx +30 -15
  74. package/src/runtime-transition.native.tsx +68 -7
  75. package/src/text-input.ts +74 -72
  76. package/src/view.ts +76 -61
@@ -0,0 +1,55 @@
1
+ // Keeps a child mounted while it animates out (decision 007, slice 2).
2
+ //
3
+ // React removes an element the moment it stops being rendered, so nothing
4
+ // can animate after that. `Presence` holds on to its child after `show`
5
+ // turns false, sets `data-state="closed"` on it, and removes it when its
6
+ // transition or animation ends. The exit itself is CSS: a
7
+ // `data-[state=closed]:` class and a `transition-*` one, so it costs no
8
+ // script beyond this. The timing lives in `usePresence`
9
+ // (`@hozo/behaviors`), which Hozo's overlays use for their panels.
10
+ //
11
+ // The Native half is `./presence.native.tsx`, written against the same
12
+ // class.
13
+
14
+ import { usePresence } from '@hozo/behaviors'
15
+ import { cloneElement, isValidElement, type ReactElement, type ReactNode, type Ref } from 'react'
16
+
17
+ export interface PresenceProps {
18
+ /** Whether the child is shown. Turning it false starts the exit. */
19
+ show: boolean
20
+ /**
21
+ * One element, which receives `data-state`. A Hozo primitive passes it
22
+ * to the DOM; a component of your own has to forward it.
23
+ */
24
+ children?: ReactNode
25
+ }
26
+
27
+ function setRef<T>(ref: Ref<T> | undefined, value: T | null) {
28
+ if (typeof ref === 'function') ref(value)
29
+ else if (ref) (ref as { current: T | null }).current = value
30
+ }
31
+
32
+ export function Presence({ show, children }: PresenceProps) {
33
+ const presence = usePresence(show)
34
+
35
+ if (!presence.mounted) return null
36
+ if (!isValidElement(children)) return children ?? null
37
+ const child = children as ReactElement<Record<string, unknown>>
38
+ const childRef = child.props.ref as Ref<Element> | undefined
39
+ const chain =
40
+ (name: 'onTransitionEnd' | 'onAnimationEnd') =>
41
+ (event: { target: EventTarget; currentTarget: EventTarget }) => {
42
+ const own = child.props[name]
43
+ if (typeof own === 'function') own(event)
44
+ presence[name](event)
45
+ }
46
+ return cloneElement(child, {
47
+ 'data-state': presence.state,
48
+ ref: (node: Element | null) => {
49
+ presence.ref(node)
50
+ setRef(childRef, node)
51
+ },
52
+ onTransitionEnd: chain('onTransitionEnd'),
53
+ onAnimationEnd: chain('onAnimationEnd'),
54
+ })
55
+ }