@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.
- package/CHANGELOG.md +42 -0
- package/README.md +24 -2
- package/dist/flat-list.native.d.ts +8 -8
- package/dist/flat-list.native.d.ts.map +1 -1
- package/dist/flat-list.native.js +55 -25
- package/dist/flat-list.native.js.map +1 -1
- package/dist/foundation.d.ts +7 -4
- package/dist/foundation.d.ts.map +1 -1
- package/dist/foundation.js +33 -16
- package/dist/foundation.js.map +1 -1
- package/dist/foundation.native.d.ts +16 -2
- package/dist/foundation.native.d.ts.map +1 -1
- package/dist/foundation.native.js.map +1 -1
- package/dist/index.d.ts +6 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +7 -9
- package/dist/index.js.map +1 -1
- package/dist/index.native.d.ts +1 -0
- package/dist/index.native.d.ts.map +1 -1
- package/dist/index.native.js +1 -0
- package/dist/index.native.js.map +1 -1
- package/dist/link.native.d.ts +16 -7
- package/dist/link.native.d.ts.map +1 -1
- package/dist/link.native.js +9 -5
- package/dist/link.native.js.map +1 -1
- package/dist/presence-context.d.ts +15 -0
- package/dist/presence-context.d.ts.map +1 -0
- package/dist/presence-context.js +11 -0
- package/dist/presence-context.js.map +1 -0
- package/dist/presence.d.ts +12 -0
- package/dist/presence.d.ts.map +1 -0
- package/dist/presence.js +45 -0
- package/dist/presence.js.map +1 -0
- package/dist/presence.native.d.ts +8 -0
- package/dist/presence.native.d.ts.map +1 -0
- package/dist/presence.native.js +66 -0
- package/dist/presence.native.js.map +1 -0
- package/dist/pressable.d.ts +9 -2
- package/dist/pressable.d.ts.map +1 -1
- package/dist/pressable.js +9 -6
- package/dist/pressable.js.map +1 -1
- package/dist/pressable.native.d.ts.map +1 -1
- package/dist/pressable.native.js +181 -195
- package/dist/pressable.native.js.map +1 -1
- package/dist/runtime-container.native.d.ts +11 -9
- package/dist/runtime-container.native.d.ts.map +1 -1
- package/dist/runtime-container.native.js +8 -6
- package/dist/runtime-container.native.js.map +1 -1
- package/dist/runtime-transition.native.d.ts +16 -1
- package/dist/runtime-transition.native.d.ts.map +1 -1
- package/dist/runtime-transition.native.js +34 -7
- package/dist/runtime-transition.native.js.map +1 -1
- package/dist/text-input.d.ts +319 -3
- package/dist/text-input.d.ts.map +1 -1
- package/dist/text-input.js +4 -3
- package/dist/text-input.js.map +1 -1
- package/dist/view.d.ts +11 -2
- package/dist/view.d.ts.map +1 -1
- package/dist/view.js +7 -6
- package/dist/view.js.map +1 -1
- package/package.json +4 -3
- package/src/flat-list.native.tsx +69 -47
- package/src/foundation.native.tsx +16 -1
- package/src/foundation.tsx +66 -25
- package/src/index.native.ts +2 -0
- package/src/index.ts +209 -219
- package/src/link.native.tsx +29 -18
- package/src/presence-context.ts +27 -0
- package/src/presence.native.tsx +81 -0
- package/src/presence.tsx +55 -0
- package/src/pressable.native.tsx +251 -243
- package/src/pressable.ts +92 -79
- package/src/runtime-container.native.tsx +30 -15
- package/src/runtime-transition.native.tsx +68 -7
- package/src/text-input.ts +74 -72
- package/src/view.ts +76 -61
package/src/presence.tsx
ADDED
|
@@ -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
|
+
}
|