@codual/react-native-ui 0.0.0-stage → 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 (141) hide show
  1. package/LICENSE +202 -0
  2. package/NOTICE +2 -0
  3. package/README.md +18 -2
  4. package/dist/announce.d.ts +2 -0
  5. package/dist/announce.js +8 -0
  6. package/dist/charts/data.d.ts +32 -0
  7. package/dist/charts/data.js +13 -0
  8. package/dist/charts/labels.d.ts +54 -0
  9. package/dist/charts/labels.js +91 -0
  10. package/dist/charts/machine.d.ts +62 -0
  11. package/dist/charts/machine.js +144 -0
  12. package/dist/charts/points.d.ts +23 -0
  13. package/dist/charts/points.js +46 -0
  14. package/dist/charts/summary.d.ts +29 -0
  15. package/dist/charts/summary.js +45 -0
  16. package/dist/charts/use-chart-interaction.d.ts +62 -0
  17. package/dist/charts/use-chart-interaction.js +79 -0
  18. package/dist/forms/async-checks.d.ts +34 -0
  19. package/dist/forms/async-checks.js +77 -0
  20. package/dist/forms/field-errors.d.ts +8 -0
  21. package/dist/forms/field-errors.js +16 -0
  22. package/dist/forms/form-types.d.ts +43 -0
  23. package/dist/forms/form-types.js +2 -0
  24. package/dist/forms/machine.d.ts +63 -0
  25. package/dist/forms/machine.js +251 -0
  26. package/dist/forms/numbers.d.ts +5 -0
  27. package/dist/forms/numbers.js +83 -0
  28. package/dist/forms/rules.d.ts +47 -0
  29. package/dist/forms/rules.js +72 -0
  30. package/dist/forms/server-errors.d.ts +17 -0
  31. package/dist/forms/server-errors.js +22 -0
  32. package/dist/forms/use-form.d.ts +49 -0
  33. package/dist/forms/use-form.js +61 -0
  34. package/dist/index.d.ts +50 -0
  35. package/dist/index.js +94 -0
  36. package/dist/lists/PagedList.d.ts +47 -0
  37. package/dist/lists/PagedList.js +81 -0
  38. package/dist/lists/controller.d.ts +23 -0
  39. package/dist/lists/controller.js +20 -0
  40. package/dist/lists/inverted-list.d.ts +23 -0
  41. package/dist/lists/inverted-list.js +60 -0
  42. package/dist/lists/machine.d.ts +81 -0
  43. package/dist/lists/machine.js +252 -0
  44. package/dist/lists/newest-end.d.ts +30 -0
  45. package/dist/lists/newest-end.js +58 -0
  46. package/dist/lists/paged-content.d.ts +46 -0
  47. package/dist/lists/paged-content.js +65 -0
  48. package/dist/lists/paged-list-types.d.ts +56 -0
  49. package/dist/lists/paged-list-types.js +2 -0
  50. package/dist/lists/system-footer.d.ts +28 -0
  51. package/dist/lists/system-footer.js +50 -0
  52. package/dist/lists/use-paged-list.d.ts +25 -0
  53. package/dist/lists/use-paged-list.js +34 -0
  54. package/dist/permissions/access.d.ts +30 -0
  55. package/dist/permissions/access.js +2 -0
  56. package/dist/permissions/android-permission.d.ts +52 -0
  57. package/dist/permissions/android-permission.js +143 -0
  58. package/dist/permissions/permission.d.ts +69 -0
  59. package/dist/permissions/permission.js +201 -0
  60. package/dist/permissions/setup.d.ts +25 -0
  61. package/dist/permissions/setup.js +41 -0
  62. package/dist/permissions/status.d.ts +29 -0
  63. package/dist/permissions/status.js +18 -0
  64. package/dist/permissions/use-permission.d.ts +23 -0
  65. package/dist/permissions/use-permission.js +29 -0
  66. package/dist/purchases/Paywall.d.ts +27 -0
  67. package/dist/purchases/Paywall.js +28 -0
  68. package/dist/purchases/paywall-content.d.ts +20 -0
  69. package/dist/purchases/paywall-content.js +34 -0
  70. package/dist/purchases/purchases.d.ts +92 -0
  71. package/dist/purchases/purchases.js +236 -0
  72. package/dist/purchases/status.d.ts +62 -0
  73. package/dist/purchases/status.js +25 -0
  74. package/dist/purchases/store.d.ts +71 -0
  75. package/dist/purchases/store.js +2 -0
  76. package/dist/purchases/system-views.d.ts +49 -0
  77. package/dist/purchases/system-views.js +91 -0
  78. package/dist/purchases/use-purchases.d.ts +3 -0
  79. package/dist/purchases/use-purchases.js +8 -0
  80. package/dist/react-native-iap/iap-purchases.d.ts +24 -0
  81. package/dist/react-native-iap/iap-purchases.js +90 -0
  82. package/dist/react-native-iap/iap-store.d.ts +56 -0
  83. package/dist/react-native-iap/iap-store.js +249 -0
  84. package/dist/react-native-iap/iap-types.d.ts +12 -0
  85. package/dist/react-native-iap/iap-types.js +11 -0
  86. package/dist/react-native-iap/index.d.ts +31 -0
  87. package/dist/react-native-iap/index.js +24 -0
  88. package/dist/react-native-permissions/index.d.ts +24 -0
  89. package/dist/react-native-permissions/index.js +88 -0
  90. package/dist/screen-states/ScreenStates.d.ts +38 -0
  91. package/dist/screen-states/ScreenStates.js +69 -0
  92. package/dist/screen-states/errors.d.ts +9 -0
  93. package/dist/screen-states/errors.js +24 -0
  94. package/dist/screen-states/machine.d.ts +97 -0
  95. package/dist/screen-states/machine.js +172 -0
  96. package/dist/screen-states/system-views.d.ts +40 -0
  97. package/dist/screen-states/system-views.js +66 -0
  98. package/dist/screen-states/use-screen-states.d.ts +23 -0
  99. package/dist/screen-states/use-screen-states.js +41 -0
  100. package/dist/search/SearchResults.d.ts +22 -0
  101. package/dist/search/SearchResults.js +25 -0
  102. package/dist/search/announcements.d.ts +17 -0
  103. package/dist/search/announcements.js +31 -0
  104. package/dist/search/labels.d.ts +15 -0
  105. package/dist/search/labels.js +14 -0
  106. package/dist/search/machine.d.ts +65 -0
  107. package/dist/search/machine.js +241 -0
  108. package/dist/search/recent-search-sync.d.ts +32 -0
  109. package/dist/search/recent-search-sync.js +87 -0
  110. package/dist/search/recent-searches.d.ts +22 -0
  111. package/dist/search/recent-searches.js +40 -0
  112. package/dist/search/system-views.d.ts +11 -0
  113. package/dist/search/system-views.js +16 -0
  114. package/dist/search/types.d.ts +51 -0
  115. package/dist/search/types.js +2 -0
  116. package/dist/search/use-search.d.ts +73 -0
  117. package/dist/search/use-search.js +123 -0
  118. package/dist/selection/config.d.ts +34 -0
  119. package/dist/selection/config.js +13 -0
  120. package/dist/selection/labels.d.ts +19 -0
  121. package/dist/selection/labels.js +23 -0
  122. package/dist/selection/machine.d.ts +80 -0
  123. package/dist/selection/machine.js +274 -0
  124. package/dist/selection/snapshot.d.ts +54 -0
  125. package/dist/selection/snapshot.js +45 -0
  126. package/dist/selection/state.d.ts +25 -0
  127. package/dist/selection/state.js +24 -0
  128. package/dist/selection/system-views.d.ts +11 -0
  129. package/dist/selection/system-views.js +22 -0
  130. package/dist/selection/use-selection.d.ts +73 -0
  131. package/dist/selection/use-selection.js +100 -0
  132. package/dist/sheets/machine.d.ts +101 -0
  133. package/dist/sheets/machine.js +213 -0
  134. package/dist/sheets/use-sheet.d.ts +78 -0
  135. package/dist/sheets/use-sheet.js +103 -0
  136. package/dist/victory-native/index.d.ts +37 -0
  137. package/dist/victory-native/index.js +41 -0
  138. package/package.json +94 -4
  139. package/react-native-iap/package.json +4 -0
  140. package/react-native-permissions/package.json +4 -0
  141. package/victory-native/package.json +4 -0
