panelui-native 0.65.0 → 0.68.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 (209) hide show
  1. package/README.md +21 -1
  2. package/lib/module/components/area-chart/index.js +18 -1
  3. package/lib/module/components/area-chart/index.js.map +1 -1
  4. package/lib/module/components/bar-chart/index.js +18 -1
  5. package/lib/module/components/bar-chart/index.js.map +1 -1
  6. package/lib/module/components/bottom-sheet/index.js.map +1 -1
  7. package/lib/module/components/candlestick-chart/index.js +18 -1
  8. package/lib/module/components/candlestick-chart/index.js.map +1 -1
  9. package/lib/module/components/carousel/carousel-lifecycle.js +51 -0
  10. package/lib/module/components/carousel/carousel-lifecycle.js.map +1 -0
  11. package/lib/module/components/carousel/index.js +61 -24
  12. package/lib/module/components/carousel/index.js.map +1 -1
  13. package/lib/module/components/context-menu/context-menu-invocation.js +23 -0
  14. package/lib/module/components/context-menu/context-menu-invocation.js.map +1 -0
  15. package/lib/module/components/context-menu/index.js +78 -9
  16. package/lib/module/components/context-menu/index.js.map +1 -1
  17. package/lib/module/components/dialog/index.js.map +1 -1
  18. package/lib/module/components/drawer/index.js.map +1 -1
  19. package/lib/module/components/flow/flow-accessibility.js +18 -2
  20. package/lib/module/components/flow/flow-accessibility.js.map +1 -1
  21. package/lib/module/components/flow/flow-identifiers.js +81 -0
  22. package/lib/module/components/flow/flow-identifiers.js.map +1 -0
  23. package/lib/module/components/flow/index.js +12 -18
  24. package/lib/module/components/flow/index.js.map +1 -1
  25. package/lib/module/components/form/index.js +7 -5
  26. package/lib/module/components/form/index.js.map +1 -1
  27. package/lib/module/components/form/typed-form.js +11 -0
  28. package/lib/module/components/form/typed-form.js.map +1 -0
  29. package/lib/module/components/form/use-form.js +4 -0
  30. package/lib/module/components/form/use-form.js.map +1 -1
  31. package/lib/module/components/heatmap-chart/index.js +27 -1
  32. package/lib/module/components/heatmap-chart/index.js.map +1 -1
  33. package/lib/module/components/line-chart/index.js +18 -1
  34. package/lib/module/components/line-chart/index.js.map +1 -1
  35. package/lib/module/components/message-scroller/index.js +187 -17
  36. package/lib/module/components/message-scroller/index.js.map +1 -1
  37. package/lib/module/components/message-scroller/message-scroller-math.js +43 -0
  38. package/lib/module/components/message-scroller/message-scroller-math.js.map +1 -0
  39. package/lib/module/components/meter/index.js +250 -0
  40. package/lib/module/components/meter/index.js.map +1 -0
  41. package/lib/module/components/meter/meter-scale.js +88 -0
  42. package/lib/module/components/meter/meter-scale.js.map +1 -0
  43. package/lib/module/components/planner/index.js +714 -0
  44. package/lib/module/components/planner/index.js.map +1 -0
  45. package/lib/module/components/planner/planner-entries.js +124 -0
  46. package/lib/module/components/planner/planner-entries.js.map +1 -0
  47. package/lib/module/components/popover/index.js.map +1 -1
  48. package/lib/module/components/radar-chart/index.js +18 -1
  49. package/lib/module/components/radar-chart/index.js.map +1 -1
  50. package/lib/module/components/scatter-chart/index.js +18 -1
  51. package/lib/module/components/scatter-chart/index.js.map +1 -1
  52. package/lib/module/components/selection-mode/index.js +26 -31
  53. package/lib/module/components/selection-mode/index.js.map +1 -1
  54. package/lib/module/components/selection-mode/selection-mode-contracts.js +48 -0
  55. package/lib/module/components/selection-mode/selection-mode-contracts.js.map +1 -0
  56. package/lib/module/components/signature/index.js +62 -20
  57. package/lib/module/components/signature/index.js.map +1 -1
  58. package/lib/module/components/signature/signature-accessibility.js +58 -0
  59. package/lib/module/components/signature/signature-accessibility.js.map +1 -0
  60. package/lib/module/components/skeleton/index.js +45 -7
  61. package/lib/module/components/skeleton/index.js.map +1 -1
  62. package/lib/module/components/slider/index.js +22 -18
  63. package/lib/module/components/slider/index.js.map +1 -1
  64. package/lib/module/components/spinner/index.js +63 -14
  65. package/lib/module/components/spinner/index.js.map +1 -1
  66. package/lib/module/components/time-picker/index.js +46 -21
  67. package/lib/module/components/time-picker/index.js.map +1 -1
  68. package/lib/module/components/time-picker/ruler-window.js +25 -0
  69. package/lib/module/components/time-picker/ruler-window.js.map +1 -0
  70. package/lib/module/hooks/breakpoint-contract.js +29 -0
  71. package/lib/module/hooks/breakpoint-contract.js.map +1 -0
  72. package/lib/module/hooks/index.js +1 -1
  73. package/lib/module/hooks/index.js.map +1 -1
  74. package/lib/module/hooks/use-breakpoint.js +46 -1
  75. package/lib/module/hooks/use-breakpoint.js.map +1 -1
  76. package/lib/module/index.js +3 -1
  77. package/lib/module/index.js.map +1 -1
  78. package/lib/module/primitives/chart-accessibility.js +68 -0
  79. package/lib/module/primitives/chart-accessibility.js.map +1 -0
  80. package/lib/module/primitives/scrim.js +94 -3
  81. package/lib/module/primitives/scrim.js.map +1 -1
  82. package/lib/typescript/src/components/area-chart/index.d.ts +2 -1
  83. package/lib/typescript/src/components/area-chart/index.d.ts.map +1 -1
  84. package/lib/typescript/src/components/bar-chart/index.d.ts +2 -1
  85. package/lib/typescript/src/components/bar-chart/index.d.ts.map +1 -1
  86. package/lib/typescript/src/components/bottom-sheet/index.d.ts +3 -0
  87. package/lib/typescript/src/components/bottom-sheet/index.d.ts.map +1 -1
  88. package/lib/typescript/src/components/candlestick-chart/index.d.ts +2 -1
  89. package/lib/typescript/src/components/candlestick-chart/index.d.ts.map +1 -1
  90. package/lib/typescript/src/components/carousel/carousel-lifecycle.d.ts +23 -0
  91. package/lib/typescript/src/components/carousel/carousel-lifecycle.d.ts.map +1 -0
  92. package/lib/typescript/src/components/carousel/index.d.ts +5 -2
  93. package/lib/typescript/src/components/carousel/index.d.ts.map +1 -1
  94. package/lib/typescript/src/components/context-menu/context-menu-invocation.d.ts +20 -0
  95. package/lib/typescript/src/components/context-menu/context-menu-invocation.d.ts.map +1 -0
  96. package/lib/typescript/src/components/context-menu/index.d.ts +18 -8
  97. package/lib/typescript/src/components/context-menu/index.d.ts.map +1 -1
  98. package/lib/typescript/src/components/dialog/index.d.ts +3 -0
  99. package/lib/typescript/src/components/dialog/index.d.ts.map +1 -1
  100. package/lib/typescript/src/components/drawer/index.d.ts +3 -0
  101. package/lib/typescript/src/components/drawer/index.d.ts.map +1 -1
  102. package/lib/typescript/src/components/flow/flow-accessibility.d.ts +8 -0
  103. package/lib/typescript/src/components/flow/flow-accessibility.d.ts.map +1 -1
  104. package/lib/typescript/src/components/flow/flow-identifiers.d.ts +24 -0
  105. package/lib/typescript/src/components/flow/flow-identifiers.d.ts.map +1 -0
  106. package/lib/typescript/src/components/flow/index.d.ts +6 -5
  107. package/lib/typescript/src/components/flow/index.d.ts.map +1 -1
  108. package/lib/typescript/src/components/form/index.d.ts +18 -5
  109. package/lib/typescript/src/components/form/index.d.ts.map +1 -1
  110. package/lib/typescript/src/components/form/typed-form.d.ts +7 -0
  111. package/lib/typescript/src/components/form/typed-form.d.ts.map +1 -0
  112. package/lib/typescript/src/components/form/use-form.d.ts +4 -0
  113. package/lib/typescript/src/components/form/use-form.d.ts.map +1 -1
  114. package/lib/typescript/src/components/heatmap-chart/index.d.ts +2 -1
  115. package/lib/typescript/src/components/heatmap-chart/index.d.ts.map +1 -1
  116. package/lib/typescript/src/components/line-chart/index.d.ts +2 -1
  117. package/lib/typescript/src/components/line-chart/index.d.ts.map +1 -1
  118. package/lib/typescript/src/components/message-scroller/index.d.ts +30 -2
  119. package/lib/typescript/src/components/message-scroller/index.d.ts.map +1 -1
  120. package/lib/typescript/src/components/message-scroller/message-scroller-math.d.ts +18 -0
  121. package/lib/typescript/src/components/message-scroller/message-scroller-math.d.ts.map +1 -0
  122. package/lib/typescript/src/components/meter/index.d.ts +270 -0
  123. package/lib/typescript/src/components/meter/index.d.ts.map +1 -0
  124. package/lib/typescript/src/components/meter/meter-scale.d.ts +57 -0
  125. package/lib/typescript/src/components/meter/meter-scale.d.ts.map +1 -0
  126. package/lib/typescript/src/components/planner/index.d.ts +274 -0
  127. package/lib/typescript/src/components/planner/index.d.ts.map +1 -0
  128. package/lib/typescript/src/components/planner/planner-entries.d.ts +75 -0
  129. package/lib/typescript/src/components/planner/planner-entries.d.ts.map +1 -0
  130. package/lib/typescript/src/components/popover/index.d.ts +3 -0
  131. package/lib/typescript/src/components/popover/index.d.ts.map +1 -1
  132. package/lib/typescript/src/components/radar-chart/index.d.ts +4 -0
  133. package/lib/typescript/src/components/radar-chart/index.d.ts.map +1 -1
  134. package/lib/typescript/src/components/scatter-chart/index.d.ts +2 -1
  135. package/lib/typescript/src/components/scatter-chart/index.d.ts.map +1 -1
  136. package/lib/typescript/src/components/selection-mode/index.d.ts +6 -4
  137. package/lib/typescript/src/components/selection-mode/index.d.ts.map +1 -1
  138. package/lib/typescript/src/components/selection-mode/selection-mode-contracts.d.ts +18 -0
  139. package/lib/typescript/src/components/selection-mode/selection-mode-contracts.d.ts.map +1 -0
  140. package/lib/typescript/src/components/signature/index.d.ts +6 -0
  141. package/lib/typescript/src/components/signature/index.d.ts.map +1 -1
  142. package/lib/typescript/src/components/signature/signature-accessibility.d.ts +27 -0
  143. package/lib/typescript/src/components/signature/signature-accessibility.d.ts.map +1 -0
  144. package/lib/typescript/src/components/skeleton/index.d.ts +25 -2
  145. package/lib/typescript/src/components/skeleton/index.d.ts.map +1 -1
  146. package/lib/typescript/src/components/slider/index.d.ts.map +1 -1
  147. package/lib/typescript/src/components/spinner/index.d.ts +25 -2
  148. package/lib/typescript/src/components/spinner/index.d.ts.map +1 -1
  149. package/lib/typescript/src/components/time-picker/index.d.ts +5 -6
  150. package/lib/typescript/src/components/time-picker/index.d.ts.map +1 -1
  151. package/lib/typescript/src/components/time-picker/ruler-window.d.ts +12 -0
  152. package/lib/typescript/src/components/time-picker/ruler-window.d.ts.map +1 -0
  153. package/lib/typescript/src/hooks/breakpoint-contract.d.ts +6 -0
  154. package/lib/typescript/src/hooks/breakpoint-contract.d.ts.map +1 -0
  155. package/lib/typescript/src/hooks/index.d.ts +1 -1
  156. package/lib/typescript/src/hooks/index.d.ts.map +1 -1
  157. package/lib/typescript/src/hooks/use-breakpoint.d.ts +22 -0
  158. package/lib/typescript/src/hooks/use-breakpoint.d.ts.map +1 -1
  159. package/lib/typescript/src/index.d.ts +4 -2
  160. package/lib/typescript/src/index.d.ts.map +1 -1
  161. package/lib/typescript/src/primitives/chart-accessibility.d.ts +33 -0
  162. package/lib/typescript/src/primitives/chart-accessibility.d.ts.map +1 -0
  163. package/lib/typescript/src/primitives/scrim.d.ts +5 -3
  164. package/lib/typescript/src/primitives/scrim.d.ts.map +1 -1
  165. package/lib/typescript/types/form-api.type-test.d.ts +2 -0
  166. package/lib/typescript/types/form-api.type-test.d.ts.map +1 -0
  167. package/package.json +34 -5
  168. package/src/components/area-chart/index.tsx +27 -2
  169. package/src/components/bar-chart/index.tsx +27 -2
  170. package/src/components/bottom-sheet/index.tsx +3 -0
  171. package/src/components/candlestick-chart/index.tsx +30 -2
  172. package/src/components/carousel/carousel-lifecycle.ts +74 -0
  173. package/src/components/carousel/index.tsx +80 -25
  174. package/src/components/context-menu/context-menu-invocation.ts +44 -0
  175. package/src/components/context-menu/index.tsx +113 -13
  176. package/src/components/dialog/index.tsx +3 -0
  177. package/src/components/drawer/index.tsx +3 -0
  178. package/src/components/flow/flow-accessibility.ts +16 -2
  179. package/src/components/flow/flow-identifiers.ts +98 -0
  180. package/src/components/flow/index.tsx +29 -19
  181. package/src/components/form/index.tsx +32 -5
  182. package/src/components/form/typed-form.ts +13 -0
  183. package/src/components/form/use-form.ts +9 -0
  184. package/src/components/heatmap-chart/index.tsx +49 -3
  185. package/src/components/line-chart/index.tsx +27 -2
  186. package/src/components/message-scroller/index.tsx +273 -13
  187. package/src/components/message-scroller/message-scroller-math.ts +62 -0
  188. package/src/components/meter/index.tsx +318 -0
  189. package/src/components/meter/meter-scale.ts +109 -0
  190. package/src/components/planner/index.tsx +886 -0
  191. package/src/components/planner/planner-entries.ts +146 -0
  192. package/src/components/popover/index.tsx +3 -0
  193. package/src/components/radar-chart/index.tsx +24 -0
  194. package/src/components/scatter-chart/index.tsx +27 -2
  195. package/src/components/selection-mode/index.tsx +37 -30
  196. package/src/components/selection-mode/selection-mode-contracts.ts +53 -0
  197. package/src/components/signature/index.tsx +107 -20
  198. package/src/components/signature/signature-accessibility.ts +67 -0
  199. package/src/components/skeleton/index.tsx +59 -5
  200. package/src/components/slider/index.tsx +28 -18
  201. package/src/components/spinner/index.tsx +80 -13
  202. package/src/components/time-picker/index.tsx +57 -29
  203. package/src/components/time-picker/ruler-window.ts +24 -0
  204. package/src/hooks/breakpoint-contract.ts +31 -0
  205. package/src/hooks/index.ts +4 -0
  206. package/src/hooks/use-breakpoint.ts +61 -10
  207. package/src/index.ts +32 -0
  208. package/src/primitives/chart-accessibility.ts +102 -0
  209. package/src/primitives/scrim.tsx +127 -7
