glacia-native 0.0.0-stage → 0.3.1
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/LICENSE +27 -0
- package/lib/module/Button.js +37 -0
- package/lib/module/Card.js +60 -0
- package/lib/module/Glass.js +9 -0
- package/lib/module/SegmentedControl.js +46 -0
- package/lib/module/Slider.js +49 -0
- package/lib/module/Switch.js +42 -0
- package/lib/module/TabBar.js +47 -0
- package/lib/module/Tabs.js +59 -0
- package/lib/module/index.js +31 -0
- package/lib/module/internal/GlassView.js +30 -0
- package/lib/module/internal/fallback.js +23 -0
- package/lib/module/internal/tokens.js +36 -0
- package/lib/typescript/Button.d.ts +14 -0
- package/lib/typescript/Card.d.ts +31 -0
- package/lib/typescript/Glass.d.ts +5 -0
- package/lib/typescript/SegmentedControl.d.ts +9 -0
- package/lib/typescript/Slider.d.ts +13 -0
- package/lib/typescript/Switch.d.ts +10 -0
- package/lib/typescript/TabBar.d.ts +15 -0
- package/lib/typescript/Tabs.d.ts +28 -0
- package/lib/typescript/index.d.ts +10 -0
- package/lib/typescript/internal/GlassView.d.ts +15 -0
- package/lib/typescript/internal/fallback.d.ts +9 -0
- package/lib/typescript/internal/tokens.d.ts +44 -0
- package/package.json +41 -3
- package/src/Button.tsx +87 -0
- package/src/Card.tsx +120 -0
- package/src/Glass.tsx +24 -0
- package/src/SegmentedControl.tsx +79 -0
- package/src/Slider.tsx +97 -0
- package/src/Switch.tsx +80 -0
- package/src/TabBar.tsx +87 -0
- package/src/Tabs.tsx +108 -0
- package/src/index.ts +22 -0
- package/src/internal/GlassView.tsx +71 -0
- package/src/internal/fallback.tsx +46 -0
- package/src/internal/tokens.ts +36 -0
- package/README.md +0 -3
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export { Glass, type GlassProps } from "./Glass";
|
|
2
|
+
export { Button, type ButtonProps } from "./Button";
|
|
3
|
+
export { Switch, type SwitchProps } from "./Switch";
|
|
4
|
+
export { Slider, type SliderProps } from "./Slider";
|
|
5
|
+
export { SegmentedControl, type SegmentedControlProps, } from "./SegmentedControl";
|
|
6
|
+
export { TabBar, type TabBarProps, type TabBarItem } from "./TabBar";
|
|
7
|
+
export { Tabs, type TabsProps, type TabItem } from "./Tabs";
|
|
8
|
+
export { Card, CardHeader, CardTitle, CardDescription, CardContent, CardBody, CardFooter, type CardProps, } from "./Card";
|
|
9
|
+
export { isGlassSupported } from "./internal/GlassView";
|
|
10
|
+
export { colors, radii, springs } from "./internal/tokens";
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { type ReactNode } from "react";
|
|
2
|
+
export interface NativeGlassProps {
|
|
3
|
+
effect?: "clear" | "regular" | "none";
|
|
4
|
+
tintColor?: string;
|
|
5
|
+
interactive?: boolean;
|
|
6
|
+
style?: any;
|
|
7
|
+
children?: ReactNode;
|
|
8
|
+
}
|
|
9
|
+
export declare const isGlassSupported: boolean;
|
|
10
|
+
/**
|
|
11
|
+
* Platform Glass View:
|
|
12
|
+
* - iOS 26+: Real UIGlassEffect via @callstack/liquid-glass
|
|
13
|
+
* - iOS < 26 / Android / Expo Go / Fallback: Smooth translucent tint View with identical layout
|
|
14
|
+
*/
|
|
15
|
+
export declare const GlassView: import("react").ForwardRefExoticComponent<NativeGlassProps & import("react").RefAttributes<any>>;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { type ReactNode } from "react";
|
|
2
|
+
export interface FallbackGlassProps {
|
|
3
|
+
effect?: "clear" | "regular" | "none";
|
|
4
|
+
tintColor?: string;
|
|
5
|
+
style?: any;
|
|
6
|
+
children?: ReactNode;
|
|
7
|
+
isDark?: boolean;
|
|
8
|
+
}
|
|
9
|
+
export declare const FallbackGlassView: import("react").ForwardRefExoticComponent<FallbackGlassProps & import("react").RefAttributes<any>>;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
export declare const radii: {
|
|
2
|
+
sm: 8;
|
|
3
|
+
md: 14;
|
|
4
|
+
lg: 20;
|
|
5
|
+
xl: 28;
|
|
6
|
+
pill: 9999;
|
|
7
|
+
};
|
|
8
|
+
export declare const springs: {
|
|
9
|
+
gentle: {
|
|
10
|
+
readonly stiffness: 170;
|
|
11
|
+
readonly damping: 26;
|
|
12
|
+
readonly mass: 1;
|
|
13
|
+
};
|
|
14
|
+
snappy: {
|
|
15
|
+
readonly stiffness: 300;
|
|
16
|
+
readonly damping: 28;
|
|
17
|
+
readonly mass: 1;
|
|
18
|
+
};
|
|
19
|
+
bouncy: {
|
|
20
|
+
readonly stiffness: 400;
|
|
21
|
+
readonly damping: 20;
|
|
22
|
+
readonly mass: 1;
|
|
23
|
+
};
|
|
24
|
+
};
|
|
25
|
+
export declare const colors: {
|
|
26
|
+
light: {
|
|
27
|
+
foreground: string;
|
|
28
|
+
muted: string;
|
|
29
|
+
glass: string;
|
|
30
|
+
glassActive: string;
|
|
31
|
+
edge: string;
|
|
32
|
+
fill: string;
|
|
33
|
+
destructive: string;
|
|
34
|
+
};
|
|
35
|
+
dark: {
|
|
36
|
+
foreground: string;
|
|
37
|
+
muted: string;
|
|
38
|
+
glass: string;
|
|
39
|
+
glassActive: string;
|
|
40
|
+
edge: string;
|
|
41
|
+
fill: string;
|
|
42
|
+
destructive: string;
|
|
43
|
+
};
|
|
44
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,44 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "glacia-native",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "0.3.1",
|
|
4
|
+
"description": "Liquid Glass UI components for React Native — real iOS 26 glass, with a clean fallback on Android and older iOS.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "https://github.com/binyamfeyissa/Glacia.git",
|
|
9
|
+
"directory": "packages/native"
|
|
10
|
+
},
|
|
11
|
+
"homepage": "https://github.com/binyamfeyissa/Glacia#readme",
|
|
12
|
+
"publishConfig": {
|
|
13
|
+
"access": "public"
|
|
14
|
+
},
|
|
15
|
+
"main": "lib/commonjs/index.js",
|
|
16
|
+
"module": "lib/module/index.js",
|
|
17
|
+
"types": "lib/typescript/index.d.ts",
|
|
18
|
+
"react-native": "src/index.ts",
|
|
19
|
+
"files": [
|
|
20
|
+
"lib",
|
|
21
|
+
"src"
|
|
22
|
+
],
|
|
23
|
+
"peerDependencies": {
|
|
24
|
+
"react": ">=18",
|
|
25
|
+
"react-native": ">=0.80",
|
|
26
|
+
"@callstack/liquid-glass": ">=0.7.0"
|
|
27
|
+
},
|
|
28
|
+
"dependencies": {
|
|
29
|
+
"glacia-tokens": "0.3.1"
|
|
30
|
+
},
|
|
31
|
+
"devDependencies": {
|
|
32
|
+
"@testing-library/react": "^16.3.3",
|
|
33
|
+
"@types/react": "^19.3.0",
|
|
34
|
+
"jsdom": "^29.1.1",
|
|
35
|
+
"react": "^19.3.1",
|
|
36
|
+
"typescript": "^5.7.3",
|
|
37
|
+
"vitest": "^3.0.0"
|
|
38
|
+
},
|
|
39
|
+
"scripts": {
|
|
40
|
+
"build": "tsc --project tsconfig.build.json",
|
|
41
|
+
"typecheck": "tsc --noEmit",
|
|
42
|
+
"test": "vitest run"
|
|
43
|
+
}
|
|
6
44
|
}
|
package/src/Button.tsx
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { forwardRef, type ReactNode } from "react";
|
|
2
|
+
import { Glass } from "./Glass";
|
|
3
|
+
import { colors, radii } from "./internal/tokens";
|
|
4
|
+
|
|
5
|
+
export interface ButtonProps {
|
|
6
|
+
variant?: "glass" | "filled" | "ghost";
|
|
7
|
+
size?: "sm" | "md" | "lg";
|
|
8
|
+
disabled?: boolean;
|
|
9
|
+
onPress?: () => void;
|
|
10
|
+
accessibilityLabel: string;
|
|
11
|
+
icon?: ReactNode;
|
|
12
|
+
children?: ReactNode;
|
|
13
|
+
style?: any;
|
|
14
|
+
tint?: string;
|
|
15
|
+
isDark?: boolean;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const sizeHeights = {
|
|
19
|
+
sm: 32,
|
|
20
|
+
md: 40,
|
|
21
|
+
lg: 48,
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
export const Button = forwardRef<any, ButtonProps>(function Button(
|
|
25
|
+
{
|
|
26
|
+
variant = "glass",
|
|
27
|
+
size = "md",
|
|
28
|
+
disabled = false,
|
|
29
|
+
onPress,
|
|
30
|
+
accessibilityLabel,
|
|
31
|
+
icon,
|
|
32
|
+
children,
|
|
33
|
+
style,
|
|
34
|
+
tint,
|
|
35
|
+
isDark = false,
|
|
36
|
+
...props
|
|
37
|
+
},
|
|
38
|
+
ref,
|
|
39
|
+
) {
|
|
40
|
+
const themeColors = isDark ? colors.dark : colors.light;
|
|
41
|
+
const height = sizeHeights[size];
|
|
42
|
+
|
|
43
|
+
const buttonStyle = {
|
|
44
|
+
height,
|
|
45
|
+
borderRadius: height / 2,
|
|
46
|
+
flexDirection: "row" as const,
|
|
47
|
+
alignItems: "center" as const,
|
|
48
|
+
justifyContent: "center" as const,
|
|
49
|
+
paddingHorizontal: size === "sm" ? 12 : size === "lg" ? 22 : 16,
|
|
50
|
+
opacity: disabled ? 0.38 : 1,
|
|
51
|
+
backgroundColor:
|
|
52
|
+
variant === "filled"
|
|
53
|
+
? tint || themeColors.fill
|
|
54
|
+
: variant === "ghost"
|
|
55
|
+
? "transparent"
|
|
56
|
+
: undefined,
|
|
57
|
+
...style,
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
const textStyle = {
|
|
61
|
+
color: variant === "filled" ? "#FFFFFF" : tint || themeColors.foreground,
|
|
62
|
+
fontSize: size === "sm" ? 13 : size === "lg" ? 16 : 14,
|
|
63
|
+
fontWeight: "500" as const,
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
return (
|
|
67
|
+
<div
|
|
68
|
+
ref={ref}
|
|
69
|
+
role="button"
|
|
70
|
+
tabIndex={disabled ? -1 : 0}
|
|
71
|
+
aria-label={accessibilityLabel}
|
|
72
|
+
aria-disabled={disabled}
|
|
73
|
+
onClick={disabled ? undefined : onPress}
|
|
74
|
+
style={{ display: "inline-block", cursor: disabled ? "default" : "pointer" }}
|
|
75
|
+
{...props}
|
|
76
|
+
>
|
|
77
|
+
<Glass
|
|
78
|
+
effect={variant === "ghost" ? "none" : "regular"}
|
|
79
|
+
interactive={!disabled}
|
|
80
|
+
style={buttonStyle}
|
|
81
|
+
>
|
|
82
|
+
{icon && <span style={{ marginRight: children ? 8 : 0 }}>{icon}</span>}
|
|
83
|
+
{typeof children === "string" ? <span style={textStyle}>{children}</span> : children}
|
|
84
|
+
</Glass>
|
|
85
|
+
</div>
|
|
86
|
+
);
|
|
87
|
+
});
|
package/src/Card.tsx
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import { forwardRef, type ReactNode } from "react";
|
|
2
|
+
import { Glass } from "./Glass";
|
|
3
|
+
import { colors, radii } from "./internal/tokens";
|
|
4
|
+
|
|
5
|
+
export interface CardProps {
|
|
6
|
+
effect?: "clear" | "regular" | "none";
|
|
7
|
+
style?: any;
|
|
8
|
+
children?: ReactNode;
|
|
9
|
+
isDark?: boolean;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export const Card = forwardRef<any, CardProps>(function Card(
|
|
13
|
+
{ effect = "regular", style, children, isDark = false, ...props },
|
|
14
|
+
ref,
|
|
15
|
+
) {
|
|
16
|
+
const cardStyle = {
|
|
17
|
+
padding: 20,
|
|
18
|
+
borderRadius: radii.lg,
|
|
19
|
+
display: "flex" as const,
|
|
20
|
+
flexDirection: "column" as const,
|
|
21
|
+
gap: 12,
|
|
22
|
+
...style,
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
return (
|
|
26
|
+
<Glass ref={ref} effect={effect} style={cardStyle} {...props}>
|
|
27
|
+
{children}
|
|
28
|
+
</Glass>
|
|
29
|
+
);
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
export function CardHeader({ style, children, ...props }: { style?: any; children?: ReactNode }) {
|
|
33
|
+
return (
|
|
34
|
+
<div style={{ display: "flex", flexDirection: "column", gap: 4, ...style }} {...props}>
|
|
35
|
+
{children}
|
|
36
|
+
</div>
|
|
37
|
+
);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function CardTitle({
|
|
41
|
+
style,
|
|
42
|
+
children,
|
|
43
|
+
isDark = false,
|
|
44
|
+
...props
|
|
45
|
+
}: {
|
|
46
|
+
style?: any;
|
|
47
|
+
children?: ReactNode;
|
|
48
|
+
isDark?: boolean;
|
|
49
|
+
}) {
|
|
50
|
+
const themeColors = isDark ? colors.dark : colors.light;
|
|
51
|
+
return (
|
|
52
|
+
<h3
|
|
53
|
+
style={{
|
|
54
|
+
margin: 0,
|
|
55
|
+
fontSize: 18,
|
|
56
|
+
fontWeight: 600,
|
|
57
|
+
color: themeColors.foreground,
|
|
58
|
+
...style,
|
|
59
|
+
}}
|
|
60
|
+
{...props}
|
|
61
|
+
>
|
|
62
|
+
{children}
|
|
63
|
+
</h3>
|
|
64
|
+
);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export function CardDescription({
|
|
68
|
+
style,
|
|
69
|
+
children,
|
|
70
|
+
isDark = false,
|
|
71
|
+
...props
|
|
72
|
+
}: {
|
|
73
|
+
style?: any;
|
|
74
|
+
children?: ReactNode;
|
|
75
|
+
isDark?: boolean;
|
|
76
|
+
}) {
|
|
77
|
+
const themeColors = isDark ? colors.dark : colors.light;
|
|
78
|
+
return (
|
|
79
|
+
<p
|
|
80
|
+
style={{
|
|
81
|
+
margin: 0,
|
|
82
|
+
fontSize: 13,
|
|
83
|
+
color: themeColors.muted,
|
|
84
|
+
...style,
|
|
85
|
+
}}
|
|
86
|
+
{...props}
|
|
87
|
+
>
|
|
88
|
+
{children}
|
|
89
|
+
</p>
|
|
90
|
+
);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export function CardContent({ style, children, ...props }: { style?: any; children?: ReactNode }) {
|
|
94
|
+
return (
|
|
95
|
+
<div style={{ display: "flex", flexDirection: "column", gap: 8, ...style }} {...props}>
|
|
96
|
+
{children}
|
|
97
|
+
</div>
|
|
98
|
+
);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export const CardBody = CardContent;
|
|
102
|
+
|
|
103
|
+
export function CardFooter({ style, children, ...props }: { style?: any; children?: ReactNode }) {
|
|
104
|
+
return (
|
|
105
|
+
<div
|
|
106
|
+
style={{
|
|
107
|
+
display: "flex",
|
|
108
|
+
flexDirection: "row",
|
|
109
|
+
alignItems: "center",
|
|
110
|
+
justifyContent: "flex-end",
|
|
111
|
+
gap: 8,
|
|
112
|
+
paddingTop: 8,
|
|
113
|
+
...style,
|
|
114
|
+
}}
|
|
115
|
+
{...props}
|
|
116
|
+
>
|
|
117
|
+
{children}
|
|
118
|
+
</div>
|
|
119
|
+
);
|
|
120
|
+
}
|
package/src/Glass.tsx
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { forwardRef } from "react";
|
|
2
|
+
import { GlassView, type NativeGlassProps } from "./internal/GlassView";
|
|
3
|
+
|
|
4
|
+
export interface GlassProps extends NativeGlassProps {
|
|
5
|
+
testID?: string;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export const Glass = forwardRef<any, GlassProps>(function Glass(
|
|
9
|
+
{ effect = "regular", tintColor, interactive = false, style, children, ...props },
|
|
10
|
+
ref,
|
|
11
|
+
) {
|
|
12
|
+
return (
|
|
13
|
+
<GlassView
|
|
14
|
+
ref={ref}
|
|
15
|
+
effect={effect}
|
|
16
|
+
tintColor={tintColor}
|
|
17
|
+
interactive={interactive}
|
|
18
|
+
style={style}
|
|
19
|
+
{...props}
|
|
20
|
+
>
|
|
21
|
+
{children}
|
|
22
|
+
</GlassView>
|
|
23
|
+
);
|
|
24
|
+
});
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { useState, forwardRef } from "react";
|
|
2
|
+
import { Glass } from "./Glass";
|
|
3
|
+
import { colors, radii } from "./internal/tokens";
|
|
4
|
+
|
|
5
|
+
export interface SegmentedControlProps {
|
|
6
|
+
values: string[];
|
|
7
|
+
selectedIndex?: number;
|
|
8
|
+
defaultIndex?: number;
|
|
9
|
+
onValueChange?: (value: string, index: number) => void;
|
|
10
|
+
style?: any;
|
|
11
|
+
isDark?: boolean;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export const SegmentedControl = forwardRef<any, SegmentedControlProps>(function SegmentedControl(
|
|
15
|
+
{
|
|
16
|
+
values,
|
|
17
|
+
selectedIndex: selectedProp,
|
|
18
|
+
defaultIndex = 0,
|
|
19
|
+
onValueChange,
|
|
20
|
+
style,
|
|
21
|
+
isDark = false,
|
|
22
|
+
...props
|
|
23
|
+
},
|
|
24
|
+
ref,
|
|
25
|
+
) {
|
|
26
|
+
const [internalIndex, setInternalIndex] = useState(defaultIndex);
|
|
27
|
+
const activeIndex = selectedProp !== undefined ? selectedProp : internalIndex;
|
|
28
|
+
const themeColors = isDark ? colors.dark : colors.light;
|
|
29
|
+
|
|
30
|
+
const handleSelect = (index: number) => {
|
|
31
|
+
if (selectedProp === undefined) {
|
|
32
|
+
setInternalIndex(index);
|
|
33
|
+
}
|
|
34
|
+
const val = values[index];
|
|
35
|
+
if (val !== undefined) {
|
|
36
|
+
onValueChange?.(val, index);
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
const containerStyle = {
|
|
41
|
+
position: "relative" as const,
|
|
42
|
+
display: "inline-flex" as const,
|
|
43
|
+
flexDirection: "row" as const,
|
|
44
|
+
alignItems: "center" as const,
|
|
45
|
+
padding: 2,
|
|
46
|
+
borderRadius: radii.md,
|
|
47
|
+
backgroundColor: "rgba(118, 118, 128, 0.12)",
|
|
48
|
+
...style,
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
return (
|
|
52
|
+
<Glass ref={ref} effect="regular" style={containerStyle} {...props}>
|
|
53
|
+
{values.map((label, idx) => {
|
|
54
|
+
const isSelected = idx === activeIndex;
|
|
55
|
+
return (
|
|
56
|
+
<button
|
|
57
|
+
key={label}
|
|
58
|
+
type="button"
|
|
59
|
+
onClick={() => handleSelect(idx)}
|
|
60
|
+
style={{
|
|
61
|
+
padding: "6px 16px",
|
|
62
|
+
borderRadius: radii.sm,
|
|
63
|
+
border: 0,
|
|
64
|
+
backgroundColor: isSelected ? "#FFFFFF" : "transparent",
|
|
65
|
+
color: isSelected ? "#000000" : themeColors.muted,
|
|
66
|
+
boxShadow: isSelected ? "0 1px 3px rgba(0, 0, 0, 0.12)" : "none",
|
|
67
|
+
fontSize: 13,
|
|
68
|
+
fontWeight: 500,
|
|
69
|
+
cursor: "pointer",
|
|
70
|
+
transition: "all 150ms ease",
|
|
71
|
+
}}
|
|
72
|
+
>
|
|
73
|
+
{label}
|
|
74
|
+
</button>
|
|
75
|
+
);
|
|
76
|
+
})}
|
|
77
|
+
</Glass>
|
|
78
|
+
);
|
|
79
|
+
});
|
package/src/Slider.tsx
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { useState, forwardRef } from "react";
|
|
2
|
+
import { Glass } from "./Glass";
|
|
3
|
+
import { colors } from "./internal/tokens";
|
|
4
|
+
|
|
5
|
+
export interface SliderProps {
|
|
6
|
+
value?: number;
|
|
7
|
+
defaultValue?: number;
|
|
8
|
+
min?: number;
|
|
9
|
+
max?: number;
|
|
10
|
+
step?: number;
|
|
11
|
+
onValueChange?: (value: number) => void;
|
|
12
|
+
disabled?: boolean;
|
|
13
|
+
accessibilityLabel: string;
|
|
14
|
+
style?: any;
|
|
15
|
+
isDark?: boolean;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export const Slider = forwardRef<any, SliderProps>(function Slider(
|
|
19
|
+
{
|
|
20
|
+
value: valueProp,
|
|
21
|
+
defaultValue = 50,
|
|
22
|
+
min = 0,
|
|
23
|
+
max = 100,
|
|
24
|
+
step = 1,
|
|
25
|
+
onValueChange,
|
|
26
|
+
disabled = false,
|
|
27
|
+
accessibilityLabel,
|
|
28
|
+
style,
|
|
29
|
+
isDark = false,
|
|
30
|
+
...props
|
|
31
|
+
},
|
|
32
|
+
ref,
|
|
33
|
+
) {
|
|
34
|
+
const [internalValue, setInternalValue] = useState(defaultValue);
|
|
35
|
+
const value = valueProp !== undefined ? valueProp : internalValue;
|
|
36
|
+
const themeColors = isDark ? colors.dark : colors.light;
|
|
37
|
+
|
|
38
|
+
const percentage = Math.min(100, Math.max(0, ((value - min) / (max - min)) * 100));
|
|
39
|
+
|
|
40
|
+
const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
|
|
41
|
+
const val = Number(e.target.value);
|
|
42
|
+
if (valueProp === undefined) {
|
|
43
|
+
setInternalValue(val);
|
|
44
|
+
}
|
|
45
|
+
onValueChange?.(val);
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
const containerStyle = {
|
|
49
|
+
position: "relative" as const,
|
|
50
|
+
height: 40,
|
|
51
|
+
justifyContent: "center" as const,
|
|
52
|
+
opacity: disabled ? 0.38 : 1,
|
|
53
|
+
...style,
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
const trackStyle = {
|
|
57
|
+
height: 8,
|
|
58
|
+
borderRadius: 4,
|
|
59
|
+
backgroundColor: "rgba(120, 120, 128, 0.2)",
|
|
60
|
+
overflow: "hidden" as const,
|
|
61
|
+
width: "100%",
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
const fillStyle = {
|
|
65
|
+
height: "100%",
|
|
66
|
+
width: `${percentage}%`,
|
|
67
|
+
backgroundColor: themeColors.fill,
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
return (
|
|
71
|
+
<div ref={ref} style={containerStyle} {...props}>
|
|
72
|
+
<Glass effect="regular" style={trackStyle}>
|
|
73
|
+
<div style={fillStyle} />
|
|
74
|
+
</Glass>
|
|
75
|
+
<input
|
|
76
|
+
type="range"
|
|
77
|
+
min={min}
|
|
78
|
+
max={max}
|
|
79
|
+
step={step}
|
|
80
|
+
value={value}
|
|
81
|
+
disabled={disabled}
|
|
82
|
+
aria-label={accessibilityLabel}
|
|
83
|
+
onChange={handleChange}
|
|
84
|
+
style={{
|
|
85
|
+
position: "absolute",
|
|
86
|
+
top: 0,
|
|
87
|
+
left: 0,
|
|
88
|
+
width: "100%",
|
|
89
|
+
height: "100%",
|
|
90
|
+
opacity: 0,
|
|
91
|
+
cursor: disabled ? "default" : "pointer",
|
|
92
|
+
margin: 0,
|
|
93
|
+
}}
|
|
94
|
+
/>
|
|
95
|
+
</div>
|
|
96
|
+
);
|
|
97
|
+
});
|
package/src/Switch.tsx
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { useState, forwardRef } from "react";
|
|
2
|
+
import { Glass } from "./Glass";
|
|
3
|
+
import { colors } from "./internal/tokens";
|
|
4
|
+
|
|
5
|
+
export interface SwitchProps {
|
|
6
|
+
checked?: boolean;
|
|
7
|
+
defaultChecked?: boolean;
|
|
8
|
+
onCheckedChange?: (checked: boolean) => void;
|
|
9
|
+
disabled?: boolean;
|
|
10
|
+
accessibilityLabel: string;
|
|
11
|
+
style?: any;
|
|
12
|
+
isDark?: boolean;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export const Switch = forwardRef<any, SwitchProps>(function Switch(
|
|
16
|
+
{
|
|
17
|
+
checked: checkedProp,
|
|
18
|
+
defaultChecked = false,
|
|
19
|
+
onCheckedChange,
|
|
20
|
+
disabled = false,
|
|
21
|
+
accessibilityLabel,
|
|
22
|
+
style,
|
|
23
|
+
isDark = false,
|
|
24
|
+
...props
|
|
25
|
+
},
|
|
26
|
+
ref,
|
|
27
|
+
) {
|
|
28
|
+
const [internalChecked, setInternalChecked] = useState(defaultChecked);
|
|
29
|
+
const isChecked = checkedProp !== undefined ? checkedProp : internalChecked;
|
|
30
|
+
const themeColors = isDark ? colors.dark : colors.light;
|
|
31
|
+
|
|
32
|
+
const toggle = () => {
|
|
33
|
+
if (disabled) return;
|
|
34
|
+
const next = !isChecked;
|
|
35
|
+
if (checkedProp === undefined) {
|
|
36
|
+
setInternalChecked(next);
|
|
37
|
+
}
|
|
38
|
+
onCheckedChange?.(next);
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
const trackStyle = {
|
|
42
|
+
width: 51,
|
|
43
|
+
height: 31,
|
|
44
|
+
borderRadius: 15.5,
|
|
45
|
+
backgroundColor: isChecked ? themeColors.fill : "rgba(120, 120, 128, 0.16)",
|
|
46
|
+
padding: 2,
|
|
47
|
+
justifyContent: "center" as const,
|
|
48
|
+
opacity: disabled ? 0.38 : 1,
|
|
49
|
+
transition: "background-color 200ms ease",
|
|
50
|
+
...style,
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
const thumbStyle = {
|
|
54
|
+
width: 27,
|
|
55
|
+
height: 27,
|
|
56
|
+
borderRadius: 13.5,
|
|
57
|
+
backgroundColor: "#FFFFFF",
|
|
58
|
+
boxShadow: "0 2px 4px rgba(0, 0, 0, 0.2)",
|
|
59
|
+
transform: isChecked ? "translateX(20px)" : "translateX(0px)",
|
|
60
|
+
transition: "transform 200ms cubic-bezier(0.25, 0.1, 0.25, 1)",
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
return (
|
|
64
|
+
<div
|
|
65
|
+
ref={ref}
|
|
66
|
+
role="switch"
|
|
67
|
+
aria-checked={isChecked}
|
|
68
|
+
aria-label={accessibilityLabel}
|
|
69
|
+
aria-disabled={disabled}
|
|
70
|
+
tabIndex={disabled ? -1 : 0}
|
|
71
|
+
onClick={toggle}
|
|
72
|
+
style={{ display: "inline-block", cursor: disabled ? "default" : "pointer" }}
|
|
73
|
+
{...props}
|
|
74
|
+
>
|
|
75
|
+
<Glass effect="regular" style={trackStyle}>
|
|
76
|
+
<div style={thumbStyle} />
|
|
77
|
+
</Glass>
|
|
78
|
+
</div>
|
|
79
|
+
);
|
|
80
|
+
});
|