@hozo/behaviors 0.1.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 (118) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +80 -0
  3. package/dist/dismissable-layer.d.ts +17 -0
  4. package/dist/dismissable-layer.d.ts.map +1 -0
  5. package/dist/dismissable-layer.js +67 -0
  6. package/dist/dismissable-layer.js.map +1 -0
  7. package/dist/dismissable-layer.native.d.ts +28 -0
  8. package/dist/dismissable-layer.native.d.ts.map +1 -0
  9. package/dist/dismissable-layer.native.js +38 -0
  10. package/dist/dismissable-layer.native.js.map +1 -0
  11. package/dist/floating-geometry.d.ts +63 -0
  12. package/dist/floating-geometry.d.ts.map +1 -0
  13. package/dist/floating-geometry.js +191 -0
  14. package/dist/floating-geometry.js.map +1 -0
  15. package/dist/floating-positioner.d.ts +24 -0
  16. package/dist/floating-positioner.d.ts.map +1 -0
  17. package/dist/floating-positioner.js +110 -0
  18. package/dist/floating-positioner.js.map +1 -0
  19. package/dist/floating-positioner.native.d.ts +43 -0
  20. package/dist/floating-positioner.native.d.ts.map +1 -0
  21. package/dist/floating-positioner.native.js +82 -0
  22. package/dist/floating-positioner.native.js.map +1 -0
  23. package/dist/focus-scope.d.ts +31 -0
  24. package/dist/focus-scope.d.ts.map +1 -0
  25. package/dist/focus-scope.js +96 -0
  26. package/dist/focus-scope.js.map +1 -0
  27. package/dist/focus-scope.native.d.ts +33 -0
  28. package/dist/focus-scope.native.d.ts.map +1 -0
  29. package/dist/focus-scope.native.js +25 -0
  30. package/dist/focus-scope.native.js.map +1 -0
  31. package/dist/hover-geometry.d.ts +64 -0
  32. package/dist/hover-geometry.d.ts.map +1 -0
  33. package/dist/hover-geometry.js +146 -0
  34. package/dist/hover-geometry.js.map +1 -0
  35. package/dist/hover-trigger.d.ts +64 -0
  36. package/dist/hover-trigger.d.ts.map +1 -0
  37. package/dist/hover-trigger.js +174 -0
  38. package/dist/hover-trigger.js.map +1 -0
  39. package/dist/hover-trigger.native.d.ts +45 -0
  40. package/dist/hover-trigger.native.d.ts.map +1 -0
  41. package/dist/hover-trigger.native.js +101 -0
  42. package/dist/hover-trigger.native.js.map +1 -0
  43. package/dist/index.d.ts +12 -0
  44. package/dist/index.d.ts.map +1 -0
  45. package/dist/index.js +12 -0
  46. package/dist/index.js.map +1 -0
  47. package/dist/index.native.d.ts +12 -0
  48. package/dist/index.native.d.ts.map +1 -0
  49. package/dist/index.native.js +12 -0
  50. package/dist/index.native.js.map +1 -0
  51. package/dist/live-region.d.ts +19 -0
  52. package/dist/live-region.d.ts.map +1 -0
  53. package/dist/live-region.js +51 -0
  54. package/dist/live-region.js.map +1 -0
  55. package/dist/live-region.native.d.ts +31 -0
  56. package/dist/live-region.native.d.ts.map +1 -0
  57. package/dist/live-region.native.js +38 -0
  58. package/dist/live-region.native.js.map +1 -0
  59. package/dist/native.d.ts +5 -0
  60. package/dist/native.d.ts.map +1 -0
  61. package/dist/native.js +4 -0
  62. package/dist/native.js.map +1 -0
  63. package/dist/portal.d.ts +26 -0
  64. package/dist/portal.d.ts.map +1 -0
  65. package/dist/portal.js +41 -0
  66. package/dist/portal.js.map +1 -0
  67. package/dist/portal.native.d.ts +44 -0
  68. package/dist/portal.native.d.ts.map +1 -0
  69. package/dist/portal.native.js +79 -0
  70. package/dist/portal.native.js.map +1 -0
  71. package/dist/roving-focus-rules.d.ts +13 -0
  72. package/dist/roving-focus-rules.d.ts.map +1 -0
  73. package/dist/roving-focus-rules.js +49 -0
  74. package/dist/roving-focus-rules.js.map +1 -0
  75. package/dist/roving-focus.d.ts +27 -0
  76. package/dist/roving-focus.d.ts.map +1 -0
  77. package/dist/roving-focus.js +52 -0
  78. package/dist/roving-focus.js.map +1 -0
  79. package/dist/roving-focus.native.d.ts +36 -0
  80. package/dist/roving-focus.native.d.ts.map +1 -0
  81. package/dist/roving-focus.native.js +28 -0
  82. package/dist/roving-focus.native.js.map +1 -0
  83. package/dist/text-child.d.ts +4 -0
  84. package/dist/text-child.d.ts.map +1 -0
  85. package/dist/text-child.js +17 -0
  86. package/dist/text-child.js.map +1 -0
  87. package/dist/text-child.native.d.ts +14 -0
  88. package/dist/text-child.native.d.ts.map +1 -0
  89. package/dist/text-child.native.js +42 -0
  90. package/dist/text-child.native.js.map +1 -0
  91. package/dist/typeahead.d.ts +36 -0
  92. package/dist/typeahead.d.ts.map +1 -0
  93. package/dist/typeahead.js +97 -0
  94. package/dist/typeahead.js.map +1 -0
  95. package/package.json +82 -0
  96. package/src/dismissable-layer.native.tsx +72 -0
  97. package/src/dismissable-layer.tsx +106 -0
  98. package/src/floating-geometry.ts +306 -0
  99. package/src/floating-positioner.native.tsx +155 -0
  100. package/src/floating-positioner.tsx +173 -0
  101. package/src/focus-scope.native.tsx +53 -0
  102. package/src/focus-scope.tsx +143 -0
  103. package/src/hover-geometry.ts +182 -0
  104. package/src/hover-trigger.native.tsx +179 -0
  105. package/src/hover-trigger.tsx +303 -0
  106. package/src/index.native.ts +77 -0
  107. package/src/index.ts +77 -0
  108. package/src/live-region.native.tsx +61 -0
  109. package/src/live-region.tsx +74 -0
  110. package/src/native.ts +15 -0
  111. package/src/portal.native.tsx +130 -0
  112. package/src/portal.tsx +68 -0
  113. package/src/roving-focus-rules.ts +79 -0
  114. package/src/roving-focus.native.tsx +81 -0
  115. package/src/roving-focus.tsx +118 -0
  116. package/src/text-child.native.tsx +44 -0
  117. package/src/text-child.ts +19 -0
  118. package/src/typeahead.ts +120 -0
