@ecohouse/ui 0.1.5 → 0.1.6
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 +34 -3
- package/dist/index.cjs +592 -102
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +263 -162
- package/dist/index.d.ts +263 -162
- package/dist/index.js +582 -104
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/components/Badge/Badge.stories.tsx +52 -0
- package/src/components/Badge/Badge.tsx +131 -0
- package/src/components/Badge/index.ts +2 -0
- package/src/components/Counter/Counter.stories.tsx +46 -0
- package/src/components/Counter/Counter.tsx +135 -0
- package/src/components/Counter/index.ts +2 -0
- package/src/components/SegmentedToggle/SegmentedToggle.stories.tsx +63 -0
- package/src/components/SegmentedToggle/SegmentedToggle.tsx +221 -0
- package/src/components/SegmentedToggle/index.ts +2 -0
- package/src/components/StatCard/StatCard.stories.tsx +20 -0
- package/src/components/StatCard/StatCard.tsx +61 -0
- package/src/components/StatCard/index.ts +2 -0
- package/src/components/index.ts +12 -0
- package/src/icons/Minus/MinusIcon.tsx +20 -0
- package/src/icons/Minus/MinusIcon.web.tsx +19 -0
- package/src/icons/Minus/index.ts +1 -0
- package/src/icons/Minus/minusPath.ts +1 -0
- package/src/icons/Plus/PlusIcon.tsx +20 -0
- package/src/icons/Plus/PlusIcon.web.tsx +19 -0
- package/src/icons/Plus/index.ts +1 -0
- package/src/icons/Plus/plusPath.ts +1 -0
- package/src/icons/Sidebar/SidebarIcon.tsx +21 -0
- package/src/icons/Sidebar/SidebarIcon.web.tsx +27 -0
- package/src/icons/Sidebar/index.ts +1 -0
- package/src/icons/Sidebar/sidebarPath.ts +3 -0
- package/src/icons/UsersAlt/UsersAltIcon.tsx +14 -0
- package/src/icons/UsersAlt/UsersAltIcon.web.tsx +13 -0
- package/src/icons/UsersAlt/index.ts +1 -0
- package/src/icons/UsersAlt/usersAltPath.ts +2 -0
- package/src/icons/index.ts +4 -0
- package/src/icons/registry.ts +12 -2
- package/src/index.ts +18 -0
- package/src/theme/colors.test.ts +6 -0
- package/src/theme/colors.ts +6 -0
- package/src/theme/index.ts +4 -0
- package/src/theme/typography.ts +36 -0
package/package.json
CHANGED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import type { Meta, StoryObj } from "@storybook/react-vite";
|
|
2
|
+
import { View, StyleSheet } from "react-native";
|
|
3
|
+
import { HomeIcon } from "../../icons";
|
|
4
|
+
import { Badge } from "./Badge";
|
|
5
|
+
|
|
6
|
+
const meta = {
|
|
7
|
+
title: "Components/Badge",
|
|
8
|
+
component: Badge,
|
|
9
|
+
args: {
|
|
10
|
+
label: "Label",
|
|
11
|
+
variant: "default",
|
|
12
|
+
icon: HomeIcon,
|
|
13
|
+
},
|
|
14
|
+
argTypes: {
|
|
15
|
+
variant: {
|
|
16
|
+
control: "select",
|
|
17
|
+
options: ["default", "transparent"],
|
|
18
|
+
},
|
|
19
|
+
},
|
|
20
|
+
parameters: {
|
|
21
|
+
backgrounds: { default: "black" },
|
|
22
|
+
},
|
|
23
|
+
} satisfies Meta<typeof Badge>;
|
|
24
|
+
|
|
25
|
+
export default meta;
|
|
26
|
+
type Story = StoryObj<typeof meta>;
|
|
27
|
+
|
|
28
|
+
export const Default: Story = {};
|
|
29
|
+
|
|
30
|
+
export const Transparent: Story = {
|
|
31
|
+
args: { variant: "transparent" },
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
export const WithNoIcon: Story = {
|
|
35
|
+
args: { icon: undefined },
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
export const AllVariants: Story = {
|
|
39
|
+
render: () => (
|
|
40
|
+
<View style={storyStyles.column}>
|
|
41
|
+
<Badge label="Label" icon={HomeIcon} variant="default" />
|
|
42
|
+
<Badge label="Label" icon={HomeIcon} variant="transparent" />
|
|
43
|
+
</View>
|
|
44
|
+
),
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
const storyStyles = StyleSheet.create({
|
|
48
|
+
column: {
|
|
49
|
+
gap: 12,
|
|
50
|
+
alignItems: "flex-start",
|
|
51
|
+
},
|
|
52
|
+
});
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import { forwardRef, useMemo, useRef, type ComponentRef } from "react";
|
|
2
|
+
import {
|
|
3
|
+
Platform,
|
|
4
|
+
StyleSheet,
|
|
5
|
+
Text,
|
|
6
|
+
View,
|
|
7
|
+
type StyleProp,
|
|
8
|
+
type ViewProps,
|
|
9
|
+
type ViewStyle,
|
|
10
|
+
} from "react-native";
|
|
11
|
+
import type { IconComponent } from "../../icons";
|
|
12
|
+
import { badgeTypography, colors } from "../../theme";
|
|
13
|
+
import { mergeRefs } from "../../utils/mergeRefs";
|
|
14
|
+
import { useApplyWebClassName } from "../../utils/useApplyWebClassName";
|
|
15
|
+
|
|
16
|
+
export type BadgeVariant = "default" | "transparent";
|
|
17
|
+
|
|
18
|
+
export interface BadgeProps extends Omit<ViewProps, "style" | "children"> {
|
|
19
|
+
label: string;
|
|
20
|
+
variant?: BadgeVariant;
|
|
21
|
+
icon?: IconComponent;
|
|
22
|
+
style?: StyleProp<ViewStyle>;
|
|
23
|
+
className?: string;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const ICON_SIZE = 20;
|
|
27
|
+
|
|
28
|
+
const webBlurStyle: ViewStyle =
|
|
29
|
+
Platform.OS === "web"
|
|
30
|
+
? ({
|
|
31
|
+
backdropFilter: "blur(10.3px)",
|
|
32
|
+
WebkitBackdropFilter: "blur(10.3px)",
|
|
33
|
+
} as ViewStyle)
|
|
34
|
+
: {};
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Variant chrome — icon color is part of the variant, not a separate prop.
|
|
38
|
+
* - default: grey fill, no border, white icon
|
|
39
|
+
* - transparent: muted white fill + 1px border, primary icon, blur on web
|
|
40
|
+
*/
|
|
41
|
+
const variantConfig = {
|
|
42
|
+
default: {
|
|
43
|
+
backgroundColor: colors.grey750,
|
|
44
|
+
iconColor: colors.white,
|
|
45
|
+
border: false,
|
|
46
|
+
blur: false,
|
|
47
|
+
},
|
|
48
|
+
transparent: {
|
|
49
|
+
backgroundColor: colors.whiteAlpha5,
|
|
50
|
+
iconColor: colors.primary,
|
|
51
|
+
border: true,
|
|
52
|
+
blur: true,
|
|
53
|
+
},
|
|
54
|
+
} as const satisfies Record<
|
|
55
|
+
BadgeVariant,
|
|
56
|
+
{ backgroundColor: string; iconColor: string; border: boolean; blur: boolean }
|
|
57
|
+
>;
|
|
58
|
+
|
|
59
|
+
export const Badge = forwardRef<ComponentRef<typeof View>, BadgeProps>(function Badge(
|
|
60
|
+
{ label, variant = "default", icon: Icon, style, className, accessibilityLabel, ...rest },
|
|
61
|
+
forwardedRef,
|
|
62
|
+
) {
|
|
63
|
+
const containerRef = useRef<ComponentRef<typeof View>>(null);
|
|
64
|
+
const preset = variantConfig[variant];
|
|
65
|
+
const resolvedClassName = className?.trim() || undefined;
|
|
66
|
+
const setContainerRef = useMemo(() => mergeRefs(containerRef, forwardedRef), [forwardedRef]);
|
|
67
|
+
|
|
68
|
+
useApplyWebClassName(containerRef, resolvedClassName);
|
|
69
|
+
|
|
70
|
+
return (
|
|
71
|
+
<View
|
|
72
|
+
{...rest}
|
|
73
|
+
ref={setContainerRef}
|
|
74
|
+
style={[
|
|
75
|
+
styles.base,
|
|
76
|
+
{ backgroundColor: preset.backgroundColor },
|
|
77
|
+
preset.border ? styles.transparentBorder : null,
|
|
78
|
+
preset.blur ? webBlurStyle : null,
|
|
79
|
+
style,
|
|
80
|
+
]}
|
|
81
|
+
accessibilityRole="text"
|
|
82
|
+
accessibilityLabel={accessibilityLabel ?? label}
|
|
83
|
+
>
|
|
84
|
+
{Icon ? (
|
|
85
|
+
<View style={styles.iconSlot}>
|
|
86
|
+
<Icon size={ICON_SIZE} color={preset.iconColor} />
|
|
87
|
+
</View>
|
|
88
|
+
) : null}
|
|
89
|
+
|
|
90
|
+
<Text style={[styles.label, Platform.OS === "android" ? styles.labelAndroid : null]}>
|
|
91
|
+
{label}
|
|
92
|
+
</Text>
|
|
93
|
+
</View>
|
|
94
|
+
);
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
const styles = StyleSheet.create({
|
|
98
|
+
base: {
|
|
99
|
+
flexDirection: "row",
|
|
100
|
+
alignItems: "center",
|
|
101
|
+
alignSelf: "flex-start",
|
|
102
|
+
minHeight: 44,
|
|
103
|
+
paddingVertical: 12,
|
|
104
|
+
paddingHorizontal: 16,
|
|
105
|
+
gap: 12,
|
|
106
|
+
borderRadius: 79,
|
|
107
|
+
overflow: "hidden",
|
|
108
|
+
borderWidth: 0,
|
|
109
|
+
borderColor: "transparent",
|
|
110
|
+
borderStyle: "solid",
|
|
111
|
+
},
|
|
112
|
+
transparentBorder: {
|
|
113
|
+
borderWidth: 1,
|
|
114
|
+
borderColor: colors.whiteAlpha5,
|
|
115
|
+
borderStyle: "solid",
|
|
116
|
+
},
|
|
117
|
+
iconSlot: {
|
|
118
|
+
width: ICON_SIZE,
|
|
119
|
+
height: ICON_SIZE,
|
|
120
|
+
alignItems: "center",
|
|
121
|
+
justifyContent: "center",
|
|
122
|
+
flexShrink: 0,
|
|
123
|
+
},
|
|
124
|
+
label: {
|
|
125
|
+
...badgeTypography,
|
|
126
|
+
color: colors.white,
|
|
127
|
+
},
|
|
128
|
+
labelAndroid: {
|
|
129
|
+
includeFontPadding: false,
|
|
130
|
+
},
|
|
131
|
+
});
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { useState } from "react";
|
|
2
|
+
import type { Meta, StoryObj } from "@storybook/react-vite";
|
|
3
|
+
import { Counter } from "./Counter";
|
|
4
|
+
|
|
5
|
+
const meta = {
|
|
6
|
+
title: "Components/Counter",
|
|
7
|
+
component: Counter,
|
|
8
|
+
args: {
|
|
9
|
+
defaultValue: 1,
|
|
10
|
+
min: 1,
|
|
11
|
+
},
|
|
12
|
+
argTypes: {
|
|
13
|
+
value: { control: "number" },
|
|
14
|
+
defaultValue: { control: "number" },
|
|
15
|
+
min: { control: "number" },
|
|
16
|
+
max: { control: "number" },
|
|
17
|
+
step: { control: "number" },
|
|
18
|
+
disabled: { control: "boolean" },
|
|
19
|
+
onValueChange: { action: "valueChange" },
|
|
20
|
+
},
|
|
21
|
+
parameters: {
|
|
22
|
+
backgrounds: { default: "black" },
|
|
23
|
+
},
|
|
24
|
+
} satisfies Meta<typeof Counter>;
|
|
25
|
+
|
|
26
|
+
export default meta;
|
|
27
|
+
type Story = StoryObj<typeof meta>;
|
|
28
|
+
|
|
29
|
+
export const Default: Story = {};
|
|
30
|
+
|
|
31
|
+
export const AtMaximum: Story = {
|
|
32
|
+
args: { defaultValue: 5, max: 5 },
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
export const Disabled: Story = {
|
|
36
|
+
args: { disabled: true },
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
function ControlledCounter() {
|
|
40
|
+
const [value, setValue] = useState(1);
|
|
41
|
+
return <Counter value={value} min={1} max={5} onValueChange={setValue} />;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export const Controlled: Story = {
|
|
45
|
+
render: () => <ControlledCounter />,
|
|
46
|
+
};
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import { forwardRef, useMemo, useRef, useState, type ComponentRef } from "react";
|
|
2
|
+
import {
|
|
3
|
+
Platform,
|
|
4
|
+
Pressable,
|
|
5
|
+
StyleSheet,
|
|
6
|
+
Text,
|
|
7
|
+
View,
|
|
8
|
+
type PressableProps,
|
|
9
|
+
type StyleProp,
|
|
10
|
+
type ViewProps,
|
|
11
|
+
type ViewStyle,
|
|
12
|
+
} from "react-native";
|
|
13
|
+
import { MinusIcon, PlusIcon } from "../../icons";
|
|
14
|
+
import { colors, counterTypography } from "../../theme";
|
|
15
|
+
import { mergeRefs } from "../../utils/mergeRefs";
|
|
16
|
+
import { useApplyWebClassName } from "../../utils/useApplyWebClassName";
|
|
17
|
+
|
|
18
|
+
export interface CounterProps extends Omit<ViewProps, "style" | "children"> {
|
|
19
|
+
value?: number;
|
|
20
|
+
defaultValue?: number;
|
|
21
|
+
onValueChange?: (value: number) => void;
|
|
22
|
+
min?: number;
|
|
23
|
+
max?: number;
|
|
24
|
+
step?: number;
|
|
25
|
+
disabled?: boolean;
|
|
26
|
+
style?: StyleProp<ViewStyle>;
|
|
27
|
+
className?: string;
|
|
28
|
+
hitSlop?: PressableProps["hitSlop"];
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const BUTTON_SIZE = 32;
|
|
32
|
+
|
|
33
|
+
const webBlurStyle: ViewStyle =
|
|
34
|
+
Platform.OS === "web"
|
|
35
|
+
? ({
|
|
36
|
+
backdropFilter: "blur(20px)",
|
|
37
|
+
WebkitBackdropFilter: "blur(20px)",
|
|
38
|
+
} as ViewStyle)
|
|
39
|
+
: {};
|
|
40
|
+
|
|
41
|
+
function clamp(value: number, min: number, max?: number) {
|
|
42
|
+
return Math.max(min, max === undefined ? value : Math.min(value, max));
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export const Counter = forwardRef<ComponentRef<typeof View>, CounterProps>(function Counter(
|
|
46
|
+
{
|
|
47
|
+
value,
|
|
48
|
+
defaultValue,
|
|
49
|
+
onValueChange,
|
|
50
|
+
min = 1,
|
|
51
|
+
max,
|
|
52
|
+
step = 1,
|
|
53
|
+
disabled = false,
|
|
54
|
+
style,
|
|
55
|
+
className,
|
|
56
|
+
hitSlop,
|
|
57
|
+
...rest
|
|
58
|
+
},
|
|
59
|
+
forwardedRef,
|
|
60
|
+
) {
|
|
61
|
+
const containerRef = useRef<ComponentRef<typeof View>>(null);
|
|
62
|
+
const isControlled = value !== undefined;
|
|
63
|
+
const [uncontrolledValue, setUncontrolledValue] = useState(() =>
|
|
64
|
+
clamp(defaultValue ?? min, min, max),
|
|
65
|
+
);
|
|
66
|
+
const resolvedValue = clamp(isControlled ? value : uncontrolledValue, min, max);
|
|
67
|
+
const resolvedClassName = className?.trim() || undefined;
|
|
68
|
+
const setContainerRef = useMemo(() => mergeRefs(containerRef, forwardedRef), [forwardedRef]);
|
|
69
|
+
const decrementDisabled = disabled || resolvedValue <= min;
|
|
70
|
+
const incrementDisabled = disabled || (max !== undefined && resolvedValue >= max);
|
|
71
|
+
|
|
72
|
+
useApplyWebClassName(containerRef, resolvedClassName);
|
|
73
|
+
|
|
74
|
+
const updateValue = (nextValue: number) => {
|
|
75
|
+
const next = clamp(nextValue, min, max);
|
|
76
|
+
if (next === resolvedValue) return;
|
|
77
|
+
if (!isControlled) setUncontrolledValue(next);
|
|
78
|
+
onValueChange?.(next);
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
return (
|
|
82
|
+
<View {...rest} ref={setContainerRef} style={[styles.base, style]}>
|
|
83
|
+
<Pressable
|
|
84
|
+
disabled={decrementDisabled}
|
|
85
|
+
hitSlop={hitSlop}
|
|
86
|
+
onPress={() => updateValue(resolvedValue - step)}
|
|
87
|
+
accessibilityRole="button"
|
|
88
|
+
accessibilityLabel="Decrease value"
|
|
89
|
+
accessibilityState={{ disabled: decrementDisabled }}
|
|
90
|
+
style={[styles.button, webBlurStyle, decrementDisabled && styles.buttonDisabled]}
|
|
91
|
+
>
|
|
92
|
+
<MinusIcon />
|
|
93
|
+
</Pressable>
|
|
94
|
+
|
|
95
|
+
<Text style={styles.value}>{resolvedValue}</Text>
|
|
96
|
+
|
|
97
|
+
<Pressable
|
|
98
|
+
disabled={incrementDisabled}
|
|
99
|
+
hitSlop={hitSlop}
|
|
100
|
+
onPress={() => updateValue(resolvedValue + step)}
|
|
101
|
+
accessibilityRole="button"
|
|
102
|
+
accessibilityLabel="Increase value"
|
|
103
|
+
accessibilityState={{ disabled: incrementDisabled }}
|
|
104
|
+
style={[styles.button, webBlurStyle, incrementDisabled && styles.buttonDisabled]}
|
|
105
|
+
>
|
|
106
|
+
<PlusIcon />
|
|
107
|
+
</Pressable>
|
|
108
|
+
</View>
|
|
109
|
+
);
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
const styles = StyleSheet.create({
|
|
113
|
+
base: {
|
|
114
|
+
flexDirection: "row",
|
|
115
|
+
alignItems: "center",
|
|
116
|
+
alignSelf: "flex-start",
|
|
117
|
+
gap: 17,
|
|
118
|
+
},
|
|
119
|
+
button: {
|
|
120
|
+
width: BUTTON_SIZE,
|
|
121
|
+
height: BUTTON_SIZE,
|
|
122
|
+
alignItems: "center",
|
|
123
|
+
justifyContent: "center",
|
|
124
|
+
borderWidth: 1,
|
|
125
|
+
borderColor: colors.whiteAlpha10,
|
|
126
|
+
borderRadius: 39,
|
|
127
|
+
},
|
|
128
|
+
buttonDisabled: {
|
|
129
|
+
opacity: 0.25,
|
|
130
|
+
},
|
|
131
|
+
value: {
|
|
132
|
+
...counterTypography,
|
|
133
|
+
color: colors.white,
|
|
134
|
+
},
|
|
135
|
+
});
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { useState } from "react";
|
|
2
|
+
import type { Meta, StoryObj } from "@storybook/react-vite";
|
|
3
|
+
import { StyleSheet, View } from "react-native";
|
|
4
|
+
import { LayoutGridIcon } from "../../icons";
|
|
5
|
+
import { SegmentedToggle } from "./SegmentedToggle";
|
|
6
|
+
|
|
7
|
+
const optionsWithIcons = [
|
|
8
|
+
{ value: "grid", label: "Label", icon: LayoutGridIcon },
|
|
9
|
+
{ value: "list", label: "Label", icon: LayoutGridIcon },
|
|
10
|
+
] as const;
|
|
11
|
+
|
|
12
|
+
const optionsWithoutIcons = [
|
|
13
|
+
{ value: "first", label: "Label" },
|
|
14
|
+
{ value: "second", label: "Label" },
|
|
15
|
+
] as const;
|
|
16
|
+
|
|
17
|
+
const meta = {
|
|
18
|
+
title: "Components/SegmentedToggle",
|
|
19
|
+
component: SegmentedToggle,
|
|
20
|
+
args: {
|
|
21
|
+
options: optionsWithIcons,
|
|
22
|
+
defaultValue: "grid",
|
|
23
|
+
},
|
|
24
|
+
parameters: {
|
|
25
|
+
backgrounds: { default: "black" },
|
|
26
|
+
},
|
|
27
|
+
} satisfies Meta<typeof SegmentedToggle>;
|
|
28
|
+
|
|
29
|
+
export default meta;
|
|
30
|
+
type Story = StoryObj<typeof meta>;
|
|
31
|
+
|
|
32
|
+
export const WithIcons: Story = {};
|
|
33
|
+
|
|
34
|
+
export const WithoutIcons: Story = {
|
|
35
|
+
args: { options: optionsWithoutIcons, defaultValue: "first" },
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
export const ContentWidth: Story = {
|
|
39
|
+
args: { fullWidth: false },
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
export const In400PixelContainer: Story = {
|
|
43
|
+
render: () => (
|
|
44
|
+
<View style={storyStyles.container}>
|
|
45
|
+
<SegmentedToggle options={optionsWithIcons} defaultValue="grid" />
|
|
46
|
+
</View>
|
|
47
|
+
),
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
function ControlledSegmentedToggle() {
|
|
51
|
+
const [value, setValue] = useState("grid");
|
|
52
|
+
return <SegmentedToggle options={optionsWithIcons} value={value} onValueChange={setValue} />;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export const Controlled: Story = {
|
|
56
|
+
render: () => <ControlledSegmentedToggle />,
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
const storyStyles = StyleSheet.create({
|
|
60
|
+
container: {
|
|
61
|
+
width: 400,
|
|
62
|
+
},
|
|
63
|
+
});
|
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
import { forwardRef, useEffect, useMemo, useRef, useState, type ComponentRef } from "react";
|
|
2
|
+
import {
|
|
3
|
+
Animated,
|
|
4
|
+
Easing,
|
|
5
|
+
Pressable,
|
|
6
|
+
StyleSheet,
|
|
7
|
+
Text,
|
|
8
|
+
View,
|
|
9
|
+
type PressableProps,
|
|
10
|
+
type StyleProp,
|
|
11
|
+
type ViewProps,
|
|
12
|
+
type ViewStyle,
|
|
13
|
+
} from "react-native";
|
|
14
|
+
import type { IconComponent } from "../../icons";
|
|
15
|
+
import { colors, segmentedToggleTypography } from "../../theme";
|
|
16
|
+
import { mergeRefs } from "../../utils/mergeRefs";
|
|
17
|
+
import { useApplyWebClassName } from "../../utils/useApplyWebClassName";
|
|
18
|
+
|
|
19
|
+
export interface SegmentedToggleOption {
|
|
20
|
+
value: string;
|
|
21
|
+
label: string;
|
|
22
|
+
icon?: IconComponent;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface SegmentedToggleProps extends Omit<ViewProps, "style" | "children"> {
|
|
26
|
+
options: readonly [SegmentedToggleOption, SegmentedToggleOption];
|
|
27
|
+
value?: string;
|
|
28
|
+
defaultValue?: string;
|
|
29
|
+
onValueChange?: (value: string) => void;
|
|
30
|
+
fullWidth?: boolean;
|
|
31
|
+
disabled?: boolean;
|
|
32
|
+
style?: StyleProp<ViewStyle>;
|
|
33
|
+
className?: string;
|
|
34
|
+
hitSlop?: PressableProps["hitSlop"];
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const ICON_SIZE = 20;
|
|
38
|
+
const ANIMATION_DURATION = 200;
|
|
39
|
+
|
|
40
|
+
export const SegmentedToggle = forwardRef<ComponentRef<typeof View>, SegmentedToggleProps>(
|
|
41
|
+
function SegmentedToggle(
|
|
42
|
+
{
|
|
43
|
+
options,
|
|
44
|
+
value,
|
|
45
|
+
defaultValue = options[0].value,
|
|
46
|
+
onValueChange,
|
|
47
|
+
fullWidth = true,
|
|
48
|
+
disabled = false,
|
|
49
|
+
style,
|
|
50
|
+
className,
|
|
51
|
+
hitSlop,
|
|
52
|
+
onLayout,
|
|
53
|
+
...rest
|
|
54
|
+
},
|
|
55
|
+
forwardedRef,
|
|
56
|
+
) {
|
|
57
|
+
const containerRef = useRef<ComponentRef<typeof View>>(null);
|
|
58
|
+
const isControlled = value !== undefined;
|
|
59
|
+
const [uncontrolledValue, setUncontrolledValue] = useState(defaultValue);
|
|
60
|
+
const selectedValue = isControlled ? value : uncontrolledValue;
|
|
61
|
+
const selectedIndex = Math.max(
|
|
62
|
+
0,
|
|
63
|
+
options.findIndex(({ value: optionValue }) => optionValue === selectedValue),
|
|
64
|
+
);
|
|
65
|
+
const resolvedClassName = className?.trim() || undefined;
|
|
66
|
+
const setContainerRef = useMemo(() => mergeRefs(containerRef, forwardedRef), [forwardedRef]);
|
|
67
|
+
const progress = useRef(new Animated.Value(selectedIndex)).current;
|
|
68
|
+
const [contentWidths, setContentWidths] = useState<[number, number]>([0, 0]);
|
|
69
|
+
const [containerWidth, setContainerWidth] = useState(0);
|
|
70
|
+
const availableOptionWidth = Math.max(0, (containerWidth - 12) / 2);
|
|
71
|
+
const widestContentWidth = Math.max(...contentWidths);
|
|
72
|
+
const contentBasedOptionWidth = widestContentWidth > 0 ? widestContentWidth + 24 * 2 : 0;
|
|
73
|
+
const optionWidth = fullWidth ? availableOptionWidth : contentBasedOptionWidth;
|
|
74
|
+
const indicatorTranslateX = progress.interpolate({
|
|
75
|
+
inputRange: [0, 1],
|
|
76
|
+
outputRange: [0, optionWidth + 4],
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
useApplyWebClassName(containerRef, resolvedClassName);
|
|
80
|
+
|
|
81
|
+
useEffect(() => {
|
|
82
|
+
const animation = Animated.timing(progress, {
|
|
83
|
+
toValue: selectedIndex,
|
|
84
|
+
duration: ANIMATION_DURATION,
|
|
85
|
+
easing: Easing.out(Easing.cubic),
|
|
86
|
+
useNativeDriver: true,
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
animation.start();
|
|
90
|
+
return () => animation.stop();
|
|
91
|
+
}, [progress, selectedIndex]);
|
|
92
|
+
|
|
93
|
+
const selectOption = (nextValue: string) => {
|
|
94
|
+
if (disabled || nextValue === selectedValue) return;
|
|
95
|
+
if (!isControlled) setUncontrolledValue(nextValue);
|
|
96
|
+
onValueChange?.(nextValue);
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
return (
|
|
100
|
+
<View
|
|
101
|
+
{...rest}
|
|
102
|
+
ref={setContainerRef}
|
|
103
|
+
onLayout={(event) => {
|
|
104
|
+
const nextWidth = event.nativeEvent.layout.width;
|
|
105
|
+
setContainerWidth((currentWidth) =>
|
|
106
|
+
currentWidth === nextWidth ? currentWidth : nextWidth,
|
|
107
|
+
);
|
|
108
|
+
onLayout?.(event);
|
|
109
|
+
}}
|
|
110
|
+
style={[
|
|
111
|
+
styles.base,
|
|
112
|
+
fullWidth ? styles.fullWidth : styles.contentWidth,
|
|
113
|
+
disabled && styles.disabled,
|
|
114
|
+
style,
|
|
115
|
+
]}
|
|
116
|
+
accessibilityRole="radiogroup"
|
|
117
|
+
>
|
|
118
|
+
{optionWidth > 0 ? (
|
|
119
|
+
<Animated.View
|
|
120
|
+
pointerEvents="none"
|
|
121
|
+
style={[
|
|
122
|
+
styles.activeIndicator,
|
|
123
|
+
{ width: optionWidth, transform: [{ translateX: indicatorTranslateX }] },
|
|
124
|
+
]}
|
|
125
|
+
/>
|
|
126
|
+
) : null}
|
|
127
|
+
{options.map(({ value: optionValue, label, icon: Icon }, index) => {
|
|
128
|
+
const selected = optionValue === selectedValue;
|
|
129
|
+
const iconColor = selected ? colors.primary : colors.whiteAlpha20;
|
|
130
|
+
const labelColor = selected ? colors.whiteAlpha86 : colors.whiteAlpha40;
|
|
131
|
+
|
|
132
|
+
return (
|
|
133
|
+
<Pressable
|
|
134
|
+
key={optionValue}
|
|
135
|
+
onPress={() => selectOption(optionValue)}
|
|
136
|
+
disabled={disabled}
|
|
137
|
+
hitSlop={hitSlop}
|
|
138
|
+
accessibilityRole="radio"
|
|
139
|
+
accessibilityLabel={label}
|
|
140
|
+
accessibilityState={{ selected, disabled }}
|
|
141
|
+
style={[
|
|
142
|
+
styles.option,
|
|
143
|
+
fullWidth ? styles.optionFullWidth : optionWidth > 0 && { minWidth: optionWidth },
|
|
144
|
+
]}
|
|
145
|
+
>
|
|
146
|
+
<View
|
|
147
|
+
onLayout={(event) => {
|
|
148
|
+
const nextWidth = event.nativeEvent.layout.width;
|
|
149
|
+
setContentWidths((currentWidths) => {
|
|
150
|
+
if (currentWidths[index] === nextWidth) return currentWidths;
|
|
151
|
+
const nextWidths: [number, number] = [currentWidths[0], currentWidths[1]];
|
|
152
|
+
nextWidths[index] = nextWidth;
|
|
153
|
+
return nextWidths;
|
|
154
|
+
});
|
|
155
|
+
}}
|
|
156
|
+
style={styles.optionContent}
|
|
157
|
+
>
|
|
158
|
+
{Icon ? <Icon size={ICON_SIZE} color={iconColor} /> : null}
|
|
159
|
+
<Text numberOfLines={1} style={[styles.label, { color: labelColor }]}>
|
|
160
|
+
{label}
|
|
161
|
+
</Text>
|
|
162
|
+
</View>
|
|
163
|
+
</Pressable>
|
|
164
|
+
);
|
|
165
|
+
})}
|
|
166
|
+
</View>
|
|
167
|
+
);
|
|
168
|
+
},
|
|
169
|
+
);
|
|
170
|
+
|
|
171
|
+
const styles = StyleSheet.create({
|
|
172
|
+
base: {
|
|
173
|
+
height: 44,
|
|
174
|
+
flexDirection: "row",
|
|
175
|
+
padding: 4,
|
|
176
|
+
gap: 4,
|
|
177
|
+
borderRadius: 60,
|
|
178
|
+
backgroundColor: colors.grey700,
|
|
179
|
+
overflow: "hidden",
|
|
180
|
+
},
|
|
181
|
+
fullWidth: {
|
|
182
|
+
width: "100%",
|
|
183
|
+
alignSelf: "stretch",
|
|
184
|
+
},
|
|
185
|
+
contentWidth: {
|
|
186
|
+
alignSelf: "flex-start",
|
|
187
|
+
},
|
|
188
|
+
disabled: {
|
|
189
|
+
opacity: 0.6,
|
|
190
|
+
},
|
|
191
|
+
option: {
|
|
192
|
+
zIndex: 1,
|
|
193
|
+
flexDirection: "row",
|
|
194
|
+
alignItems: "center",
|
|
195
|
+
justifyContent: "center",
|
|
196
|
+
paddingHorizontal: 24,
|
|
197
|
+
borderRadius: 60,
|
|
198
|
+
},
|
|
199
|
+
optionContent: {
|
|
200
|
+
maxWidth: "100%",
|
|
201
|
+
flexDirection: "row",
|
|
202
|
+
alignItems: "center",
|
|
203
|
+
gap: 12,
|
|
204
|
+
},
|
|
205
|
+
optionFullWidth: {
|
|
206
|
+
flex: 1,
|
|
207
|
+
minWidth: 0,
|
|
208
|
+
},
|
|
209
|
+
activeIndicator: {
|
|
210
|
+
position: "absolute",
|
|
211
|
+
top: 4,
|
|
212
|
+
left: 4,
|
|
213
|
+
height: 36,
|
|
214
|
+
backgroundColor: colors.grey600,
|
|
215
|
+
borderRadius: 60,
|
|
216
|
+
},
|
|
217
|
+
label: {
|
|
218
|
+
...segmentedToggleTypography,
|
|
219
|
+
flexShrink: 1,
|
|
220
|
+
},
|
|
221
|
+
});
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { Meta, StoryObj } from "@storybook/react-vite";
|
|
2
|
+
import { UsersAltIcon } from "../../icons";
|
|
3
|
+
import { StatCard } from "./StatCard";
|
|
4
|
+
|
|
5
|
+
const meta = {
|
|
6
|
+
title: "Components/StatCard",
|
|
7
|
+
component: StatCard,
|
|
8
|
+
args: {
|
|
9
|
+
label: "Label",
|
|
10
|
+
icon: UsersAltIcon,
|
|
11
|
+
},
|
|
12
|
+
parameters: {
|
|
13
|
+
backgrounds: { default: "black" },
|
|
14
|
+
},
|
|
15
|
+
} satisfies Meta<typeof StatCard>;
|
|
16
|
+
|
|
17
|
+
export default meta;
|
|
18
|
+
type Story = StoryObj<typeof meta>;
|
|
19
|
+
|
|
20
|
+
export const Default: Story = {};
|