react-native-terra-ui 0.11.17 → 0.12.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.
Files changed (109) hide show
  1. package/CHANGELOG.md +23 -0
  2. package/lib/module/components/accordion/Accordion.js +109 -0
  3. package/lib/module/components/accordion/Accordion.js.map +1 -0
  4. package/lib/module/components/accordion/context.js +34 -0
  5. package/lib/module/components/accordion/context.js.map +1 -0
  6. package/lib/module/components/accordion/hooks/use-accordion-core.js +46 -0
  7. package/lib/module/components/accordion/hooks/use-accordion-core.js.map +1 -0
  8. package/lib/module/components/accordion/index.js +5 -0
  9. package/lib/module/components/accordion/index.js.map +1 -0
  10. package/lib/module/components/accordion/parts/AccordionContent.js +98 -0
  11. package/lib/module/components/accordion/parts/AccordionContent.js.map +1 -0
  12. package/lib/module/components/accordion/parts/AccordionIndicator.js +60 -0
  13. package/lib/module/components/accordion/parts/AccordionIndicator.js.map +1 -0
  14. package/lib/module/components/accordion/parts/AccordionItem.js +53 -0
  15. package/lib/module/components/accordion/parts/AccordionItem.js.map +1 -0
  16. package/lib/module/components/accordion/parts/AccordionTrigger.js +85 -0
  17. package/lib/module/components/accordion/parts/AccordionTrigger.js.map +1 -0
  18. package/lib/module/components/accordion/types.js +4 -0
  19. package/lib/module/components/accordion/types.js.map +1 -0
  20. package/lib/module/components/accordion/utils.js +15 -0
  21. package/lib/module/components/accordion/utils.js.map +1 -0
  22. package/lib/module/components/avatar/Avatar.js +15 -3
  23. package/lib/module/components/avatar/Avatar.js.map +1 -1
  24. package/lib/module/components/badge/Badge.js +220 -64
  25. package/lib/module/components/badge/Badge.js.map +1 -1
  26. package/lib/module/components/badge/WithBadge.js +94 -0
  27. package/lib/module/components/badge/WithBadge.js.map +1 -0
  28. package/lib/module/components/badge/index.js +1 -0
  29. package/lib/module/components/badge/index.js.map +1 -1
  30. package/lib/module/components/badge/utils.js +3 -3
  31. package/lib/module/components/chip/Chip.js +1 -0
  32. package/lib/module/components/chip/Chip.js.map +1 -1
  33. package/lib/module/components/header/parts/HeaderBar.js +105 -69
  34. package/lib/module/components/header/parts/HeaderBar.js.map +1 -1
  35. package/lib/module/components/header/variants/LargeTitleHeader/parts/LargeTitleContent.js +50 -33
  36. package/lib/module/components/header/variants/LargeTitleHeader/parts/LargeTitleContent.js.map +1 -1
  37. package/lib/module/components/index.js +1 -0
  38. package/lib/module/components/index.js.map +1 -1
  39. package/lib/module/components/input/parts/InputField.js +96 -82
  40. package/lib/module/components/input/parts/InputField.js.map +1 -1
  41. package/lib/module/components/text/Text.js +10 -6
  42. package/lib/module/components/text/Text.js.map +1 -1
  43. package/lib/module/theme/tokens/primitives.js +24 -4
  44. package/lib/module/theme/tokens/primitives.js.map +1 -1
  45. package/lib/typescript/src/components/accordion/Accordion.d.ts +33 -0
  46. package/lib/typescript/src/components/accordion/Accordion.d.ts.map +1 -0
  47. package/lib/typescript/src/components/accordion/context.d.ts +25 -0
  48. package/lib/typescript/src/components/accordion/context.d.ts.map +1 -0
  49. package/lib/typescript/src/components/accordion/hooks/use-accordion-core.d.ts +11 -0
  50. package/lib/typescript/src/components/accordion/hooks/use-accordion-core.d.ts.map +1 -0
  51. package/lib/typescript/src/components/accordion/index.d.ts +8 -0
  52. package/lib/typescript/src/components/accordion/index.d.ts.map +1 -0
  53. package/lib/typescript/src/components/accordion/parts/AccordionContent.d.ts +8 -0
  54. package/lib/typescript/src/components/accordion/parts/AccordionContent.d.ts.map +1 -0
  55. package/lib/typescript/src/components/accordion/parts/AccordionIndicator.d.ts +16 -0
  56. package/lib/typescript/src/components/accordion/parts/AccordionIndicator.d.ts.map +1 -0
  57. package/lib/typescript/src/components/accordion/parts/AccordionItem.d.ts +16 -0
  58. package/lib/typescript/src/components/accordion/parts/AccordionItem.d.ts.map +1 -0
  59. package/lib/typescript/src/components/accordion/parts/AccordionTrigger.d.ts +11 -0
  60. package/lib/typescript/src/components/accordion/parts/AccordionTrigger.d.ts.map +1 -0
  61. package/lib/typescript/src/components/accordion/types.d.ts +65 -0
  62. package/lib/typescript/src/components/accordion/types.d.ts.map +1 -0
  63. package/lib/typescript/src/components/accordion/utils.d.ts +8 -0
  64. package/lib/typescript/src/components/accordion/utils.d.ts.map +1 -0
  65. package/lib/typescript/src/components/avatar/Avatar.d.ts.map +1 -1
  66. package/lib/typescript/src/components/badge/Badge.d.ts +13 -9
  67. package/lib/typescript/src/components/badge/Badge.d.ts.map +1 -1
  68. package/lib/typescript/src/components/badge/WithBadge.d.ts +18 -0
  69. package/lib/typescript/src/components/badge/WithBadge.d.ts.map +1 -0
  70. package/lib/typescript/src/components/badge/index.d.ts +2 -1
  71. package/lib/typescript/src/components/badge/index.d.ts.map +1 -1
  72. package/lib/typescript/src/components/badge/types.d.ts +35 -12
  73. package/lib/typescript/src/components/badge/types.d.ts.map +1 -1
  74. package/lib/typescript/src/components/badge/utils.d.ts +3 -3
  75. package/lib/typescript/src/components/badge/utils.d.ts.map +1 -1
  76. package/lib/typescript/src/components/header/parts/HeaderBar.d.ts.map +1 -1
  77. package/lib/typescript/src/components/index.d.ts +1 -0
  78. package/lib/typescript/src/components/index.d.ts.map +1 -1
  79. package/lib/typescript/src/components/input/parts/InputField.d.ts.map +1 -1
  80. package/lib/typescript/src/theme/tokens/primitives.d.ts.map +1 -1
  81. package/package.json +1 -1
  82. package/src/components/accordion/Accordion.tsx +116 -0
  83. package/src/components/accordion/context.ts +45 -0
  84. package/src/components/accordion/hooks/use-accordion-core.ts +51 -0
  85. package/src/components/accordion/index.ts +14 -0
  86. package/src/components/accordion/parts/AccordionContent.tsx +96 -0
  87. package/src/components/accordion/parts/AccordionIndicator.tsx +67 -0
  88. package/src/components/accordion/parts/AccordionItem.tsx +55 -0
  89. package/src/components/accordion/parts/AccordionTrigger.tsx +88 -0
  90. package/src/components/accordion/types.ts +71 -0
  91. package/src/components/accordion/utils.ts +12 -0
  92. package/src/components/avatar/Avatar.tsx +12 -2
  93. package/src/components/badge/Badge.tsx +178 -59
  94. package/src/components/badge/WithBadge.tsx +79 -0
  95. package/src/components/badge/index.ts +2 -1
  96. package/src/components/badge/types.ts +36 -12
  97. package/src/components/badge/utils.ts +3 -3
  98. package/src/components/chip/Chip.tsx +1 -0
  99. package/src/components/header/parts/HeaderBar.tsx +100 -57
  100. package/src/components/header/variants/LargeTitleHeader/parts/LargeTitleContent.tsx +47 -29
  101. package/src/components/index.ts +1 -0
  102. package/src/components/input/parts/InputField.tsx +63 -55
  103. package/src/components/text/Text.tsx +10 -6
  104. package/src/theme/tokens/primitives.ts +24 -4
  105. package/lib/module/components/badge/parts/BadgeIndicator.js +0 -212
  106. package/lib/module/components/badge/parts/BadgeIndicator.js.map +0 -1
  107. package/lib/typescript/src/components/badge/parts/BadgeIndicator.d.ts +0 -19
  108. package/lib/typescript/src/components/badge/parts/BadgeIndicator.d.ts.map +0 -1
  109. package/src/components/badge/parts/BadgeIndicator.tsx +0 -178
