@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
package/LICENSE ADDED
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright [yyyy] [name of copyright owner]
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
package/NOTICE ADDED
@@ -0,0 +1,2 @@
1
+ Codual UI
2
+ Copyright 2026 Hyperion Arc Inc.
package/README.md CHANGED
@@ -1,3 +1,19 @@
1
- # Temporary Holding Version
1
+ # @codual/react-native-ui
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Headless behaviors for React Native apps: screen states, lists (including chat-style reversed lists), forms,
4
+ purchases, selection, sheets and dismissal, search, permission prompts and chart interactions. The same behaviors,
5
+ with the same tested edge cases, as Codual UI's SwiftUI and Jetpack Compose packages. Your app keeps its own views.
6
+
7
+ ```sh
8
+ npm install @codual/react-native-ui
9
+ ```
10
+
11
+ Optional adapters, each with its library as an optional peer dependency:
12
+
13
+ - `@codual/react-native-ui/react-native-iap` for purchases with react-native-iap
14
+ - `@codual/react-native-ui/react-native-permissions` for permission prompts on iOS with react-native-permissions
15
+ - `@codual/react-native-ui/victory-native` for chart touch with Victory Native
16
+
17
+ Documentation, the specs and the skill for coding agents: [github.com/codual-ai/ui](https://github.com/codual-ai/ui).
18
+
19
+ Apache License 2.0. Copyright 2026 Hyperion Arc Inc.
@@ -0,0 +1,2 @@
1
+ /** Speaks `text` politely: queued on iOS, after whatever VoiceOver is saying; Android's TalkBack queues anyway. */
2
+ export declare function announce(text: string): void;
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.announce = announce;
4
+ const react_native_1 = require("react-native");
5
+ /** Speaks `text` politely: queued on iOS, after whatever VoiceOver is saying; Android's TalkBack queues anyway. */
6
+ function announce(text) {
7
+ react_native_1.AccessibilityInfo.announceForAccessibilityWithOptions(text, { queue: true });
8
+ }
@@ -0,0 +1,32 @@
1
+ /** A point of a series. `x` is a number: a time as epoch milliseconds, say, as Victory Native takes. */
2
+ export interface ChartPoint {
3
+ readonly x: number;
4
+ readonly y: number;
5
+ }
6
+ /** One line or set of bars. `id` matches a series with its previous-period series; `name` is read out. */
7
+ export interface ChartSeries {
8
+ readonly id: string;
9
+ readonly name: string;
10
+ readonly points: readonly ChartPoint[];
11
+ }
12
+ /**
13
+ * What a range's load returns: the series and, when the previous period is compared, that period's series with
14
+ * the same IDs, aligned to this period's x values by the app.
15
+ */
16
+ export interface ChartData {
17
+ readonly series: readonly ChartSeries[];
18
+ readonly previousPeriod?: readonly ChartSeries[];
19
+ }
20
+ /** What a chart's load is for: the selected range, and whether the previous period is compared. */
21
+ export interface ChartQuery<Range> {
22
+ readonly range: Range;
23
+ readonly comparesToPreviousPeriod: boolean;
24
+ }
25
+ /** A load's data with its series in x order and without gaps, and every x a selection can snap to. */
26
+ export interface PlottedData {
27
+ readonly series: readonly ChartSeries[];
28
+ readonly previousPeriod: readonly ChartSeries[];
29
+ /** Every x any series has a point at, in order, once each. */
30
+ readonly xValues: readonly number[];
31
+ }
32
+ export declare function plot(data: ChartData): PlottedData;
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.plot = plot;
4
+ /** Its points in x order, without gaps (values that aren't finite). */
5
+ function plotted(series) {
6
+ const points = series.points.filter((point) => Number.isFinite(point.y)).sort((a, b) => a.x - b.x);
7
+ return { ...series, points };
8
+ }
9
+ function plot(data) {
10
+ const series = data.series.map(plotted);
11
+ const xValues = [...new Set(series.flatMap((each) => each.points.map((point) => point.x)))].sort((a, b) => a - b);
12
+ return { series, previousPeriod: (data.previousPeriod ?? []).map(plotted), xValues };
13
+ }
@@ -0,0 +1,54 @@
1
+ import type { ChartSelection } from "./points";
2
+ import type { ChartSeriesSummary } from "./summary";
3
+ export interface ChartChangeText {
4
+ readonly name: string;
5
+ readonly first: string;
6
+ readonly firstX: string;
7
+ readonly last: string;
8
+ readonly lastX: string;
9
+ readonly direction: "up" | "down" | "unchanged";
10
+ /** The size of the change, a percentage when there is one; unused when unchanged. */
11
+ readonly amount: string;
12
+ }
13
+ /** The sentences a screen reader hears; pass your own to translate them. */
14
+ export interface ChartLabels {
15
+ readonly title: (title: string) => string;
16
+ readonly change: (text: ChartChangeText) => string;
17
+ readonly lowAndHigh: (text: {
18
+ readonly low: string;
19
+ readonly lowX: string;
20
+ readonly high: string;
21
+ readonly highX: string;
22
+ }) => string;
23
+ readonly comparison: (text: {
24
+ readonly direction: "higher" | "lower" | "same";
25
+ readonly amount: string;
26
+ }) => string;
27
+ readonly updating: string;
28
+ /** One series' point: "3 Nov, 1.20". */
29
+ readonly point: (x: string, value: string) => string;
30
+ /** Several series' values at one x: "3 Nov: Price 1.20, Volume 300". */
31
+ readonly points: (x: string, values: readonly {
32
+ readonly name: string;
33
+ readonly value: string;
34
+ }[]) => string;
35
+ }
36
+ export declare const defaultChartLabels: ChartLabels;
37
+ /** How a chart writes its values for people: the app's formatters, or numbers in the person's locale. */
38
+ export interface ChartFormat {
39
+ readonly formatX: (x: number) => string;
40
+ readonly formatY: (y: number) => string;
41
+ readonly percent: (fraction: number) => string;
42
+ }
43
+ /** What the x values are: numbers, or times as epoch milliseconds, read out as dates and times. */
44
+ export type ChartXKind = "number" | "time";
45
+ export declare function chartFormat(options: {
46
+ readonly formatX?: ((x: number) => string) | undefined;
47
+ readonly formatY?: ((y: number) => string) | undefined;
48
+ readonly xKind?: ChartXKind | undefined;
49
+ readonly locale?: string | undefined;
50
+ }): ChartFormat;
51
+ /** The title, then each series' change, low and high, and comparison, as a screen reader hears it. */
52
+ export declare function spokenSummary(title: string, seriesSummaries: readonly ChartSeriesSummary[], isStale: boolean, labels: ChartLabels, format: ChartFormat): string;
53
+ /** The selected point's text: its x and each series' value, with the series' name when there are several. */
54
+ export declare function pointText(selection: ChartSelection, seriesCount: number, labels: ChartLabels, format: ChartFormat): string;
@@ -0,0 +1,91 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.defaultChartLabels = void 0;
4
+ exports.chartFormat = chartFormat;
5
+ exports.spokenSummary = spokenSummary;
6
+ exports.pointText = pointText;
7
+ exports.defaultChartLabels = {
8
+ title: (title) => `${title}.`,
9
+ change: ({ name, first, firstX, last, lastX, direction, amount }) => {
10
+ const from = `${name}: from ${first} (${firstX}) to ${last} (${lastX})`;
11
+ return direction === "unchanged" ? `${from}, unchanged.` : `${from}, ${direction} ${amount}.`;
12
+ },
13
+ lowAndHigh: ({ low, lowX, high, highX }) => `Low ${low} (${lowX}), high ${high} (${highX}).`,
14
+ comparison: ({ direction, amount }) => direction === "same"
15
+ ? "The same as the previous period."
16
+ : `${amount} ${direction} than the previous period.`,
17
+ updating: "Updating.",
18
+ point: (x, value) => `${x}, ${value}`,
19
+ points: (x, values) => `${x}: ${values.map(({ name, value }) => `${name} ${value}`).join(", ")}`,
20
+ };
21
+ function chartFormat(options) {
22
+ const numbers = new Intl.NumberFormat(options.locale);
23
+ const percents = new Intl.NumberFormat(options.locale, { style: "percent", maximumFractionDigits: 1 });
24
+ const times = new Intl.DateTimeFormat(options.locale, { dateStyle: "medium", timeStyle: "short" });
25
+ const defaultX = options.xKind === "time" ? (x) => times.format(x) : (x) => numbers.format(x);
26
+ return {
27
+ formatX: options.formatX ?? defaultX,
28
+ formatY: options.formatY ?? ((y) => numbers.format(y)),
29
+ percent: (fraction) => percents.format(fraction),
30
+ };
31
+ }
32
+ /** The size of a change, without its sign: a percentage when there is one, or else the amount. */
33
+ function amount(change, relative, format) {
34
+ return relative === undefined ? format.formatY(Math.abs(change)) : format.percent(Math.abs(relative));
35
+ }
36
+ function direction(change, up, down, same) {
37
+ if (change > 0)
38
+ return up;
39
+ return change < 0 ? down : same;
40
+ }
41
+ function seriesSentences(summary, labels, format) {
42
+ const { formatX, formatY } = format;
43
+ const sentences = [
44
+ labels.change({
45
+ name: summary.name,
46
+ first: formatY(summary.first.y),
47
+ firstX: formatX(summary.first.x),
48
+ last: formatY(summary.last.y),
49
+ lastX: formatX(summary.last.x),
50
+ direction: direction(summary.change, "up", "down", "unchanged"),
51
+ amount: amount(summary.change, summary.relativeChange, format),
52
+ }),
53
+ labels.lowAndHigh({
54
+ low: formatY(summary.low.y),
55
+ lowX: formatX(summary.low.x),
56
+ high: formatY(summary.high.y),
57
+ highX: formatX(summary.high.x),
58
+ }),
59
+ ];
60
+ if (summary.comparison !== undefined)
61
+ sentences.push(comparisonSentence(summary.comparison, labels, format));
62
+ return sentences;
63
+ }
64
+ function comparisonSentence(comparison, labels, format) {
65
+ return labels.comparison({
66
+ direction: direction(comparison.change, "higher", "lower", "same"),
67
+ amount: amount(comparison.change, comparison.relativeChange, format),
68
+ });
69
+ }
70
+ /** The title, then each series' change, low and high, and comparison, as a screen reader hears it. */
71
+ function spokenSummary(title, seriesSummaries, isStale, labels, format) {
72
+ const sentences = [
73
+ labels.title(title),
74
+ ...seriesSummaries.flatMap((each) => seriesSentences(each, labels, format)),
75
+ ];
76
+ if (isStale)
77
+ sentences.push(labels.updating);
78
+ return sentences.join(" ");
79
+ }
80
+ /** The selected point's text: its x and each series' value, with the series' name when there are several. */
81
+ function pointText(selection, seriesCount, labels, format) {
82
+ const [only] = selection.points;
83
+ if (seriesCount === 1 && only !== undefined) {
84
+ return labels.point(format.formatX(only.point.x), format.formatY(only.point.y));
85
+ }
86
+ const values = selection.points.map((each) => ({
87
+ name: each.seriesName,
88
+ value: format.formatY(each.point.y),
89
+ }));
90
+ return labels.points(format.formatX(selection.x), values);
91
+ }
@@ -0,0 +1,62 @@
1
+ import { type ScreenStatesSnapshot } from "../screen-states/machine";
2
+ import { type ChartData, type ChartQuery, type PlottedData } from "./data";
3
+ import { type ChartSelection } from "./points";
4
+ /** Loads the series for a query. Abort the work when `signal` aborts: the range changed or the chart went away. */
5
+ export type LoadChart<Range> = (query: ChartQuery<Range>, signal: AbortSignal) => Promise<ChartData>;
6
+ export interface ChartInteractionSource<Range> {
7
+ readonly load: LoadChart<Range>;
8
+ readonly isOffline: (error: unknown) => boolean;
9
+ readonly haptic: (() => void) | undefined;
10
+ readonly keepsSelectionOnRelease: boolean;
11
+ }
12
+ export interface ChartInteractionSnapshot<Range> extends ScreenStatesSnapshot<PlottedData> {
13
+ readonly query: ChartQuery<Range>;
14
+ /** The series shown were loaded for another range or compare choice, whose load is running. */
15
+ readonly isStale: boolean;
16
+ /** The point being touched, or stepped to by a screen reader. */
17
+ readonly selection: ChartSelection | undefined;
18
+ }
19
+ /**
20
+ * The state machine behind `useChartInteraction`. The selected query's series are a screen state; a new query
21
+ * aborts the running load and keeps the series on screen, stale, until its answer. `source` is read when a load
22
+ * starts and on every touch, so it can change between renders.
23
+ */
24
+ export declare class ChartInteractionMachine<Range> {
25
+ private readonly source;
26
+ private readonly screen;
27
+ private readonly listeners;
28
+ private query;
29
+ private selected;
30
+ private answers;
31
+ private derived;
32
+ constructor(source: () => ChartInteractionSource<Range>, initialQuery: ChartQuery<Range>);
33
+ readonly subscribe: (listener: () => void) => (() => void);
34
+ readonly getSnapshot: () => ChartInteractionSnapshot<Range>;
35
+ /** Loads the selected query, unless its series are already on screen or loading. */
36
+ readonly appear: () => void;
37
+ readonly disappear: () => void;
38
+ readonly retry: () => void;
39
+ readonly refresh: () => Promise<void>;
40
+ readonly dismissRefreshError: () => void;
41
+ /** Switches range: the series on screen stay, stale, until the new range's answer; older answers are dropped. */
42
+ readonly select: (range: Range) => void;
43
+ /** Turns the previous-period comparison on or off, loading again as a range switch does. */
44
+ readonly compareToPreviousPeriod: (isOn: boolean) => void;
45
+ /** Selects the point nearest `x`, ticking when the point changes. Does nothing unless series are shown. */
46
+ readonly scrub: (x: number) => void;
47
+ /** The touch ended: clears the selection unless it stays on release. */
48
+ readonly endScrub: () => void;
49
+ readonly clearSelection: () => void;
50
+ /** A screen reader's next: the point after the selected one, or the first. No tick. */
51
+ readonly selectNextPoint: () => void;
52
+ /** A screen reader's previous: the point before the selected one, or the last. No tick. */
53
+ readonly selectPreviousPoint: () => void;
54
+ private step;
55
+ private change;
56
+ private loadContent;
57
+ private shownContent;
58
+ private isStale;
59
+ private selection;
60
+ private setSelected;
61
+ private derive;
62
+ }
@@ -0,0 +1,144 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ChartInteractionMachine = void 0;
4
+ const machine_1 = require("../screen-states/machine");
5
+ const data_1 = require("./data");
6
+ const points_1 = require("./points");
7
+ /**
8
+ * The state machine behind `useChartInteraction`. The selected query's series are a screen state; a new query
9
+ * aborts the running load and keeps the series on screen, stale, until its answer. `source` is read when a load
10
+ * starts and on every touch, so it can change between renders.
11
+ */
12
+ class ChartInteractionMachine {
13
+ source;
14
+ screen;
15
+ listeners = new Set();
16
+ query;
17
+ selected;
18
+ answers = 0;
19
+ derived;
20
+ constructor(source, initialQuery) {
21
+ this.source = source;
22
+ this.query = initialQuery;
23
+ this.screen = new machine_1.ScreenStatesMachine(() => ({
24
+ load: (signal) => this.loadContent(signal),
25
+ isEmpty: (content) => content.data.xValues.length === 0,
26
+ isOffline: this.source().isOffline,
27
+ }));
28
+ }
29
+ subscribe = (listener) => {
30
+ this.listeners.add(listener);
31
+ const unsubscribe = this.screen.subscribe(listener);
32
+ return () => {
33
+ this.listeners.delete(listener);
34
+ unsubscribe();
35
+ };
36
+ };
37
+ getSnapshot = () => {
38
+ const from = this.screen.getSnapshot();
39
+ if (this.derived?.from !== from)
40
+ this.derived = { from, to: this.derive(from) };
41
+ return this.derived.to;
42
+ };
43
+ /** Loads the selected query, unless its series are already on screen or loading. */
44
+ appear = () => {
45
+ if (this.isStale() && !this.screen.isLoadRunning)
46
+ this.screen.reload();
47
+ else
48
+ this.screen.appear();
49
+ };
50
+ disappear = () => this.screen.disappear();
51
+ retry = () => this.screen.retry();
52
+ refresh = () => this.screen.refresh();
53
+ dismissRefreshError = () => this.screen.dismissRefreshError();
54
+ /** Switches range: the series on screen stay, stale, until the new range's answer; older answers are dropped. */
55
+ select = (range) => this.change({ ...this.query, range });
56
+ /** Turns the previous-period comparison on or off, loading again as a range switch does. */
57
+ compareToPreviousPeriod = (isOn) => this.change({ ...this.query, comparesToPreviousPeriod: isOn });
58
+ /** Selects the point nearest `x`, ticking when the point changes. Does nothing unless series are shown. */
59
+ scrub = (x) => {
60
+ const content = this.shownContent();
61
+ const next = content === undefined ? undefined : (0, points_1.selectionNearest)(content.data, x);
62
+ if (content === undefined || next === undefined)
63
+ return;
64
+ if (next.index !== this.selection()?.index)
65
+ this.source().haptic?.();
66
+ this.setSelected({ selection: next, contentId: content.id });
67
+ };
68
+ /** The touch ended: clears the selection unless it stays on release. */
69
+ endScrub = () => {
70
+ if (!this.source().keepsSelectionOnRelease)
71
+ this.setSelected(undefined);
72
+ };
73
+ clearSelection = () => this.setSelected(undefined);
74
+ /** A screen reader's next: the point after the selected one, or the first. No tick. */
75
+ selectNextPoint = () => this.step((index) => (index === undefined ? 0 : index + 1));
76
+ /** A screen reader's previous: the point before the selected one, or the last. No tick. */
77
+ selectPreviousPoint = () => this.step((index) => (index === undefined ? Number.MAX_SAFE_INTEGER : index - 1));
78
+ step(next) {
79
+ const content = this.shownContent();
80
+ if (content === undefined)
81
+ return;
82
+ const last = content.data.xValues.length - 1;
83
+ const target = Math.min(Math.max(next(this.selection()?.index), 0), last);
84
+ const selection = (0, points_1.selectionAt)(content.data, target);
85
+ if (selection !== undefined)
86
+ this.setSelected({ selection, contentId: content.id });
87
+ }
88
+ change(query) {
89
+ if (sameQuery(query, this.query))
90
+ return;
91
+ this.query = query;
92
+ this.selected = undefined;
93
+ this.derived = undefined;
94
+ const shown = this.shownContent();
95
+ if (shown === undefined || !sameQuery(shown.query, query)) {
96
+ this.screen.reload();
97
+ return;
98
+ }
99
+ // Back to the series still on screen: the switch away is aborted, and they load again as a refresh, so a
100
+ // failure keeps them.
101
+ this.screen.disappear();
102
+ void this.screen.refresh();
103
+ }
104
+ async loadContent(signal) {
105
+ const query = this.query;
106
+ const loaded = await this.source().load(query, signal);
107
+ // A previous period the load sends anyway is never compared while the comparison is off.
108
+ const data = (0, data_1.plot)(query.comparesToPreviousPeriod ? loaded : { series: loaded.series });
109
+ this.answers += 1;
110
+ return { query, data, id: this.answers };
111
+ }
112
+ shownContent() {
113
+ const { state } = this.screen.getSnapshot();
114
+ return state.status === "content" ? state.value : undefined;
115
+ }
116
+ isStale() {
117
+ const content = this.shownContent();
118
+ return content !== undefined && !sameQuery(content.query, this.query);
119
+ }
120
+ selection() {
121
+ const content = this.shownContent();
122
+ return this.selected?.contentId === content?.id ? this.selected?.selection : undefined;
123
+ }
124
+ setSelected(selected) {
125
+ this.selected = selected;
126
+ this.derived = undefined;
127
+ for (const listener of this.listeners)
128
+ listener();
129
+ }
130
+ derive(from) {
131
+ const state = from.state.status === "content" ? { status: "content", value: from.state.value.data } : from.state;
132
+ return {
133
+ ...from,
134
+ state,
135
+ query: this.query,
136
+ isStale: this.isStale(),
137
+ selection: this.selection(),
138
+ };
139
+ }
140
+ }
141
+ exports.ChartInteractionMachine = ChartInteractionMachine;
142
+ function sameQuery(a, b) {
143
+ return a.range === b.range && a.comparesToPreviousPeriod === b.comparesToPreviousPeriod;
144
+ }
@@ -0,0 +1,23 @@
1
+ import type { ChartPoint, PlottedData } from "./data";
2
+ /** The point the person is touching or a screen reader stepped to. */
3
+ export interface ChartSelection {
4
+ /** The x of the point the touch snapped to. */
5
+ readonly x: number;
6
+ /** Each series' point nearest `x`, in series order; a series with no points has none. */
7
+ readonly points: readonly ChartSelectedPoint[];
8
+ /** Where `x` is among every x of the data. */
9
+ readonly index: number;
10
+ }
11
+ export interface ChartSelectedPoint {
12
+ readonly seriesId: string;
13
+ readonly seriesName: string;
14
+ readonly point: ChartPoint;
15
+ }
16
+ /** The index of the value in `sorted` nearest `target`, the earlier on a tie, or `undefined` when there are none. */
17
+ export declare function nearestIndex(sorted: readonly number[], target: number): number | undefined;
18
+ export declare function selectionAt(data: PlottedData, index: number): ChartSelection | undefined;
19
+ /**
20
+ * The selection at the x nearest `x`: across every series, the earlier point on a tie. For a gesture handler that
21
+ * already has an x (Victory Native's press state, say); the hook's `scrub` does this and ticks.
22
+ */
23
+ export declare function selectionNearest(data: PlottedData, x: number): ChartSelection | undefined;