@lotics/ui 1.6.1 → 1.9.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/src/popover.tsx CHANGED
@@ -11,6 +11,7 @@ import { colors } from "./colors";
11
11
  import { IconButton } from "./icon_button";
12
12
  import { Portal } from "./portal";
13
13
  import { Divider } from "./divider";
14
+ import { useOverlayScope } from "./overlay_scope";
14
15
  import { PopoverNavContext, type PopoverNavContextValue } from "./popover_nav";
15
16
 
16
17
  export type PopoverSide = "top" | "right" | "bottom" | "left";
@@ -76,6 +77,8 @@ export function Popover(props: PopoverProps) {
76
77
  const isControlled = controlledOpen !== undefined;
77
78
  const open = isControlled ? controlledOpen : uncontrolledOpen;
78
79
 
80
+ useOverlayScope(open);
81
+
79
82
  const onOpenChange = useCallback(
80
83
  (newOpen: boolean) => {
81
84
  if (!isControlled) {
@@ -0,0 +1,68 @@
1
+ import { type ReactNode } from "react";
2
+ import { View, StyleSheet } from "react-native";
3
+ import { Card } from "./card";
4
+ import { Text } from "./text";
5
+ import { Divider } from "./divider";
6
+ import { SPACE } from "./spacing";
7
+
8
+ interface SectionCardProps {
9
+ title: string;
10
+ /**
11
+ * One-line context under the title. Mercury, Stripe, Linear all show this
12
+ * — a bare title says "what this is" but the description says "why this
13
+ * is here and what it answers". Skip only when the title is unambiguous
14
+ * on its own.
15
+ */
16
+ description?: string;
17
+ /** Body content. Author owns the internal layout. */
18
+ children: ReactNode;
19
+ /**
20
+ * Optional footer separated by a hairline. The right place for trend
21
+ * captions ("Tăng 12% so với tháng trước"), source notes, or
22
+ * cross-references. Skip if the body already self-explains.
23
+ */
24
+ footer?: ReactNode;
25
+ }
26
+
27
+ /**
28
+ * Section-shaped card with semantic title + description + body + optional
29
+ * footer slots. The shape every well-designed dashboard section takes:
30
+ * Linear's checklist sections, Stripe's metric cards, Mercury's account
31
+ * panels. Mirrors the shadcn `Card` + `CardHeader` + `CardContent` +
32
+ * `CardFooter` composition pattern.
33
+ *
34
+ * Uses Card under the hood — picks up the border + soft shadow defaults.
35
+ * Adds generous 32px internal padding so the description + body + footer
36
+ * breathe; bare Card's 20px feels cramped once you have structured content.
37
+ */
38
+ export function SectionCard(props: SectionCardProps) {
39
+ return (
40
+ <Card style={styles.card}>
41
+ <View style={styles.container}>
42
+ <View style={styles.header}>
43
+ <Text size="lg" weight="semibold">
44
+ {props.title}
45
+ </Text>
46
+ {props.description && (
47
+ <Text size="sm" color="muted">
48
+ {props.description}
49
+ </Text>
50
+ )}
51
+ </View>
52
+ {props.children}
53
+ {props.footer && (
54
+ <>
55
+ <Divider />
56
+ {props.footer}
57
+ </>
58
+ )}
59
+ </View>
60
+ </Card>
61
+ );
62
+ }
63
+
64
+ const styles = StyleSheet.create({
65
+ card: { padding: SPACE.xl }, // 32px — Stripe / Mercury internal padding
66
+ container: { gap: SPACE.lg },
67
+ header: { gap: SPACE.xs },
68
+ });
package/src/spacing.ts ADDED
@@ -0,0 +1,23 @@
1
+ /**
2
+ * 8-grid spacing tokens. Every gap, padding, and margin in a dashboard
3
+ * should come from this scale — multiples of 8 read as "designed",
4
+ * multiples of 4 read as "developer-tuned-on-the-fly", arbitrary numbers
5
+ * read as broken.
6
+ *
7
+ * Aliases (xs/sm/md/lg/xl/xxl) match the Text size scale so authors can
8
+ * reason about spacing and typography on the same vocabulary.
9
+ *
10
+ * Why not extend further (3xl, 4xl)? Lotics surfaces fit comfortably in
11
+ * the 4-48px range. A "between-section gap" of 64px+ usually means the
12
+ * sections should be on separate routes, not the same page.
13
+ */
14
+ export const SPACE = {
15
+ xs: 4,
16
+ sm: 8,
17
+ md: 16,
18
+ lg: 24,
19
+ xl: 32,
20
+ xxl: 48,
21
+ } as const;
22
+
23
+ export type SpaceToken = keyof typeof SPACE;
@@ -0,0 +1,85 @@
1
+ import { useMemo } from "react";
2
+ import { View } from "react-native";
3
+ import { colors } from "./colors";
4
+
5
+ export interface SparklineProps {
6
+ /**
7
+ * Series of values in chronological order. Two or more required to draw
8
+ * a line; one value or empty renders an empty box of the requested
9
+ * height (preserves layout while data loads).
10
+ */
11
+ data: number[];
12
+ /** Pixel height of the chart. Default 32. */
13
+ height?: number;
14
+ /** Pixel width. Default 100. */
15
+ width?: number;
16
+ /** Stroke color. Default `colors.zinc[700]`. */
17
+ color?: string;
18
+ /** Whether to fill the area under the line. Default false. */
19
+ filled?: boolean;
20
+ }
21
+
22
+ /**
23
+ * Compact line trend, ~40px tall. Sits next to a metric to answer "is
24
+ * this good?" — static numbers answer "what" but not "trending up or
25
+ * down". Used in dashboards (Stripe, Mercury, Linear all ship this
26
+ * pattern).
27
+ *
28
+ * Implementation: native HTML `<svg>` rather than `react-native-svg`.
29
+ * Custom-code apps are web-only and Vite resolves react-native-svg's
30
+ * Fabric native paths incorrectly (Metro handles the platform variants
31
+ * but Vite doesn't). Wrapping in RN's `<View>` preserves the layout
32
+ * surface; SVG inside renders cleanly on the DOM.
33
+ */
34
+ export function Sparkline(props: SparklineProps) {
35
+ const { data, height = 32, width = 100, color = colors.zinc[700], filled = false } = props;
36
+
37
+ const { path, fillPath } = useMemo(() => {
38
+ if (data.length < 2) return { path: null, fillPath: null };
39
+ const min = Math.min(...data);
40
+ const max = Math.max(...data);
41
+ // Flat data → horizontal line at vertical center. Avoid /0 division.
42
+ const range = max - min || 1;
43
+ // Stroke width budget — inset by 1.5px so the line doesn't clip the
44
+ // SVG viewport on either side.
45
+ const inset = 1.5;
46
+ const w = width - inset * 2;
47
+ const h = height - inset * 2;
48
+ const stepX = w / (data.length - 1);
49
+ let p = "";
50
+ data.forEach((v, i) => {
51
+ const x = inset + i * stepX;
52
+ // Y axis inverted in SVG (higher value = smaller y). Normalize to
53
+ // 0-1 then map into the inset area.
54
+ const y = inset + h - ((v - min) / range) * h;
55
+ p += `${i === 0 ? "M" : "L"} ${x.toFixed(2)} ${y.toFixed(2)} `;
56
+ });
57
+ const trimmed = p.trim();
58
+ return {
59
+ path: trimmed,
60
+ fillPath: filled
61
+ ? `${trimmed} L ${width - inset} ${height - inset} L ${inset} ${height - inset} Z`
62
+ : null,
63
+ };
64
+ }, [data, width, height, filled]);
65
+
66
+ if (!path) {
67
+ return <View style={{ width, height }} />;
68
+ }
69
+
70
+ return (
71
+ <View style={{ width, height }}>
72
+ <svg width={width} height={height} viewBox={`0 0 ${width} ${height}`}>
73
+ {fillPath && <path d={fillPath} fill={color} fillOpacity={0.1} />}
74
+ <path
75
+ d={path}
76
+ stroke={color}
77
+ strokeWidth={1.5}
78
+ fill="none"
79
+ strokeLinejoin="round"
80
+ strokeLinecap="round"
81
+ />
82
+ </svg>
83
+ </View>
84
+ );
85
+ }
@@ -0,0 +1,65 @@
1
+ import { View, StyleSheet } from "react-native";
2
+ import { colors } from "./colors";
3
+
4
+ interface Segment {
5
+ /** Identifier for the segment — used as a React key. */
6
+ key: string;
7
+ /** Numeric weight. Each segment's width is `value / total`. */
8
+ value: number;
9
+ /** CSS color. */
10
+ color: string;
11
+ }
12
+
13
+ interface StackedProgressBarProps {
14
+ segments: Segment[];
15
+ /** Total value across all segments. Pass explicitly so loading + empty
16
+ * states render consistently (an empty array would otherwise look like
17
+ * "data loaded with no values"). */
18
+ total: number;
19
+ /** Bar pixel height. Default 14 — the dashboard hero-progress size.
20
+ * Drop to 6-8 for inline status bars in tight rows; bump to 20-24
21
+ * when the bar IS the section's main visualization. */
22
+ height?: number;
23
+ loading?: boolean;
24
+ }
25
+
26
+ /**
27
+ * Horizontal segmented bar for funnels / status breakdowns / category
28
+ * distributions. The Mercury / Linear stage-breakdown pattern — handles
29
+ * sparse data gracefully because zero-value segments collapse to zero
30
+ * width, and a single dominant value renders as one long segment rather
31
+ * than a "broken" bar chart with five empty stages.
32
+ *
33
+ * Pair with `<LegendItem />` rows below to name the colored segments.
34
+ * Without a legend, the colored bar is decorative — viewers can't map
35
+ * colors back to meaning.
36
+ */
37
+ export function StackedProgressBar(props: StackedProgressBarProps) {
38
+ const { segments, total, height = 14, loading } = props;
39
+ if (loading || total === 0) {
40
+ return <View style={[styles.bar, { height, backgroundColor: colors.zinc[100] }]} />;
41
+ }
42
+ return (
43
+ <View style={[styles.bar, styles.barFilled, { height }]}>
44
+ {segments
45
+ .filter((s) => s.value > 0)
46
+ .map((seg) => (
47
+ <View
48
+ key={seg.key}
49
+ style={{ flex: seg.value, backgroundColor: seg.color }}
50
+ />
51
+ ))}
52
+ </View>
53
+ );
54
+ }
55
+
56
+ const styles = StyleSheet.create({
57
+ bar: {
58
+ borderRadius: 999,
59
+ overflow: "hidden",
60
+ },
61
+ barFilled: {
62
+ backgroundColor: colors.zinc[100],
63
+ flexDirection: "row",
64
+ },
65
+ });
package/src/text.css CHANGED
@@ -1,10 +1,26 @@
1
+ /*
2
+ * Letter-spacing curve: positive at small sizes (improves legibility for
3
+ * Vietnamese diacritics + Inter's `1`/`l`/`i` at 12px), neutral at body,
4
+ * negative at display. The single biggest "designed" tell on web type —
5
+ * Radix, Linear, Geist all use this curve.
6
+ *
7
+ * Inter stylistic alternates: `cv11` (single-storey `a`), `ss01` (alternate
8
+ * `1`), `ss03` (alternate `g`) — disambiguates similar glyphs without
9
+ * shifting metrics. No layout impact, premium-character signal.
10
+ */
11
+ * {
12
+ font-feature-settings: "cv11", "ss01", "ss03";
13
+ }
14
+
1
15
  [data-text-size="xs"] {
2
16
  font-size: 12px;
3
17
  line-height: 16px;
18
+ letter-spacing: 0.01em;
4
19
  }
5
20
  [data-text-size="sm"] {
6
21
  font-size: 14px;
7
22
  line-height: 20px;
23
+ letter-spacing: 0.0025em;
8
24
  }
9
25
  [data-text-size="md"] {
10
26
  font-size: 16px;
@@ -13,14 +29,17 @@
13
29
  [data-text-size="lg"] {
14
30
  font-size: 18px;
15
31
  line-height: 24px;
32
+ letter-spacing: -0.005em;
16
33
  }
17
34
  [data-text-size="xl"] {
18
- font-size: 28;
35
+ font-size: 28px;
19
36
  line-height: 34px;
37
+ letter-spacing: -0.015em;
20
38
  }
21
39
  [data-text-size="xxl"] {
22
40
  font-size: 32px;
23
41
  line-height: 38px;
42
+ letter-spacing: -0.02em;
24
43
  }
25
44
 
26
45
  /* Refer to `use_screen_size` for breakpoints */
package/src/theme.tsx ADDED
@@ -0,0 +1,61 @@
1
+ import { createContext, useContext, type ReactNode } from "react";
2
+
3
+ /**
4
+ * Default platform accent — refined OKLCH blue. Used by chart fills,
5
+ * focus rings, and any primitive that asks "what's the brand color".
6
+ * Apps that need a different accent wrap their root in `LoticsThemeProvider`.
7
+ *
8
+ * Why OKLCH instead of hex? Perceptual uniformity — `oklch(0.6 0.118 250)`
9
+ * sits at the same perceptual lightness/saturation as the `oklch(0.6 0.118
10
+ * 184.704)` (teal) chị's workspace uses, just shifted in hue. Hex shifts
11
+ * lightness as hue rotates and the eye picks it up as inconsistency.
12
+ */
13
+ export const DEFAULT_ACCENT = "oklch(0.6 0.118 250)";
14
+
15
+ interface LoticsTheme {
16
+ /** Single brand accent. Chart fills, hero CTAs, focus rings. */
17
+ accent: string;
18
+ }
19
+
20
+ const LoticsThemeContext = createContext<LoticsTheme>({ accent: DEFAULT_ACCENT });
21
+
22
+ interface LoticsThemeProviderProps {
23
+ /** Brand accent. Overrides the platform default. Accepts any CSS color
24
+ * value (OKLCH recommended, hex / hsl also fine). */
25
+ accent?: string;
26
+ children: ReactNode;
27
+ }
28
+
29
+ /**
30
+ * App-root provider that supplies brand tokens to @lotics/ui primitives.
31
+ * Wrap your top-level app element to override the platform defaults:
32
+ *
33
+ * // src/main.tsx
34
+ * <LoticsThemeProvider accent="oklch(0.6 0.118 184.704)">
35
+ * <App />
36
+ * </LoticsThemeProvider>
37
+ *
38
+ * Components that consume theme tokens use `useLoticsTheme()`. Each
39
+ * primitive also accepts a per-instance `color` prop for one-off
40
+ * customization without needing a different provider.
41
+ *
42
+ * Scope is intentionally narrow — accent only. Semantic colors (success,
43
+ * danger) already work via existing `colors.green[600]` etc. Adding more
44
+ * theme tokens requires a real product reason.
45
+ */
46
+ export function LoticsThemeProvider(props: LoticsThemeProviderProps) {
47
+ const accent = props.accent ?? DEFAULT_ACCENT;
48
+ return (
49
+ <LoticsThemeContext.Provider value={{ accent }}>
50
+ {props.children}
51
+ </LoticsThemeContext.Provider>
52
+ );
53
+ }
54
+
55
+ /**
56
+ * Read the current theme. Primitives that need the accent color call this
57
+ * hook; apps don't need it directly (use the provider's prop instead).
58
+ */
59
+ export function useLoticsTheme(): LoticsTheme {
60
+ return useContext(LoticsThemeContext);
61
+ }
@@ -0,0 +1,65 @@
1
+ import { View, StyleSheet } from "react-native";
2
+ import { Text } from "./text";
3
+ import { colors } from "./colors";
4
+
5
+ export interface TrendChipProps {
6
+ /**
7
+ * Percent delta vs comparator. Positive = up, negative = down, 0 = flat.
8
+ * Convention: signed. `value={12}` → "↑ 12%". `value={-5}` → "↓ 5%".
9
+ */
10
+ value: number;
11
+ /**
12
+ * Semantic override. By default, up = good (green) and down = bad (red).
13
+ * For metrics where down is good (e.g. response time, error rate), pass
14
+ * `goodDirection="down"` to flip the colors without changing the arrow.
15
+ */
16
+ goodDirection?: "up" | "down";
17
+ }
18
+
19
+ /**
20
+ * Inline ±% chip that sits next to a metric. Answers "is this good?" — the
21
+ * single most common missing piece on dashboards that show only "what".
22
+ * Stripe, Mercury, Linear all ship this pattern.
23
+ *
24
+ * Color logic: trend direction × goodDirection. Up + good="up" = green.
25
+ * Up + good="down" = red. Symmetric for down. Flat = neutral.
26
+ */
27
+ export function TrendChip(props: TrendChipProps) {
28
+ const { value, goodDirection = "up" } = props;
29
+
30
+ const direction = value > 0 ? "up" : value < 0 ? "down" : "flat";
31
+ const arrow = direction === "up" ? "↑" : direction === "down" ? "↓" : "→";
32
+
33
+ const isGood =
34
+ direction === "flat"
35
+ ? null
36
+ : (direction === "up" && goodDirection === "up") ||
37
+ (direction === "down" && goodDirection === "down");
38
+
39
+ const tone = isGood === null ? "neutral" : isGood ? "good" : "bad";
40
+
41
+ return (
42
+ <View style={[styles.chip, styles[`chip_${tone}`]]}>
43
+ <Text size="xs" weight="medium" style={styles[`text_${tone}`]}>
44
+ {arrow} {Math.abs(value)}%
45
+ </Text>
46
+ </View>
47
+ );
48
+ }
49
+
50
+ const styles = StyleSheet.create({
51
+ chip: {
52
+ paddingHorizontal: 6,
53
+ paddingVertical: 2,
54
+ borderRadius: 4,
55
+ alignSelf: "flex-start",
56
+ },
57
+ // Tinted backgrounds (50-shade) keep the chip readable + non-shouty.
58
+ // Pairs with same-hue 700-shade text → 4.5+ contrast.
59
+ chip_good: { backgroundColor: colors.green[50] },
60
+ chip_bad: { backgroundColor: colors.red[50] },
61
+ chip_neutral: { backgroundColor: colors.zinc[100] },
62
+ text_good: { color: colors.green[700] },
63
+ text_bad: { color: colors.red[700] },
64
+ text_neutral: { color: colors.zinc[600] },
65
+ });
@@ -0,0 +1,56 @@
1
+ import { TrendingDown, TrendingUp } from "lucide-react";
2
+ import { View, StyleSheet } from "react-native";
3
+ import { Text } from "./text";
4
+ import { colors } from "./colors";
5
+ import { SPACE } from "./spacing";
6
+
7
+ interface TrendFooterProps {
8
+ /** Signed percentage change. Positive = up, negative = down, 0 = no
9
+ * arrow. Caller is responsible for skipping the component entirely
10
+ * when there's no comparator (e.g., last-period base = 0). */
11
+ value: number;
12
+ /** Suffix after the percentage, e.g. "so với tháng trước". */
13
+ periodLabel: string;
14
+ /** Optional detail line below, e.g. "Tháng trước: 50.000.000 đ". */
15
+ detail?: string;
16
+ /** Override the up=green convention for metrics where down is good
17
+ * (response time, error rate). Defaults to `up`. */
18
+ goodDirection?: "up" | "down";
19
+ }
20
+
21
+ /**
22
+ * Footer caption that pairs the shadcn TrendingUp icon with a directional
23
+ * Vietnamese sentence — the "Tăng X% so với tháng trước" pattern at the
24
+ * bottom of every chart card on Stripe, Mercury, Linear.
25
+ *
26
+ * Goes inside `<SectionCard footer={...} />`. The Card adds the hairline
27
+ * divider above; this component owns only the icon + text composition.
28
+ */
29
+ export function TrendFooter(props: TrendFooterProps) {
30
+ const { value, periodLabel, detail, goodDirection = "up" } = props;
31
+ const up = value > 0;
32
+ const flat = value === 0;
33
+ const isGood = flat ? null : (up && goodDirection === "up") || (!up && goodDirection === "down");
34
+ const color = isGood === null ? colors.zinc[600] : isGood ? colors.green[700] : colors.red[700];
35
+ const Icon = up ? TrendingUp : TrendingDown;
36
+ return (
37
+ <View style={styles.container}>
38
+ <View style={styles.row}>
39
+ <Icon size={16} color={color} />
40
+ <Text size="sm" weight="medium" style={{ color }}>
41
+ {up ? "Tăng" : "Giảm"} {Math.abs(value)}% {periodLabel}
42
+ </Text>
43
+ </View>
44
+ {detail && (
45
+ <Text size="xs" color="muted">
46
+ {detail}
47
+ </Text>
48
+ )}
49
+ </View>
50
+ );
51
+ }
52
+
53
+ const styles = StyleSheet.create({
54
+ container: { gap: SPACE.xs },
55
+ row: { flexDirection: "row", alignItems: "center", gap: SPACE.sm },
56
+ });