glacia-native 0.3.5 → 0.5.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/lib/module/Bars.js +176 -0
- package/lib/module/Button.js +15 -2
- package/lib/module/Card.js +29 -14
- package/lib/module/Carousel.js +103 -0
- package/lib/module/Dialog.js +43 -32
- package/lib/module/Display.js +240 -0
- package/lib/module/Feedback.js +172 -0
- package/lib/module/Form.js +51 -12
- package/lib/module/Forms2.js +227 -0
- package/lib/module/Glass.js +11 -2
- package/lib/module/Input.js +19 -7
- package/lib/module/Overlays.js +166 -0
- package/lib/module/Pickers.js +243 -0
- package/lib/module/SegmentedControl.js +65 -21
- package/lib/module/Slider.js +121 -25
- package/lib/module/Switch.js +33 -15
- package/lib/module/TabBar.js +63 -25
- package/lib/module/Table.js +24 -6
- package/lib/module/Tabs.js +55 -33
- package/lib/module/Text.js +37 -0
- package/lib/module/ai.js +49 -20
- package/lib/module/charts/BarChart.js +4 -2
- package/lib/module/charts/FunnelChart.js +4 -2
- package/lib/module/charts/HeatmapChart.js +4 -2
- package/lib/module/charts/LineChart.js +4 -2
- package/lib/module/charts/PlotChart.js +11 -3
- package/lib/module/charts/RadarChart.js +4 -2
- package/lib/module/charts/RingChart.js +4 -2
- package/lib/module/charts/ScatterChart.js +20 -3
- package/lib/module/charts/WaterfallChart.js +12 -3
- package/lib/module/charts/advanced.js +12 -6
- package/lib/module/charts/extra.js +6 -3
- package/lib/module/index.js +58 -1
- package/lib/module/internal/GlassView.js +31 -17
- package/lib/module/internal/fallback.js +14 -3
- package/lib/module/internal/glass-routing.js +56 -0
- package/lib/module/internal/motion.js +55 -0
- package/lib/module/internal/primitives.js +413 -0
- package/lib/module/internal/tokens.js +55 -1
- package/lib/typescript/Bars.d.ts +121 -0
- package/lib/typescript/Button.d.ts +11 -1
- package/lib/typescript/Card.d.ts +20 -8
- package/lib/typescript/Carousel.d.ts +47 -0
- package/lib/typescript/Dialog.d.ts +19 -6
- package/lib/typescript/Display.d.ts +119 -0
- package/lib/typescript/Feedback.d.ts +138 -0
- package/lib/typescript/Form.d.ts +27 -11
- package/lib/typescript/Forms2.d.ts +128 -0
- package/lib/typescript/Glass.d.ts +10 -1
- package/lib/typescript/Input.d.ts +11 -1
- package/lib/typescript/Overlays.d.ts +105 -0
- package/lib/typescript/Pickers.d.ts +105 -0
- package/lib/typescript/SegmentedControl.d.ts +11 -1
- package/lib/typescript/Slider.d.ts +13 -1
- package/lib/typescript/Switch.d.ts +12 -1
- package/lib/typescript/TabBar.d.ts +13 -1
- package/lib/typescript/Table.d.ts +14 -5
- package/lib/typescript/Tabs.d.ts +8 -5
- package/lib/typescript/Text.d.ts +28 -0
- package/lib/typescript/ai.d.ts +12 -9
- package/lib/typescript/charts/BarChart.d.ts +1 -1
- package/lib/typescript/charts/FunnelChart.d.ts +1 -1
- package/lib/typescript/charts/HeatmapChart.d.ts +1 -1
- package/lib/typescript/charts/LineChart.d.ts +1 -1
- package/lib/typescript/charts/PlotChart.d.ts +1 -1
- package/lib/typescript/charts/RadarChart.d.ts +1 -1
- package/lib/typescript/charts/RingChart.d.ts +1 -1
- package/lib/typescript/charts/ScatterChart.d.ts +1 -1
- package/lib/typescript/charts/WaterfallChart.d.ts +1 -1
- package/lib/typescript/charts/advanced.d.ts +5 -5
- package/lib/typescript/charts/extra.d.ts +2 -2
- package/lib/typescript/index.d.ts +9 -1
- package/lib/typescript/internal/GlassView.d.ts +21 -4
- package/lib/typescript/internal/fallback.d.ts +10 -1
- package/lib/typescript/internal/glass-routing.d.ts +30 -0
- package/lib/typescript/internal/motion.d.ts +19 -0
- package/lib/typescript/internal/primitives.d.ts +41 -0
- package/lib/typescript/internal/tokens.d.ts +61 -0
- package/package.json +2 -2
- package/src/Bars.tsx +570 -0
- package/src/Button.tsx +47 -22
- package/src/Card.tsx +52 -31
- package/src/Carousel.tsx +233 -0
- package/src/Dialog.tsx +99 -64
- package/src/Display.tsx +550 -0
- package/src/Feedback.tsx +579 -0
- package/src/Form.tsx +90 -25
- package/src/Forms2.tsx +608 -0
- package/src/Glass.tsx +24 -1
- package/src/Input.tsx +33 -12
- package/src/Overlays.tsx +474 -0
- package/src/Pickers.tsx +590 -0
- package/src/SegmentedControl.tsx +104 -32
- package/src/Slider.tsx +151 -47
- package/src/Switch.tsx +56 -32
- package/src/TabBar.tsx +100 -30
- package/src/Table.tsx +38 -14
- package/src/Tabs.tsx +85 -32
- package/src/Text.tsx +83 -0
- package/src/ai.tsx +77 -31
- package/src/charts/BarChart.tsx +4 -2
- package/src/charts/FunnelChart.tsx +4 -2
- package/src/charts/HeatmapChart.tsx +4 -2
- package/src/charts/LineChart.tsx +4 -2
- package/src/charts/PlotChart.tsx +23 -3
- package/src/charts/RadarChart.tsx +4 -2
- package/src/charts/RingChart.tsx +4 -2
- package/src/charts/ScatterChart.tsx +24 -3
- package/src/charts/WaterfallChart.tsx +21 -3
- package/src/charts/advanced.tsx +12 -6
- package/src/charts/extra.tsx +6 -3
- package/src/index.ts +101 -1
- package/src/internal/GlassView.tsx +51 -19
- package/src/internal/fallback.tsx +23 -8
- package/src/internal/glass-routing.ts +68 -0
- package/src/internal/motion.ts +60 -0
- package/src/internal/primitives.ts +482 -0
- package/src/internal/tokens.ts +62 -0
- package/lib/module/charts/primitives.js +0 -212
- package/lib/typescript/charts/primitives.d.ts +0 -23
- package/src/charts/primitives.ts +0 -262
package/src/index.ts
CHANGED
|
@@ -86,5 +86,105 @@ export {
|
|
|
86
86
|
} from "./Table";
|
|
87
87
|
// Charts, drawn with react-native-svg using the same maths as the web package.
|
|
88
88
|
export * from "./charts";
|
|
89
|
+
// Basics.
|
|
90
|
+
export { Text, type TextProps, type TextVariant } from "./Text";
|
|
91
|
+
export {
|
|
92
|
+
Avatar,
|
|
93
|
+
Badge,
|
|
94
|
+
Separator,
|
|
95
|
+
Skeleton,
|
|
96
|
+
Spinner,
|
|
97
|
+
Progress,
|
|
98
|
+
initialsOf,
|
|
99
|
+
type AvatarProps,
|
|
100
|
+
type AvatarSize,
|
|
101
|
+
type BadgeProps,
|
|
102
|
+
type SeparatorProps,
|
|
103
|
+
type SkeletonProps,
|
|
104
|
+
type SpinnerProps,
|
|
105
|
+
type SpinnerSize,
|
|
106
|
+
type ProgressProps,
|
|
107
|
+
type ProgressVariant,
|
|
108
|
+
} from "./Display";
|
|
109
|
+
// Feedback and display.
|
|
110
|
+
export {
|
|
111
|
+
Toast,
|
|
112
|
+
Banner,
|
|
113
|
+
List,
|
|
114
|
+
ListItem,
|
|
115
|
+
ListSection,
|
|
116
|
+
Accordion,
|
|
117
|
+
type ToastProps,
|
|
118
|
+
type ToastAction,
|
|
119
|
+
type BannerProps,
|
|
120
|
+
type ListProps,
|
|
121
|
+
type ListItemProps,
|
|
122
|
+
type ListSectionProps,
|
|
123
|
+
type AccordionProps,
|
|
124
|
+
type AccordionSection,
|
|
125
|
+
} from "./Feedback";
|
|
126
|
+
// Bars, buttons and navigation.
|
|
127
|
+
export {
|
|
128
|
+
SearchBar,
|
|
129
|
+
Chip,
|
|
130
|
+
FAB,
|
|
131
|
+
IconButton,
|
|
132
|
+
AppBar,
|
|
133
|
+
type SearchBarProps,
|
|
134
|
+
type ChipProps,
|
|
135
|
+
type FABProps,
|
|
136
|
+
type IconButtonProps,
|
|
137
|
+
type AppBarProps,
|
|
138
|
+
type AppBarAction,
|
|
139
|
+
} from "./Bars";
|
|
140
|
+
// Overlays.
|
|
141
|
+
export {
|
|
142
|
+
Menu,
|
|
143
|
+
Sheet,
|
|
144
|
+
Drawer,
|
|
145
|
+
anchorPopover,
|
|
146
|
+
type MenuProps,
|
|
147
|
+
type MenuItem,
|
|
148
|
+
type SheetProps,
|
|
149
|
+
type DrawerProps,
|
|
150
|
+
} from "./Overlays";
|
|
151
|
+
// Carousel.
|
|
152
|
+
export {
|
|
153
|
+
Carousel,
|
|
154
|
+
PageControl,
|
|
155
|
+
type CarouselProps,
|
|
156
|
+
type PageControlProps,
|
|
157
|
+
} from "./Carousel";
|
|
158
|
+
// Forms and pickers.
|
|
159
|
+
export {
|
|
160
|
+
Stepper,
|
|
161
|
+
Toggle,
|
|
162
|
+
ToggleGroup,
|
|
163
|
+
InputOTP,
|
|
164
|
+
ButtonGroup,
|
|
165
|
+
type StepperProps,
|
|
166
|
+
type ToggleProps,
|
|
167
|
+
type ToggleGroupProps,
|
|
168
|
+
type ToggleGroupItem,
|
|
169
|
+
type InputOTPProps,
|
|
170
|
+
type ButtonGroupProps,
|
|
171
|
+
type ButtonGroupItem,
|
|
172
|
+
} from "./Forms2";
|
|
173
|
+
export {
|
|
174
|
+
Calendar,
|
|
175
|
+
DatePicker,
|
|
176
|
+
TimePicker,
|
|
177
|
+
monthGrid,
|
|
178
|
+
startOfDay,
|
|
179
|
+
sameDay,
|
|
180
|
+
parseTime,
|
|
181
|
+
formatTime,
|
|
182
|
+
WEEKDAYS,
|
|
183
|
+
MONTHS,
|
|
184
|
+
type CalendarProps,
|
|
185
|
+
type CalendarCell,
|
|
186
|
+
type DatePickerProps,
|
|
187
|
+
type TimePickerProps,
|
|
188
|
+
} from "./Pickers";
|
|
89
189
|
export { isGlassSupported } from "./internal/GlassView";
|
|
90
|
-
export { colors, radii, springs } from "./internal/tokens";
|
|
190
|
+
export { colors, radii, springs, typeScale, toneColor, type Tone } from "./internal/tokens";
|
|
@@ -1,59 +1,89 @@
|
|
|
1
1
|
import { forwardRef, type ReactNode } from "react";
|
|
2
|
-
import {
|
|
2
|
+
import type { StyleProp, ViewStyle } from "react-native";
|
|
3
3
|
|
|
4
|
-
|
|
4
|
+
import { FallbackGlassView } from "./fallback";
|
|
5
|
+
import { defaultRouter } from "./glass-routing";
|
|
6
|
+
import { useIsDark } from "./motion";
|
|
5
7
|
|
|
6
8
|
export interface NativeGlassProps {
|
|
7
9
|
effect?: "clear" | "regular" | "none";
|
|
8
10
|
tintColor?: string;
|
|
9
11
|
interactive?: boolean;
|
|
10
|
-
|
|
12
|
+
/** Passed to whichever surface renders, so tests can find the glass itself. */
|
|
13
|
+
testID?: string;
|
|
14
|
+
style?: StyleProp<ViewStyle>;
|
|
11
15
|
children?: ReactNode;
|
|
16
|
+
/**
|
|
17
|
+
* Explicit dark override for the fallback tint. When omitted the phone's
|
|
18
|
+
* colour scheme decides, so developers never thread this prop themselves.
|
|
19
|
+
*/
|
|
20
|
+
isDark?: boolean;
|
|
21
|
+
/**
|
|
22
|
+
* Any further React Native view prop (accessibility, onLayout, pointer
|
|
23
|
+
* events, …) is passed straight through to whichever surface renders, so a
|
|
24
|
+
* component can put its own accessibility contract on the glass itself.
|
|
25
|
+
*/
|
|
26
|
+
[prop: string]: any;
|
|
12
27
|
}
|
|
13
28
|
|
|
14
|
-
|
|
15
|
-
|
|
29
|
+
/**
|
|
30
|
+
* The installed `@callstack/liquid-glass` module's routing decision.
|
|
31
|
+
*
|
|
32
|
+
* Resolved lazily and memoised on first use, rather than at module load, so the
|
|
33
|
+
* decision is taken when a surface first renders. On a device that is the same
|
|
34
|
+
* answer either way — `isLiquidGlassSupported` is a constant of the installed
|
|
35
|
+
* build — but it keeps the choice a property of the module rather than of
|
|
36
|
+
* import order, which is what makes both branches reachable in tests.
|
|
37
|
+
*/
|
|
38
|
+
let cachedRouter: ReturnType<typeof defaultRouter> | null = null;
|
|
16
39
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
40
|
+
function router(): ReturnType<typeof defaultRouter> {
|
|
41
|
+
if (cachedRouter === null) cachedRouter = defaultRouter();
|
|
42
|
+
return cachedRouter;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** Clears the memoised decision. Only tests call this, to drive both branches. */
|
|
46
|
+
export function resetGlassRouter() {
|
|
47
|
+
cachedRouter = null;
|
|
25
48
|
}
|
|
26
49
|
|
|
27
|
-
|
|
50
|
+
/** Whether this build renders Apple's glass. */
|
|
51
|
+
export const isGlassSupported = defaultRouter().supported;
|
|
28
52
|
|
|
29
53
|
/**
|
|
30
|
-
* Platform Glass View:
|
|
54
|
+
* Platform Glass View — the single place a glass surface is chosen:
|
|
31
55
|
* - iOS 26+: Real UIGlassEffect via @callstack/liquid-glass
|
|
32
|
-
* - iOS < 26 / Android / Expo Go
|
|
56
|
+
* - iOS < 26 / Android / Expo Go: translucent tint View with identical layout
|
|
33
57
|
*/
|
|
34
58
|
export const GlassView = forwardRef<any, NativeGlassProps>(function GlassView(
|
|
35
59
|
{
|
|
36
60
|
effect = "regular",
|
|
37
61
|
tintColor,
|
|
38
62
|
interactive = false,
|
|
63
|
+
testID,
|
|
39
64
|
style,
|
|
40
65
|
children,
|
|
66
|
+
isDark: isDarkProp,
|
|
41
67
|
...props
|
|
42
68
|
},
|
|
43
69
|
ref,
|
|
44
70
|
) {
|
|
45
|
-
|
|
71
|
+
const isDark = useIsDark(isDarkProp);
|
|
72
|
+
const decision = router();
|
|
73
|
+
if (decision.supported && decision.LiquidGlassView) {
|
|
74
|
+
const LiquidGlassView = decision.LiquidGlassView;
|
|
46
75
|
return (
|
|
47
|
-
<
|
|
76
|
+
<LiquidGlassView
|
|
48
77
|
ref={ref}
|
|
49
78
|
effect={effect}
|
|
50
79
|
tintColor={tintColor}
|
|
51
80
|
interactive={interactive}
|
|
81
|
+
testID={testID}
|
|
52
82
|
style={style}
|
|
53
83
|
{...props}
|
|
54
84
|
>
|
|
55
85
|
{children}
|
|
56
|
-
</
|
|
86
|
+
</LiquidGlassView>
|
|
57
87
|
);
|
|
58
88
|
}
|
|
59
89
|
|
|
@@ -62,6 +92,8 @@ export const GlassView = forwardRef<any, NativeGlassProps>(function GlassView(
|
|
|
62
92
|
ref={ref}
|
|
63
93
|
effect={effect}
|
|
64
94
|
tintColor={tintColor}
|
|
95
|
+
testID={testID}
|
|
96
|
+
isDark={isDark}
|
|
65
97
|
style={style}
|
|
66
98
|
{...props}
|
|
67
99
|
>
|
|
@@ -1,18 +1,30 @@
|
|
|
1
1
|
import { forwardRef, type ReactNode } from "react";
|
|
2
|
+
import type { StyleProp, ViewStyle } from "react-native";
|
|
3
|
+
|
|
2
4
|
import { colors, radii } from "./tokens";
|
|
5
|
+
import { primitives } from "./primitives";
|
|
3
6
|
|
|
4
7
|
export interface FallbackGlassProps {
|
|
5
8
|
effect?: "clear" | "regular" | "none";
|
|
6
9
|
tintColor?: string;
|
|
7
|
-
|
|
10
|
+
testID?: string;
|
|
11
|
+
style?: StyleProp<ViewStyle>;
|
|
8
12
|
children?: ReactNode;
|
|
9
13
|
isDark?: boolean;
|
|
10
14
|
}
|
|
11
15
|
|
|
16
|
+
/**
|
|
17
|
+
* Glacia's own glass surface for every platform where Apple's material is not
|
|
18
|
+
* available: Android, iOS before 26, and Expo Go.
|
|
19
|
+
*
|
|
20
|
+
* It renders the resolver's real React Native `View` with the same shape and
|
|
21
|
+
* layout as the native glass, so switching branches never shifts the layout.
|
|
22
|
+
*/
|
|
12
23
|
export const FallbackGlassView = forwardRef<any, FallbackGlassProps>(function FallbackGlassView(
|
|
13
24
|
{
|
|
14
25
|
effect = "regular",
|
|
15
26
|
tintColor,
|
|
27
|
+
testID,
|
|
16
28
|
style,
|
|
17
29
|
children,
|
|
18
30
|
isDark = false,
|
|
@@ -21,26 +33,29 @@ export const FallbackGlassView = forwardRef<any, FallbackGlassProps>(function Fa
|
|
|
21
33
|
ref,
|
|
22
34
|
) {
|
|
23
35
|
const themeColors = isDark ? colors.dark : colors.light;
|
|
36
|
+
const { View } = primitives();
|
|
24
37
|
|
|
25
38
|
const bg =
|
|
26
39
|
effect === "none"
|
|
27
40
|
? "transparent"
|
|
28
41
|
: effect === "clear"
|
|
29
|
-
|
|
30
|
-
|
|
42
|
+
? isDark
|
|
43
|
+
? "rgba(28, 28, 30, 0.35)"
|
|
44
|
+
: "rgba(255, 255, 255, 0.35)"
|
|
45
|
+
: tintColor || themeColors.glass;
|
|
31
46
|
|
|
32
|
-
const containerStyle = {
|
|
47
|
+
const containerStyle: ViewStyle = {
|
|
33
48
|
backgroundColor: bg,
|
|
34
49
|
borderColor: themeColors.edge,
|
|
35
50
|
borderWidth: 0.5,
|
|
36
51
|
borderRadius: radii.md,
|
|
37
|
-
overflow: "hidden"
|
|
38
|
-
...style,
|
|
52
|
+
overflow: "hidden",
|
|
53
|
+
...(style as ViewStyle),
|
|
39
54
|
};
|
|
40
55
|
|
|
41
56
|
return (
|
|
42
|
-
<
|
|
57
|
+
<View ref={ref} testID={testID} style={containerStyle} {...props}>
|
|
43
58
|
{children}
|
|
44
|
-
</
|
|
59
|
+
</View>
|
|
45
60
|
);
|
|
46
61
|
});
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
// The glass switch — the law of this library.
|
|
2
|
+
//
|
|
3
|
+
// Every glass surface in every Glacia Native component goes through
|
|
4
|
+
// `GlassView`, which routes here. On iOS 26+ `@callstack/liquid-glass` reports
|
|
5
|
+
// `isLiquidGlassSupported === true` and the surface is rendered by Apple's
|
|
6
|
+
// real `UIGlassEffect` (`LiquidGlassView`). Everywhere else — older iOS,
|
|
7
|
+
// Android, Expo Go, or a build without the module — it is Glacia's own
|
|
8
|
+
// translucent fallback of identical shape.
|
|
9
|
+
//
|
|
10
|
+
// The decision is split out from the React component so both branches can be
|
|
11
|
+
// exercised in tests: `routerFor` takes the module as an argument, and the
|
|
12
|
+
// default export routes on whatever is installed. On a device the argument is
|
|
13
|
+
// always the installed module, so the routing logic is unchanged.
|
|
14
|
+
|
|
15
|
+
declare const require: any;
|
|
16
|
+
|
|
17
|
+
export interface LiquidGlassModule {
|
|
18
|
+
LiquidGlassView?: any;
|
|
19
|
+
isLiquidGlassSupported?: boolean;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** The routing decision for one glass surface. */
|
|
23
|
+
export interface GlassRouter {
|
|
24
|
+
/** True when Apple's real glass renders this surface. */
|
|
25
|
+
supported: boolean;
|
|
26
|
+
/** Apple's `LiquidGlassView`, or null when the fallback renders instead. */
|
|
27
|
+
LiquidGlassView: any;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Reads a liquid-glass module and decides which branch renders.
|
|
32
|
+
*
|
|
33
|
+
* A module only counts as supported when it exports the view *and* reports
|
|
34
|
+
* support; a missing export falls back rather than crashing.
|
|
35
|
+
*/
|
|
36
|
+
export function routerFor(module: LiquidGlassModule | null | undefined): GlassRouter {
|
|
37
|
+
const view = module?.LiquidGlassView ?? null;
|
|
38
|
+
return {
|
|
39
|
+
supported: Boolean(module?.isLiquidGlassSupported) && Boolean(view),
|
|
40
|
+
LiquidGlassView: view,
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Loads `@callstack/liquid-glass` at runtime.
|
|
46
|
+
*
|
|
47
|
+
* Resolved through `require` rather than a static import so the package stays
|
|
48
|
+
* importable where the native module is absent (jsdom tests, web preview).
|
|
49
|
+
* `isLiquidGlassSupported` is a constant baked by the package's own platform
|
|
50
|
+
* resolution: its iOS build reads the native module and answers true only on
|
|
51
|
+
* iOS 26+; the default build answers false.
|
|
52
|
+
*/
|
|
53
|
+
export function loadLiquidGlass(): LiquidGlassModule | null {
|
|
54
|
+
try {
|
|
55
|
+
const module = require("@callstack/liquid-glass");
|
|
56
|
+
if (module && (module.LiquidGlassView || module.isLiquidGlassSupported !== undefined)) {
|
|
57
|
+
return module as LiquidGlassModule;
|
|
58
|
+
}
|
|
59
|
+
return null;
|
|
60
|
+
} catch {
|
|
61
|
+
return null;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** The installed module's routing decision, computed once per process. */
|
|
66
|
+
export function defaultRouter(): GlassRouter {
|
|
67
|
+
return routerFor(loadLiquidGlass());
|
|
68
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
// Shared component plumbing: dark-mode defaulting and spring motion.
|
|
2
|
+
//
|
|
3
|
+
// Two rules apply to every Glacia component, so they live here instead of
|
|
4
|
+
// being repeated (and forgotten) in each file:
|
|
5
|
+
// - `useIsDark` falls back to the phone's own colour scheme when the caller
|
|
6
|
+
// does not pass `isDark`, so a theme-aware app needs no extra prop threading.
|
|
7
|
+
// - `useSpring` animates a number towards its target with the package's spring
|
|
8
|
+
// tokens through React Native's Animated API. No CSS, no timers of our own.
|
|
9
|
+
|
|
10
|
+
import { useEffect, useRef } from "react";
|
|
11
|
+
|
|
12
|
+
import { primitives } from "./primitives";
|
|
13
|
+
import { springs } from "./tokens";
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Resolves the theme. An explicit `isDark` always wins; otherwise the phone's
|
|
17
|
+
* colour scheme decides, defaulting to light when it is unknown.
|
|
18
|
+
*/
|
|
19
|
+
export function useIsDark(isDark?: boolean): boolean {
|
|
20
|
+
const { useColorScheme } = primitives();
|
|
21
|
+
const scheme = typeof useColorScheme === "function" ? useColorScheme() : null;
|
|
22
|
+
return isDark ?? scheme === "dark";
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export type SpringName = keyof typeof springs;
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* A value that springs towards `target` whenever it changes.
|
|
29
|
+
*
|
|
30
|
+
* Returns the `Animated.Value` to put in a style, plus whether motion is real
|
|
31
|
+
* on this platform. A caller that gets `animated === false` renders the target
|
|
32
|
+
* directly, so the DOM preview and tests show the settled state rather than an
|
|
33
|
+
* animation the environment cannot advance.
|
|
34
|
+
*/
|
|
35
|
+
export function useSpring(target: number, spring: SpringName = "snappy") {
|
|
36
|
+
const { Animated, isNative } = primitives();
|
|
37
|
+
const animated = Boolean(isNative && Animated?.Value && Animated?.spring);
|
|
38
|
+
|
|
39
|
+
// Off-device there is no Animated; a plain ref keeps the call sites identical.
|
|
40
|
+
const value = useRef<any>(null);
|
|
41
|
+
if (value.current === null) {
|
|
42
|
+
value.current = animated ? new Animated.Value(target) : { value: target };
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
useEffect(() => {
|
|
46
|
+
if (!animated) {
|
|
47
|
+
value.current.value = target;
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
const animation = Animated.spring(value.current, {
|
|
51
|
+
toValue: target,
|
|
52
|
+
...springs[spring],
|
|
53
|
+
useNativeDriver: false,
|
|
54
|
+
});
|
|
55
|
+
animation.start();
|
|
56
|
+
return () => animation.stop?.();
|
|
57
|
+
}, [animated, spring, target]);
|
|
58
|
+
|
|
59
|
+
return { value: value.current, animated };
|
|
60
|
+
}
|