@@ -0,0 +1,88 @@
1
+ import { Children, type ComponentRef, forwardRef, type ReactNode } from "react";
2
+ import { type GestureResponderEvent, Pressable, type PressableProps } from "react-native";
3
+
4
+ import { StyleSheet } from "react-native-unistyles";
5
+
6
+ import { Text } from "../../text";
7
+ import { useAccordionItem } from "../context";
8
+
9
+ export interface AccordionTriggerProps extends Omit<PressableProps, "children" | "disabled"> {
10
+ /**
11
+ * The header row. Strings and numbers become the item's title; put an
12
+ * `Accordion.Indicator` last to show the open state.
13
+ */
14
+ children?: ReactNode;
15
+ }
16
+
17
+ function renderTitle(children: ReactNode): ReactNode {
18
+ return Children.map(children, (child) => {
19
+ if (typeof child === "string") {
20
+ return child.trim().length > 0 ? (
21
+ <Text variant="body-lg" weight="medium" style={styles.title}>
22
+ {child}
23
+ </Text>
24
+ ) : null;
25
+ }
26
+ if (typeof child === "number") {
27
+ return (
28
+ <Text variant="body-lg" weight="medium" style={styles.title}>
29
+ {child}
30
+ </Text>
31
+ );
32
+ }
33
+ return child;
34
+ });
35
+ }
36
+
37
+ export const AccordionTrigger = forwardRef<ComponentRef<typeof Pressable>, AccordionTriggerProps>(
38
+ function AccordionTrigger({ children, onPress, style, ...rest }, ref) {
39
+ const { isExpanded, isDisabled, toggle } = useAccordionItem();
40
+
41
+ const handlePress = (event: GestureResponderEvent) => {
42
+ onPress?.(event);
43
+ toggle();
44
+ };
45
+
46
+ return (
47
+ <Pressable
48
+ ref={ref}
49
+ accessibilityRole="button"
50
+ accessibilityState={{ expanded: isExpanded, disabled: isDisabled }}
51
+ disabled={isDisabled}
52
+ onPress={handlePress}
53
+ style={(state) => [
54
+ styles.trigger,
55
+ state.pressed && styles.pressed,
56
+ isDisabled && styles.disabled,
57
+ typeof style === "function" ? style(state) : style,
58
+ ]}
59
+ {...rest}
60
+ >
61
+ {renderTitle(children)}
62
+ </Pressable>
63
+ );
64
+ }
65
+ );
66
+
67
+ // ─── Styles ───────────────────────────────────────────────────────────────────
68
+
69
+ const styles = StyleSheet.create((theme) => ({
70
+ trigger: {
71
+ flexDirection: "row",
72
+ alignItems: "center",
73
+ gap: theme.spacing["3"],
74
+ minHeight: 48,
75
+ paddingVertical: theme.spacing["4"],
76
+ },
77
+ pressed: {
78
+ opacity: theme.opacity.pressed,
79
+ },
80
+ disabled: {
81
+ opacity: theme.opacity.disabled,
82
+ },
83
+ // Takes the free space so an indicator after it sits at the row's end, and
84
+ // wraps a long title instead of pushing the indicator off.
85
+ title: {
86
+ flex: 1,
87
+ },
88
+ }));
@@ -0,0 +1,71 @@
1
+ import type { ReactNode } from "react";
2
+ import type { ViewProps } from "react-native";
3
+
4
+ export type AccordionSelectionMode = "single" | "multiple";
5
+
6
+ export type AccordionVariant = "plain" | "surface";
7
+
8
+ interface AccordionBaseProps extends Omit<ViewProps, "children"> {
9
+ /**
10
+ * Visual treatment. `'plain'` draws the items straight onto whatever sits
11
+ * behind the accordion; `'surface'` puts them in a filled, rounded container.
12
+ * Defaults to `'plain'`.
13
+ */
14
+ variant?: AccordionVariant;
15
+ /** Draws a hairline rule between items. Defaults to `true`. */
16
+ divided?: boolean;
17
+ /** Disables every item. Defaults to `false`. */
18
+ isDisabled?: boolean;
19
+ /** `Accordion.Item` elements. */
20
+ children?: ReactNode;
21
+ }
22
+
23
+ export interface AccordionSingleProps extends AccordionBaseProps {
24
+ /**
25
+ * `'single'` keeps at most one item open; opening another closes the current
26
+ * one. Defaults to `'single'`.
27
+ */
28
+ selectionMode?: "single";
29
+ /**
30
+ * The open item, controlled. `null` means none is open — `undefined` leaves
31
+ * the accordion uncontrolled.
32
+ */
33
+ value?: string | null;
34
+ /** The item open on first render, uncontrolled. Defaults to `undefined`. */
35
+ defaultValue?: string;
36
+ /** Called with the newly open item, or `null` when the open item closes. */
37
+ onValueChange?: (value: string | null) => void;
38
+ /**
39
+ * Lets the open item close by pressing its trigger again. `false` keeps one
40
+ * item open at all times once any has been opened. Defaults to `true`.
41
+ */
42
+ isCollapsible?: boolean;
43
+ }
44
+
45
+ export interface AccordionMultipleProps extends AccordionBaseProps {
46
+ /** `'multiple'` lets any number of items be open at once. */
47
+ selectionMode: "multiple";
48
+ /** The open items, controlled. */
49
+ value?: string[];
50
+ /** The items open on first render, uncontrolled. Defaults to `[]`. */
51
+ defaultValue?: string[];
52
+ /** Called with every open item after one is toggled. */
53
+ onValueChange?: (value: string[]) => void;
54
+ /** Multiple mode can always close an item. */
55
+ isCollapsible?: never;
56
+ }
57
+
58
+ export type AccordionProps = AccordionSingleProps | AccordionMultipleProps;
59
+
60
+ export interface AccordionItemState {
61
+ /** Whether the item's content is open. */
62
+ isExpanded: boolean;
63
+ /** Whether the item, or the whole accordion, is disabled. */
64
+ isDisabled: boolean;
65
+ }
66
+
67
+ export interface AccordionItemContextValue extends AccordionItemState {
68
+ value: string;
69
+ /** Opens or closes this item, following the accordion's selection rules. */
70
+ toggle: () => void;
71
+ }
@@ -0,0 +1,12 @@
1
+ import { Easing, ReduceMotion, type WithTimingConfig } from "react-native-reanimated";
2
+
3
+ /**
4
+ * Shared by the content's height and the indicator's rotation so the chevron
5
+ * finishes turning as the panel finishes opening. Honours the system
6
+ * reduce-motion setting, which makes both jump straight to their end state.
7
+ */
8
+ export const ACCORDION_TIMING: WithTimingConfig = {
9
+ duration: 250,
10
+ easing: Easing.bezier(0.25, 0.1, 0.25, 1),
11
+ reduceMotion: ReduceMotion.System,
12
+ };
@@ -4,6 +4,7 @@ import { Text, View } from "react-native";
4
4
  import { StyleSheet, useUnistyles } from "react-native-unistyles";
