@stylhero/native 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (48) hide show
  1. package/CHANGELOG.md +25 -0
  2. package/LICENSE +21 -0
  3. package/README.md +85 -2
  4. package/dist/components/Badge.d.ts +11 -0
  5. package/dist/components/Badge.js +31 -0
  6. package/dist/components/Badge.js.map +1 -0
  7. package/dist/components/Button.d.ts +24 -0
  8. package/dist/components/Button.js +59 -0
  9. package/dist/components/Button.js.map +1 -0
  10. package/dist/components/Card.d.ts +17 -0
  11. package/dist/components/Card.js +22 -0
  12. package/dist/components/Card.js.map +1 -0
  13. package/dist/components/IconButton.d.ts +13 -0
  14. package/dist/components/IconButton.js +28 -0
  15. package/dist/components/IconButton.js.map +1 -0
  16. package/dist/components/ListItem.d.ts +24 -0
  17. package/dist/components/ListItem.js +34 -0
  18. package/dist/components/ListItem.js.map +1 -0
  19. package/dist/components/Stack.d.ts +14 -0
  20. package/dist/components/Stack.js +19 -0
  21. package/dist/components/Stack.js.map +1 -0
  22. package/dist/components/Text.d.ts +15 -0
  23. package/dist/components/Text.js +28 -0
  24. package/dist/components/Text.js.map +1 -0
  25. package/dist/components/TextField.d.ts +24 -0
  26. package/dist/components/TextField.js +40 -0
  27. package/dist/components/TextField.js.map +1 -0
  28. package/dist/index.d.ts +10 -0
  29. package/dist/index.js +11 -0
  30. package/dist/index.js.map +1 -0
  31. package/dist/theme/ThemeProvider.d.ts +33 -0
  32. package/dist/theme/ThemeProvider.js +45 -0
  33. package/dist/theme/ThemeProvider.js.map +1 -0
  34. package/dist/theme/tokens.d.ts +152 -0
  35. package/dist/theme/tokens.js +150 -0
  36. package/dist/theme/tokens.js.map +1 -0
  37. package/package.json +63 -4
  38. package/src/components/Badge.tsx +54 -0
  39. package/src/components/Button.tsx +128 -0
  40. package/src/components/Card.tsx +72 -0
  41. package/src/components/IconButton.tsx +59 -0
  42. package/src/components/ListItem.tsx +104 -0
  43. package/src/components/Stack.tsx +35 -0
  44. package/src/components/Text.tsx +46 -0
  45. package/src/components/TextField.tsx +123 -0
  46. package/src/index.ts +18 -0
  47. package/src/theme/ThemeProvider.tsx +87 -0
  48. package/src/theme/tokens.ts +163 -0
