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.
- package/README.md +21 -1
- package/lib/module/components/area-chart/index.js +18 -1
- package/lib/module/components/area-chart/index.js.map +1 -1
- package/lib/module/components/bar-chart/index.js +18 -1
- package/lib/module/components/bar-chart/index.js.map +1 -1
- package/lib/module/components/bottom-sheet/index.js.map +1 -1
- package/lib/module/components/candlestick-chart/index.js +18 -1
- package/lib/module/components/candlestick-chart/index.js.map +1 -1
- package/lib/module/components/carousel/carousel-lifecycle.js +51 -0
- package/lib/module/components/carousel/carousel-lifecycle.js.map +1 -0
- package/lib/module/components/carousel/index.js +61 -24
- package/lib/module/components/carousel/index.js.map +1 -1
- package/lib/module/components/context-menu/context-menu-invocation.js +23 -0
- package/lib/module/components/context-menu/context-menu-invocation.js.map +1 -0
- package/lib/module/components/context-menu/index.js +78 -9
- package/lib/module/components/context-menu/index.js.map +1 -1
- package/lib/module/components/dialog/index.js.map +1 -1
- package/lib/module/components/drawer/index.js.map +1 -1
- package/lib/module/components/flow/flow-accessibility.js +18 -2
- package/lib/module/components/flow/flow-accessibility.js.map +1 -1
- package/lib/module/components/flow/flow-identifiers.js +81 -0
- package/lib/module/components/flow/flow-identifiers.js.map +1 -0
- package/lib/module/components/flow/index.js +12 -18
- package/lib/module/components/flow/index.js.map +1 -1
- package/lib/module/components/form/index.js +7 -5
- package/lib/module/components/form/index.js.map +1 -1
- package/lib/module/components/form/typed-form.js +11 -0
- package/lib/module/components/form/typed-form.js.map +1 -0
- package/lib/module/components/form/use-form.js +4 -0
- package/lib/module/components/form/use-form.js.map +1 -1
- package/lib/module/components/heatmap-chart/index.js +27 -1
- package/lib/module/components/heatmap-chart/index.js.map +1 -1
- package/lib/module/components/line-chart/index.js +18 -1
- package/lib/module/components/line-chart/index.js.map +1 -1
- package/lib/module/components/message-scroller/index.js +187 -17
- package/lib/module/components/message-scroller/index.js.map +1 -1
- package/lib/module/components/message-scroller/message-scroller-math.js +43 -0
- package/lib/module/components/message-scroller/message-scroller-math.js.map +1 -0
- package/lib/module/components/meter/index.js +250 -0
- package/lib/module/components/meter/index.js.map +1 -0
- package/lib/module/components/meter/meter-scale.js +88 -0
- package/lib/module/components/meter/meter-scale.js.map +1 -0
- package/lib/module/components/planner/index.js +714 -0
- package/lib/module/components/planner/index.js.map +1 -0
- package/lib/module/components/planner/planner-entries.js +124 -0
- package/lib/module/components/planner/planner-entries.js.map +1 -0
- package/lib/module/components/popover/index.js.map +1 -1
- package/lib/module/components/radar-chart/index.js +18 -1
- package/lib/module/components/radar-chart/index.js.map +1 -1
- package/lib/module/components/scatter-chart/index.js +18 -1
- package/lib/module/components/scatter-chart/index.js.map +1 -1
- package/lib/module/components/selection-mode/index.js +26 -31
- package/lib/module/components/selection-mode/index.js.map +1 -1
- package/lib/module/components/selection-mode/selection-mode-contracts.js +48 -0
- package/lib/module/components/selection-mode/selection-mode-contracts.js.map +1 -0
- package/lib/module/components/signature/index.js +62 -20
- package/lib/module/components/signature/index.js.map +1 -1
- package/lib/module/components/signature/signature-accessibility.js +58 -0
- package/lib/module/components/signature/signature-accessibility.js.map +1 -0
- package/lib/module/components/skeleton/index.js +45 -7
- package/lib/module/components/skeleton/index.js.map +1 -1
- package/lib/module/components/slider/index.js +22 -18
- package/lib/module/components/slider/index.js.map +1 -1
- package/lib/module/components/spinner/index.js +63 -14
- package/lib/module/components/spinner/index.js.map +1 -1
- package/lib/module/components/time-picker/index.js +46 -21
- package/lib/module/components/time-picker/index.js.map +1 -1
- package/lib/module/components/time-picker/ruler-window.js +25 -0
- package/lib/module/components/time-picker/ruler-window.js.map +1 -0
- package/lib/module/hooks/breakpoint-contract.js +29 -0
- package/lib/module/hooks/breakpoint-contract.js.map +1 -0
- package/lib/module/hooks/index.js +1 -1
- package/lib/module/hooks/index.js.map +1 -1
- package/lib/module/hooks/use-breakpoint.js +46 -1
- package/lib/module/hooks/use-breakpoint.js.map +1 -1
- package/lib/module/index.js +3 -1
- package/lib/module/index.js.map +1 -1
- package/lib/module/primitives/chart-accessibility.js +68 -0
- package/lib/module/primitives/chart-accessibility.js.map +1 -0
- package/lib/module/primitives/scrim.js +94 -3
- package/lib/module/primitives/scrim.js.map +1 -1
- package/lib/typescript/src/components/area-chart/index.d.ts +2 -1
- package/lib/typescript/src/components/area-chart/index.d.ts.map +1 -1
- package/lib/typescript/src/components/bar-chart/index.d.ts +2 -1
- package/lib/typescript/src/components/bar-chart/index.d.ts.map +1 -1
- package/lib/typescript/src/components/bottom-sheet/index.d.ts +3 -0
- package/lib/typescript/src/components/bottom-sheet/index.d.ts.map +1 -1
- package/lib/typescript/src/components/candlestick-chart/index.d.ts +2 -1
- package/lib/typescript/src/components/candlestick-chart/index.d.ts.map +1 -1
- package/lib/typescript/src/components/carousel/carousel-lifecycle.d.ts +23 -0
- package/lib/typescript/src/components/carousel/carousel-lifecycle.d.ts.map +1 -0
- package/lib/typescript/src/components/carousel/index.d.ts +5 -2
- package/lib/typescript/src/components/carousel/index.d.ts.map +1 -1
- package/lib/typescript/src/components/context-menu/context-menu-invocation.d.ts +20 -0
- package/lib/typescript/src/components/context-menu/context-menu-invocation.d.ts.map +1 -0
- package/lib/typescript/src/components/context-menu/index.d.ts +18 -8
- package/lib/typescript/src/components/context-menu/index.d.ts.map +1 -1
- package/lib/typescript/src/components/dialog/index.d.ts +3 -0
- package/lib/typescript/src/components/dialog/index.d.ts.map +1 -1
- package/lib/typescript/src/components/drawer/index.d.ts +3 -0
- package/lib/typescript/src/components/drawer/index.d.ts.map +1 -1
- package/lib/typescript/src/components/flow/flow-accessibility.d.ts +8 -0
- package/lib/typescript/src/components/flow/flow-accessibility.d.ts.map +1 -1
- package/lib/typescript/src/components/flow/flow-identifiers.d.ts +24 -0
- package/lib/typescript/src/components/flow/flow-identifiers.d.ts.map +1 -0
- package/lib/typescript/src/components/flow/index.d.ts +6 -5
- package/lib/typescript/src/components/flow/index.d.ts.map +1 -1
- package/lib/typescript/src/components/form/index.d.ts +18 -5
- package/lib/typescript/src/components/form/index.d.ts.map +1 -1
- package/lib/typescript/src/components/form/typed-form.d.ts +7 -0
- package/lib/typescript/src/components/form/typed-form.d.ts.map +1 -0
- package/lib/typescript/src/components/form/use-form.d.ts +4 -0
- package/lib/typescript/src/components/form/use-form.d.ts.map +1 -1
- package/lib/typescript/src/components/heatmap-chart/index.d.ts +2 -1
- package/lib/typescript/src/components/heatmap-chart/index.d.ts.map +1 -1
- package/lib/typescript/src/components/line-chart/index.d.ts +2 -1
- package/lib/typescript/src/components/line-chart/index.d.ts.map +1 -1
- package/lib/typescript/src/components/message-scroller/index.d.ts +30 -2
- package/lib/typescript/src/components/message-scroller/index.d.ts.map +1 -1
- package/lib/typescript/src/components/message-scroller/message-scroller-math.d.ts +18 -0
- package/lib/typescript/src/components/message-scroller/message-scroller-math.d.ts.map +1 -0
- package/lib/typescript/src/components/meter/index.d.ts +270 -0
- package/lib/typescript/src/components/meter/index.d.ts.map +1 -0
- package/lib/typescript/src/components/meter/meter-scale.d.ts +57 -0
- package/lib/typescript/src/components/meter/meter-scale.d.ts.map +1 -0
- package/lib/typescript/src/components/planner/index.d.ts +274 -0
- package/lib/typescript/src/components/planner/index.d.ts.map +1 -0
- package/lib/typescript/src/components/planner/planner-entries.d.ts +75 -0
- package/lib/typescript/src/components/planner/planner-entries.d.ts.map +1 -0
- package/lib/typescript/src/components/popover/index.d.ts +3 -0
- package/lib/typescript/src/components/popover/index.d.ts.map +1 -1
- package/lib/typescript/src/components/radar-chart/index.d.ts +4 -0
- package/lib/typescript/src/components/radar-chart/index.d.ts.map +1 -1
- package/lib/typescript/src/components/scatter-chart/index.d.ts +2 -1
- package/lib/typescript/src/components/scatter-chart/index.d.ts.map +1 -1
- package/lib/typescript/src/components/selection-mode/index.d.ts +6 -4
- package/lib/typescript/src/components/selection-mode/index.d.ts.map +1 -1
- package/lib/typescript/src/components/selection-mode/selection-mode-contracts.d.ts +18 -0
- package/lib/typescript/src/components/selection-mode/selection-mode-contracts.d.ts.map +1 -0
- package/lib/typescript/src/components/signature/index.d.ts +6 -0
- package/lib/typescript/src/components/signature/index.d.ts.map +1 -1
- package/lib/typescript/src/components/signature/signature-accessibility.d.ts +27 -0
- package/lib/typescript/src/components/signature/signature-accessibility.d.ts.map +1 -0
- package/lib/typescript/src/components/skeleton/index.d.ts +25 -2
- package/lib/typescript/src/components/skeleton/index.d.ts.map +1 -1
- package/lib/typescript/src/components/slider/index.d.ts.map +1 -1
- package/lib/typescript/src/components/spinner/index.d.ts +25 -2
- package/lib/typescript/src/components/spinner/index.d.ts.map +1 -1
- package/lib/typescript/src/components/time-picker/index.d.ts +5 -6
- package/lib/typescript/src/components/time-picker/index.d.ts.map +1 -1
- package/lib/typescript/src/components/time-picker/ruler-window.d.ts +12 -0
- package/lib/typescript/src/components/time-picker/ruler-window.d.ts.map +1 -0
- package/lib/typescript/src/hooks/breakpoint-contract.d.ts +6 -0
- package/lib/typescript/src/hooks/breakpoint-contract.d.ts.map +1 -0
- package/lib/typescript/src/hooks/index.d.ts +1 -1
- package/lib/typescript/src/hooks/index.d.ts.map +1 -1
- package/lib/typescript/src/hooks/use-breakpoint.d.ts +22 -0
- package/lib/typescript/src/hooks/use-breakpoint.d.ts.map +1 -1
- package/lib/typescript/src/index.d.ts +4 -2
- package/lib/typescript/src/index.d.ts.map +1 -1
- package/lib/typescript/src/primitives/chart-accessibility.d.ts +33 -0
- package/lib/typescript/src/primitives/chart-accessibility.d.ts.map +1 -0
- package/lib/typescript/src/primitives/scrim.d.ts +5 -3
- package/lib/typescript/src/primitives/scrim.d.ts.map +1 -1
- package/lib/typescript/types/form-api.type-test.d.ts +2 -0
- package/lib/typescript/types/form-api.type-test.d.ts.map +1 -0
- package/package.json +34 -5
- package/src/components/area-chart/index.tsx +27 -2
- package/src/components/bar-chart/index.tsx +27 -2
- package/src/components/bottom-sheet/index.tsx +3 -0
- package/src/components/candlestick-chart/index.tsx +30 -2
- package/src/components/carousel/carousel-lifecycle.ts +74 -0
- package/src/components/carousel/index.tsx +80 -25
- package/src/components/context-menu/context-menu-invocation.ts +44 -0
- package/src/components/context-menu/index.tsx +113 -13
- package/src/components/dialog/index.tsx +3 -0
- package/src/components/drawer/index.tsx +3 -0
- package/src/components/flow/flow-accessibility.ts +16 -2
- package/src/components/flow/flow-identifiers.ts +98 -0
- package/src/components/flow/index.tsx +29 -19
- package/src/components/form/index.tsx +32 -5
- package/src/components/form/typed-form.ts +13 -0
- package/src/components/form/use-form.ts +9 -0
- package/src/components/heatmap-chart/index.tsx +49 -3
- package/src/components/line-chart/index.tsx +27 -2
- package/src/components/message-scroller/index.tsx +273 -13
- package/src/components/message-scroller/message-scroller-math.ts +62 -0
- package/src/components/meter/index.tsx +318 -0
- package/src/components/meter/meter-scale.ts +109 -0
- package/src/components/planner/index.tsx +886 -0
- package/src/components/planner/planner-entries.ts +146 -0
- package/src/components/popover/index.tsx +3 -0
- package/src/components/radar-chart/index.tsx +24 -0
- package/src/components/scatter-chart/index.tsx +27 -2
- package/src/components/selection-mode/index.tsx +37 -30
- package/src/components/selection-mode/selection-mode-contracts.ts +53 -0
- package/src/components/signature/index.tsx +107 -20
- package/src/components/signature/signature-accessibility.ts +67 -0
- package/src/components/skeleton/index.tsx +59 -5
- package/src/components/slider/index.tsx +28 -18
- package/src/components/spinner/index.tsx +80 -13
- package/src/components/time-picker/index.tsx +57 -29
- package/src/components/time-picker/ruler-window.ts +24 -0
- package/src/hooks/breakpoint-contract.ts +31 -0
- package/src/hooks/index.ts +4 -0
- package/src/hooks/use-breakpoint.ts +61 -10
- package/src/index.ts +32 -0
- package/src/primitives/chart-accessibility.ts +102 -0
- 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
|
+
}
|