5
5
 
6
6
  import type { RadiusKey } from "#theme/types";
7
+ import { FONT_WEIGHT_VALUE, SYSTEM_FONT } from "#utils/typography";
7
8
  import { Icon } from "../icon";
8
9
  import { Image } from "../image";
9
10
  import type { AvatarProps, AvatarShape, AvatarSize } from "./types";
@@ -16,8 +17,9 @@ const SIZE_PX: Record<AvatarSize, number> = {
16
17
  xl: 64,
17
18
  };
18
19
 
20
+ // `xs` sits at the 10dp legibility floor rather than scaling the ratio down.
19
21
  const INITIALS_FONT_SIZE: Record<AvatarSize, number> = {
20
- xs: 9,
22
+ xs: 10,
21
23
  sm: 12,
22
24
  md: 15,
23
25
  lg: 18,
@@ -75,6 +77,11 @@ export function Avatar({
75
77
  const bg = styles.base.backgroundColor ?? "";
76
78
  const fg = styles.base.color ?? "";
77
79
 
80
+ // Initials follow the theme's semibold face so a custom font family reaches
81
+ // the avatar too; the system font takes a numeric weight instead.
82
+ const initialsFont = theme.typography.fonts.semibold;
83
+ const isSystemInitialsFont = initialsFont === SYSTEM_FONT;
84
+
78
85
  const fallbackContent =
79
86
  fallback !== undefined ? (
80
87
  fallback
@@ -82,10 +89,13 @@ export function Avatar({
82
89
  <Text
83
90
  style={{
84
91
  fontSize: INITIALS_FONT_SIZE[size],
85
- fontWeight: "600",
92
+ fontFamily: isSystemInitialsFont ? undefined : initialsFont,
93
+ fontWeight: isSystemInitialsFont ? FONT_WEIGHT_VALUE.semibold : undefined,
86
94
  color: fg,
87
95
  includeFontPadding: false,
88
96
  }}
97
+ // The circle is fixed-size, so cap scaling like a control label.
98
+ maxFontSizeMultiplier={theme.typography.variants["label-sm"].maxFontSizeMultiplier}
89
99
  numberOfLines={1}
90
100
  >
91
101
  {getInitials(name)}
@@ -1,78 +1,197 @@
1
- import { type ComponentRef, forwardRef } from "react";
2
- import { View } from "react-native";
1
+ import { Text as RNText, View } from "react-native";
3
2
 
4
- import { StyleSheet } from "react-native-unistyles";
3
+ import { StyleSheet, useUnistyles } from "react-native-unistyles";
5
4
 
6
- import { BadgeIndicator } from "./parts/BadgeIndicator";
7
- import type { BadgeProps } from "./types";
8
- import { getAnchorPosition } from "./utils";
5
+ import type { ColorToken, RoleTextVariant } from "#theme/types";
6
+ import { resolveThemeColor } from "#utils/resolve-theme-color";
7
+ import { FONT_WEIGHT_VALUE, SYSTEM_FONT } from "#utils/typography";
8
+ import type { BadgeProps, BadgeSize } from "./types";
9
+ import { getCustomBadgeColors, hasLabel } from "./utils";
10
+
11
+ const SCHEME_NAMES = new Set(["primary", "secondary", "success", "warning", "danger"]);
12
+
13
+ type BadgeScheme = "primary" | "secondary" | "success" | "warning" | "danger";
14
+
15
+ /** Label foreground per scheme — mirrors the spec's token table. */
16
+ const FG_TOKEN: Record<BadgeScheme, ColorToken> = {
17
+ primary: "accent.solid.fg",
18
+ secondary: "neutral.solid.fg",
19
+ success: "success.solid.fg",
20
+ warning: "warning.solid.fg",
21
+ danger: "danger.solid.fg",
22
+ };
23
+
24
+ const LABEL_VARIANT: Record<BadgeSize, RoleTextVariant> = {
25
+ sm: "label-sm",
26
+ md: "label-md",
27
+ lg: "label-lg",
28
+ };
9
29
 
10
30
  /**
11
- * Overlays a count or status indicator on one corner of the element it wraps.
31
+ * A dot, or a pill carrying a count or short label.
12
32
  *
13
33
  * ```tsx
14
- * <Badge placement="top-right" color="danger" label={3}>
15
- * <Toolbar.Button icon="bell" accessibilityLabel="Notifications" />
16
- * </Badge>
34
+ * <Text>Appearing</Text>
35
+ * <Badge color="secondary" size="sm" label={56} />
17
36
  * ```
18
37
  *
19
- * The child renders untouched — `Badge` contributes the positioning context and
20
- * the overlay, nothing else. A round child usually wants a negative `offset`,
21
- * since the corner being anchored to is the bounding box's, not the circle's.
38
+ * Stands on its own, inline in a row beside the text it counts. To pin one to
39
+ * the corner of an icon or button, wrap that element in `WithBadge`, which
40
+ * renders this and owns only the position.
41
+ *
42
+ * `useUnistyles` is called unconditionally, which is what subscribes this
43
+ * component to theme changes. The background below comes from a variant, which
44
+ * unistyles applies straight to the native node on a theme switch; without a
45
+ * re-render React would go on believing the node holds the previous theme's
46
+ * value. `Chip` documents the same hazard on its root.
22
47
  */
23
- const Badge = forwardRef<ComponentRef<typeof View>, BadgeProps>(function Badge(
24
- {
25
- children,
26
- placement = "top-right",
27
- label,
28
- color = "danger",
29
- size = "md",
30
- offset = 0,
31
- isVisible = true,
32
- fullWidth = false,
33
- accessibilityLabel,
34
- style,
35
- ...rest
36
- },
37
- ref
38
- ) {
39
- styles.useVariants({ fullWidth });
48
+ export const Badge = ({
49
+ label,
50
+ color = "danger",
51
+ size = "md",
52
+ ring = false,
53
+ accessibilityLabel,
54
+ style,
55
+ ...rest
56
+ }: BadgeProps) => {
57
+ const { theme } = useUnistyles();
58
+ // A literal never matches one of the five variant options, so it's cast to
59
+ // satisfy `useVariants`'s literal-union type while still being compared
60
+ // against the real value below.
61
+ styles.useVariants({ color: color as BadgeScheme, size, ring });
62
+
63
+ const custom = SCHEME_NAMES.has(color) ? null : getCustomBadgeColors(color);
64
+ const labelled = hasLabel(label);
65
+ const foreground = custom ? custom.fg : FG_TOKEN[color as BadgeScheme];
66
+
67
+ // An explicit label wins even on a dot; a dot with nothing to announce is
68
+ // decorative and hidden rather than read out as an unlabelled element.
69
+ const announced = accessibilityLabel ?? (labelled ? String(label) : undefined);
70
+ const accessibility = announced
71
+ ? { accessible: true, accessibilityLabel: announced }
72
+ : {
73
+ accessibilityElementsHidden: true,
74
+ importantForAccessibility: "no-hide-descendants" as const,
75
+ "aria-hidden": true,
76
+ };
40
77
 
41
78
  return (
42
- // `style` last so an explicit `alignSelf` beats `fullWidth`.
43
- <View ref={ref} style={[styles.wrapper, style]} {...rest}>
44
- {children}
45
- {isVisible ? (
46
- <View pointerEvents="box-none" style={[styles.anchor, getAnchorPosition(placement, size, offset)]}>
47
- <BadgeIndicator label={label} color={color} size={size} accessibilityLabel={accessibilityLabel} />
48
- </View>
79
+ <View
80
+ style={[
81
+ styles.indicator,
82
+ labelled ? styles.pill : styles.dot,
83
+ custom ? { backgroundColor: custom.bg } : null,
84
+ style,
85
+ ]}
86
+ {...rest}
87
+ {...accessibility}
88
+ >
89
+ {labelled ? (
90
+ <RNText
91
+ style={[styles.label, { color: resolveThemeColor(foreground, theme) }]}
92
+ maxFontSizeMultiplier={theme.typography.variants[LABEL_VARIANT[size]].maxFontSizeMultiplier}
93
+ numberOfLines={1}
94
+ >
95
+ {label}
96
+ </RNText>
49
97
  ) : null}
50
98
  </View>
51
99
  );
52
- });
53
-
54
- export { Badge };
100
+ };
55
101
 
56
102
  // ─── Styles ───────────────────────────────────────────────────────────────────
57
103
  //
58
- // The indicator's insets are computed in `getAnchorPosition`, not declared here:
59
- // `offset` is an open-ended number, so it could never be a variant condition,
60
- // and the position involves no theme tokens at all. That leaves this stylesheet
61
- // with only what genuinely varies declaratively.
62
- const styles = StyleSheet.create(() => ({
63
- wrapper: {
64
- position: "relative",
65
- variants: {
66
- // `alignSelf: 'flex-start'` hugs the child, so the indicator anchors to
67
- // the child's bounds rather than the surrounding container's. A child
68
- // that was stretching needs `fullWidth` to go on doing so.
69
- fullWidth: {
70
- true: { alignSelf: "stretch" },
71
- false: { alignSelf: "flex-start" },
104
+ // `color` is a plain variant rather than a compound one: `Badge` renders solid
105
+ // only, so there is no second paint axis for it to interact with.
106
+ //
107
+ // React Native's box model is border-box, so the ring below eats into the
108
+ // badge rather than growing it — the dot's outer diameter stays exactly what
109
+ // `size` declares, with no compensation arithmetic, ring or not.
110
+ const styles = StyleSheet.create((theme) => {
111
+ const c = theme.color;
112
+ // A custom font encodes its weight in the family name, so only the system
113
+ // font takes a numeric `fontWeight` — the same branch `Chip` makes. Inlined
114
+ // rather than extracted: the unistyles Babel plugin rewrites this factory,
115
+ // and an imported function called from inside it is not in scope at runtime.
116
+ const labelFont = theme.typography.fonts.semibold;
117
+ const isSystemLabelFont = labelFont === SYSTEM_FONT;
118
+
119
+ return {
120
+ indicator: {
121
+ alignItems: "center",
122
+ justifyContent: "center",
123
+ borderRadius: theme.radius.full,
124
+ borderColor: c["surface.canvas"],
125
+ variants: {
126
+ // No styles of its own; it only has to exist for the compound rows
127
+ // below to match on. The ring's width depends on size as well.
128
+ size: { sm: {}, md: {}, lg: {} },
129
+ ring: {
130
+ true: {},
131
+ false: { borderWidth: 0 },
132
+ },
133
+ color: {
134
+ primary: { backgroundColor: c["accent.solid.bg"] },
135
+ secondary: { backgroundColor: c["neutral.solid.bg"] },
136
+ success: { backgroundColor: c["success.solid.bg"] },
137
+ warning: { backgroundColor: c["warning.solid.bg"] },
138
+ danger: { backgroundColor: c["danger.solid.bg"] },
139
+ },
72
140
  },
141
+ compoundVariants: [
142
+ { ring: true, size: "sm", styles: { borderWidth: 1.5 } },
143
+ { ring: true, size: "md", styles: { borderWidth: 2 } },
144
+ { ring: true, size: "lg", styles: { borderWidth: 2 } },
145
+ ],
73
146
  },
74
- },
75
- anchor: {
76
- position: "absolute",
77
- },
78
- }));
147
+ // These diameters are mirrored by `DOT_SIZE` in `./utils.ts`, which the
148
+ // anchor arithmetic reads. The Babel plugin needs them literal here, so a
149
+ // test asserts the two stay equal.
150
+ dot: {
151
+ variants: {
152
+ size: {
153
+ sm: { width: 12, height: 12 },
154
+ md: { width: 16, height: 16 },
155
+ lg: { width: 20, height: 20 },
156
+ },
157
+ },
158
+ },
159
+ pill: {
160
+ variants: {
161
+ size: {
162
+ sm: { height: 18, minWidth: 18, paddingHorizontal: 5 },
163
+ md: { height: 22, minWidth: 22, paddingHorizontal: 6 },
164
+ lg: { height: 26, minWidth: 26, paddingHorizontal: 8 },
165
+ },
166
+ },
167
+ },
168
+ // Spelled out rather than derived from `LABEL_VARIANT`: the unistyles Babel
169
+ // plugin reads this object statically, so computed variant keys are not
170
+ // safe here even though they would resolve correctly under test.
171
+ label: {
172
+ textAlign: "center",
173
+ includeFontPadding: false,
174
+ fontFamily: isSystemLabelFont ? undefined : labelFont,
175
+ fontWeight: isSystemLabelFont ? FONT_WEIGHT_VALUE.semibold : undefined,
176
+ variants: {
177
+ size: {
178
+ sm: {
179
+ fontSize: theme.typography.variants["label-sm"].fontSize,
180
+ lineHeight: theme.typography.variants["label-sm"].lineHeight,
181
+ letterSpacing: theme.typography.variants["label-sm"].letterSpacing,
182
+ },
183
+ md: {
184
+ fontSize: theme.typography.variants["label-md"].fontSize,
185
+ lineHeight: theme.typography.variants["label-md"].lineHeight,
186
+ letterSpacing: theme.typography.variants["label-md"].letterSpacing,
187
+ },
188
+ lg: {
189
+ fontSize: theme.typography.variants["label-lg"].fontSize,
190
+ lineHeight: theme.typography.variants["label-lg"].lineHeight,
191
+ letterSpacing: theme.typography.variants["label-lg"].letterSpacing,
192
+ },
193
+ },
194
+ },
195
+ },
196
+ };
197
+ });
@@ -0,0 +1,79 @@
1
+ import { type ComponentRef, forwardRef } from "react";
2
+ import { View } from "react-native";
3
+
4
+ import { StyleSheet } from "react-native-unistyles";
5
+
6
+ import { Badge } from "./Badge";
7
+ import type { WithBadgeProps } from "./types";
8
+ import { getAnchorPosition } from "./utils";
9
+
10
+ /**
11
+ * Pins a `Badge` to one corner of the element it wraps.
12
+ *
13
+ * ```tsx
14
+ * <WithBadge placement="top-right" color="danger" label={3}>
15
+ * <Toolbar.Button icon="bell" accessibilityLabel="Notifications" />
16
+ * </WithBadge>
17
+ * ```
18
+ *
19
+ * The child renders untouched — `WithBadge` contributes the positioning context
20
+ * and the overlay, nothing else. A round child usually wants a negative
21
+ * `offset`, since the corner being anchored to is the bounding box's, not the
22
+ * circle's. For a badge that sits inline beside text, use `Badge` on its own.
23
+ */
24
+ const WithBadge = forwardRef<ComponentRef<typeof View>, WithBadgeProps>(function WithBadge(
25
+ {
26
+ children,
27
+ placement = "top-right",
28
+ label,
29
+ color = "danger",
30
+ size = "md",
31
+ offset = 0,
32
+ isVisible = true,
33
+ fullWidth = false,
34
+ accessibilityLabel,
35
+ style,
36
+ ...rest
37
+ },
38
+ ref
39
+ ) {
40
+ styles.useVariants({ fullWidth });
41
+
42
+ return (
43
+ // `style` last so an explicit `alignSelf` beats `fullWidth`.
44
+ <View ref={ref} style={[styles.wrapper, style]} {...rest}>
45
+ {children}
46
+ {isVisible ? (
47
+ <View pointerEvents="box-none" style={[styles.anchor, getAnchorPosition(placement, size, offset)]}>
48
+ <Badge label={label} color={color} size={size} ring accessibilityLabel={accessibilityLabel} />
49
+ </View>
50
+ ) : null}
51
+ </View>
52
+ );
53
+ });
54
+
55
+ export { WithBadge };
56
+
57
+ // ─── Styles ───────────────────────────────────────────────────────────────────
58
+ //
59
+ // The badge's insets are computed in `getAnchorPosition`, not declared here:
60
+ // `offset` is an open-ended number, so it could never be a variant condition,
61
+ // and the position involves no theme tokens at all. That leaves this stylesheet
62
+ // with only what genuinely varies declaratively.
63
+ const styles = StyleSheet.create(() => ({
64
+ wrapper: {
65
+ position: "relative",
66
+ variants: {
67
+ // `alignSelf: 'flex-start'` hugs the child, so the indicator anchors to
68
+ // the child's bounds rather than the surrounding container's. A child
69
+ // that was stretching needs `fullWidth` to go on doing so.
70
+ fullWidth: {
71
+ true: { alignSelf: "stretch" },
72
+ false: { alignSelf: "flex-start" },
73
+ },
74
+ },
75
+ },
76
+ anchor: {
77
+ position: "absolute",
78
+ },
79
+ }));
@@ -1,2 +1,3 @@
1
1
  export { Badge } from "./Badge";
2
- export type { BadgeColor, BadgePlacement, BadgeProps, BadgeSize } from "./types";
2
+ export type { BadgeColor, BadgePlacement, BadgeProps, BadgeSize, WithBadgeProps } from "./types";
3
+ export { WithBadge } from "./WithBadge";
@@ -1,7 +1,7 @@
1
1
  import type { ReactNode } from "react";
2
2
  import type { ViewProps } from "react-native";
3
3
 
4
- /** Corner of the wrapped child the indicator is anchored to. */
4
+ /** Corner of the wrapped child a `WithBadge` anchors its badge to. */
5
5
  export type BadgePlacement = "top-right" | "top-left" | "bottom-right" | "bottom-left";
6
6
 
7
7
  /** Scale of the dot, pill, and label. */
@@ -9,7 +9,7 @@ export type BadgeSize = "sm" | "md" | "lg";
9
9
 
10
10
  /**
11
11
  * Semantic color scheme, or a raw CSS color literal (`#fff`, `rgb(...)`, `hsl(...)`).
12
- * A literal paints the indicator with that exact value and derives a readable
12
+ * A literal paints the badge with that exact value and derives a readable
13
13
  * foreground for the label.
14
14
  */
15
15
  export type BadgeColor =
@@ -21,37 +21,61 @@ export type BadgeColor =
21
21
  // literals last so scheme-name autocomplete still surfaces
22
22
  | (string & {});
23
23
 
24
- export interface BadgeProps extends ViewProps {
25
- /** The element being badged. Rendered untouched; `Badge` only adds the overlay. */
24
+ export interface BadgeProps extends Omit<ViewProps, "children"> {
25
+ /**
26
+ * Text in the badge. Omit it (or pass an empty string) for a dot.
27
+ * `0` renders as a label; a caller hiding a zero count leaves the badge out.
28
+ */
29
+ label?: string | number;
30
+ /** Semantic scheme or raw color literal. Defaults to `'danger'`. */
31
+ color?: BadgeColor;
32
+ /** Scale of the badge. Defaults to `'md'`. */
33
+ size?: BadgeSize;
34
+ /**
35
+ * Draws a `surface.canvas` ring around the badge, so it stays legible over
36
+ * whatever it overlaps. `WithBadge` turns it on. A badge inline beside text
37
+ * overlaps nothing, so it defaults to `false`: on a card the ring would only
38
+ * read as an outline.
39
+ */
40
+ ring?: boolean;
41
+ /**
42
+ * Announced in place of the label. Defaults to the label's own text; an
43
+ * unlabelled dot is hidden from assistive technology.
44
+ */
45
+ accessibilityLabel?: string;
46
+ }
47
+
48
+ export interface WithBadgeProps extends ViewProps {
49
+ /** The element being badged. Rendered untouched; `WithBadge` only adds the overlay. */
26
50
  children: ReactNode;
27
51
  /**
28
- * Corner the indicator sits on. Defaults to `'top-right'`.
52
+ * Corner the badge sits on. Defaults to `'top-right'`.
29
53
  *
30
- * The indicator overhangs the child, so an ancestor that clips — a `Surface`
54
+ * The badge overhangs the child, so an ancestor that clips — a `Surface`
31
55
  * with a radius, or a `Screen` list row — will cut it off. Pad that container
32
- * by the overhang, or lift the `Badge` outside it.
56
+ * by the overhang, or lift the `WithBadge` outside it.
33
57
  */
34
58
  placement?: BadgePlacement;
35
59
  /**
36
- * Text in the indicator. Omit it (or pass an empty string) for a dot.
60
+ * Text in the badge. Omit it (or pass an empty string) for a dot.
37
61
  * `0` renders as a label — use `isVisible` to suppress a zero count.
38
62
  */
39
63
  label?: string | number;
40
64
  /** Semantic scheme or raw color literal. Defaults to `'danger'`. */
41
65
  color?: BadgeColor;
42
- /** Scale of the indicator. Defaults to `'md'`. */
66
+ /** Scale of the badge. Defaults to `'md'`. */
43
67
  size?: BadgeSize;
44
68
  /**
45
- * Nudges the indicator along both axes from the corner `placement` picked.
69
+ * Nudges the badge along both axes from the corner `placement` picked.
46
70
  * Positive moves it away from the child's centre, negative pulls it in.
47
71
  * A number applies to both axes; `{ x, y }` sets them separately.
48
72
  *
49
73
  * The corner it anchors to is the child's **bounding box**, so a round child
50
- * needs a negative offset to bring the indicator onto the visible edge — a
74
+ * needs a negative offset to bring the badge onto the visible edge — a
51
75
  * circle only meets its box at 45°, about `0.21 × width` away. Defaults to `0`.
52
76
  */
53
77
  offset?: number | { x?: number; y?: number };
54
- /** Renders the indicator. The child renders either way. Defaults to `true`. */
78
+ /** Renders the badge. The child renders either way. Defaults to `true`. */
55
79
  isVisible?: boolean;
56
80
  /**
57
81
  * Stretches the wrapper along its parent's cross axis, so a child that