@@ -0,0 +1 @@
1
+ {"version":3,"file":"roving-focus.native.js","sourceRoot":"","sources":["../src/roving-focus.native.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,EAAE,aAAa,EAAkB,UAAU,EAAE,OAAO,EAAE,MAAM,OAAO,CAAA;AACjF,OAAO,EAAkB,IAAI,EAAkB,MAAM,cAAc,CAAA;AAInE,OAAO,EACL,SAAS,EAIT,QAAQ,GACT,MAAM,yBAAyB,CAAA;AAOhC,MAAM,kBAAkB,GAAG,aAAa,CAAiC,IAAI,CAAC,CAAA;AAwB9E;;GAEG;AACH,MAAM,UAAU,gBAAgB,CAAC,EAC/B,QAAQ,EACR,MAAM,EACN,cAAc,EACd,KAAK,EACL,GAAG,KAAK,EACc;IACtB,MAAM,YAAY,GAAG,OAAO,CAC1B,GAAG,EAAE,CAAC,CAAC;QACL,MAAM;QACN,WAAW,EAAE,cAAc;KAC5B,CAAC,EACF,CAAC,MAAM,EAAE,cAAc,CAAC,CACzB,CAAA;IAED,OAAO,KAAK,CAAC,aAAa,CACxB,IAAI,EACJ,EAAE,KAAK,EAAE,GAAG,KAAK,EAAE,EACnB,KAAK,CAAC,aAAa,CAAC,kBAAkB,CAAC,QAAQ,EAAE,EAAE,KAAK,EAAE,YAAY,EAAE,EAAE,QAAQ,CAAC,CACpF,CAAA;AACH,CAAC;AAED,MAAM,UAAU,aAAa,CAAC,KAAa;IACzC,MAAM,OAAO,GAAG,UAAU,CAAC,kBAAkB,CAAC,CAAA;IAC9C,IAAI,CAAC,OAAO,EAAE,CAAC;QACb,OAAO;YACL,QAAQ,EAAE,IAAI;YACd,OAAO,EAAE,GAAG,EAAE,GAAE,CAAC;SAClB,CAAA;IACH,CAAC;IAED,OAAO;QACL,QAAQ,EAAE,OAAO,CAAC,MAAM,KAAK,KAAK;QAClC,OAAO,EAAE,GAAG,EAAE,CAAC,OAAO,CAAC,WAAW,CAAC,KAAK,CAAC;KAC1C,CAAA;AACH,CAAC"}
@@ -0,0 +1,4 @@
1
+ import type { ReactNode } from 'react';
2
+ /** Identity. See above, and `text-child.native.tsx` for the real one. */
3
+ export declare function hozoTextChildren(children: ReactNode): ReactNode;
4
+ //# sourceMappingURL=text-child.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"text-child.d.ts","sourceRoot":"","sources":["../src/text-child.ts"],"names":[],"mappings":"AAaA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,OAAO,CAAA;AAEtC,yEAAyE;AACzE,wBAAgB,gBAAgB,CAAC,QAAQ,EAAE,SAAS,GAAG,SAAS,CAE/D"}
@@ -0,0 +1,17 @@
1
+ // The Web half, which the browser already is.
2
+ //
3
+ // A string inside a `<div>` is a text node and there is nothing to fix, so
4
+ // this returns what it was given. It exists for the same reason
5
+ // `disclosure.tsx` and `ruby.tsx` do: a package resolves `@hozo/engine`'s
6
+ // types through the Web entry whichever platform it is building for, so a
7
+ // name the native halves import has to be nameable here or `@hozo/core`
8
+ // cannot compile.
9
+ //
10
+ // `.ts` rather than `.tsx`, since there is no JSX in it -- and that
11
+ // matters for `@hozo/behaviors`'s own tests, which run through Node's type
12
+ // stripping and have no JSX transform.
13
+ /** Identity. See above, and `text-child.native.tsx` for the real one. */
14
+ export function hozoTextChildren(children) {
15
+ return children;
16
+ }
17
+ //# sourceMappingURL=text-child.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"text-child.js","sourceRoot":"","sources":["../src/text-child.ts"],"names":[],"mappings":"AAAA,8CAA8C;AAC9C,EAAE;AACF,2EAA2E;AAC3E,gEAAgE;AAChE,0EAA0E;AAC1E,0EAA0E;AAC1E,wEAAwE;AACxE,kBAAkB;AAClB,EAAE;AACF,oEAAoE;AACpE,2EAA2E;AAC3E,uCAAuC;AAIvC,yEAAyE;AACzE,MAAM,UAAU,gBAAgB,CAAC,QAAmB;IAClD,OAAO,QAAQ,CAAA;AACjB,CAAC"}
@@ -0,0 +1,14 @@
1
+ import { type ReactNode } from 'react';
2
+ /**
3
+ * Children with every text one inside a `Text`, and nothing else touched.
4
+ *
5
+ * Per child rather than around the lot, which is what the backend does:
6
+ * `<View><Text>a</Text>tail</View>` lowers to two `Text` elements rather
7
+ * than one wrapping both, and wrapping everything would put whatever else
8
+ * is in there inside a `Text` too.
9
+ *
10
+ * Numbers as well as strings. React renders both as text nodes, and React
11
+ * Native rejects both for the same reason.
12
+ */
13
+ export declare function hozoTextChildren(children: ReactNode): ReactNode;
14
+ //# sourceMappingURL=text-child.native.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"text-child.native.d.ts","sourceRoot":"","sources":["../src/text-child.native.tsx"],"names":[],"mappings":"AAyBA,OAAO,EAAY,KAAK,SAAS,EAAE,MAAM,OAAO,CAAA;AAGhD;;;;;;;;;;GAUG;AACH,wBAAgB,gBAAgB,CAAC,QAAQ,EAAE,SAAS,GAAG,SAAS,CAI/D"}
@@ -0,0 +1,42 @@
1
+ import { jsx as _jsx } from "react/jsx-runtime";
2
+ // Text inside a box, which React Native will not have.
3
+ //
4
+ // "Text strings must be rendered within a `<Text>` component" is what a
5
+ // bare string inside a `View` gets, at runtime, on a device. Hozo's Native
6
+ // backend already knows this and wraps every literal text child it lowers:
7
+ //
8
+ // <ListItem>Apple</ListItem> => <View role="listitem"><Text>Apple</Text></View>
9
+ //
10
+ // Uniformly, for every primitive that becomes a `View` -- there is no
11
+ // component where the answer is different, which is worth saying because
12
+ // it is what makes this mechanical rather than a judgement about which
13
+ // boxes people put labels in.
14
+ //
15
+ // The uncompiled fallbacks are supposed to behave the way that output
16
+ // does, and did not: they passed children through, so `<ListItem>Apple
17
+ // </ListItem>` crashed on exactly the path that exists for files the
18
+ // compiler could not reach (#295). Two of them were fixed by hand in
19
+ // #294, which is where the second occurrence stopped being a coincidence.
20
+ //
21
+ // This is stricter than the compiler in one place, deliberately. The
22
+ // backend leaves `<View>{label}</View>` alone because it cannot know what
23
+ // the expression is; this runs at render time, where the answer is
24
+ // simply available. A fallback being right about a case the compiler has
25
+ // to leave is not a divergence worth removing.
26
+ import { Children } from 'react';
27
+ import { Text } from 'react-native';
28
+ /**
29
+ * Children with every text one inside a `Text`, and nothing else touched.
30
+ *
31
+ * Per child rather than around the lot, which is what the backend does:
32
+ * `<View><Text>a</Text>tail</View>` lowers to two `Text` elements rather
33
+ * than one wrapping both, and wrapping everything would put whatever else
34
+ * is in there inside a `Text` too.
35
+ *
36
+ * Numbers as well as strings. React renders both as text nodes, and React
37
+ * Native rejects both for the same reason.
38
+ */
39
+ export function hozoTextChildren(children) {
40
+ return Children.map(children, (child) => typeof child === 'string' || typeof child === 'number' ? _jsx(Text, { children: child }) : child);
41
+ }
42
+ //# sourceMappingURL=text-child.native.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"text-child.native.js","sourceRoot":"","sources":["../src/text-child.native.tsx"],"names":[],"mappings":";AAAA,uDAAuD;AACvD,EAAE;AACF,wEAAwE;AACxE,2EAA2E;AAC3E,2EAA2E;AAC3E,EAAE;AACF,sFAAsF;AACtF,EAAE;AACF,sEAAsE;AACtE,yEAAyE;AACzE,uEAAuE;AACvE,8BAA8B;AAC9B,EAAE;AACF,sEAAsE;AACtE,uEAAuE;AACvE,qEAAqE;AACrE,qEAAqE;AACrE,0EAA0E;AAC1E,EAAE;AACF,qEAAqE;AACrE,0EAA0E;AAC1E,mEAAmE;AACnE,yEAAyE;AACzE,+CAA+C;AAE/C,OAAO,EAAE,QAAQ,EAAkB,MAAM,OAAO,CAAA;AAChD,OAAO,EAAE,IAAI,EAAE,MAAM,cAAc,CAAA;AAEnC;;;;;;;;;;GAUG;AACH,MAAM,UAAU,gBAAgB,CAAC,QAAmB;IAClD,OAAO,QAAQ,CAAC,GAAG,CAAC,QAAQ,EAAE,CAAC,KAAK,EAAE,EAAE,CACtC,OAAO,KAAK,KAAK,QAAQ,IAAI,OAAO,KAAK,KAAK,QAAQ,CAAC,CAAC,CAAC,KAAC,IAAI,cAAE,KAAK,GAAQ,CAAC,CAAC,CAAC,KAAK,CACtF,CAAA;AACH,CAAC"}
@@ -0,0 +1,36 @@
1
+ /** How long a keystroke stays part of the same search, in milliseconds. */
2
+ export declare const TYPEAHEAD_TIMEOUT_MS = 1000;
3
+ /**
4
+ * Derives the search string after `key`, given what was typed before and time elapsed.
5
+ */
6
+ export declare function nextSearch(previous: string, key: string, sinceLastKey: number): string;
7
+ export interface TypeaheadOptions {
8
+ /** The items' accessible labels in order. */
9
+ labels: readonly string[];
10
+ /** Which item currently has focus/active index. */
11
+ active: number;
12
+ /** Items that cannot take focus, by index. */
13
+ disabled?: readonly number[];
14
+ }
15
+ /**
16
+ * Searches for the next matching index based on query string.
17
+ * Supports consecutive single-letter cycling (e.g. pressing 's' repeatedly walks every 's' item).
18
+ * If a multi-character query yields no match, safely falls back to the last typed character
19
+ * with an explicit recursion guard to prevent any possibility of infinite loops.
20
+ */
21
+ export declare function searchIndex(search: string, options: TypeaheadOptions, allowFallback?: boolean): number | null;
22
+ /**
23
+ * Checks whether a keypress is printable search text rather than an action key.
24
+ */
25
+ export declare function isTypeaheadKey(key: string, searching: boolean): boolean;
26
+ /**
27
+ * React Hook managing incremental typeahead search state.
28
+ */
29
+ export declare function useTypeahead(labels: readonly string[], active: number, onSelect: (index: number) => void, disabled?: readonly number[]): {
30
+ handleKeyDown: (event: {
31
+ key: string;
32
+ defaultPrevented?: boolean;
33
+ }) => void;
34
+ reset: () => void;
35
+ };
36
+ //# sourceMappingURL=typeahead.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"typeahead.d.ts","sourceRoot":"","sources":["../src/typeahead.ts"],"names":[],"mappings":"AAEA,2EAA2E;AAC3E,eAAO,MAAM,oBAAoB,OAAO,CAAA;AAExC;;GAEG;AACH,wBAAgB,UAAU,CAAC,QAAQ,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM,GAAG,MAAM,CAGtF;AAED,MAAM,WAAW,gBAAgB;IAC/B,6CAA6C;IAC7C,MAAM,EAAE,SAAS,MAAM,EAAE,CAAA;IACzB,mDAAmD;IACnD,MAAM,EAAE,MAAM,CAAA;IACd,8CAA8C;IAC9C,QAAQ,CAAC,EAAE,SAAS,MAAM,EAAE,CAAA;CAC7B;AAED;;;;;GAKG;AACH,wBAAgB,WAAW,CACzB,MAAM,EAAE,MAAM,EACd,OAAO,EAAE,gBAAgB,EACzB,aAAa,UAAO,GACnB,MAAM,GAAG,IAAI,CAyBf;AAED;;GAEG;AACH,wBAAgB,cAAc,CAAC,GAAG,EAAE,MAAM,EAAE,SAAS,EAAE,OAAO,GAAG,OAAO,CAIvE;AAED;;GAEG;AACH,wBAAgB,YAAY,CAC1B,MAAM,EAAE,SAAS,MAAM,EAAE,EACzB,MAAM,EAAE,MAAM,EACd,QAAQ,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,IAAI,EACjC,QAAQ,GAAE,SAAS,MAAM,EAAO;2BAKF;QAAE,GAAG,EAAE,MAAM,CAAC;QAAC,gBAAgB,CAAC,EAAE,OAAO,CAAA;KAAE;;EAuC1E"}
@@ -0,0 +1,97 @@
1
+ import { useRef } from 'react';
2
+ /** How long a keystroke stays part of the same search, in milliseconds. */
3
+ export const TYPEAHEAD_TIMEOUT_MS = 1000;
4
+ /**
5
+ * Derives the search string after `key`, given what was typed before and time elapsed.
6
+ */
7
+ export function nextSearch(previous, key, sinceLastKey) {
8
+ const buffer = sinceLastKey > TYPEAHEAD_TIMEOUT_MS ? '' : previous;
9
+ return buffer + key.toLowerCase();
10
+ }
11
+ /**
12
+ * Searches for the next matching index based on query string.
13
+ * Supports consecutive single-letter cycling (e.g. pressing 's' repeatedly walks every 's' item).
14
+ * If a multi-character query yields no match, safely falls back to the last typed character
15
+ * with an explicit recursion guard to prevent any possibility of infinite loops.
16
+ */
17
+ export function searchIndex(search, options, allowFallback = true) {
18
+ const { labels, active, disabled = [] } = options;
19
+ if (search === '' || labels.length === 0)
20
+ return null;
21
+ const first = search.slice(0, 1);
22
+ const repeated = search.length > 1 && [...search].every((character) => character === first);
23
+ const needle = repeated ? first : search;
24
+ const from = repeated ? active + 1 : active;
25
+ for (let offset = 0; offset < labels.length; offset += 1) {
26
+ const index = (from + offset + labels.length) % labels.length;
27
+ if (disabled.includes(index))
28
+ continue;
29
+ const label = labels[index];
30
+ if (label?.trim().toLowerCase().startsWith(needle))
31
+ return index;
32
+ }
33
+ // Smart fallback: If multi-character search (e.g. "tts") has no match,
34
+ // try matching just the newly typed last character ("s").
35
+ // Hard guard: only recurse once if search.length > 1 and allowFallback is true.
36
+ if (allowFallback && search.length > 1) {
37
+ const lastChar = search.slice(-1);
38
+ return searchIndex(lastChar, options, false);
39
+ }
40
+ return null;
41
+ }
42
+ /**
43
+ * Checks whether a keypress is printable search text rather than an action key.
44
+ */
45
+ export function isTypeaheadKey(key, searching) {
46
+ if (key.length !== 1)
47
+ return false;
48
+ if (key === ' ')
49
+ return searching;
50
+ return true;
51
+ }
52
+ /**
53
+ * React Hook managing incremental typeahead search state.
54
+ */
55
+ export function useTypeahead(labels, active, onSelect, disabled = []) {
56
+ const bufferRef = useRef('');
57
+ const lastKeyTimeRef = useRef(0);
58
+ const handleKeyDown = (event) => {
59
+ if (event.defaultPrevented)
60
+ return;
61
+ const now = Date.now();
62
+ const elapsed = now - lastKeyTimeRef.current;
63
+ const isSearching = elapsed <= TYPEAHEAD_TIMEOUT_MS && bufferRef.current.length > 0;
64
+ if (!isTypeaheadKey(event.key, isSearching))
65
+ return;
66
+ const query = nextSearch(bufferRef.current, event.key, elapsed);
67
+ lastKeyTimeRef.current = now;
68
+ // First try matching full query without fallback
69
+ let next = searchIndex(query, { labels, active, disabled }, false);
70
+ if (next !== null) {
71
+ bufferRef.current = query;
72
+ }
73
+ else if (query.length > 1) {
74
+ // Fallback: full query yielded no match, try newly pressed key as fresh start
75
+ const fallbackKey = event.key.toLowerCase();
76
+ next = searchIndex(fallbackKey, { labels, active, disabled }, false);
77
+ if (next !== null) {
78
+ bufferRef.current = fallbackKey;
79
+ }
80
+ else {
81
+ bufferRef.current = query;
82
+ }
83
+ }
84
+ else {
85
+ bufferRef.current = query;
86
+ }
87
+ if (next !== null && next !== active) {
88
+ onSelect(next);
89
+ }
90
+ };
91
+ const reset = () => {
92
+ bufferRef.current = '';
93
+ lastKeyTimeRef.current = 0;
94
+ };
95
+ return { handleKeyDown, reset };
96
+ }
97
+ //# sourceMappingURL=typeahead.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"typeahead.js","sourceRoot":"","sources":["../src/typeahead.ts"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,EAAE,CAAA;QACtB,cAAc,CAAC,OAAO,GAAG,CAAC,CAAA;IAC5B,CAAC,CAAA;IAED,OAAO,EAAE,aAAa,EAAE,KAAK,EAAE,CAAA;AACjC,CAAC"}
package/package.json ADDED
@@ -0,0 +1,82 @@
1
+ {
2
+ "name": "@hozo/behaviors",
3
+ "version": "0.1.0",
4
+ "description": "Universal runtime behaviors substrate (LiveRegion, Portal, DismissableLayer, FocusScope, RovingFocus, FloatingPositioner) for Hozo.",
5
+ "keywords": [
6
+ "react-native",
7
+ "behaviors",
8
+ "focus",
9
+ "roving-focus",
10
+ "typeahead",
11
+ "popper",
12
+ "floating-ui",
13
+ "portal",
14
+ "live-region"
15
+ ],
16
+ "license": "MIT",
17
+ "repository": {
18
+ "type": "git",
19
+ "url": "git+https://github.com/iray-tno/hozo.git",
20
+ "directory": "packages/behaviors"
21
+ },
22
+ "homepage": "https://iray-tno.github.io/hozo/",
23
+ "bugs": {
24
+ "url": "https://github.com/iray-tno/hozo/issues"
25
+ },
26
+ "type": "module",
27
+ "main": "./dist/index.js",
28
+ "types": "./dist/index.d.ts",
29
+ "sideEffects": false,
30
+ "exports": {
31
+ ".": {
32
+ "react-native": {
33
+ "types": "./dist/index.native.d.ts",
34
+ "default": "./dist/index.native.js"
35
+ },
36
+ "types": "./dist/index.d.ts",
37
+ "default": "./dist/index.js"
38
+ },
39
+ "./native": {
40
+ "types": "./dist/native.d.ts",
41
+ "default": "./dist/native.js"
42
+ },
43
+ "./package.json": "./package.json"
44
+ },
45
+ "files": [
46
+ "dist",
47
+ "src",
48
+ "!src/**/*.test.ts",
49
+ "!src/**/*.test.tsx",
50
+ "!src/**/*.bench.ts"
51
+ ],
52
+ "publishConfig": {
53
+ "access": "public",
54
+ "provenance": true
55
+ },
56
+ "dependencies": {},
57
+ "peerDependencies": {
58
+ "react": "^19.0.0",
59
+ "react-dom": "^19.0.0",
60
+ "react-native": ">=0.86"
61
+ },
62
+ "peerDependenciesMeta": {
63
+ "react-dom": {
64
+ "optional": true
65
+ },
66
+ "react-native": {
67
+ "optional": true
68
+ }
69
+ },
70
+ "devDependencies": {
71
+ "@types/react": "^19.2.18",
72
+ "@types/react-dom": "^19.2.4",
73
+ "react": "^19.2.8",
74
+ "react-dom": "^19.2.8",
75
+ "react-native": "^0.87.1"
76
+ },
77
+ "scripts": {
78
+ "build": "node ../../scripts/clean-package-dist.mjs && tsc -p .",
79
+ "typecheck": "tsc -p . --noEmit",
80
+ "test": "node -e \"require('node:fs').rmSync('.test-build', { recursive: true, force: true })\" && tsc -p tsconfig.test.json && node --test .test-build/*.test.js"
81
+ }
82
+ }
@@ -0,0 +1,72 @@
1
+ import React, { type ReactNode, useEffect } from 'react'
2
+ import { BackHandler, type StyleProp, View, type ViewStyle } from 'react-native'
3
+
4
+ export interface DismissableLayerProps {
5
+ /**
6
+ * Tailwind classes, the same prop the Web half takes.
7
+ *
8
+ * On a tag the compiler lowers it is gone by runtime, replaced by a
9
+ * `StyleSheet` entry. Anywhere else it is carried and ignored here --
10
+ * this side has no CSS engine to resolve a class list against -- and
11
+ * the type still has to accept it, because an app is type-checked
12
+ * against the source the compiler reads rather than its output.
13
+ */
14
+ className?: string
15
+ children?: ReactNode
16
+ onDismiss?: () => void
17
+ onEscapeKeyDown?: () => void
18
+ onPointerDownOutside?: () => void
19
+ disableOutsidePointerEvents?: boolean
20
+ style?: StyleProp<ViewStyle>
21
+ }
22
+
23
+ type NativeLayerInstance = {
24
+ onDismiss?: () => void
25
+ onEscapeKeyDown?: () => void
26
+ }
27
+
28
+ const nativeLayers: NativeLayerInstance[] = []
29
+
30
+ /**
31
+ * Universal `<DismissableLayer>` component for React Native.
32
+ * Listens to BackHandler on Android and manages layered dismiss stack.
33
+ */
34
+ export function DismissableLayer({
35
+ children,
36
+ onDismiss,
37
+ onEscapeKeyDown,
38
+ style,
39
+ ...props
40
+ }: DismissableLayerProps) {
41
+ useEffect(() => {
42
+ const instance: NativeLayerInstance = {
43
+ onDismiss,
44
+ onEscapeKeyDown,
45
+ }
46
+ nativeLayers.push(instance)
47
+
48
+ // `BackHandler` imported rather than read off `globalThis`, where React Native
49
+ // has never put it. The lookup always returned `undefined` and the
50
+ // `if` around it always failed, so this did nothing at all -- silently,
51
+ // which is the worst way for an accessibility affordance to be absent.
52
+ const subscription = BackHandler.addEventListener('hardwareBackPress', () => {
53
+ const top = nativeLayers[nativeLayers.length - 1]
54
+ if (top === instance) {
55
+ instance.onEscapeKeyDown?.()
56
+ instance.onDismiss?.()
57
+ return true // Handled
58
+ }
59
+ return false
60
+ })
61
+
62
+ return () => {
63
+ const idx = nativeLayers.indexOf(instance)
64
+ if (idx !== -1) {
65
+ nativeLayers.splice(idx, 1)
66
+ }
67
+ subscription.remove()
68
+ }
69
+ }, [onDismiss, onEscapeKeyDown])
70
+
71
+ return React.createElement(View, { style, ...props }, children)
72
+ }
@@ -0,0 +1,106 @@
1
+ import { type CSSProperties, type ReactNode, useEffect, useRef } from 'react'
2
+
3
+ export interface DismissableLayerProps {
4
+ children?: ReactNode
5
+ onDismiss?: () => void
6
+ onEscapeKeyDown?: (event: KeyboardEvent) => void
7
+ onPointerDownOutside?: (event: PointerEvent) => void
8
+ disableOutsidePointerEvents?: boolean
9
+ className?: string
10
+ style?: CSSProperties
11
+ }
12
+
13
+ type LayerInstance = {
14
+ node: HTMLElement | null
15
+ onDismiss?: () => void
16
+ onEscapeKeyDown?: (event: KeyboardEvent) => void
17
+ onPointerDownOutside?: (event: PointerEvent) => void
18
+ }
19
+
20
+ // Global stack of active dismissable layers (LIFO: last opened is top-most)
21
+ const layers: LayerInstance[] = []
22
+
23
+ /**
24
+ * Universal `<DismissableLayer>` component for Web.
25
+ * Detects Escape key presses and pointer down outside the layer boundary.
26
+ * Manages layer stacking order so only the top-most active layer dismisses.
27
+ */
28
+ export function DismissableLayer({
29
+ children,
30
+ onDismiss,
31
+ onEscapeKeyDown,
32
+ onPointerDownOutside,
33
+ disableOutsidePointerEvents = false,
34
+ className,
35
+ style,
36
+ }: DismissableLayerProps) {
37
+ const ref = useRef<HTMLDivElement>(null)
38
+
39
+ useEffect(() => {
40
+ const node = ref.current
41
+ const instance: LayerInstance = {
42
+ node,
43
+ onDismiss,
44
+ onEscapeKeyDown,
45
+ onPointerDownOutside,
46
+ }
47
+
48
+ layers.push(instance)
49
+
50
+ const handleKeyDown = (event: KeyboardEvent) => {
51
+ if (event.key !== 'Escape') return
52
+ // Only the top-most layer reacts to Escape
53
+ const topLayer = layers[layers.length - 1]
54
+ if (topLayer !== instance) return
55
+
56
+ instance.onEscapeKeyDown?.(event)
57
+ if (!event.defaultPrevented) {
58
+ instance.onDismiss?.()
59
+ }
60
+ }
61
+
62
+ const handlePointerDown = (event: PointerEvent) => {
63
+ const topLayer = layers[layers.length - 1]
64
+ if (topLayer !== instance) return
65
+ if (!instance.node) return
66
+
67
+ const target = event.target as Node | null
68
+ const isOutside = target && !instance.node.contains(target)
69
+
70
+ if (isOutside) {
71
+ if (disableOutsidePointerEvents) {
72
+ event.preventDefault()
73
+ }
74
+ instance.onPointerDownOutside?.(event)
75
+ if (!event.defaultPrevented) {
76
+ instance.onDismiss?.()
77
+ }
78
+ }
79
+ }
80
+
81
+ document.addEventListener('keydown', handleKeyDown)
82
+ document.addEventListener('pointerdown', handlePointerDown)
83
+
84
+ return () => {
85
+ const index = layers.indexOf(instance)
86
+ if (index !== -1) {
87
+ layers.splice(index, 1)
88
+ }
89
+ document.removeEventListener('keydown', handleKeyDown)
90
+ document.removeEventListener('pointerdown', handlePointerDown)
91
+ }
92
+ }, [onDismiss, onEscapeKeyDown, onPointerDownOutside, disableOutsidePointerEvents])
93
+
94
+ return (
95
+ <div
96
+ ref={ref}
97
+ className={className}
98
+ style={{
99
+ ...style,
100
+ pointerEvents: 'auto',
101
+ }}
102
+ >
103
+ {children}
104
+ </div>
105
+ )
106
+ }