@@ -0,0 +1,318 @@
1
+ import { forwardRef, useEffect } from 'react';
2
+ import { View, type LayoutChangeEvent, type ViewProps } from 'react-native';
3
+ import Animated, {
4
+ useAnimatedStyle,
5
+ useReducedMotion,
6
+ useSharedValue,
7
+ withSpring,
8
+ withTiming,
9
+ type SharedValue,
10
+ } from 'react-native-reanimated';
11
+ import { tv, type VariantProps } from 'tailwind-variants';
12
+ import { Text } from '../../primitives/text';
13
+ import {
14
+ clamp,
15
+ colorFor,
16
+ fractionOf,
17
+ formatValue,
18
+ litSegments,
19
+ type MeterColor,
20
+ type MeterThreshold,
21
+ } from './meter-scale';
22
+
23
+ const SPRING = { damping: 20, stiffness: 180, mass: 0.6 } as const;
24
+ /** Milliseconds for a segment to light or go out. */
25
+ const SEGMENT_DURATION = 180;
26
+ /** How faint an unlit segment sits. Present, but plainly not counted. */
27
+ const SEGMENT_UNLIT = 0.22;
28
+
29
+ const meterVariants = tv({
30
+ slots: {
31
+ root: 'w-full gap-2',
32
+ header: 'flex-row items-center justify-between gap-2',
33
+ track: 'w-full overflow-hidden rounded-full',
34
+ indicator: 'h-full rounded-full',
35
+ segments: 'w-full flex-row',
36
+ segment: 'flex-1 rounded-full',
37
+ },
38
+ variants: {
39
+ color: {
40
+ primary: { track: 'bg-primary/16', indicator: 'bg-primary', segment: 'bg-primary' },
41
+ success: { track: 'bg-success/16', indicator: 'bg-success', segment: 'bg-success' },
42
+ warning: { track: 'bg-warning/16', indicator: 'bg-warning', segment: 'bg-warning' },
43
+ destructive: {
44
+ track: 'bg-destructive/16',
45
+ indicator: 'bg-destructive',
46
+ segment: 'bg-destructive',
47
+ },
48
+ info: { track: 'bg-info/16', indicator: 'bg-info', segment: 'bg-info' },
49
+ muted: {
50
+ track: 'bg-muted',
51
+ indicator: 'bg-muted-foreground',
52
+ segment: 'bg-muted-foreground',
53
+ },
54
+ },
55
+ size: {
56
+ sm: { track: 'h-1.5', segments: 'h-1.5 gap-1', segment: 'h-1.5' },
57
+ md: { track: 'h-2', segments: 'h-2 gap-1.5', segment: 'h-2' },
58
+ lg: { track: 'h-3', segments: 'h-3 gap-1.5', segment: 'h-3' },
59
+ },
60
+ },
61
+ defaultVariants: {
62
+ color: 'primary',
63
+ size: 'md',
64
+ },
65
+ });
66
+
67
+ type MeterVariantProps = VariantProps<typeof meterVariants>;
68
+
69
+ export interface MeterProps
70
+ extends Omit<ViewProps, 'children'>,
71
+ MeterVariantProps {
72
+ className?: string;
73
+ /** The measurement, somewhere between `minValue` and `maxValue`. */
74
+ value: number;
75
+ /**
76
+ * The bottom of the scale — the value at which the bar reads as empty.
77
+ * Defaults to `0`.
78
+ */
79
+ minValue?: number;
80
+ /**
81
+ * The top of the scale — the value at which the bar reads as full. Defaults
82
+ * to `100`, so a bare percentage needs neither bound set.
83
+ */
84
+ maxValue?: number;
85
+ /**
86
+ * What is being measured. Also the accessibility label, and a meter without
87
+ * one announces a number with nothing attached to it — "seventy-five
88
+ * percent" of what is not an answer. Pass `accessibilityLabel` instead only
89
+ * where the caption is drawn some other way.
90
+ */
91
+ label?: string;
92
+ /** Draw the value above the track, opposite the label. */
93
+ showValueLabel?: boolean;
94
+ /**
95
+ * Text for the value label. Overrides the formatted value — use it for a
96
+ * word where a number reads worse: `Strong`, `Almost full`.
97
+ *
98
+ * It is spoken whether or not it is drawn, so it can give a screen reader
99
+ * better words than the caption without changing the caption. Pair it with
100
+ * `showValueLabel` to draw it too.
101
+ */
102
+ valueLabel?: string;
103
+ /**
104
+ * How to write the value, through `Intl.NumberFormat`. A `percent` style
105
+ * formats how far up the scale the value sits; every other style formats
106
+ * the value itself, so `{ style: 'unit', unit: 'gigabyte' }` against a
107
+ * `maxValue` of `256` reads `64 GB` rather than a percentage of it. Falls
108
+ * back to a rounded percent when omitted.
109
+ */
110
+ formatOptions?: Intl.NumberFormatOptions;
111
+ /**
112
+ * Points on the scale where the colour changes, each `{ from, color }`. The
113
+ * highest one the reading has reached wins, so the order you list them in
114
+ * does not matter; below all of them the `color` prop applies.
115
+ *
116
+ * This is the difference between a meter and a bar: the colour is a
117
+ * judgement about the reading. Which direction is bad is yours to say —
118
+ * thresholds climbing to `destructive` suit a disk filling up, thresholds
119
+ * falling to it suit a battery running down.
120
+ */
121
+ thresholds?: MeterThreshold[];
122
+ /**
123
+ * Draw the scale as this many discrete blocks instead of one continuous
124
+ * bar. Blocks are all or nothing, which is the point of them: four blocks
125
+ * say "three out of four" where a bar says "about seventy percent", and a
126
+ * password is not seventy percent strong.
127
+ *
128
+ * Any value above the floor lights at least one block, so a reading that
129
+ * is not empty never looks it.
130
+ */
131
+ segments?: number;
132
+ /** Extra classes for the fill, or for a lit segment. */
133
+ indicatorClassName?: string;
134
+ /** Extra classes for the label + value row. */
135
+ headerClassName?: string;
136
+ }
137
+
138
+ interface SegmentProps {
139
+ index: number;
140
+ /** How many blocks are lit, as a shared value so lighting stays off the JS thread. */
141
+ lit: SharedValue<number>;
142
+ className: string;
143
+ reducedMotion: boolean;
144
+ }
145
+
146
+ /**
147
+ * One block of a segmented meter. It reads the lit count rather than a boolean
148
+ * so the whole row animates from one shared value, without a render per block.
149
+ */
150
+ function Segment({ index, lit, className, reducedMotion }: SegmentProps) {
151
+ const style = useAnimatedStyle(() => {
152
+ const on = lit.value > index ? 1 : SEGMENT_UNLIT;
153
+ return {
154
+ opacity: reducedMotion
155
+ ? on
156
+ : withTiming(on, { duration: SEGMENT_DURATION }),
157
+ };
158
+ });
159
+
160
+ return <Animated.View style={style} className={className} />;
161
+ }
162
+
163
+ /**
164
+ * A measurement on a fixed scale: disk used, battery left, a score, a password's
165
+ * strength. The fill is driven on the UI thread, so a value change costs one
166
+ * render and nothing per frame.
167
+ *
168
+ * A meter is not a progress bar. Progress is a task moving towards an end that
169
+ * finishing is the point of; a meter is a reading that happens to sit where it
170
+ * sits, and may go back down. Reach for
171
+ * [Progress](/docs/components/progress) when something is completing.
172
+ *
173
+ * `thresholds` paint the bar by where the reading falls, and `segments` draws
174
+ * the scale as discrete blocks for readings that are counted rather than
175
+ * measured.
176
+ *
177
+ * The value is read against `minValue` / `maxValue`, which default to 0 and
178
+ * 100 — so a percentage needs neither. Set them and the meter speaks in
179
+ * whatever is being measured: bytes, degrees, points. Nothing has to be
180
+ * converted to a percent on the way in, and the screen reader is told the real
181
+ * scale rather than a derived one.
182
+ *
183
+ * React Native has no meter role, so this announces as a progress bar with its
184
+ * value, range and unit attached. That is the closest the platform offers, and
185
+ * it is why `label` matters: the role will not distinguish a reading from a
186
+ * task, and the name is what does.
187
+ */
188
+ export const Meter = forwardRef<View, MeterProps>(
189
+ (
190
+ {
191
+ className,
192
+ indicatorClassName,
193
+ headerClassName,
194
+ value,
195
+ minValue = 0,
196
+ maxValue = 100,
197
+ label,
198
+ showValueLabel = false,
199
+ valueLabel,
200
+ formatOptions,
201
+ thresholds,
202
+ segments,
203
+ color,
204
+ size,
205
+ accessibilityLabel,
206
+ ...props
207
+ },
208
+ ref
209
+ ) => {
210
+ const held = clamp(value, minValue, maxValue);
211
+ const fraction = fractionOf(value, minValue, maxValue);
212
+ const resolvedColor = colorFor(held, color ?? 'primary', thresholds);
213
+ const slots = meterVariants({ color: resolvedColor, size });
214
+ const reducedMotion = useReducedMotion();
215
+
216
+ const trackWidth = useSharedValue(0);
217
+ const progress = useSharedValue(fraction);
218
+ // Any reading above the floor lights a block, so "a little" never looks
219
+ // like "none". Rounding down would leave the first quarter of a
220
+ // four-block meter dark, which is the reading it is least able to afford.
221
+ const litCount = segments ? litSegments(fraction, segments) : 0;
222
+ const lit = useSharedValue(litCount);
223
+
224
+ // Reduce motion lands on the value instead of springing to it — the
225
+ // number is the information, and the overshoot is decoration.
226
+ useEffect(() => {
227
+ progress.value = reducedMotion ? fraction : withSpring(fraction, SPRING);
228
+ }, [fraction, progress, reducedMotion]);
229
+
230
+ useEffect(() => {
231
+ lit.value = litCount;
232
+ }, [lit, litCount]);
233
+
234
+ const onLayout = (event: LayoutChangeEvent) => {
235
+ trackWidth.value = event.nativeEvent.layout.width;
236
+ };
237
+
238
+ const fillStyle = useAnimatedStyle(() => ({
239
+ width: trackWidth.value * progress.value,
240
+ }));
241
+
242
+ const spoken = formatValue(held, fraction, valueLabel, formatOptions);
243
+ const showValue = showValueLabel;
244
+ const hasHeader = label != null || showValue;
245
+
246
+ const accessibility = {
247
+ accessibilityRole: 'progressbar' as const,
248
+ accessibilityLabel: accessibilityLabel ?? label,
249
+ accessibilityValue: {
250
+ min: minValue,
251
+ max: maxValue,
252
+ now: held,
253
+ // Without this the scale is read as a bare number. `text` is what
254
+ // carries the unit — the percent sign, the gigabytes, the word.
255
+ text: spoken,
256
+ },
257
+ };
258
+
259
+ const bar =
260
+ segments && segments > 0 ? (
261
+ <View
262
+ ref={ref}
263
+ {...accessibility}
264
+ className={slots.segments({ className })}
265
+ {...props}
266
+ >
267
+ {Array.from({ length: segments }, (_, index) => (
268
+ <Segment
269
+ key={index}
270
+ index={index}
271
+ lit={lit}
272
+ reducedMotion={reducedMotion}
273
+ className={slots.segment({ className: indicatorClassName })}
274
+ />
275
+ ))}
276
+ </View>
277
+ ) : (
278
+ <View
279
+ ref={ref}
280
+ {...accessibility}
281
+ className={slots.track({ className })}
282
+ onLayout={onLayout}
283
+ {...props}
284
+ >
285
+ <Animated.View
286
+ style={fillStyle}
287
+ className={slots.indicator({ className: indicatorClassName })}
288
+ />
289
+ </View>
290
+ );
291
+
292
+ if (!hasHeader) return bar;
293
+
294
+ return (
295
+ <View className={slots.root()}>
296
+ <View className={slots.header({ className: headerClassName })}>
297
+ {label != null ? (
298
+ <Text size="sm" weight="medium" numberOfLines={1}>
299
+ {label}
300
+ </Text>
301
+ ) : (
302
+ <View />
303
+ )}
304
+ {showValue ? (
305
+ <Text size="sm" muted numberOfLines={1}>
306
+ {spoken}
307
+ </Text>
308
+ ) : null}
309
+ </View>
310
+ {bar}
311
+ </View>
312
+ );
313
+ }
314
+ );
315
+
316
+ Meter.displayName = 'Meter';
317
+
318
+ export type { MeterColor, MeterThreshold };
@@ -0,0 +1,109 @@
1
+ /**
2
+ * Where a reading sits on a meter's scale, and what colour it has earned.
3
+ *
4
+ * Kept apart from the component because it is the part that can be wrong
5
+ * without looking wrong: a threshold picked in the wrong order, or a segment
6
+ * count that rounds a real reading down to nothing, is a meter that lies
7
+ * quietly. None of it touches React, so it can be tested on its own.
8
+ */
9
+
10
+ /** The token a meter can be painted with. */
11
+ export type MeterColor =
12
+ | 'primary'
13
+ | 'success'
14
+ | 'warning'
15
+ | 'destructive'
16
+ | 'info'
17
+ | 'muted';
18
+
19
+ /**
20
+ * A point on the scale, and the colour the meter takes from there upwards.
21
+ *
22
+ * `from` is in the same units as `value` — not a percentage of the range — so
23
+ * a threshold on a 0–8 GB meter is written in gigabytes.
24
+ */
25
+ export interface MeterThreshold {
26
+ /** The value at which this colour takes over. */
27
+ from: number;
28
+ /** What the bar is painted with at or above `from`. */
29
+ color: MeterColor;
30
+ }
31
+
32
+ /** `value` held inside the scale, so a stray number cannot escape the track. */
33
+ export function clamp(value: number, min: number, max: number) {
34
+ if (!(value > min)) return min;
35
+ if (value > max) return max;
36
+ return value;
37
+ }
38
+
39
+ /**
40
+ * How far up the scale the value sits, 0–1. An empty or inverted range has no
41
+ * meaningful position in it, so it reads as empty rather than dividing by zero.
42
+ */
43
+ export function fractionOf(value: number, min: number, max: number) {
44
+ const span = max - min;
45
+ if (!(span > 0)) return 0;
46
+ return clamp((value - min) / span, 0, 1);
47
+ }
48
+
49
+ /**
50
+ * The colour the reading has earned: the highest threshold at or below the
51
+ * value, or the base colour when it has reached none of them.
52
+ *
53
+ * Highest-wins rather than first-wins, so the caller can list thresholds in
54
+ * any order and get the same answer. A rule that depended on the order would
55
+ * make a reordered array a silent behaviour change.
56
+ */
57
+ export function colorFor(
58
+ value: number,
59
+ base: MeterColor | undefined,
60
+ thresholds: MeterThreshold[] | undefined
61
+ ) {
62
+ if (!thresholds?.length) return base;
63
+ let winner: MeterThreshold | undefined;
64
+ for (const threshold of thresholds) {
65
+ if (value < threshold.from) continue;
66
+ if (!winner || threshold.from > winner.from) winner = threshold;
67
+ }
68
+ return winner?.color ?? base;
69
+ }
70
+
71
+ /**
72
+ * How many blocks of a segmented meter are lit.
73
+ *
74
+ * Rounded up, so any reading above the floor lights at least one. Rounding
75
+ * down would leave the whole first block of a four-block meter dark, and "a
76
+ * little" looking like "none" is the reading a meter can least afford to get
77
+ * wrong.
78
+ */
79
+ export function litSegments(fraction: number, segments: number) {
80
+ if (!(segments > 0)) return 0;
81
+ return Math.min(Math.ceil(fraction * segments), segments);
82
+ }
83
+
84
+ /**
85
+ * The value caption: an explicit override, an `Intl` rendering, or a rounded
86
+ * percent.
87
+ *
88
+ * A `percent` style is given the fraction, because that is what a percentage
89
+ * of the scale means; every other style is given the value, because a byte
90
+ * count or a score is a quantity and not a proportion.
91
+ */
92
+ export function formatValue(
93
+ value: number,
94
+ fraction: number,
95
+ valueLabel?: string,
96
+ formatOptions?: Intl.NumberFormatOptions
97
+ ) {
98
+ if (valueLabel != null) return valueLabel;
99
+ if (formatOptions) {
100
+ try {
101
+ return new Intl.NumberFormat(undefined, formatOptions).format(
102
+ formatOptions.style === 'percent' ? fraction : value
103
+ );
104
+ } catch {
105
+ // Some engines ship a partial Intl; fall through to the plain percent.
106
+ }
107
+ }
108
+ return `${Math.round(fraction * 100)}%`;
109
+ }