@@ -0,0 +1,101 @@
1
+ /** What a presented sheet hands back to the screen that presented it, once it closes. */
2
+ export type SheetResult<Item, Value> = {
3
+ readonly status: "saved";
4
+ readonly item: Item;
5
+ readonly value: Value;
6
+ } | {
7
+ readonly status: "cancelled";
8
+ readonly item: Item;
9
+ };
10
+ /** The last save failed. The sheet stays open with its changes; the next save clears it. */
11
+ export interface SaveError {
12
+ readonly error: unknown;
13
+ }
14
+ export interface SheetSnapshot<Item> {
15
+ /** The item whose sheet is on screen, or `undefined` when no sheet is open. */
16
+ readonly item: Item | undefined;
17
+ /** Items waiting for the open sheet to close, oldest first. */
18
+ readonly queued: readonly Item[];
19
+ /** A close was asked for while the sheet held unsaved changes; the discard confirmation is showing. */
20
+ readonly isConfirmingDiscard: boolean;
21
+ /** The save is running; nothing can close the sheet until it ends. */
22
+ readonly isSaving: boolean;
23
+ readonly saveError: SaveError | undefined;
24
+ /** Whether the open sheet holds changes that closing would throw away. Reset when the sheet closes. */
25
+ readonly hasUnsavedChanges: boolean;
26
+ /** Whether the system's own ways of closing (swipe, tap outside) are blocked. */
27
+ readonly isDismissDisabled: boolean;
28
+ }
29
+ /** The app's save. Abort the work when `signal` aborts: the component using the sheet unmounted. */
30
+ export type SaveSheet<Value> = (signal: AbortSignal) => Promise<Value>;
31
+ export interface SheetSource<Item, Value, Opener> {
32
+ readonly onResult: (result: SheetResult<Item, Value>) => void;
33
+ readonly keyExtractor: (item: Item) => unknown;
34
+ /** Gives focus back to the element that opened a sheet, once that sheet is off screen. */
35
+ readonly restoreFocus: (opener: Opener) => void;
36
+ }
37
+ /**
38
+ * How long a closed sheet may take to report that it is off screen before the next queued one shows anyway, so a
39
+ * sheet component that never reports it can't hold up the queue for ever.
40
+ */
41
+ export declare const HIDE_TIMEOUT_MS = 1000;
42
+ /**
43
+ * The state machine behind `useSheet`. One sheet is open at a time and later ones queue; a sheet with unsaved
44
+ * changes asks before it closes, and one that is saving can't close. `source` is read when a result is reported, so
45
+ * it can change between renders. `Opener` is whatever the hook gives focus back to.
46
+ */
47
+ export declare class SheetMachine<Item, Value, Opener = unknown> {
48
+ private readonly source;
49
+ private snapshot;
50
+ private readonly listeners;
51
+ private shown;
52
+ private waiting;
53
+ private saving;
54
+ private generation;
55
+ private focusToRestore;
56
+ /** A sheet closed and hasn't reported that it is off screen yet; the next one waits for it. */
57
+ private hiding;
58
+ /**
59
+ * Closes whose sheet component hasn't yet reported it off screen. A report that arrives after the timeout already
60
+ * showed the next sheet answers one of these, so it never closes that next sheet.
61
+ */
62
+ private unreported;
63
+ constructor(source: () => SheetSource<Item, Value, Opener>);
64
+ readonly subscribe: (listener: () => void) => (() => void);
65
+ readonly getSnapshot: () => SheetSnapshot<Item>;
66
+ /** Shows the sheet for `item`, or queues it while another is open; the item shown or queued isn't added again. */
67
+ readonly present: (item: Item, opener?: Opener) => void;
68
+ /**
69
+ * A close request, from the sheet's Cancel or close button, `Modal`'s `onRequestClose` (a swipe, the back button):
70
+ * closes a sheet with no unsaved changes, asks before closing one with changes, and does nothing while saving or
71
+ * asking.
72
+ */
73
+ readonly requestDismiss: () => void;
74
+ /**
75
+ * A bottom-sheet library's sheet closed: after a gesture it allowed, which reports canceled, or after the sheet
76
+ * closed it, which finishes hiding. Either way the sheet is off screen, so the next queued one shows.
77
+ */
78
+ readonly dismissed: () => void;
79
+ /**
80
+ * The closed sheet is off screen (`Modal`'s `onDismiss`): the next queued sheet shows, and focus goes back to the
81
+ * opener unless the platform already returned it (`restoresFocus` false).
82
+ */
83
+ readonly hidden: (restoresFocus: boolean) => void;
84
+ private finishHiding;
85
+ /** Discard Changes, from the confirmation: closes the sheet and reports canceled. */
86
+ readonly discardChanges: () => void;
87
+ /** Keep Editing, from the confirmation, or the confirmation closing: back to the sheet, with its changes. */
88
+ readonly keepEditing: () => void;
89
+ /** Sets whether the open sheet holds changes that closing would throw away, from the sheet's form state. */
90
+ readonly setHasUnsavedChanges: (hasUnsavedChanges: boolean) => void;
91
+ /**
92
+ * Runs the app's save, during which nothing closes the sheet. A save that resolves closes the sheet and reports it
93
+ * as saved; one that rejects keeps the sheet open and sets `saveError`. Does nothing while a save runs.
94
+ */
95
+ readonly save: (work: SaveSheet<Value>) => void;
96
+ /** Aborts a running save and drops its result, when the component using the sheet unmounts. */
97
+ readonly dispose: () => void;
98
+ private finishSave;
99
+ private close;
100
+ private update;
101
+ }
@@ -0,0 +1,213 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SheetMachine = exports.HIDE_TIMEOUT_MS = void 0;
4
+ /**
5
+ * How long a closed sheet may take to report that it is off screen before the next queued one shows anyway, so a
6
+ * sheet component that never reports it can't hold up the queue for ever.
7
+ */
8
+ exports.HIDE_TIMEOUT_MS = 1000;
9
+ const INITIAL = {
10
+ item: undefined,
11
+ queued: [],
12
+ isConfirmingDiscard: false,
13
+ isSaving: false,
14
+ saveError: undefined,
15
+ hasUnsavedChanges: false,
16
+ isDismissDisabled: false,
17
+ };
18
+ /**
19
+ * The state machine behind `useSheet`. One sheet is open at a time and later ones queue; a sheet with unsaved
20
+ * changes asks before it closes, and one that is saving can't close. `source` is read when a result is reported, so
21
+ * it can change between renders. `Opener` is whatever the hook gives focus back to.
22
+ */
23
+ class SheetMachine {
24
+ source;
25
+ snapshot = INITIAL;
26
+ listeners = new Set();
27
+ shown;
28
+ waiting = [];
29
+ saving;
30
+ generation = 0;
31
+ focusToRestore;
32
+ /** A sheet closed and hasn't reported that it is off screen yet; the next one waits for it. */
33
+ hiding;
34
+ /**
35
+ * Closes whose sheet component hasn't yet reported it off screen. A report that arrives after the timeout already
36
+ * showed the next sheet answers one of these, so it never closes that next sheet.
37
+ */
38
+ unreported = 0;
39
+ constructor(source) {
40
+ this.source = source;
41
+ }
42
+ subscribe = (listener) => {
43
+ this.listeners.add(listener);
44
+ return () => this.listeners.delete(listener);
45
+ };
46
+ getSnapshot = () => this.snapshot;
47
+ /** Shows the sheet for `item`, or queues it while another is open; the item shown or queued isn't added again. */
48
+ present = (item, opener) => {
49
+ const entry = { item, opener };
50
+ const { keyExtractor } = this.source();
51
+ const key = keyExtractor(item);
52
+ const isSame = (other) => Object.is(keyExtractor(other.item), key);
53
+ if (this.shown === undefined) {
54
+ this.waiting = this.waiting.filter((other) => !isSame(other));
55
+ if (this.hiding === undefined) {
56
+ this.shown = entry;
57
+ }
58
+ else {
59
+ // A sheet is still leaving the screen: this one goes first, once it has gone.
60
+ this.waiting = [entry, ...this.waiting];
61
+ }
62
+ this.update({});
63
+ return;
64
+ }
65
+ if (isSame(this.shown) || this.waiting.some(isSame))
66
+ return;
67
+ this.waiting = [...this.waiting, entry];
68
+ this.update({});
69
+ };
70
+ /**
71
+ * A close request, from the sheet's Cancel or close button, `Modal`'s `onRequestClose` (a swipe, the back button):
72
+ * closes a sheet with no unsaved changes, asks before closing one with changes, and does nothing while saving or
73
+ * asking.
74
+ */
75
+ requestDismiss = () => {
76
+ const { isSaving, isConfirmingDiscard, hasUnsavedChanges } = this.snapshot;
77
+ if (this.shown === undefined || isSaving || isConfirmingDiscard)
78
+ return;
79
+ if (hasUnsavedChanges) {
80
+ this.update({ isConfirmingDiscard: true });
81
+ }
82
+ else {
83
+ this.close((item) => ({ status: "cancelled", item }));
84
+ }
85
+ };
86
+ /**
87
+ * A bottom-sheet library's sheet closed: after a gesture it allowed, which reports canceled, or after the sheet
88
+ * closed it, which finishes hiding. Either way the sheet is off screen, so the next queued one shows.
89
+ */
90
+ dismissed = () => {
91
+ if (this.unreported > 0) {
92
+ this.unreported -= 1;
93
+ }
94
+ else {
95
+ this.close((closing) => ({ status: "cancelled", item: closing }));
96
+ // The component closed it, so it is already off screen: this is its report.
97
+ this.unreported = Math.max(0, this.unreported - 1);
98
+ }
99
+ this.finishHiding(true);
100
+ };
101
+ /**
102
+ * The closed sheet is off screen (`Modal`'s `onDismiss`): the next queued sheet shows, and focus goes back to the
103
+ * opener unless the platform already returned it (`restoresFocus` false).
104
+ */
105
+ hidden = (restoresFocus) => {
106
+ this.unreported = Math.max(0, this.unreported - 1);
107
+ this.finishHiding(restoresFocus);
108
+ };
109
+ finishHiding(restoresFocus) {
110
+ if (this.hiding === undefined)
111
+ return;
112
+ clearTimeout(this.hiding);
113
+ this.hiding = undefined;
114
+ const opener = this.focusToRestore;
115
+ this.focusToRestore = undefined;
116
+ if (restoresFocus && opener !== undefined)
117
+ this.source().restoreFocus(opener);
118
+ const [next, ...rest] = this.waiting;
119
+ if (this.shown !== undefined || next === undefined)
120
+ return;
121
+ this.shown = next;
122
+ this.waiting = rest;
123
+ this.update({});
124
+ }
125
+ /** Discard Changes, from the confirmation: closes the sheet and reports canceled. */
126
+ discardChanges = () => {
127
+ if (this.snapshot.isConfirmingDiscard)
128
+ this.close((item) => ({ status: "cancelled", item }));
129
+ };
130
+ /** Keep Editing, from the confirmation, or the confirmation closing: back to the sheet, with its changes. */
131
+ keepEditing = () => {
132
+ if (this.snapshot.isConfirmingDiscard)
133
+ this.update({ isConfirmingDiscard: false });
134
+ };
135
+ /** Sets whether the open sheet holds changes that closing would throw away, from the sheet's form state. */
136
+ setHasUnsavedChanges = (hasUnsavedChanges) => {
137
+ if (this.shown !== undefined && this.snapshot.hasUnsavedChanges !== hasUnsavedChanges) {
138
+ this.update({ hasUnsavedChanges });
139
+ }
140
+ };
141
+ /**
142
+ * Runs the app's save, during which nothing closes the sheet. A save that resolves closes the sheet and reports it
143
+ * as saved; one that rejects keeps the sheet open and sets `saveError`. Does nothing while a save runs.
144
+ */
145
+ save = (work) => {
146
+ const { isSaving, isConfirmingDiscard } = this.snapshot;
147
+ if (this.shown === undefined || isSaving || isConfirmingDiscard)
148
+ return;
149
+ const controller = new AbortController();
150
+ this.saving = controller;
151
+ const id = this.generation;
152
+ this.update({ isSaving: true, saveError: undefined });
153
+ let saved;
154
+ try {
155
+ saved = work(controller.signal);
156
+ }
157
+ catch (error) {
158
+ saved = Promise.reject(error);
159
+ }
160
+ saved.then((value) => this.finishSave(id, () => this.close((item) => ({ status: "saved", item, value }))), (error) => this.finishSave(id, () => this.update({ saveError: { error } })));
161
+ };
162
+ /** Aborts a running save and drops its result, when the component using the sheet unmounts. */
163
+ dispose = () => {
164
+ clearTimeout(this.hiding);
165
+ this.hiding = undefined;
166
+ this.unreported = 0;
167
+ this.generation += 1;
168
+ this.saving?.abort();
169
+ this.saving = undefined;
170
+ if (this.snapshot.isSaving)
171
+ this.update({ isSaving: false });
172
+ };
173
+ finishSave(id, finish) {
174
+ if (id !== this.generation || !this.snapshot.isSaving)
175
+ return;
176
+ this.saving = undefined;
177
+ this.update({ isSaving: false });
178
+ finish();
179
+ }
180
+ close(result) {
181
+ const closing = this.shown;
182
+ if (closing === undefined)
183
+ return;
184
+ this.generation += 1;
185
+ this.saving?.abort();
186
+ this.saving = undefined;
187
+ this.focusToRestore = closing.opener;
188
+ this.shown = undefined;
189
+ this.update({
190
+ isSaving: false,
191
+ isConfirmingDiscard: false,
192
+ saveError: undefined,
193
+ hasUnsavedChanges: false,
194
+ });
195
+ // Nothing new shows until this sheet is off screen: iOS's Modal can't present again while it animates away.
196
+ clearTimeout(this.hiding);
197
+ this.unreported += 1;
198
+ this.hiding = setTimeout(() => this.finishHiding(true), exports.HIDE_TIMEOUT_MS);
199
+ this.source().onResult(result(closing.item));
200
+ }
201
+ update(change) {
202
+ const next = { ...this.snapshot, ...change };
203
+ this.snapshot = {
204
+ ...next,
205
+ item: this.shown?.item,
206
+ queued: this.waiting.map((entry) => entry.item),
207
+ isDismissDisabled: next.hasUnsavedChanges || next.isConfirmingDiscard || next.isSaving,
208
+ };
209
+ for (const listener of this.listeners)
210
+ listener();
211
+ }
212
+ }
213
+ exports.SheetMachine = SheetMachine;
@@ -0,0 +1,78 @@
1
+ import { type RefObject } from "react";
2
+ import { type HostInstance } from "react-native";
3
+ import { type SaveSheet, type SheetResult, type SheetSnapshot } from "./machine";
4
+ /** The discard confirmation's text, which the app translates or replaces. */
5
+ export interface SheetLabels {
6
+ readonly discardTitle: string;
7
+ readonly discard: string;
8
+ readonly keepEditing: string;
9
+ }
10
+ export declare const defaultSheetLabels: SheetLabels;
11
+ export interface SheetOptions<Item, Value> {
12
+ /** Called once for each presented item, when its sheet closes. */
13
+ readonly onResult?: (result: SheetResult<Item, Value>) => void;
14
+ /** The identity of an item: presenting one with the key of the item shown or queued does nothing. */
15
+ readonly keyExtractor?: (item: Item) => unknown;
16
+ readonly labels?: Partial<SheetLabels>;
17
+ /**
18
+ * Shows the discard confirmation as a system `Alert` (the default). Pass `false` to draw your own from
19
+ * `isConfirmingDiscard`, calling `discardChanges` and `keepEditing`.
20
+ */
21
+ readonly systemConfirmation?: boolean;
22
+ }
23
+ export interface PresentOptions {
24
+ /** The element that opened the sheet, which gets screen-reader focus back when the sheet closes. */
25
+ readonly opener?: RefObject<HostInstance | null>;
26
+ }
27
+ /** What a sheet component needs from the sheet, for an adapter to map onto its own props. */
28
+ export interface SheetBinding {
29
+ readonly isOpen: boolean;
30
+ /** Whether a swipe down or tap outside must not close the sheet: unsaved changes, the confirmation, or a save. */
31
+ readonly isDismissDisabled: boolean;
32
+ /** A close request, for the component's back button, backdrop press or blocked swipe. */
33
+ readonly requestDismiss: () => void;
34
+ /**
35
+ * The component's sheet closed (a bottom-sheet library's `onClose`): after a gesture it allowed, or after the sheet
36
+ * closed it. The next queued sheet waits for this, so it never shows while the last one is still animating away.
37
+ */
38
+ readonly dismissed: () => void;
39
+ /**
40
+ * The closed sheet is off screen, for a component that reports that apart from its gestures (`Modal`'s
41
+ * `onDismiss`). Pass `false` when the platform has already given focus back to the opener.
42
+ */
43
+ readonly hidden: (restoresFocus: boolean) => void;
44
+ }
45
+ /** Maps a sheet onto a sheet component's props, such as a bottom-sheet library's. */
46
+ export type SheetAdapter<Props> = (binding: SheetBinding) => Props;
47
+ /** The props `Modal` needs, from `bind(modalSheet)`: a swipe it allows and the back button go through the sheet. */
48
+ export interface ModalSheetProps {
49
+ readonly visible: boolean;
50
+ readonly onRequestClose: () => void;
51
+ readonly onDismiss: () => void;
52
+ readonly allowSwipeDismissal: boolean;
53
+ }
54
+ /**
55
+ * `Modal`'s adapter. iOS reports a swipe it allowed, and one `allowSwipeDismissal` blocked, through
56
+ * `onRequestClose`, as Android does the back button; each is a close request. iOS's `onDismiss` says the modal has
57
+ * finished animating away, and iOS has given focus back by then; Android has no `onDismiss`, so `useSheet` shows the
58
+ * next sheet once a render with `visible` false has committed, and gives focus back itself.
59
+ */
60
+ export declare const modalSheet: SheetAdapter<ModalSheetProps>;
61
+ export interface SheetController<Item, Value> extends SheetSnapshot<Item> {
62
+ readonly present: (item: Item, options?: PresentOptions) => void;
63
+ readonly requestDismiss: () => void;
64
+ readonly dismissed: () => void;
65
+ readonly discardChanges: () => void;
66
+ readonly keepEditing: () => void;
67
+ readonly setHasUnsavedChanges: (hasUnsavedChanges: boolean) => void;
68
+ readonly save: (work: SaveSheet<Value>) => void;
69
+ /** The props for a sheet component: `<Modal {...sheet.bind(modalSheet)}>`, or a bottom-sheet library's adapter. */
70
+ readonly bind: <Props>(adapter: SheetAdapter<Props>) => Props;
71
+ }
72
+ /**
73
+ * A sheet that edits an item and hands a result back: `present` an item, render it with `bind(modalSheet)` (or an
74
+ * adapter for a bottom-sheet library), and call `requestDismiss` from its Cancel button and `save` from its Save
75
+ * button. Android's back button is a close request while a sheet is open; a save still running at unmount is
76
+ * aborted and reports nothing.
77
+ */
78
+ export declare function useSheet<Item, Value = void>(options?: SheetOptions<Item, Value>): SheetController<Item, Value>;
@@ -0,0 +1,103 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.modalSheet = exports.defaultSheetLabels = void 0;
4
+ exports.useSheet = useSheet;
5
+ const react_1 = require("react");
6
+ const react_native_1 = require("react-native");
7
+ const machine_1 = require("./machine");
8
+ exports.defaultSheetLabels = {
9
+ discardTitle: "Discard Changes?",
10
+ discard: "Discard Changes",
11
+ keepEditing: "Keep Editing",
12
+ };
13
+ /**
14
+ * `Modal`'s adapter. iOS reports a swipe it allowed, and one `allowSwipeDismissal` blocked, through
15
+ * `onRequestClose`, as Android does the back button; each is a close request. iOS's `onDismiss` says the modal has
16
+ * finished animating away, and iOS has given focus back by then; Android has no `onDismiss`, so `useSheet` shows the
17
+ * next sheet once a render with `visible` false has committed, and gives focus back itself.
18
+ */
19
+ const modalSheet = (binding) => ({
20
+ visible: binding.isOpen,
21
+ onRequestClose: binding.requestDismiss,
22
+ onDismiss: () => binding.hidden(false),
23
+ allowSwipeDismissal: !binding.isDismissDisabled,
24
+ });
25
+ exports.modalSheet = modalSheet;
26
+ /**
27
+ * A sheet that edits an item and hands a result back: `present` an item, render it with `bind(modalSheet)` (or an
28
+ * adapter for a bottom-sheet library), and call `requestDismiss` from its Cancel button and `save` from its Save
29
+ * button. Android's back button is a close request while a sheet is open; a save still running at unmount is
30
+ * aborted and reports nothing.
31
+ */
32
+ function useSheet(options = {}) {
33
+ const source = {
34
+ onResult: options.onResult ?? (() => { }),
35
+ keyExtractor: options.keyExtractor ?? ((item) => item),
36
+ restoreFocus: (opener) => {
37
+ if (opener.current != null)
38
+ react_native_1.AccessibilityInfo.sendAccessibilityEvent(opener.current, "focus");
39
+ },
40
+ };
41
+ const labels = { ...exports.defaultSheetLabels, ...options.labels };
42
+ const latest = (0, react_1.useRef)(source);
43
+ const latestLabels = (0, react_1.useRef)(labels);
44
+ (0, react_1.useLayoutEffect)(() => {
45
+ latest.current = source;
46
+ latestLabels.current = labels;
47
+ });
48
+ const [machine] = (0, react_1.useState)(() => new machine_1.SheetMachine(() => latest.current));
49
+ const snapshot = (0, react_1.useSyncExternalStore)(machine.subscribe, machine.getSnapshot);
50
+ const isOpen = snapshot.item !== undefined;
51
+ (0, react_1.useEffect)(() => machine.dispose, [machine]);
52
+ (0, react_1.useEffect)(() => {
53
+ if (!isOpen)
54
+ return;
55
+ // A bottom-sheet library draws no window of its own, so back reaches the app; `Modal` takes it before this.
56
+ const subscription = react_native_1.BackHandler.addEventListener("hardwareBackPress", () => {
57
+ machine.requestDismiss();
58
+ return true;
59
+ });
60
+ return () => subscription.remove();
61
+ }, [isOpen, machine]);
62
+ // Set when the sheet is bound to `Modal`, whose Android version reports no dismissal.
63
+ const isModal = (0, react_1.useRef)(false);
64
+ (0, react_1.useEffect)(() => {
65
+ // Android's Modal is gone once a render without it has committed. A bottom-sheet library reports it itself.
66
+ if (!isOpen && isModal.current && react_native_1.Platform.OS === "android")
67
+ machine.hidden(true);
68
+ }, [isOpen, machine]);
69
+ const systemConfirmation = options.systemConfirmation ?? true;
70
+ (0, react_1.useEffect)(() => {
71
+ if (!systemConfirmation || !snapshot.isConfirmingDiscard)
72
+ return;
73
+ const text = latestLabels.current;
74
+ react_native_1.Alert.alert(text.discardTitle, undefined, [
75
+ { text: text.keepEditing, style: "cancel", onPress: machine.keepEditing },
76
+ { text: text.discard, style: "destructive", onPress: machine.discardChanges },
77
+ ], { cancelable: true, onDismiss: machine.keepEditing });
78
+ }, [systemConfirmation, snapshot.isConfirmingDiscard, machine]);
79
+ return (0, react_1.useMemo)(() => {
80
+ const binding = {
81
+ isOpen: snapshot.item !== undefined,
82
+ isDismissDisabled: snapshot.isDismissDisabled,
83
+ requestDismiss: machine.requestDismiss,
84
+ dismissed: machine.dismissed,
85
+ hidden: machine.hidden,
86
+ };
87
+ return {
88
+ ...snapshot,
89
+ present: (item, presentOptions) => machine.present(item, presentOptions?.opener),
90
+ requestDismiss: machine.requestDismiss,
91
+ dismissed: binding.dismissed,
92
+ discardChanges: machine.discardChanges,
93
+ keepEditing: machine.keepEditing,
94
+ setHasUnsavedChanges: machine.setHasUnsavedChanges,
95
+ save: machine.save,
96
+ bind: (adapter) => {
97
+ if (adapter === exports.modalSheet)
98
+ isModal.current = true;
99
+ return adapter(binding);
100
+ },
101
+ };
102
+ }, [snapshot, machine]);
103
+ }
@@ -0,0 +1,37 @@
1
+ import type { ChartInteractionController } from "../charts/use-chart-interaction";
2
+ /** A Reanimated shared value: read on the UI thread. */
3
+ export interface SharedValueLike<T> {
4
+ readonly value: T;
5
+ }
6
+ /** The parts of Victory Native's `useChartPressState().state` the scrub reads. */
7
+ export interface VictoryPressState {
8
+ readonly isActive: SharedValueLike<boolean>;
9
+ readonly x: {
10
+ readonly value: SharedValueLike<number>;
11
+ };
12
+ }
13
+ /**
14
+ * The parts of Reanimated the scrub uses, with the call back to JavaScript from Reanimated 4
15
+ * (`scheduleOnRN` from react-native-worklets) or from Reanimated 3 (`runOnJS`).
16
+ */
17
+ export type Reanimated = {
18
+ readonly useAnimatedReaction: <T>(prepare: () => T, react: (current: T, previous: T | null) => void, dependencies?: unknown[]) => void;
19
+ } & ({
20
+ readonly scheduleOnRN: <Args extends unknown[]>(action: (...args: Args) => void, ...args: Args) => void;
21
+ } | {
22
+ readonly runOnJS: <Args extends unknown[]>(action: (...args: Args) => void) => (...args: Args) => void;
23
+ });
24
+ /**
25
+ * Scrubs `chart` from Victory Native's press state, with the Reanimated functions the app passes (nothing is imported):
26
+ * the pressed x snaps to the nearest point and ticks, and the press ending ends the scrub. Inside a `ScrollView`,
27
+ * give the chart `chartPressConfig={{ pan: { activateAfterLongPress: 300 } }}` so a swipe still scrolls.
28
+ *
29
+ * ```tsx
30
+ * import { useAnimatedReaction } from "react-native-reanimated";
31
+ * import { scheduleOnRN } from "react-native-worklets"; // Reanimated 4; on Reanimated 3, pass `runOnJS`
32
+ * import { useVictoryScrub } from "@codual/react-native-ui/victory-native";
33
+ * const { state } = useChartPressState({ x: 0, y: { y: 0 } });
34
+ * useVictoryScrub(chart, state, { useAnimatedReaction, scheduleOnRN });
35
+ * ```
36
+ */
37
+ export declare function useVictoryScrub(chart: Pick<ChartInteractionController<unknown>, "scrub" | "endScrub">, press: VictoryPressState, reanimated: Reanimated): void;
@@ -0,0 +1,41 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.useVictoryScrub = useVictoryScrub;
4
+ /**
5
+ * Scrubs `chart` from Victory Native's press state, with the Reanimated functions the app passes (nothing is imported):
6
+ * the pressed x snaps to the nearest point and ticks, and the press ending ends the scrub. Inside a `ScrollView`,
7
+ * give the chart `chartPressConfig={{ pan: { activateAfterLongPress: 300 } }}` so a swipe still scrolls.
8
+ *
9
+ * ```tsx
10
+ * import { useAnimatedReaction } from "react-native-reanimated";
11
+ * import { scheduleOnRN } from "react-native-worklets"; // Reanimated 4; on Reanimated 3, pass `runOnJS`
12
+ * import { useVictoryScrub } from "@codual/react-native-ui/victory-native";
13
+ * const { state } = useChartPressState({ x: 0, y: { y: 0 } });
14
+ * useVictoryScrub(chart, state, { useAnimatedReaction, scheduleOnRN });
15
+ * ```
16
+ */
17
+ function useVictoryScrub(chart, press, reanimated) {
18
+ const { scrub, endScrub } = chart;
19
+ const { useAnimatedReaction } = reanimated;
20
+ const scheduleOnRN = "scheduleOnRN" in reanimated ? reanimated.scheduleOnRN : undefined;
21
+ const runOnJS = "runOnJS" in reanimated ? reanimated.runOnJS : undefined;
22
+ useAnimatedReaction(() => {
23
+ "worklet";
24
+ return press.isActive.value ? press.x.value.value : undefined;
25
+ }, (x, previous) => {
26
+ "worklet";
27
+ // The first run has no previous value (null): an idle press then is no release.
28
+ if (x === (previous ?? undefined))
29
+ return;
30
+ if (x === undefined) {
31
+ if (scheduleOnRN)
32
+ scheduleOnRN(endScrub);
33
+ else
34
+ runOnJS?.(endScrub)();
35
+ }
36
+ else if (scheduleOnRN)
37
+ scheduleOnRN(scrub, x);
38
+ else
39
+ runOnJS?.(scrub)(x);
40
+ }, [scrub, endScrub]);
41
+ }
package/package.json CHANGED
@@ -1,6 +1,96 @@
1
1
  {
2
2
  "name": "@codual/react-native-ui",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.0",
4
+ "description": "Headless behaviors for React Native apps, the same as Codual UI's SwiftUI package, built for coding agents.",
5
+ "repository": {
6
+ "type": "git",
7
+ "url": "git+https://github.com/codual-ai/ui.git",
8
+ "directory": "react-native"
9
+ },
10
+ "homepage": "https://github.com/codual-ai/ui#readme",
11
+ "bugs": "https://github.com/codual-ai/ui/issues",
12
+ "license": "Apache-2.0",
13
+ "type": "commonjs",
14
+ "main": "dist/index.js",
15
+ "types": "dist/index.d.ts",
16
+ "typesVersions": {
17
+ "*": {
18
+ "react-native-iap": [
19
+ "dist/react-native-iap/index.d.ts"
20
+ ],
21
+ "react-native-permissions": [
22
+ "dist/react-native-permissions/index.d.ts"
23
+ ],
24
+ "victory-native": [
25
+ "dist/victory-native/index.d.ts"
26
+ ]
27
+ }
28
+ },
29
+ "exports": {
30
+ ".": {
31
+ "types": "./dist/index.d.ts",
32
+ "default": "./dist/index.js"
33
+ },
34
+ "./react-native-iap": {
35
+ "types": "./dist/react-native-iap/index.d.ts",
36
+ "default": "./dist/react-native-iap/index.js"
37
+ },
38
+ "./react-native-permissions": {
39
+ "types": "./dist/react-native-permissions/index.d.ts",
40
+ "default": "./dist/react-native-permissions/index.js"
41
+ },
42
+ "./victory-native": {
43
+ "types": "./dist/victory-native/index.d.ts",
44
+ "default": "./dist/victory-native/index.js"
45
+ }
46
+ },
47
+ "files": [
48
+ "dist",
49
+ "react-native-iap",
50
+ "react-native-permissions",
51
+ "victory-native",
52
+ "NOTICE"
53
+ ],
54
+ "scripts": {
55
+ "build": "node -e \"fs.rmSync('dist',{recursive:true,force:true})\" && tsc -p tsconfig.build.json",
56
+ "typecheck": "tsc -p tsconfig.json --noEmit",
57
+ "test": "jest",
58
+ "lint": "biome check --error-on-warnings .",
59
+ "format": "biome format --write .",
60
+ "check": "pnpm typecheck && pnpm lint && pnpm test && pnpm build",
61
+ "prepack": "pnpm build && node -e \"for (const f of ['LICENSE','NOTICE']) fs.copyFileSync('../' + f, f)\""
62
+ },
63
+ "engines": {
64
+ "node": ">=22.18"
65
+ },
66
+ "peerDependencies": {
67
+ "react": ">=19",
68
+ "react-native": ">=0.78",
69
+ "react-native-iap": ">=17.4",
70
+ "react-native-permissions": ">=5"
71
+ },
72
+ "peerDependenciesMeta": {
73
+ "react-native-iap": {
74
+ "optional": true
75
+ },
76
+ "react-native-permissions": {
77
+ "optional": true
78
+ }
79
+ },
80
+ "devDependencies": {
81
+ "@biomejs/biome": "2.6.4",
82
+ "@react-native/babel-preset": "0.88.1",
83
+ "@react-native/jest-preset": "0.88.1",
84
+ "@testing-library/react-native": "14.1.0",
85
+ "@types/jest": "29.5.14",
86
+ "@types/react": "19.3.0",
87
+ "jest": "29.7.0",
88
+ "react": "19.3.1",
89
+ "react-native": "0.88.1",
90
+ "react-native-iap": "17.4.0",
91
+ "react-native-permissions": "5.6.3",
92
+ "test-renderer": "1.3.1",
93
+ "typescript": "5.9.3"
94
+ },
95
+ "packageManager": "pnpm@10.15.0"
96
+ }