@@ -0,0 +1,72 @@
1
+ import type { ReactNode } from "react";
2
+ import { Pressable, View, type StyleProp, type ViewProps, type ViewStyle } from "react-native";
3
+ import { useTheme } from "../theme/ThemeProvider";
4
+ import { Text } from "./Text";
5
+
6
+ export type CardProps = Omit<ViewProps, "style"> & {
7
+ title?: string;
8
+ subtitle?: string;
9
+ /** Top-right slot next to the title, e.g. a Badge or IconButton. */
10
+ action?: ReactNode;
11
+ /** Bottom row, right-aligned, e.g. Buttons. */
12
+ footer?: ReactNode;
13
+ /** "outlined" drops the shadow. */
14
+ variant?: "elevated" | "outlined";
15
+ /** Makes the whole card one button. Give it an accessibilityLabel if the title isn't enough. */
16
+ onPress?: () => void;
17
+ children?: ReactNode;
18
+ style?: StyleProp<ViewStyle>;
19
+ };
20
+
21
+ export function Card({ title, subtitle, action, footer, variant = "elevated", onPress, children, style, accessibilityLabel, ...props }: CardProps) {
22
+ const theme = useTheme();
23
+ const base: ViewStyle = {
24
+ gap: theme.spacing[3],
25
+ padding: theme.spacing[6],
26
+ backgroundColor: theme.colors.surfaceRaised,
27
+ borderWidth: 1,
28
+ borderColor: theme.colors.line,
29
+ borderRadius: theme.radius.lg,
30
+ boxShadow: variant === "elevated" ? theme.shadows.card : undefined,
31
+ };
32
+
33
+ const content = (
34
+ <>
35
+ {(title || subtitle || action) && (
36
+ <View style={{ flexDirection: "row", alignItems: "flex-start", gap: theme.spacing[3] }}>
37
+ <View style={{ flex: 1, gap: 2 }}>
38
+ {title ? <Text variant="h3">{title}</Text> : null}
39
+ {subtitle ? <Text variant="small">{subtitle}</Text> : null}
40
+ </View>
41
+ {action}
42
+ </View>
43
+ )}
44
+ {children}
45
+ {footer ? (
46
+ <View style={{ flexDirection: "row", flexWrap: "wrap", justifyContent: "flex-end", gap: theme.spacing[2], marginTop: theme.spacing[2] }}>
47
+ {footer}
48
+ </View>
49
+ ) : null}
50
+ </>
51
+ );
52
+
53
+ if (onPress) {
54
+ return (
55
+ <Pressable
56
+ accessibilityRole="button"
57
+ accessibilityLabel={accessibilityLabel ?? ([title, subtitle].filter(Boolean).join(", ") || undefined)}
58
+ onPress={onPress}
59
+ style={({ pressed }) => [base, pressed && { backgroundColor: theme.colors.surfaceSunken }, style]}
60
+ {...props}
61
+ >
62
+ {content}
63
+ </Pressable>
64
+ );
65
+ }
66
+
67
+ return (
68
+ <View style={[base, style]} accessibilityLabel={accessibilityLabel} {...props}>
69
+ {content}
70
+ </View>
71
+ );
72
+ }
@@ -0,0 +1,59 @@
1
+ import type { ComponentRef, ReactElement, ReactNode, Ref } from "react";
2
+ import { Pressable, View, type PressableProps, type StyleProp, type ViewStyle } from "react-native";
3
+ import { useTheme } from "../theme/ThemeProvider";
4
+ import { MIN_TOUCH_TARGET } from "./Button";
5
+
6
+ export type IconButtonProps = Omit<PressableProps, "children" | "style" | "accessibilityLabel"> & {
7
+ ref?: Ref<ComponentRef<typeof View>>;
8
+ /** Required: an icon has no visible text, so screen readers need this. */
9
+ accessibilityLabel: string;
10
+ children: ReactNode;
11
+ size?: "sm" | "md";
12
+ variant?: "ghost" | "secondary";
13
+ style?: StyleProp<ViewStyle>;
14
+ };
15
+
16
+ /** Square button holding only an icon. Always at least 44pt to touch. */
17
+ export function IconButton({
18
+ ref,
19
+ children,
20
+ size = "md",
21
+ variant = "ghost",
22
+ disabled,
23
+ style,
24
+ hitSlop,
25
+ ...props
26
+ }: IconButtonProps): ReactElement {
27
+ const theme = useTheme();
28
+ const dim = size === "sm" ? 32 : 40;
29
+ return (
30
+ <Pressable
31
+ ref={ref}
32
+ accessibilityRole="button"
33
+ accessibilityState={{ disabled: Boolean(disabled) }}
34
+ disabled={disabled}
35
+ hitSlop={hitSlop ?? Math.ceil((MIN_TOUCH_TARGET - dim) / 2)}
36
+ style={({ pressed }) => [
37
+ {
38
+ width: dim,
39
+ height: dim,
40
+ alignItems: "center",
41
+ justifyContent: "center",
42
+ borderRadius: theme.radius.md,
43
+ borderWidth: 1,
44
+ borderColor: variant === "secondary" ? theme.colors.line : "transparent",
45
+ backgroundColor: pressed
46
+ ? theme.colors.surfaceSunken
47
+ : variant === "secondary"
48
+ ? theme.colors.surfaceRaised
49
+ : "transparent",
50
+ opacity: disabled ? 0.5 : 1,
51
+ },
52
+ style,
53
+ ]}
54
+ {...props}
55
+ >
56
+ {children}
57
+ </Pressable>
58
+ );
59
+ }
@@ -0,0 +1,104 @@
1
+ import type { ReactNode } from "react";
2
+ import { Pressable, View, type StyleProp, type ViewStyle } from "react-native";
3
+ import { useTheme } from "../theme/ThemeProvider";
4
+ import { Text } from "./Text";
5
+
6
+ export type ListItemProps = {
7
+ title: string;
8
+ subtitle?: string;
9
+ /** Left slot: icon, Avatar, or product image. */
10
+ leading?: ReactNode;
11
+ /** Right slot: price, Badge, Switch... */
12
+ trailing?: ReactNode;
13
+ /** Shows a ">" hint that the row opens something. Defaults to true when pressable. */
14
+ chevron?: boolean;
15
+ onPress?: () => void;
16
+ disabled?: boolean;
17
+ /** Overrides the spoken label (defaults to "title, subtitle"). */
18
+ accessibilityLabel?: string;
19
+ accessibilityHint?: string;
20
+ testID?: string;
21
+ style?: StyleProp<ViewStyle>;
22
+ };
23
+
24
+ /**
25
+ * One row of a list. Read by screen readers as a single element, so a row
26
+ * like "RTX 4070, 12 GB, $549" is one swipe instead of three.
27
+ */
28
+ export function ListItem({
29
+ title,
30
+ subtitle,
31
+ leading,
32
+ trailing,
33
+ chevron,
34
+ onPress,
35
+ disabled,
36
+ accessibilityLabel,
37
+ accessibilityHint,
38
+ testID,
39
+ style,
40
+ }: ListItemProps) {
41
+ const theme = useTheme();
42
+ const showChevron = chevron ?? Boolean(onPress);
43
+ const label = accessibilityLabel ?? [title, subtitle].filter(Boolean).join(", ");
44
+
45
+ const row = (pressed: boolean): StyleProp<ViewStyle> => [
46
+ {
47
+ flexDirection: "row",
48
+ alignItems: "center",
49
+ gap: theme.spacing[3],
50
+ minHeight: 56,
51
+ paddingVertical: theme.spacing[2],
52
+ paddingHorizontal: theme.spacing[4],
53
+ backgroundColor: pressed ? theme.colors.surfaceSunken : "transparent",
54
+ opacity: disabled ? 0.5 : 1,
55
+ },
56
+ style,
57
+ ];
58
+
59
+ const content = (
60
+ <>
61
+ {leading}
62
+ <View style={{ flex: 1, gap: 2 }}>
63
+ <Text variant="body" numberOfLines={2}>
64
+ {title}
65
+ </Text>
66
+ {subtitle ? (
67
+ <Text variant="small" numberOfLines={2}>
68
+ {subtitle}
69
+ </Text>
70
+ ) : null}
71
+ </View>
72
+ {trailing}
73
+ {showChevron ? (
74
+ // h3 size, but variant "body" so the chevron never gets the "header" role
75
+ <Text variant="body" tone="muted" style={theme.text("h3")} importantForAccessibility="no" accessibilityElementsHidden>
76
+ {"›"}
77
+ </Text>
78
+ ) : null}
79
+ </>
80
+ );
81
+
82
+ if (onPress) {
83
+ return (
84
+ <Pressable
85
+ accessibilityRole="button"
86
+ accessibilityLabel={label}
87
+ accessibilityHint={accessibilityHint}
88
+ accessibilityState={{ disabled: Boolean(disabled) }}
89
+ disabled={disabled}
90
+ onPress={onPress}
91
+ testID={testID}
92
+ style={({ pressed }) => row(pressed && !disabled)}
93
+ >
94
+ {content}
95
+ </Pressable>
96
+ );
97
+ }
98
+
99
+ return (
100
+ <View accessible accessibilityLabel={label} accessibilityHint={accessibilityHint} testID={testID} style={row(false)}>
101
+ {content}
102
+ </View>
103
+ );
104
+ }
@@ -0,0 +1,35 @@
1
+ import { View, type ViewProps, type ViewStyle } from "react-native";
2
+ import { useTheme } from "../theme/ThemeProvider";
3
+ import type { SpacingToken } from "../theme/tokens";
4
+
5
+ export type StackProps = ViewProps & {
6
+ direction?: "column" | "row";
7
+ /** Spacing token between children. */
8
+ gap?: SpacingToken;
9
+ align?: ViewStyle["alignItems"];
10
+ justify?: ViewStyle["justifyContent"];
11
+ wrap?: boolean;
12
+ /** Spacing token around the content. */
13
+ padding?: SpacingToken;
14
+ };
15
+
16
+ /** Flex layout with token-based gaps. */
17
+ export function Stack({ direction = "column", gap, align, justify, wrap, padding, style, ...props }: StackProps) {
18
+ const { spacing } = useTheme();
19
+ return (
20
+ <View
21
+ style={[
22
+ {
23
+ flexDirection: direction,
24
+ gap: gap ? spacing[gap] : undefined,
25
+ alignItems: align,
26
+ justifyContent: justify,
27
+ flexWrap: wrap ? "wrap" : undefined,
28
+ padding: padding ? spacing[padding] : undefined,
29
+ },
30
+ style,
31
+ ]}
32
+ {...props}
33
+ />
34
+ );
35
+ }
@@ -0,0 +1,46 @@
1
+ import { Text as RNText, type TextProps as RNTextProps } from "react-native";
2
+ import { useTheme } from "../theme/ThemeProvider";
3
+ import type { ColorToken, TypographyToken } from "../theme/tokens";
4
+
5
+ export type TextVariant = TypographyToken | "strong";
6
+ export type TextTone = "ink" | "muted" | "primary" | "success" | "danger";
7
+
8
+ export type TextProps = RNTextProps & {
9
+ /** Type style from tokens.json. "strong" is body in bold ink. */
10
+ variant?: TextVariant;
11
+ tone?: TextTone;
12
+ };
13
+
14
+ const toneColor: Record<TextTone, ColorToken> = {
15
+ ink: "ink",
16
+ muted: "inkMuted",
17
+ primary: "primary",
18
+ success: "success",
19
+ danger: "danger",
20
+ };
21
+
22
+ const headings = new Set<TextVariant>(["display", "h1", "h2", "h3"]);
23
+
24
+ /**
25
+ * Themed text. Headings get the "header" role so screen readers can jump
26
+ * between them, and cap font scaling at 1.5x so big titles don't overflow;
27
+ * body text scales up to 2x with the user's font size setting.
28
+ */
29
+ export function Text({ variant = "body", tone, style, ...props }: TextProps) {
30
+ const theme = useTheme();
31
+ const isHeading = headings.has(variant);
32
+ const base = variant === "strong" ? theme.text("body", "700") : theme.text(variant);
33
+ return (
34
+ <RNText
35
+ accessibilityRole={isHeading ? "header" : undefined}
36
+ maxFontSizeMultiplier={isHeading ? 1.5 : 2}
37
+ style={[
38
+ base,
39
+ { color: theme.colors[toneColor[tone ?? (variant === "small" ? "muted" : "ink")]] },
40
+ variant === "label" && { textTransform: "uppercase" },
41
+ style,
42
+ ]}
43
+ {...props}
44
+ />
45
+ );
46
+ }
@@ -0,0 +1,123 @@
1
+ import { useEffect, useRef, useState, type ComponentRef, type ReactElement, type ReactNode, type Ref } from "react";
2
+ import {
3
+ AccessibilityInfo,
4
+ Platform,
5
+ TextInput,
6
+ View,
7
+ type StyleProp,
8
+ type TextInputProps,
9
+ type ViewStyle,
10
+ } from "react-native";
11
+ import { useTheme } from "../theme/ThemeProvider";
12
+ import { Text } from "./Text";
13
+
14
+ export type TextFieldProps = Omit<TextInputProps, "style" | "editable"> & {
15
+ ref?: Ref<ComponentRef<typeof TextInput>>;
16
+ /** Visible label, also read by screen readers. */
17
+ label: string;
18
+ /** Help text under the field. Replaced by `error` when there is one. */
19
+ hint?: string;
20
+ /** Error message. Turns the border red and is announced when it appears. */
21
+ error?: string;
22
+ /**
23
+ * Announce a new `error` to screen readers (iOS announcement, Android live
24
+ * region). Default true. Set it to false when the screen already moves focus
25
+ * to the first invalid field or shows its own error summary, so the error
26
+ * isn't read twice. The error is still in the field's hint either way.
27
+ */
28
+ announceError?: boolean;
29
+ disabled?: boolean;
30
+ /** Icon or text before / after the input, e.g. a search icon or "$". */
31
+ leading?: ReactNode;
32
+ trailing?: ReactNode;
33
+ style?: StyleProp<ViewStyle>;
34
+ };
35
+
36
+ export function TextField({
37
+ ref,
38
+ label,
39
+ hint,
40
+ error,
41
+ announceError = true,
42
+ disabled,
43
+ leading,
44
+ trailing,
45
+ style,
46
+ onFocus,
47
+ onBlur,
48
+ accessibilityHint,
49
+ ...props
50
+ }: TextFieldProps): ReactElement {
51
+ const theme = useTheme();
52
+ const [focused, setFocused] = useState(false);
53
+ const message = error ?? hint;
54
+
55
+ // iOS has no live regions, so announce a new error explicitly.
56
+ const lastError = useRef<string | undefined>(undefined);
57
+ useEffect(() => {
58
+ if (announceError && error && error !== lastError.current && Platform.OS === "ios") {
59
+ AccessibilityInfo.announceForAccessibility(`${label}: ${error}`);
60
+ }
61
+ lastError.current = error;
62
+ }, [announceError, error, label]);
63
+
64
+ const borderColor = error ? theme.colors.danger : focused ? theme.colors.primary : theme.colors.lineStrong;
65
+
66
+ return (
67
+ <View style={[{ gap: theme.spacing[2] }, style]}>
68
+ <Text style={[theme.text("small", "600"), { color: theme.colors.ink }]} importantForAccessibility="no" accessibilityElementsHidden>
69
+ {label}
70
+ </Text>
71
+ <View
72
+ style={{
73
+ flexDirection: "row",
74
+ alignItems: "center",
75
+ minHeight: 44,
76
+ paddingHorizontal: theme.spacing[3],
77
+ gap: theme.spacing[2],
78
+ backgroundColor: theme.colors.surfaceRaised,
79
+ borderWidth: focused || error ? 2 : 1,
80
+ // keep the text from shifting when the border thickens
81
+ margin: focused || error ? 0 : 1,
82
+ borderColor,
83
+ borderRadius: theme.radius.md,
84
+ opacity: disabled ? 0.6 : 1,
85
+ }}
86
+ >
87
+ {leading}
88
+ <TextInput
89
+ ref={ref}
90
+ accessibilityLabel={label}
91
+ accessibilityHint={accessibilityHint ?? message}
92
+ accessibilityState={{ disabled: Boolean(disabled) }}
93
+ aria-invalid={error ? true : undefined}
94
+ editable={!disabled}
95
+ placeholderTextColor={theme.colors.inkMuted}
96
+ selectionColor={theme.colors.primary}
97
+ maxFontSizeMultiplier={2}
98
+ onFocus={(e) => {
99
+ setFocused(true);
100
+ onFocus?.(e);
101
+ }}
102
+ onBlur={(e) => {
103
+ setFocused(false);
104
+ onBlur?.(e);
105
+ }}
106
+ style={[theme.text("body"), { flex: 1, color: theme.colors.ink, paddingVertical: theme.spacing[2] }]}
107
+ {...props}
108
+ />
109
+ {trailing}
110
+ </View>
111
+ {message ? (
112
+ <Text
113
+ variant="small"
114
+ tone={error ? "danger" : "muted"}
115
+ accessibilityLiveRegion={error && announceError ? "polite" : "none"}
116
+ importantForAccessibility={error ? "yes" : "no"}
117
+ >
118
+ {message}
119
+ </Text>
120
+ ) : null}
121
+ </View>
122
+ );
123
+ }
package/src/index.ts ADDED
@@ -0,0 +1,18 @@
1
+ export {
2
+ ThemeProvider,
3
+ useTheme,
4
+ createTheme,
5
+ type Theme,
6
+ type ThemeProviderProps,
7
+ type FontResolver,
8
+ } from "./theme/ThemeProvider";
9
+ export * from "./theme/tokens";
10
+
11
+ export { Text, type TextProps, type TextVariant, type TextTone } from "./components/Text";
12
+ export { Stack, type StackProps } from "./components/Stack";
13
+ export { Button, MIN_TOUCH_TARGET, type ButtonProps, type ButtonVariant, type ButtonSize } from "./components/Button";
14
+ export { IconButton, type IconButtonProps } from "./components/IconButton";
15
+ export { Badge, type BadgeProps, type BadgeTone } from "./components/Badge";
16
+ export { Card, type CardProps } from "./components/Card";
17
+ export { TextField, type TextFieldProps } from "./components/TextField";
18
+ export { ListItem, type ListItemProps } from "./components/ListItem";
@@ -0,0 +1,87 @@
1
+ import { createContext, useContext, useMemo, type ReactNode } from "react";
2
+ import { useColorScheme, type TextStyle } from "react-native";
3
+ import {
4
+ colors,
5
+ radius,
6
+ shadows,
7
+ spacing,
8
+ typography,
9
+ type ColorScheme,
10
+ type Colors,
11
+ type FontFamilyToken,
12
+ type TypographyToken,
13
+ } from "./tokens";
14
+
15
+ type FontWeight = "400" | "500" | "600" | "700" | "800";
16
+
17
+ /**
18
+ * Maps a token font family + weight to a loaded font name, e.g. with
19
+ * `@expo-google-fonts/plus-jakarta-sans`:
20
+ * (family, weight) => family === "mono" ? undefined : `PlusJakartaSans_${names[weight]}`
21
+ * Custom fonts bake the weight into the family name, so when this returns a
22
+ * name the style drops `fontWeight` (Android would otherwise fake-bold it).
23
+ * Returning undefined keeps the platform system font with `fontWeight`.
24
+ */
25
+ export type FontResolver = (family: FontFamilyToken, weight: FontWeight) => string | undefined;
26
+
27
+ export type Theme = {
28
+ scheme: ColorScheme;
29
+ colors: Colors;
30
+ spacing: typeof spacing;
31
+ radius: typeof radius;
32
+ shadows: Record<keyof (typeof shadows)["light"], string>;
33
+ /** Text style for a type token, with the font resolver applied. */
34
+ text: (variant: TypographyToken, weight?: FontWeight) => TextStyle;
35
+ };
36
+
37
+ export function createTheme(scheme: ColorScheme, resolveFont?: FontResolver): Theme {
38
+ const cache = new Map<string, TextStyle>();
39
+ return {
40
+ scheme,
41
+ colors: colors[scheme],
42
+ spacing,
43
+ radius,
44
+ shadows: shadows[scheme],
45
+ text(variant, weight) {
46
+ const key = `${variant}:${weight ?? ""}`;
47
+ const cached = cache.get(key);
48
+ if (cached) return cached;
49
+ const t = typography[variant];
50
+ const fontWeight = (weight ?? t.fontWeight) as FontWeight;
51
+ const fontFamily = resolveFont?.(t.family, fontWeight);
52
+ const style: TextStyle = {
53
+ fontSize: t.fontSize,
54
+ lineHeight: t.lineHeight,
55
+ ...("letterSpacing" in t ? { letterSpacing: t.letterSpacing } : null),
56
+ ...(fontFamily ? { fontFamily } : { fontWeight }),
57
+ ...(t.family === "mono" && !fontFamily ? { fontVariant: ["tabular-nums"] } : null),
58
+ };
59
+ cache.set(key, style);
60
+ return style;
61
+ },
62
+ };
63
+ }
64
+
65
+ const ThemeContext = createContext<Theme | null>(null);
66
+
67
+ export type ThemeProviderProps = {
68
+ /** "system" (default) follows the device setting and updates live. */
69
+ colorScheme?: ColorScheme | "system";
70
+ fonts?: FontResolver;
71
+ children: ReactNode;
72
+ };
73
+
74
+ export function ThemeProvider({ colorScheme = "system", fonts, children }: ThemeProviderProps) {
75
+ const system = useColorScheme();
76
+ const scheme: ColorScheme =
77
+ colorScheme === "system" ? (system === "dark" ? "dark" : "light") : colorScheme;
78
+ const theme = useMemo(() => createTheme(scheme, fonts), [scheme, fonts]);
79
+ return <ThemeContext.Provider value={theme}>{children}</ThemeContext.Provider>;
80
+ }
81
+
82
+ const fallback = createTheme("light");
83
+
84
+ /** The current theme. Outside a ThemeProvider it returns the light theme with system fonts. */
85
+ export function useTheme(): Theme {
86
+ return useContext(ThemeContext) ?? fallback;
87
+ }