@delacour/react-native-ui 0.1.0-alpha.20260925060251 → 0.1.0-alpha.20260925060619
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/package.json +3 -2
- package/src/components/chip/AGENTS.md +108 -0
- package/src/components/chip/chip-close-button.tsx +48 -0
- package/src/components/chip/chip-end-content.tsx +15 -0
- package/src/components/chip/chip-label.tsx +21 -0
- package/src/components/chip/chip-start-content.tsx +16 -0
- package/src/components/chip/chip.context.tsx +66 -0
- package/src/components/chip/chip.tsx +264 -0
- package/src/components/chip/chip.types.ts +10 -0
- package/src/components/chip/chip.variants.test.ts +335 -0
- package/src/components/chip/chip.variants.ts +321 -0
- package/src/components/chip/index.ts +24 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@delacour/react-native-ui",
|
|
3
|
-
"version": "0.1.0-alpha.
|
|
3
|
+
"version": "0.1.0-alpha.20260925060619",
|
|
4
4
|
"description": "React Native UI components — Uniwind, Reanimated, Gesture Handler",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -39,6 +39,7 @@
|
|
|
39
39
|
"./button": "./src/components/button/index.ts",
|
|
40
40
|
"./chart": "./src/components/chart/index.ts",
|
|
41
41
|
"./checkbox": "./src/components/checkbox/index.ts",
|
|
42
|
+
"./chip": "./src/components/chip/index.ts",
|
|
42
43
|
"./field": "./src/components/field/index.ts",
|
|
43
44
|
"./icon": "./src/components/icon/index.ts",
|
|
44
45
|
"./input": "./src/components/input/index.ts",
|
|
@@ -85,7 +86,7 @@
|
|
|
85
86
|
},
|
|
86
87
|
"peerDependencies": {
|
|
87
88
|
"@central-icons-react-native/round-outlined-radius-1-stroke-1.5": "^1.1",
|
|
88
|
-
"@delacour/react-native-charts": "0.1.0-alpha.
|
|
89
|
+
"@delacour/react-native-charts": "0.1.0-alpha.20260925060619",
|
|
89
90
|
"@gorhom/bottom-sheet": "^5.2.8",
|
|
90
91
|
"@legendapp/list": ">=3.3",
|
|
91
92
|
"expo-linear-gradient": ">=15",
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
# Chip
|
|
2
|
+
|
|
3
|
+
An interactive pill — a filter that is on or off, a tag, or a token that can be
|
|
4
|
+
removed. Compound root plus `Chip.Label`, `Chip.StartContent`, `Chip.EndContent`
|
|
5
|
+
and `Chip.CloseButton`.
|
|
6
|
+
|
|
7
|
+
`import { Chip } from "@delacour/react-native-ui/chip";`
|
|
8
|
+
|
|
9
|
+
## Files
|
|
10
|
+
|
|
11
|
+
| File | What it holds |
|
|
12
|
+
| --- | --- |
|
|
13
|
+
| `index.ts` | → `@delacour/react-native-ui/chip` |
|
|
14
|
+
| `chip.tsx` | Root + the `Object.assign` compound surface |
|
|
15
|
+
| `chip-label.tsx` | `Chip.Label` |
|
|
16
|
+
| `chip-start-content.tsx` | `Chip.StartContent` |
|
|
17
|
+
| `chip-end-content.tsx` | `Chip.EndContent` |
|
|
18
|
+
| `chip-close-button.tsx` | `Chip.CloseButton`, the remove pressable |
|
|
19
|
+
| `chip.context.tsx` | `ChipContext`, `useChip()`, `useChipContext()`, `useChipPart()` |
|
|
20
|
+
| `chip.types.ts` | Prop types shared by two or more parts |
|
|
21
|
+
| `chip.variants.ts` | Pure `tv()` slots, the foreground map, hit slops, `resolveChipMode` / `resolveChipSurface` / `resolveChipForegroundToken` — no RN imports |
|
|
22
|
+
| `chip.variants.test.ts` | |
|
|
23
|
+
|
|
24
|
+
## Design
|
|
25
|
+
|
|
26
|
+
- **What a chip does is read from its props, never from a `type` flag.**
|
|
27
|
+
`resolveChipMode` returns `static`, `button` or `toggle`. No handler is a tag,
|
|
28
|
+
rendered as a plain `View` — a detector under every tag in a list of fifty
|
|
29
|
+
would announce each as a button with no action, the same reason
|
|
30
|
+
[`Badge`](../badge/AGENTS.md) stays inert. `onPress` or `onLongPress` is a
|
|
31
|
+
button. **Any** selection prop — `isSelected` even when `false`,
|
|
32
|
+
`defaultSelected`, `onSelectedChange` — is a toggle, and it outranks a bare
|
|
33
|
+
press handler, because a chip with an on state that is announced as a plain
|
|
34
|
+
button leaves a screen-reader user unable to tell which chips are on. A
|
|
35
|
+
toggle still calls `onPress` after flipping.
|
|
36
|
+
- **Selection is announced as `selected`, with the `button` role.** That is the
|
|
37
|
+
native idiom for a filter on both platforms — VoiceOver reads "Selected",
|
|
38
|
+
TalkBack "selected" — and it matches what a system segmented filter reports.
|
|
39
|
+
`checkbox` would promise a box that is not drawn. A caller's own
|
|
40
|
+
`accessibilityState` is merged under it, and `Pressable` merges `disabled` on
|
|
41
|
+
top.
|
|
42
|
+
- **Controlled or uncontrolled, through `useControllableState`**, the same hook
|
|
43
|
+
and the same prop shape as [`Checkbox`](../checkbox/AGENTS.md):
|
|
44
|
+
`isSelected` / `defaultSelected` / `onSelectedChange`. A toggle ticks a
|
|
45
|
+
`selection` haptic by default; a button plays none, as a badge does. Pass
|
|
46
|
+
`haptic` to override either.
|
|
47
|
+
- **Tone is Badge's, not a copy of it.** `CHIP_COLORS` and `CHIP_SIZES` *are*
|
|
48
|
+
`BADGE_COLORS` and `BADGE_SIZES`, and the resting entries of
|
|
49
|
+
`CHIP_FOREGROUND_TOKEN` are Badge's own objects. The class strings in
|
|
50
|
+
`chipVariants` are restated (Tailwind's scanner is static, so a class has to
|
|
51
|
+
appear literally somewhere), and a test pins every resting cell's `bg-*`,
|
|
52
|
+
`border-*` and label colour to the badge of the same variant and colour. A
|
|
53
|
+
soft `success` chip beside a soft `success` badge is one shade by
|
|
54
|
+
construction.
|
|
55
|
+
- **Two resting variants, `soft` and `outline`.** `solid` is what selection
|
|
56
|
+
paints, so a resting solid chip would read as already on; `ghost` has neither
|
|
57
|
+
fill nor border, so there is no edge to aim at. `soft` is the default.
|
|
58
|
+
- **Selection is a surface, not a modifier.** `CHIP_SURFACES` is the two resting
|
|
59
|
+
variants plus `selected`, and a selected chip looks the same whichever it rests
|
|
60
|
+
on — a solid fill and matching border in its colour. The `isSelected`
|
|
61
|
+
compound variants sit after the resting ones, so tailwind-merge lets them win.
|
|
62
|
+
- **A selected `default` chip inverts** to `bg-foreground` / `text-background`.
|
|
63
|
+
Badge's solid default is `secondary`, which this theme sets a hair from
|
|
64
|
+
`muted` — the soft default — so a selected default chip would look unselected.
|
|
65
|
+
A test asserts all eighteen surface-and-colour cells are distinct.
|
|
66
|
+
- **The border is reserved in every state.** A chip toggles in place inside a
|
|
67
|
+
wrapping row; a box that grew two points on selection would reflow every chip
|
|
68
|
+
after it. `overflow-hidden` keeps the pressed fade inside the capsule.
|
|
69
|
+
- **Roomier than a badge, and still never a height.** A chip is aimed at and a
|
|
70
|
+
badge is only read, so each size pads at least as generously as the badge of
|
|
71
|
+
that size — a test asserts it. There is no `h-*`, so the label grows with OS
|
|
72
|
+
font scaling. The icon step is Badge's, so a glyph moves between the two
|
|
73
|
+
without changing size.
|
|
74
|
+
- **Hit slop is vertical and capped at four points.** Chips wrap with `gap-2`;
|
|
75
|
+
slop past half that gap reaches into the neighbouring row and makes a tap
|
|
76
|
+
between two chips ambiguous. `CHIP_HIT_SLOP` is never horizontal for the same
|
|
77
|
+
reason. The close glyph gets `CHIP_CLOSE_HIT_SLOP` on every side instead — it
|
|
78
|
+
is 14–18 points, and its slop only reaches into its own chip's padding.
|
|
79
|
+
- **`onClose` is its own pressable, never a mode of the root.** The remove
|
|
80
|
+
control is a `Chip.CloseButton` composed in last, so its tap is claimed by the
|
|
81
|
+
inner detector and never also toggles the chip. It presses with `fade` —
|
|
82
|
+
a spring on a glyph that small reads as a jitter — and inherits the current
|
|
83
|
+
surface's colour, so it turns with the label on selection.
|
|
84
|
+
- **On a pressable chip, removal is an accessibility action, not a second
|
|
85
|
+
element.** iOS folds every descendant of an accessible view into one element,
|
|
86
|
+
so a close control inside a button or toggle chip was unreachable by swipe and
|
|
87
|
+
its label was read as part of the chip's — "React Native, Remove React
|
|
88
|
+
Native". `resolveChipCloseExposure` decides: a static chip leaves the close
|
|
89
|
+
control as its own element; a pressable one hides it
|
|
90
|
+
(`accessibilityElementsHidden`, `no-hide-descendants`) and adds a `remove`
|
|
91
|
+
action, labelled by `closeAccessibilityLabel`, that calls `onClose` —
|
|
92
|
+
VoiceOver's Actions rotor, TalkBack's actions menu. A caller's own
|
|
93
|
+
`accessibilityActions` are kept beside it.
|
|
94
|
+
- **`self-start` is Badge's, and beside a taller sibling in a row it top-aligns
|
|
95
|
+
the chip.** It is what keeps a chip content-sized in a column, which is the
|
|
96
|
+
common case; in a `flex-row items-center` holding something taller — a
|
|
97
|
+
`Switch` — pass `className="self-center"`, which tailwind-merge lets win. The
|
|
98
|
+
playground's `controlled-and-uncontrolled` demo does exactly this.
|
|
99
|
+
- **Icons are composed, never passed as props.** The root wraps its subtree in an
|
|
100
|
+
`IconDefaultsProvider` and a `TextClassProvider` resolved from the *current*
|
|
101
|
+
surface, so a bare `<Icon>` or `<Text>` follows the chip into and out of
|
|
102
|
+
selection. `useChip()` exposes `isSelected` for a custom child that needs more.
|
|
103
|
+
- **String children** are wrapped in a `Chip.Label` automatically, consecutive
|
|
104
|
+
strings collapsing into one — the same rule as [`Badge`](../badge/AGENTS.md).
|
|
105
|
+
- **No group, yet.** Single- and multi-select filter rows are composed from
|
|
106
|
+
controlled chips (the playground's `filter-row` demo). A `Chip.Group` owning
|
|
107
|
+
the value, as `Checkbox.Group` does, is the obvious next step once a second
|
|
108
|
+
caller needs it.
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import type { ReactElement } from "react";
|
|
2
|
+
import { IconCrossSmall } from "../../icons/central";
|
|
3
|
+
import { Icon } from "../icon";
|
|
4
|
+
import { Pressable, type PressableProps } from "../pressable";
|
|
5
|
+
import { useChipPart } from "./chip.context";
|
|
6
|
+
import { CHIP_CLOSE_HIT_SLOP, chipVariants } from "./chip.variants";
|
|
7
|
+
|
|
8
|
+
export type ChipCloseButtonProps = Omit<PressableProps, "asChild" | "busy" | "children" | "disabled">;
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* The chip's trailing remove control.
|
|
12
|
+
*
|
|
13
|
+
* A pressable of its own rather than a mode of the root, which is what keeps a
|
|
14
|
+
* removal from also toggling the chip or firing its `onPress`: the two gestures
|
|
15
|
+
* belong to two detectors, and the inner one claims the tap.
|
|
16
|
+
*
|
|
17
|
+
* The root composes one in whenever `onClose` is set, so reach for this by hand
|
|
18
|
+
* only to place it somewhere other than last.
|
|
19
|
+
*
|
|
20
|
+
* The glyph is left bare: it inherits the chip's icon size and its current
|
|
21
|
+
* surface's colour from the root's `IconDefaultsProvider`, so it turns with the
|
|
22
|
+
* label when the chip is selected. `CHIP_CLOSE_HIT_SLOP` grows a 14–18 point
|
|
23
|
+
* glyph into something a thumb can find.
|
|
24
|
+
*/
|
|
25
|
+
export function ChipCloseButton({
|
|
26
|
+
accessibilityLabel = "Remove",
|
|
27
|
+
className,
|
|
28
|
+
feedback = "fade",
|
|
29
|
+
hitSlop,
|
|
30
|
+
...props
|
|
31
|
+
}: ChipCloseButtonProps): ReactElement {
|
|
32
|
+
const { size, isDisabled } = useChipPart("Chip.CloseButton");
|
|
33
|
+
|
|
34
|
+
return (
|
|
35
|
+
<Pressable
|
|
36
|
+
accessibilityLabel={accessibilityLabel}
|
|
37
|
+
accessibilityRole="button"
|
|
38
|
+
className={chipVariants({ size }).closeButton({ className })}
|
|
39
|
+
disabled={isDisabled}
|
|
40
|
+
feedback={feedback}
|
|
41
|
+
hitSlop={hitSlop ?? CHIP_CLOSE_HIT_SLOP[size]}
|
|
42
|
+
{...props}
|
|
43
|
+
>
|
|
44
|
+
<Icon icon={IconCrossSmall} />
|
|
45
|
+
</Pressable>
|
|
46
|
+
);
|
|
47
|
+
}
|
|
48
|
+
ChipCloseButton.displayName = "DelacourUI.Chip.CloseButton";
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { ReactElement } from "react";
|
|
2
|
+
import { View } from "react-native";
|
|
3
|
+
import type { ChipSlotProps } from "./chip.types";
|
|
4
|
+
import { chipVariants } from "./chip.variants";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* A centred wrapper for trailing content that is not an `Icon` — a count.
|
|
8
|
+
*
|
|
9
|
+
* An `Icon` needs no wrapper: it inherits the chip's icon size and its
|
|
10
|
+
* surface's colour from the root's `IconDefaultsProvider`.
|
|
11
|
+
*/
|
|
12
|
+
export function ChipEndContent({ className, ...props }: ChipSlotProps): ReactElement {
|
|
13
|
+
return <View className={chipVariants().endContent({ className })} {...props} />;
|
|
14
|
+
}
|
|
15
|
+
ChipEndContent.displayName = "DelacourUI.Chip.EndContent";
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { ReactElement } from "react";
|
|
2
|
+
import type { TextProps } from "react-native";
|
|
3
|
+
import { Text } from "../text";
|
|
4
|
+
import { useChipPart } from "./chip.context";
|
|
5
|
+
import { chipVariants } from "./chip.variants";
|
|
6
|
+
|
|
7
|
+
export type ChipLabelProps = TextProps & { className?: string };
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* The chip's text.
|
|
11
|
+
*
|
|
12
|
+
* Reads the chip's variant, colour and selected state itself, so an
|
|
13
|
+
* icon-plus-text chip changes colour on selection without the caller threading
|
|
14
|
+
* anything through: a React Native `View` does not cascade colour to a `Text`
|
|
15
|
+
* descendant, so a colour set on the root would be lost.
|
|
16
|
+
*/
|
|
17
|
+
export function ChipLabel({ className, ...props }: ChipLabelProps): ReactElement {
|
|
18
|
+
const { variant, color, size, isSelected } = useChipPart("Chip.Label");
|
|
19
|
+
return <Text className={chipVariants({ color, isSelected, size, variant }).label({ className })} {...props} />;
|
|
20
|
+
}
|
|
21
|
+
ChipLabel.displayName = "DelacourUI.Chip.Label";
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { ReactElement } from "react";
|
|
2
|
+
import { View } from "react-native";
|
|
3
|
+
import type { ChipSlotProps } from "./chip.types";
|
|
4
|
+
import { chipVariants } from "./chip.variants";
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* A centred wrapper for leading content that is not an `Icon` — an avatar, a
|
|
8
|
+
* status dot.
|
|
9
|
+
*
|
|
10
|
+
* An `Icon` needs no wrapper: it inherits the chip's icon size and its
|
|
11
|
+
* surface's colour from the root's `IconDefaultsProvider`.
|
|
12
|
+
*/
|
|
13
|
+
export function ChipStartContent({ className, ...props }: ChipSlotProps): ReactElement {
|
|
14
|
+
return <View className={chipVariants().startContent({ className })} {...props} />;
|
|
15
|
+
}
|
|
16
|
+
ChipStartContent.displayName = "DelacourUI.Chip.StartContent";
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { createContext, type ReactElement, type ReactNode, use } from "react";
|
|
2
|
+
import type { ChipColor, ChipSize, ChipVariant } from "./chip.variants";
|
|
3
|
+
|
|
4
|
+
export type ChipContextValue = {
|
|
5
|
+
/** Size of the chip. */
|
|
6
|
+
size: ChipSize;
|
|
7
|
+
/** How the chip is painted while unselected. */
|
|
8
|
+
variant: ChipVariant;
|
|
9
|
+
/** What the chip means. */
|
|
10
|
+
color: ChipColor;
|
|
11
|
+
/** Whether the chip is selected. Always `false` for a chip that cannot be. */
|
|
12
|
+
isSelected: boolean;
|
|
13
|
+
/** Whether the chip is disabled. */
|
|
14
|
+
isDisabled: boolean;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
const ChipContext = createContext<ChipContextValue | null>(null);
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Supplies the enclosing chip's variant, colour, size and state to its subtree.
|
|
21
|
+
*
|
|
22
|
+
* Lives in its own module, importing nothing but `chip.variants`, so a part can
|
|
23
|
+
* read it without importing `./chip`. That import would close a cycle, and Metro
|
|
24
|
+
* serves a partially initialised module for a cycle — leaving the context
|
|
25
|
+
* `undefined` at import time and red-boxing the app on a cold start.
|
|
26
|
+
*/
|
|
27
|
+
export function ChipProvider({ value, children }: { value: ChipContextValue; children: ReactNode }): ReactElement {
|
|
28
|
+
return <ChipContext value={value}>{children}</ChipContext>;
|
|
29
|
+
}
|
|
30
|
+
ChipProvider.displayName = "DelacourUI.Chip.Provider";
|
|
31
|
+
|
|
32
|
+
/** The enclosing chip's context, or null outside a `<Chip>`. */
|
|
33
|
+
export function useChipContext(): ChipContextValue | null {
|
|
34
|
+
return use(ChipContext);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Reads the enclosing chip's variant, colour, size and state.
|
|
39
|
+
*
|
|
40
|
+
* Lets a custom child restyle itself when the chip is selected without the chip
|
|
41
|
+
* passing props down through every slot. Throws outside a `<Chip>` — use
|
|
42
|
+
* {@link useChipContext} where the enclosing chip is optional.
|
|
43
|
+
*/
|
|
44
|
+
export function useChip(): ChipContextValue {
|
|
45
|
+
const context = useChipContext();
|
|
46
|
+
if (!context) {
|
|
47
|
+
throw new Error("useChip must be called inside a <Chip>.");
|
|
48
|
+
}
|
|
49
|
+
return context;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* The enclosing chip's context, for a compound part that cannot work without
|
|
54
|
+
* one.
|
|
55
|
+
*
|
|
56
|
+
* Internal: deliberately not re-exported from `index.ts`. A caller outside the
|
|
57
|
+
* library wants {@link useChip}, whose error message names the hook rather than
|
|
58
|
+
* a part.
|
|
59
|
+
*/
|
|
60
|
+
export function useChipPart(component: string): ChipContextValue {
|
|
61
|
+
const context = useChipContext();
|
|
62
|
+
if (!context) {
|
|
63
|
+
throw new Error(`${component} must be rendered inside a <Chip>.`);
|
|
64
|
+
}
|
|
65
|
+
return context;
|
|
66
|
+
}
|
|
@@ -0,0 +1,264 @@
|
|
|
1
|
+
import { Children, type ReactElement, type ReactNode, useCallback, useMemo } from "react";
|
|
2
|
+
import { type AccessibilityActionEvent, View } from "react-native";
|
|
3
|
+
import { useControllableState } from "../../hooks/use-controllable-state";
|
|
4
|
+
import { IconDefaultsProvider } from "../icon";
|
|
5
|
+
import { Pressable, type PressableProps } from "../pressable";
|
|
6
|
+
import { TextClassProvider } from "../text/text.context";
|
|
7
|
+
import { type ChipContextValue, ChipProvider } from "./chip.context";
|
|
8
|
+
import {
|
|
9
|
+
CHIP_HIT_SLOP,
|
|
10
|
+
type ChipColor,
|
|
11
|
+
type ChipSize,
|
|
12
|
+
type ChipVariant,
|
|
13
|
+
chipVariants,
|
|
14
|
+
resolveChipCloseExposure,
|
|
15
|
+
resolveChipForegroundToken,
|
|
16
|
+
resolveChipMode,
|
|
17
|
+
} from "./chip.variants";
|
|
18
|
+
import { ChipCloseButton } from "./chip-close-button";
|
|
19
|
+
import { ChipEndContent } from "./chip-end-content";
|
|
20
|
+
import { ChipLabel } from "./chip-label";
|
|
21
|
+
import { ChipStartContent } from "./chip-start-content";
|
|
22
|
+
|
|
23
|
+
export type ChipProps = Omit<PressableProps, "asChild" | "busy" | "children" | "disabled"> & {
|
|
24
|
+
/** How the chip is painted while unselected. */
|
|
25
|
+
variant?: ChipVariant;
|
|
26
|
+
/** What the chip means. Also the fill it takes when selected. */
|
|
27
|
+
color?: ChipColor;
|
|
28
|
+
size?: ChipSize;
|
|
29
|
+
/**
|
|
30
|
+
* Controlled selected state. Setting it — even to `false` — makes the chip a
|
|
31
|
+
* toggle, announced with its state; a tap reports the flip through
|
|
32
|
+
* `onSelectedChange`.
|
|
33
|
+
*/
|
|
34
|
+
isSelected?: boolean;
|
|
35
|
+
/** Starting selected state while uncontrolled. Also makes the chip a toggle. */
|
|
36
|
+
defaultSelected?: boolean;
|
|
37
|
+
onSelectedChange?: (isSelected: boolean) => void;
|
|
38
|
+
isDisabled?: boolean;
|
|
39
|
+
/** Composes a trailing remove control in. Its press never toggles the chip or reaches `onPress`. */
|
|
40
|
+
onClose?: () => void;
|
|
41
|
+
/** Name a screen reader gives the remove control. Defaults to `Remove`. */
|
|
42
|
+
closeAccessibilityLabel?: string;
|
|
43
|
+
children?: ReactNode;
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
function ChipRoot({
|
|
47
|
+
variant = "soft",
|
|
48
|
+
color = "default",
|
|
49
|
+
size = "md",
|
|
50
|
+
isSelected,
|
|
51
|
+
defaultSelected,
|
|
52
|
+
onSelectedChange,
|
|
53
|
+
isDisabled = false,
|
|
54
|
+
onClose,
|
|
55
|
+
closeAccessibilityLabel,
|
|
56
|
+
onPress,
|
|
57
|
+
onLongPress,
|
|
58
|
+
haptic,
|
|
59
|
+
feedback = "scale",
|
|
60
|
+
pressedScale,
|
|
61
|
+
pressedOpacity,
|
|
62
|
+
accessibilityState,
|
|
63
|
+
accessibilityActions,
|
|
64
|
+
onAccessibilityAction,
|
|
65
|
+
className,
|
|
66
|
+
children,
|
|
67
|
+
...props
|
|
68
|
+
}: ChipProps): ReactElement {
|
|
69
|
+
const mode = resolveChipMode({ defaultSelected, isSelected, onLongPress, onPress, onSelectedChange });
|
|
70
|
+
const closeExposure = resolveChipCloseExposure({ hasClose: onClose !== undefined, mode });
|
|
71
|
+
|
|
72
|
+
const [ownSelected, setSelected] = useControllableState({
|
|
73
|
+
defaultValue: defaultSelected ?? false,
|
|
74
|
+
onChange: onSelectedChange,
|
|
75
|
+
value: isSelected,
|
|
76
|
+
});
|
|
77
|
+
const selected = mode === "toggle" && ownSelected;
|
|
78
|
+
|
|
79
|
+
const handlePress = useCallback(() => {
|
|
80
|
+
if (mode === "toggle") setSelected(!selected);
|
|
81
|
+
onPress?.();
|
|
82
|
+
}, [mode, onPress, selected, setSelected]);
|
|
83
|
+
|
|
84
|
+
const context = useMemo<ChipContextValue>(
|
|
85
|
+
() => ({ color, isDisabled, isSelected: selected, size, variant }),
|
|
86
|
+
[color, isDisabled, selected, size, variant]
|
|
87
|
+
);
|
|
88
|
+
|
|
89
|
+
const slots = chipVariants({ color, isDisabled, isSelected: selected, size, variant });
|
|
90
|
+
|
|
91
|
+
// Icons composed into the chip adopt these unless told otherwise, and turn
|
|
92
|
+
// with the label when the chip is selected.
|
|
93
|
+
const iconClassName = slots.icon();
|
|
94
|
+
const foregroundToken = resolveChipForegroundToken({ color, isSelected: selected, variant });
|
|
95
|
+
const iconDefaults = useMemo(
|
|
96
|
+
() => ({ className: iconClassName, color: foregroundToken }),
|
|
97
|
+
[iconClassName, foregroundToken]
|
|
98
|
+
);
|
|
99
|
+
|
|
100
|
+
const content = useMemo(() => wrapTextChildren(children), [children]);
|
|
101
|
+
const rootClassName = slots.root({ className });
|
|
102
|
+
|
|
103
|
+
// Inside a pressable chip the close control is folded into the chip's own
|
|
104
|
+
// accessibility element, so it is hidden there and offered as the `remove`
|
|
105
|
+
// action below instead — see `resolveChipCloseExposure`.
|
|
106
|
+
const closeHidden = closeExposure === "action";
|
|
107
|
+
const inner = (
|
|
108
|
+
<IconDefaultsProvider value={iconDefaults}>
|
|
109
|
+
<TextClassProvider value={slots.label()}>
|
|
110
|
+
{content}
|
|
111
|
+
{onClose ? (
|
|
112
|
+
<ChipCloseButton
|
|
113
|
+
accessibilityElementsHidden={closeHidden}
|
|
114
|
+
accessible={!closeHidden}
|
|
115
|
+
accessibilityLabel={closeAccessibilityLabel}
|
|
116
|
+
importantForAccessibility={closeHidden ? "no-hide-descendants" : "auto"}
|
|
117
|
+
onPress={onClose}
|
|
118
|
+
/>
|
|
119
|
+
) : null}
|
|
120
|
+
</TextClassProvider>
|
|
121
|
+
</IconDefaultsProvider>
|
|
122
|
+
);
|
|
123
|
+
|
|
124
|
+
const actions =
|
|
125
|
+
closeExposure === "action"
|
|
126
|
+
? [...(accessibilityActions ?? []), { label: closeAccessibilityLabel ?? "Remove", name: "remove" }]
|
|
127
|
+
: accessibilityActions;
|
|
128
|
+
|
|
129
|
+
const handleAccessibilityAction = (event: AccessibilityActionEvent) => {
|
|
130
|
+
if (closeExposure === "action" && event.nativeEvent.actionName === "remove" && !isDisabled) {
|
|
131
|
+
onClose?.();
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
onAccessibilityAction?.(event);
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
// A chip with nothing to do stays a plain box — see `resolveChipMode`.
|
|
138
|
+
if (mode === "static") {
|
|
139
|
+
return (
|
|
140
|
+
<ChipProvider value={context}>
|
|
141
|
+
<View
|
|
142
|
+
accessibilityActions={accessibilityActions}
|
|
143
|
+
className={rootClassName}
|
|
144
|
+
onAccessibilityAction={onAccessibilityAction}
|
|
145
|
+
{...props}
|
|
146
|
+
>
|
|
147
|
+
{inner}
|
|
148
|
+
</View>
|
|
149
|
+
</ChipProvider>
|
|
150
|
+
);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
// A toggle reports `selected`, which VoiceOver reads as "Selected" and
|
|
154
|
+
// TalkBack as "selected" — the native idiom for a filter — and a plain
|
|
155
|
+
// button reports nothing extra. Pressable merges `disabled` in on top.
|
|
156
|
+
const state = mode === "toggle" ? { ...accessibilityState, selected } : accessibilityState;
|
|
157
|
+
|
|
158
|
+
return (
|
|
159
|
+
<ChipProvider value={context}>
|
|
160
|
+
<Pressable
|
|
161
|
+
accessibilityActions={actions}
|
|
162
|
+
accessibilityRole="button"
|
|
163
|
+
accessibilityState={state}
|
|
164
|
+
className={rootClassName}
|
|
165
|
+
disabled={isDisabled}
|
|
166
|
+
feedback={feedback}
|
|
167
|
+
haptic={haptic ?? (mode === "toggle" ? "selection" : false)}
|
|
168
|
+
hitSlop={CHIP_HIT_SLOP[size]}
|
|
169
|
+
onAccessibilityAction={handleAccessibilityAction}
|
|
170
|
+
onLongPress={onLongPress}
|
|
171
|
+
onPress={handlePress}
|
|
172
|
+
pressedOpacity={pressedOpacity}
|
|
173
|
+
pressedScale={pressedScale}
|
|
174
|
+
{...props}
|
|
175
|
+
>
|
|
176
|
+
{inner}
|
|
177
|
+
</Pressable>
|
|
178
|
+
</ChipProvider>
|
|
179
|
+
);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* Wraps bare text children in a `Chip.Label`.
|
|
184
|
+
*
|
|
185
|
+
* React Native cannot render a string outside a `<Text>`, so `<Chip>Design</Chip>`
|
|
186
|
+
* would otherwise crash. Consecutive strings and numbers are collected into a
|
|
187
|
+
* single label rather than one each — `{count} open` is one piece of text, and
|
|
188
|
+
* wrapping the parts separately would space them apart by the chip's gap.
|
|
189
|
+
*/
|
|
190
|
+
function wrapTextChildren(children: ReactNode): ReactNode {
|
|
191
|
+
const items = Children.toArray(children);
|
|
192
|
+
const output: ReactNode[] = [];
|
|
193
|
+
let run: (string | number)[] = [];
|
|
194
|
+
|
|
195
|
+
const flushRun = () => {
|
|
196
|
+
if (run.length === 0) return;
|
|
197
|
+
output.push(<ChipLabel key={`label-${output.length}`}>{run.join("")}</ChipLabel>);
|
|
198
|
+
run = [];
|
|
199
|
+
};
|
|
200
|
+
|
|
201
|
+
for (const child of items) {
|
|
202
|
+
if (typeof child === "string" || typeof child === "number") {
|
|
203
|
+
run.push(child);
|
|
204
|
+
continue;
|
|
205
|
+
}
|
|
206
|
+
flushRun();
|
|
207
|
+
output.push(child);
|
|
208
|
+
}
|
|
209
|
+
flushRun();
|
|
210
|
+
|
|
211
|
+
return output;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* An interactive pill — a filter that is on or off, a tag, or a token that can
|
|
216
|
+
* be removed.
|
|
217
|
+
*
|
|
218
|
+
* What it does is decided by the props it is given, never by a `type` flag:
|
|
219
|
+
*
|
|
220
|
+
* - **Nothing** — a tag. A plain `View`, not announced as a control.
|
|
221
|
+
* - **`onPress` / `onLongPress`** — a button.
|
|
222
|
+
* - **`isSelected`, `defaultSelected` or `onSelectedChange`** — a filter. A tap
|
|
223
|
+
* flips the state, a `selection` haptic ticks, and a screen reader hears
|
|
224
|
+
* whether it is selected. Controlled or uncontrolled, like `Checkbox`.
|
|
225
|
+
* - **`onClose`** — adds a trailing remove control, a pressable of its own, so
|
|
226
|
+
* removing a chip never also toggles it. Combines with any of the above; on
|
|
227
|
+
* a pressable chip a screen reader reaches it as a `remove` action.
|
|
228
|
+
*
|
|
229
|
+
* Shares Badge's `color` and `size` axes and its tones: an unselected chip is
|
|
230
|
+
* painted exactly as the badge of the same `variant` and `color`. Selection
|
|
231
|
+
* replaces that with a solid fill in the chip's colour, and a selected
|
|
232
|
+
* `default` chip inverts. The border is reserved in every state, so selecting a
|
|
233
|
+
* chip never reflows the row it sits in.
|
|
234
|
+
*
|
|
235
|
+
* Icons are composed in, not passed as props. Anything in the subtree inherits
|
|
236
|
+
* the chip's icon size and its current surface's colour, and follows it when
|
|
237
|
+
* the chip is selected.
|
|
238
|
+
*
|
|
239
|
+
* @example
|
|
240
|
+
* <Chip color="success">Shipped</Chip>
|
|
241
|
+
*
|
|
242
|
+
* @example
|
|
243
|
+
* const [open, setOpen] = useState(false);
|
|
244
|
+
* <Chip isSelected={open} onSelectedChange={setOpen}>
|
|
245
|
+
* <Icon icon={IconFilter} />
|
|
246
|
+
* <Chip.Label>Open only</Chip.Label>
|
|
247
|
+
* </Chip>
|
|
248
|
+
*
|
|
249
|
+
* @example
|
|
250
|
+
* <Chip onClose={() => remove(tag)} variant="outline">
|
|
251
|
+
* {tag}
|
|
252
|
+
* </Chip>
|
|
253
|
+
*/
|
|
254
|
+
export const Chip = Object.assign(ChipRoot, {
|
|
255
|
+
/** The chip's text. Picks its colour and type scale from the chip's variant, colour, size and selection. */
|
|
256
|
+
Label: ChipLabel,
|
|
257
|
+
/** A centred wrapper for leading content that is not an `Icon` — an avatar, a status dot. */
|
|
258
|
+
StartContent: ChipStartContent,
|
|
259
|
+
/** A centred wrapper for trailing content that is not an `Icon` — a count. */
|
|
260
|
+
EndContent: ChipEndContent,
|
|
261
|
+
/** The trailing remove control. Composed in automatically whenever `onClose` is set. */
|
|
262
|
+
CloseButton: ChipCloseButton,
|
|
263
|
+
displayName: "DelacourUI.Chip",
|
|
264
|
+
});
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { ViewProps } from "react-native";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The shape of a chip's leading and trailing slots.
|
|
5
|
+
*
|
|
6
|
+
* Shared by `Chip.StartContent` and `Chip.EndContent`, which are identical apart
|
|
7
|
+
* from where the root places them, so it lives in a leaf rather than in one of
|
|
8
|
+
* the two files arbitrarily.
|
|
9
|
+
*/
|
|
10
|
+
export type ChipSlotProps = ViewProps & { className?: string };
|
|
@@ -0,0 +1,335 @@
|
|
|
1
|
+
import { describe, expect, test } from "bun:test";
|
|
2
|
+
import { declaredTokens } from "../../styles/theme-tokens.test";
|
|
3
|
+
import { ICON_SIZE_TOKENS } from "../../styles/tokens";
|
|
4
|
+
import { BADGE_COLORS, BADGE_FOREGROUND_TOKEN, BADGE_SIZES, badgeVariants } from "../badge/badge.variants";
|
|
5
|
+
import {
|
|
6
|
+
CHIP_CLOSE_HIT_SLOP,
|
|
7
|
+
CHIP_COLORS,
|
|
8
|
+
CHIP_FOREGROUND_TOKEN,
|
|
9
|
+
CHIP_HIT_SLOP,
|
|
10
|
+
CHIP_SIZES,
|
|
11
|
+
CHIP_SURFACES,
|
|
12
|
+
CHIP_VARIANTS,
|
|
13
|
+
chipVariants,
|
|
14
|
+
resolveChipCloseExposure,
|
|
15
|
+
resolveChipForegroundToken,
|
|
16
|
+
resolveChipMode,
|
|
17
|
+
resolveChipSurface,
|
|
18
|
+
} from "./chip.variants";
|
|
19
|
+
|
|
20
|
+
const LIGHT = declaredTokens("light");
|
|
21
|
+
const DARK = declaredTokens("dark");
|
|
22
|
+
|
|
23
|
+
/** Tailwind's spacing step a class string sets its horizontal padding from. */
|
|
24
|
+
function paddingX(cls: string): number {
|
|
25
|
+
return Number(cls.match(/\bpx-([\d.]+)\b/)?.[1]);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** Tailwind's spacing step a class string sets its vertical padding from. */
|
|
29
|
+
function paddingY(cls: string): number {
|
|
30
|
+
return Number(cls.match(/\bpy-([\d.]+)\b/)?.[1]);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** Tailwind's spacing step a class string sets its gap from. */
|
|
34
|
+
function gapOf(cls: string): number {
|
|
35
|
+
return Number(cls.match(/\bgap-([\d.]+)\b/)?.[1]);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** Position of a label's font size on the type scale, largest last. */
|
|
39
|
+
const TEXT_SCALE = ["text-xs", "text-sm", "text-base", "text-lg"] as const;
|
|
40
|
+
function textStep(cls: string): number {
|
|
41
|
+
return TEXT_SCALE.findIndex((step) => new RegExp(`\\b${step}\\b`).test(cls));
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** Position of a slot's `size-icon-*` token on the shared icon scale. */
|
|
45
|
+
function iconStep(cls: string): number {
|
|
46
|
+
const token = cls.match(/\bsize-(icon-[\w-]+)\b/)?.[1];
|
|
47
|
+
return ICON_SIZE_TOKENS.indexOf(token as (typeof ICON_SIZE_TOKENS)[number]);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** The `text-*` colour token a label class string resolves to, minus the prefix. */
|
|
51
|
+
function labelColorToken(cls: string): string | undefined {
|
|
52
|
+
return cls.match(/\btext-((?:[a-z]+-)*(?:foreground|background))\b/)?.[1];
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** The `bg-*` and `border-*` colour utilities of a root class string, sorted. */
|
|
56
|
+
function surfaceClasses(cls: string): string[] {
|
|
57
|
+
return cls
|
|
58
|
+
.split(/\s+/)
|
|
59
|
+
.filter((c) => /^bg-/.test(c) || (/^border-/.test(c) && c !== "border"))
|
|
60
|
+
.sort();
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
describe("the chip's axes", () => {
|
|
64
|
+
// A chip and a badge sitting side by side in one row mean the same thing by
|
|
65
|
+
// the same colour. A private colour list is how the two drift apart.
|
|
66
|
+
test("share Badge's colours and sizes", () => {
|
|
67
|
+
expect([...CHIP_COLORS]).toEqual([...BADGE_COLORS]);
|
|
68
|
+
expect([...CHIP_SIZES]).toEqual([...BADGE_SIZES]);
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
// `solid` is what selection paints, so a resting solid chip would be
|
|
72
|
+
// indistinguishable from a selected one.
|
|
73
|
+
test("rest on soft or outline only — solid is reserved for selected", () => {
|
|
74
|
+
expect([...CHIP_VARIANTS]).toEqual(["soft", "outline"]);
|
|
75
|
+
});
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
describe("chipVariants root slot", () => {
|
|
79
|
+
test("defaults to the soft default md chip", () => {
|
|
80
|
+
const cls = chipVariants().root();
|
|
81
|
+
expect(cls).toContain("bg-muted");
|
|
82
|
+
expect(cls).toContain("px-3");
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
test("never stretches and is a capsule, at every size", () => {
|
|
86
|
+
for (const size of CHIP_SIZES) {
|
|
87
|
+
const cls = chipVariants({ size }).root();
|
|
88
|
+
expect(cls).toContain("self-start");
|
|
89
|
+
expect(cls).toContain("rounded-full");
|
|
90
|
+
}
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
// Selecting a chip must not make it wider — in a wrapping row that would
|
|
94
|
+
// reflow every chip after it.
|
|
95
|
+
test("reserves the border in every state, so selection never resizes the box", () => {
|
|
96
|
+
for (const variant of CHIP_VARIANTS) {
|
|
97
|
+
for (const isSelected of [false, true]) {
|
|
98
|
+
expect(chipVariants({ variant, isSelected }).root()).toMatch(/\bborder\b/);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
test("clips, so a pressed chip fades to the edge of its own box", () => {
|
|
104
|
+
expect(chipVariants().root()).toContain("overflow-hidden");
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
// Tone is shared with Badge: a resting chip is painted exactly as the
|
|
108
|
+
// badge of the same variant and colour is.
|
|
109
|
+
test("paints a resting chip as the matching badge", () => {
|
|
110
|
+
for (const variant of CHIP_VARIANTS) {
|
|
111
|
+
for (const color of CHIP_COLORS) {
|
|
112
|
+
const chip = chipVariants({ variant, color });
|
|
113
|
+
const badge = badgeVariants({ variant, color });
|
|
114
|
+
expect(surfaceClasses(chip.root())).toEqual(surfaceClasses(badge.root()));
|
|
115
|
+
expect(labelColorToken(chip.label())).toBe(labelColorToken(badge.label()));
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
test("paints a selected chip as a solid fill in its colour", () => {
|
|
121
|
+
for (const color of ["primary", "success", "warning", "destructive", "info"] as const) {
|
|
122
|
+
for (const variant of CHIP_VARIANTS) {
|
|
123
|
+
const slots = chipVariants({ variant, color, isSelected: true });
|
|
124
|
+
expect(slots.root()).toContain(`bg-${color}`);
|
|
125
|
+
expect(slots.root()).toContain(`border-${color}`);
|
|
126
|
+
expect(slots.label()).toContain(`text-${color}-foreground`);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
// Badge's solid default is `secondary`, which this theme sets a hair from
|
|
132
|
+
// `muted` — a selected default chip would look unselected. It inverts
|
|
133
|
+
// instead.
|
|
134
|
+
test("inverts a selected default chip rather than tinting it", () => {
|
|
135
|
+
const slots = chipVariants({ color: "default", isSelected: true });
|
|
136
|
+
expect(slots.root()).toContain("bg-foreground");
|
|
137
|
+
expect(slots.label()).toContain("text-background");
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
test("reads the same when selected, whatever it rested on", () => {
|
|
141
|
+
for (const color of CHIP_COLORS) {
|
|
142
|
+
const soft = chipVariants({ variant: "soft", color, isSelected: true });
|
|
143
|
+
const outline = chipVariants({ variant: "outline", color, isSelected: true });
|
|
144
|
+
expect(surfaceClasses(soft.root())).toEqual(surfaceClasses(outline.root()));
|
|
145
|
+
expect(soft.label()).toBe(outline.label());
|
|
146
|
+
}
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
test("gives every surface and colour pair a distinct treatment", () => {
|
|
150
|
+
const seen = new Set<string>();
|
|
151
|
+
for (const surface of CHIP_SURFACES) {
|
|
152
|
+
for (const color of CHIP_COLORS) {
|
|
153
|
+
const slots =
|
|
154
|
+
surface === "selected"
|
|
155
|
+
? chipVariants({ color, isSelected: true })
|
|
156
|
+
: chipVariants({ color, isSelected: false, variant: surface });
|
|
157
|
+
seen.add(`${surfaceClasses(slots.root()).join(" ")}|${labelColorToken(slots.label())}`);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
expect(seen.size).toBe(CHIP_SURFACES.length * CHIP_COLORS.length);
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
test("adds the disabled treatment only when disabled", () => {
|
|
164
|
+
expect(chipVariants({ isDisabled: true }).root()).toContain("opacity-50");
|
|
165
|
+
expect(chipVariants({ isDisabled: false }).root()).not.toContain("opacity-50");
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
test("merges an incoming className last", () => {
|
|
169
|
+
expect(chipVariants().root({ className: "bg-info" })).toContain("bg-info");
|
|
170
|
+
expect(chipVariants().root({ className: "bg-info" })).not.toContain("bg-muted");
|
|
171
|
+
});
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
describe("chipVariants size axis", () => {
|
|
175
|
+
test("scales horizontal padding, vertical padding and gap together", () => {
|
|
176
|
+
const roots = CHIP_SIZES.map((size) => chipVariants({ size }).root());
|
|
177
|
+
for (const scale of [roots.map(paddingX), roots.map(paddingY), roots.map(gapOf)]) {
|
|
178
|
+
expect(scale.every(Number.isFinite)).toBe(true);
|
|
179
|
+
expect(new Set(scale).size).toBe(CHIP_SIZES.length);
|
|
180
|
+
expect([...scale]).toEqual([...scale].sort((a, b) => a - b));
|
|
181
|
+
}
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
// A chip is something to aim at, so it is roomier than the badge of the
|
|
185
|
+
// same size — never smaller.
|
|
186
|
+
test("pads at least as generously as the badge of the same size", () => {
|
|
187
|
+
for (const size of CHIP_SIZES) {
|
|
188
|
+
const chip = chipVariants({ size }).root();
|
|
189
|
+
const badge = badgeVariants({ size }).root();
|
|
190
|
+
expect(paddingX(chip)).toBeGreaterThanOrEqual(paddingX(badge));
|
|
191
|
+
expect(paddingY(chip)).toBeGreaterThanOrEqual(paddingY(badge));
|
|
192
|
+
}
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
test("takes no height, so the label grows with OS font scaling", () => {
|
|
196
|
+
for (const size of CHIP_SIZES) {
|
|
197
|
+
expect(chipVariants({ size }).root()).not.toMatch(/\bh-/);
|
|
198
|
+
}
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
test("scales label text with size, at medium weight", () => {
|
|
202
|
+
const labels = CHIP_SIZES.map((size) => chipVariants({ size }).label());
|
|
203
|
+
const steps = labels.map(textStep);
|
|
204
|
+
expect(steps.every((step) => step >= 0)).toBe(true);
|
|
205
|
+
expect([...steps]).toEqual([...steps].sort((a, b) => a - b));
|
|
206
|
+
for (const label of labels) expect(label).toContain("font-medium");
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
test("matches Badge's icon step at every size", () => {
|
|
210
|
+
for (const size of CHIP_SIZES) {
|
|
211
|
+
const step = iconStep(chipVariants({ size }).icon());
|
|
212
|
+
expect(step).toBeGreaterThanOrEqual(0);
|
|
213
|
+
expect(step).toBe(iconStep(badgeVariants({ size }).icon()));
|
|
214
|
+
}
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
test("the label slot carries the text colour, which the root must not", () => {
|
|
218
|
+
for (const variant of CHIP_VARIANTS) {
|
|
219
|
+
for (const color of CHIP_COLORS) {
|
|
220
|
+
for (const isSelected of [false, true]) {
|
|
221
|
+
const slots = chipVariants({ variant, color, isSelected });
|
|
222
|
+
expect(labelColorToken(slots.label())).toBeDefined();
|
|
223
|
+
expect(slots.root()).not.toMatch(/\btext-(?!center\b)/);
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
});
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
describe("resolveChipSurface", () => {
|
|
231
|
+
test("selection wins over the resting variant", () => {
|
|
232
|
+
expect(resolveChipSurface({ variant: "soft", isSelected: true })).toBe("selected");
|
|
233
|
+
expect(resolveChipSurface({ variant: "outline", isSelected: true })).toBe("selected");
|
|
234
|
+
});
|
|
235
|
+
|
|
236
|
+
test("an unselected chip rests on its variant", () => {
|
|
237
|
+
expect(resolveChipSurface({ variant: "soft", isSelected: false })).toBe("soft");
|
|
238
|
+
expect(resolveChipSurface({ variant: "outline", isSelected: false })).toBe("outline");
|
|
239
|
+
});
|
|
240
|
+
});
|
|
241
|
+
|
|
242
|
+
describe("CHIP_FOREGROUND_TOKEN", () => {
|
|
243
|
+
test("reuses Badge's tokens for the resting surfaces", () => {
|
|
244
|
+
expect(CHIP_FOREGROUND_TOKEN.soft).toEqual(BADGE_FOREGROUND_TOKEN.soft);
|
|
245
|
+
expect(CHIP_FOREGROUND_TOKEN.outline).toEqual(BADGE_FOREGROUND_TOKEN.outline);
|
|
246
|
+
});
|
|
247
|
+
|
|
248
|
+
// A composed Icon sits beside the label; two maps that can drift is how a
|
|
249
|
+
// chip ends up with a grey glyph beside white text.
|
|
250
|
+
test("matches the colour its own label slot resolves to, in every state", () => {
|
|
251
|
+
for (const variant of CHIP_VARIANTS) {
|
|
252
|
+
for (const color of CHIP_COLORS) {
|
|
253
|
+
for (const isSelected of [false, true]) {
|
|
254
|
+
const label = chipVariants({ variant, color, isSelected }).label();
|
|
255
|
+
expect(labelColorToken(label)).toBe(resolveChipForegroundToken({ variant, color, isSelected }));
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
});
|
|
260
|
+
|
|
261
|
+
test("names only tokens both themes declare", () => {
|
|
262
|
+
for (const surface of CHIP_SURFACES) {
|
|
263
|
+
for (const color of CHIP_COLORS) {
|
|
264
|
+
const token = CHIP_FOREGROUND_TOKEN[surface][color];
|
|
265
|
+
expect(LIGHT.has(token)).toBe(true);
|
|
266
|
+
expect(DARK.has(token)).toBe(true);
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
});
|
|
270
|
+
});
|
|
271
|
+
|
|
272
|
+
describe("resolveChipMode", () => {
|
|
273
|
+
const noop = () => {};
|
|
274
|
+
|
|
275
|
+
test("a chip with nothing to do stays a plain view", () => {
|
|
276
|
+
expect(resolveChipMode({})).toBe("static");
|
|
277
|
+
});
|
|
278
|
+
|
|
279
|
+
test("a press handler makes it a button", () => {
|
|
280
|
+
expect(resolveChipMode({ onPress: noop })).toBe("button");
|
|
281
|
+
expect(resolveChipMode({ onLongPress: noop })).toBe("button");
|
|
282
|
+
});
|
|
283
|
+
|
|
284
|
+
// Setting `isSelected` — even to false — says the chip has an on state, and
|
|
285
|
+
// a screen reader has to hear which one it is in.
|
|
286
|
+
test("any selection prop makes it a toggle, whatever else is set", () => {
|
|
287
|
+
expect(resolveChipMode({ isSelected: false })).toBe("toggle");
|
|
288
|
+
expect(resolveChipMode({ isSelected: true })).toBe("toggle");
|
|
289
|
+
expect(resolveChipMode({ defaultSelected: false })).toBe("toggle");
|
|
290
|
+
expect(resolveChipMode({ onSelectedChange: noop })).toBe("toggle");
|
|
291
|
+
expect(resolveChipMode({ isSelected: false, onPress: noop })).toBe("toggle");
|
|
292
|
+
});
|
|
293
|
+
});
|
|
294
|
+
|
|
295
|
+
describe("hit slop", () => {
|
|
296
|
+
// Chips wrap in rows eight points apart (`gap-2`). More than four points
|
|
297
|
+
// of vertical slop reaches into the next row and makes a tap ambiguous.
|
|
298
|
+
test("the root never reaches past half a gap-2 row gap", () => {
|
|
299
|
+
for (const size of CHIP_SIZES) {
|
|
300
|
+
const slop = CHIP_HIT_SLOP[size];
|
|
301
|
+
expect(slop.top).toBeLessThanOrEqual(4);
|
|
302
|
+
expect(slop.bottom).toBeLessThanOrEqual(4);
|
|
303
|
+
expect(slop.left).toBe(0);
|
|
304
|
+
expect(slop.right).toBe(0);
|
|
305
|
+
}
|
|
306
|
+
});
|
|
307
|
+
|
|
308
|
+
test("the close glyph grows its target with the chip", () => {
|
|
309
|
+
const slops = CHIP_SIZES.map((size) => CHIP_CLOSE_HIT_SLOP[size]);
|
|
310
|
+
expect(slops.every((slop) => slop > 0)).toBe(true);
|
|
311
|
+
expect([...slops]).toEqual([...slops].sort((a, b) => a - b));
|
|
312
|
+
});
|
|
313
|
+
});
|
|
314
|
+
|
|
315
|
+
describe("resolveChipCloseExposure", () => {
|
|
316
|
+
test("nothing to expose without onClose", () => {
|
|
317
|
+
for (const mode of ["static", "button", "toggle"] as const) {
|
|
318
|
+
expect(resolveChipCloseExposure({ mode, hasClose: false })).toBe("none");
|
|
319
|
+
}
|
|
320
|
+
});
|
|
321
|
+
|
|
322
|
+
// A static chip is not an accessibility element, so the close control is
|
|
323
|
+
// reachable on its own and needs nothing more.
|
|
324
|
+
test("a static chip leaves the close control as its own element", () => {
|
|
325
|
+
expect(resolveChipCloseExposure({ mode: "static", hasClose: true })).toBe("element");
|
|
326
|
+
});
|
|
327
|
+
|
|
328
|
+
// iOS folds every descendant of an accessible view into one element, so a
|
|
329
|
+
// close control inside a pressable chip is unreachable by swipe — it becomes
|
|
330
|
+
// an action on the chip instead.
|
|
331
|
+
test("a pressable chip offers removal as an accessibility action", () => {
|
|
332
|
+
expect(resolveChipCloseExposure({ mode: "button", hasClose: true })).toBe("action");
|
|
333
|
+
expect(resolveChipCloseExposure({ mode: "toggle", hasClose: true })).toBe("action");
|
|
334
|
+
});
|
|
335
|
+
});
|
|
@@ -0,0 +1,321 @@
|
|
|
1
|
+
import type { VariantProps } from "tailwind-variants";
|
|
2
|
+
import { tv } from "../../lib/tv";
|
|
3
|
+
import {
|
|
4
|
+
BADGE_COLORS,
|
|
5
|
+
BADGE_FOREGROUND_TOKEN,
|
|
6
|
+
BADGE_SIZES,
|
|
7
|
+
type BadgeColor,
|
|
8
|
+
type BadgeSize,
|
|
9
|
+
} from "../badge/badge.variants";
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* How an unselected chip is painted.
|
|
13
|
+
*
|
|
14
|
+
* Two of Badge's four, and deliberately not `solid`: a solid fill is what
|
|
15
|
+
* selection paints, so a resting solid chip would read as already selected.
|
|
16
|
+
* `ghost` is left out because a chip with no fill and no border has no edge to
|
|
17
|
+
* aim at.
|
|
18
|
+
*/
|
|
19
|
+
export const CHIP_VARIANTS = ["soft", "outline"] as const;
|
|
20
|
+
|
|
21
|
+
/** What the chip means. Badge's list, so a chip and a badge agree on colour. */
|
|
22
|
+
export const CHIP_COLORS = BADGE_COLORS;
|
|
23
|
+
|
|
24
|
+
/** Badge's sizes, so a chip and a badge of one size carry the same glyph. */
|
|
25
|
+
export const CHIP_SIZES = BADGE_SIZES;
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Every surface a chip can wear: its two resting variants, and selection.
|
|
29
|
+
*
|
|
30
|
+
* Selection is a surface rather than a modifier because it replaces the resting
|
|
31
|
+
* paint wholesale — a selected chip looks the same whichever variant it rests on.
|
|
32
|
+
*/
|
|
33
|
+
export const CHIP_SURFACES = [...CHIP_VARIANTS, "selected"] as const;
|
|
34
|
+
|
|
35
|
+
export type ChipVariant = (typeof CHIP_VARIANTS)[number];
|
|
36
|
+
export type ChipColor = BadgeColor;
|
|
37
|
+
export type ChipSize = BadgeSize;
|
|
38
|
+
export type ChipSurface = (typeof CHIP_SURFACES)[number];
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* What a chip does when touched — decided by which props it was given.
|
|
42
|
+
*
|
|
43
|
+
* - `static` — a tag. A plain `View`, announced as text.
|
|
44
|
+
* - `button` — `onPress` or `onLongPress`, and no selection.
|
|
45
|
+
* - `toggle` — any selection prop. Announced with its selected state.
|
|
46
|
+
*/
|
|
47
|
+
export type ChipMode = "static" | "button" | "toggle";
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Theme token whose value gives an `Icon` composed into the chip its colour.
|
|
51
|
+
*
|
|
52
|
+
* The resting surfaces are Badge's own entries, not copies — a soft `success`
|
|
53
|
+
* chip and a soft `success` badge are the same shade by construction. Only
|
|
54
|
+
* `selected` is new.
|
|
55
|
+
*
|
|
56
|
+
* A selected `default` chip inverts to `foreground`/`background` rather than
|
|
57
|
+
* taking Badge's solid `secondary`: this theme sets `secondary` a hair from
|
|
58
|
+
* `muted`, so a selected default chip would look exactly like a resting one.
|
|
59
|
+
*/
|
|
60
|
+
export const CHIP_FOREGROUND_TOKEN: Record<ChipSurface, Record<ChipColor, string>> = {
|
|
61
|
+
soft: BADGE_FOREGROUND_TOKEN.soft,
|
|
62
|
+
outline: BADGE_FOREGROUND_TOKEN.outline,
|
|
63
|
+
selected: {
|
|
64
|
+
default: "background",
|
|
65
|
+
primary: "primary-foreground",
|
|
66
|
+
success: "success-foreground",
|
|
67
|
+
warning: "warning-foreground",
|
|
68
|
+
destructive: "destructive-foreground",
|
|
69
|
+
info: "info-foreground",
|
|
70
|
+
},
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Vertical slop that brings the root towards the 44pt minimum touch target.
|
|
75
|
+
*
|
|
76
|
+
* Capped at four points and never horizontal: chips wrap in rows with a
|
|
77
|
+
* `gap-2` between them, and slop past half that gap reaches into the
|
|
78
|
+
* neighbouring chip, making a tap between two of them ambiguous — worse than a
|
|
79
|
+
* target that is merely adequate.
|
|
80
|
+
*/
|
|
81
|
+
export const CHIP_HIT_SLOP: Record<ChipSize, { top: number; bottom: number; left: number; right: number }> = {
|
|
82
|
+
sm: { top: 4, bottom: 4, left: 0, right: 0 },
|
|
83
|
+
md: { top: 4, bottom: 4, left: 0, right: 0 },
|
|
84
|
+
lg: { top: 2, bottom: 2, left: 0, right: 0 },
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Slop around the close glyph, on every side.
|
|
89
|
+
*
|
|
90
|
+
* The glyph is 14–18 points; this is what makes it something a thumb can find.
|
|
91
|
+
* It reaches into the chip's own padding and past its edge — both are the
|
|
92
|
+
* chip's space, and the inner detector claims the tap before the root sees it.
|
|
93
|
+
*/
|
|
94
|
+
export const CHIP_CLOSE_HIT_SLOP: Record<ChipSize, number> = { sm: 8, md: 10, lg: 12 };
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Styling for every part of a chip.
|
|
98
|
+
*
|
|
99
|
+
* Shares Badge's two axes and its tones — a resting chip is painted exactly as
|
|
100
|
+
* the badge of the same variant and colour, a test pins every cell — and adds a
|
|
101
|
+
* third, `isSelected`, which replaces the resting paint with a solid fill in the
|
|
102
|
+
* chip's colour. The selected cells come **after** the resting ones, so
|
|
103
|
+
* tailwind-merge lets them win whatever the chip rests on.
|
|
104
|
+
*
|
|
105
|
+
* `border border-transparent` sits in the base for the reason it does in Badge,
|
|
106
|
+
* with more at stake: a chip toggles in place inside a wrapping row, and a box
|
|
107
|
+
* that grew two points on selection would reflow every chip after it.
|
|
108
|
+
*
|
|
109
|
+
* Padding is a step roomier than Badge at every size — a chip is aimed at, a
|
|
110
|
+
* badge is only read — and still never a height, so the label grows with OS
|
|
111
|
+
* font scaling. The icon step is Badge's, so a glyph moves between the two
|
|
112
|
+
* without changing size.
|
|
113
|
+
*
|
|
114
|
+
* Free of React Native imports so it stays unit-testable. See AGENTS.md.
|
|
115
|
+
*/
|
|
116
|
+
export const chipVariants = tv({
|
|
117
|
+
slots: {
|
|
118
|
+
root: "flex-row items-center justify-center self-start overflow-hidden rounded-full border border-transparent",
|
|
119
|
+
label: "text-center font-medium",
|
|
120
|
+
startContent: "items-center justify-center",
|
|
121
|
+
endContent: "items-center justify-center",
|
|
122
|
+
closeButton: "items-center justify-center rounded-full",
|
|
123
|
+
/** Edge length an `Icon` composed into the chip inherits. */
|
|
124
|
+
icon: "",
|
|
125
|
+
},
|
|
126
|
+
variants: {
|
|
127
|
+
variant: {
|
|
128
|
+
soft: {},
|
|
129
|
+
outline: { root: "bg-transparent" },
|
|
130
|
+
},
|
|
131
|
+
color: {
|
|
132
|
+
default: {},
|
|
133
|
+
primary: {},
|
|
134
|
+
success: {},
|
|
135
|
+
warning: {},
|
|
136
|
+
destructive: {},
|
|
137
|
+
info: {},
|
|
138
|
+
},
|
|
139
|
+
size: {
|
|
140
|
+
sm: {
|
|
141
|
+
root: "gap-1 px-2.5 py-1",
|
|
142
|
+
label: "text-xs",
|
|
143
|
+
closeButton: "-mr-0.5",
|
|
144
|
+
icon: "size-icon-xs",
|
|
145
|
+
},
|
|
146
|
+
md: {
|
|
147
|
+
root: "gap-1.5 px-3 py-1.5",
|
|
148
|
+
label: "text-sm",
|
|
149
|
+
closeButton: "-mr-1",
|
|
150
|
+
icon: "size-icon-sm",
|
|
151
|
+
},
|
|
152
|
+
lg: {
|
|
153
|
+
root: "gap-2 px-4 py-2",
|
|
154
|
+
label: "text-base",
|
|
155
|
+
closeButton: "-mr-1.5",
|
|
156
|
+
icon: "size-icon-md",
|
|
157
|
+
},
|
|
158
|
+
},
|
|
159
|
+
// The empty `false` branches are load-bearing typing, not placeholders.
|
|
160
|
+
// See the note in button.variants.ts.
|
|
161
|
+
isSelected: { true: {}, false: {} },
|
|
162
|
+
isDisabled: { true: { root: "opacity-50" }, false: {} },
|
|
163
|
+
},
|
|
164
|
+
compoundVariants: [
|
|
165
|
+
{ variant: "soft", color: "default", class: { root: "bg-muted", label: "text-muted-foreground" } },
|
|
166
|
+
{ variant: "soft", color: "primary", class: { root: "bg-tertiary", label: "text-tertiary-foreground" } },
|
|
167
|
+
{
|
|
168
|
+
variant: "soft",
|
|
169
|
+
color: "success",
|
|
170
|
+
class: { root: "bg-success-soft", label: "text-success-soft-foreground" },
|
|
171
|
+
},
|
|
172
|
+
{
|
|
173
|
+
variant: "soft",
|
|
174
|
+
color: "warning",
|
|
175
|
+
class: { root: "bg-warning-soft", label: "text-warning-soft-foreground" },
|
|
176
|
+
},
|
|
177
|
+
{
|
|
178
|
+
variant: "soft",
|
|
179
|
+
color: "destructive",
|
|
180
|
+
class: { root: "bg-destructive-soft", label: "text-destructive-soft-foreground" },
|
|
181
|
+
},
|
|
182
|
+
{ variant: "soft", color: "info", class: { root: "bg-info-soft", label: "text-info-soft-foreground" } },
|
|
183
|
+
|
|
184
|
+
{ variant: "outline", color: "default", class: { root: "border-border", label: "text-muted-foreground" } },
|
|
185
|
+
{ variant: "outline", color: "primary", class: { root: "border-primary", label: "text-foreground" } },
|
|
186
|
+
{
|
|
187
|
+
variant: "outline",
|
|
188
|
+
color: "success",
|
|
189
|
+
class: { root: "border-success", label: "text-success-soft-foreground" },
|
|
190
|
+
},
|
|
191
|
+
{
|
|
192
|
+
variant: "outline",
|
|
193
|
+
color: "warning",
|
|
194
|
+
class: { root: "border-warning", label: "text-warning-soft-foreground" },
|
|
195
|
+
},
|
|
196
|
+
{
|
|
197
|
+
variant: "outline",
|
|
198
|
+
color: "destructive",
|
|
199
|
+
class: { root: "border-destructive", label: "text-destructive-soft-foreground" },
|
|
200
|
+
},
|
|
201
|
+
{ variant: "outline", color: "info", class: { root: "border-info", label: "text-info-soft-foreground" } },
|
|
202
|
+
|
|
203
|
+
{
|
|
204
|
+
isSelected: true,
|
|
205
|
+
color: "default",
|
|
206
|
+
class: { root: "border-foreground bg-foreground", label: "text-background" },
|
|
207
|
+
},
|
|
208
|
+
{
|
|
209
|
+
isSelected: true,
|
|
210
|
+
color: "primary",
|
|
211
|
+
class: { root: "border-primary bg-primary", label: "text-primary-foreground" },
|
|
212
|
+
},
|
|
213
|
+
{
|
|
214
|
+
isSelected: true,
|
|
215
|
+
color: "success",
|
|
216
|
+
class: { root: "border-success bg-success", label: "text-success-foreground" },
|
|
217
|
+
},
|
|
218
|
+
{
|
|
219
|
+
isSelected: true,
|
|
220
|
+
color: "warning",
|
|
221
|
+
class: { root: "border-warning bg-warning", label: "text-warning-foreground" },
|
|
222
|
+
},
|
|
223
|
+
{
|
|
224
|
+
isSelected: true,
|
|
225
|
+
color: "destructive",
|
|
226
|
+
class: { root: "border-destructive bg-destructive", label: "text-destructive-foreground" },
|
|
227
|
+
},
|
|
228
|
+
{
|
|
229
|
+
isSelected: true,
|
|
230
|
+
color: "info",
|
|
231
|
+
class: { root: "border-info bg-info", label: "text-info-foreground" },
|
|
232
|
+
},
|
|
233
|
+
],
|
|
234
|
+
defaultVariants: {
|
|
235
|
+
variant: "soft",
|
|
236
|
+
color: "default",
|
|
237
|
+
size: "md",
|
|
238
|
+
isSelected: false,
|
|
239
|
+
isDisabled: false,
|
|
240
|
+
},
|
|
241
|
+
});
|
|
242
|
+
|
|
243
|
+
/**
|
|
244
|
+
* The surface a chip wears — its resting variant, or `selected`.
|
|
245
|
+
*
|
|
246
|
+
* Pure, so the icon colour and the label colour are chosen by one rule that
|
|
247
|
+
* `bun test` reaches. See AGENTS.md.
|
|
248
|
+
*/
|
|
249
|
+
export function resolveChipSurface({
|
|
250
|
+
variant,
|
|
251
|
+
isSelected,
|
|
252
|
+
}: {
|
|
253
|
+
variant: ChipVariant;
|
|
254
|
+
isSelected: boolean;
|
|
255
|
+
}): ChipSurface {
|
|
256
|
+
return isSelected ? "selected" : variant;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
/** The theme token an `Icon` composed into the chip takes its colour from. */
|
|
260
|
+
export function resolveChipForegroundToken({
|
|
261
|
+
variant,
|
|
262
|
+
color,
|
|
263
|
+
isSelected,
|
|
264
|
+
}: {
|
|
265
|
+
variant: ChipVariant;
|
|
266
|
+
color: ChipColor;
|
|
267
|
+
isSelected: boolean;
|
|
268
|
+
}): string {
|
|
269
|
+
return CHIP_FOREGROUND_TOKEN[resolveChipSurface({ isSelected, variant })][color];
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
/**
|
|
273
|
+
* What the root renders as — see {@link ChipMode}.
|
|
274
|
+
*
|
|
275
|
+
* A chip is content until it is given something to do, as a badge is. Any
|
|
276
|
+
* selection prop — `isSelected` even when `false`, `defaultSelected`,
|
|
277
|
+
* `onSelectedChange` — says the chip has an on state, and a screen reader must
|
|
278
|
+
* hear which one it is in, so selection outranks a bare press handler.
|
|
279
|
+
*
|
|
280
|
+
* `onClose` is deliberately not read: the dismiss control is its own
|
|
281
|
+
* pressable, so a chip that can only be removed still has an inert root.
|
|
282
|
+
*/
|
|
283
|
+
export function resolveChipMode({
|
|
284
|
+
isSelected,
|
|
285
|
+
defaultSelected,
|
|
286
|
+
onSelectedChange,
|
|
287
|
+
onPress,
|
|
288
|
+
onLongPress,
|
|
289
|
+
}: {
|
|
290
|
+
isSelected?: boolean;
|
|
291
|
+
defaultSelected?: boolean;
|
|
292
|
+
onSelectedChange?: (isSelected: boolean) => void;
|
|
293
|
+
onPress?: () => void;
|
|
294
|
+
onLongPress?: () => void;
|
|
295
|
+
}): ChipMode {
|
|
296
|
+
if (isSelected !== undefined || defaultSelected !== undefined || onSelectedChange !== undefined) return "toggle";
|
|
297
|
+
if (onPress !== undefined || onLongPress !== undefined) return "button";
|
|
298
|
+
return "static";
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
/**
|
|
302
|
+
* How the remove control reaches assistive technology.
|
|
303
|
+
*
|
|
304
|
+
* - `none` — there is no `onClose`.
|
|
305
|
+
* - `element` — a static chip is not an accessibility element itself, so the
|
|
306
|
+
* close control is announced on its own, as a button.
|
|
307
|
+
* - `action` — a pressable chip is one accessibility element, and iOS folds its
|
|
308
|
+
* descendants into it: the close control is unreachable by swipe and its
|
|
309
|
+
* label is read as part of the chip's. It is hidden, and removal is offered as
|
|
310
|
+
* a `remove` accessibility action on the chip instead — VoiceOver's Actions
|
|
311
|
+
* rotor, TalkBack's actions menu.
|
|
312
|
+
*/
|
|
313
|
+
export type ChipCloseExposure = "none" | "element" | "action";
|
|
314
|
+
|
|
315
|
+
/** See {@link ChipCloseExposure}. Pure, so `bun test` reaches it. */
|
|
316
|
+
export function resolveChipCloseExposure({ mode, hasClose }: { mode: ChipMode; hasClose: boolean }): ChipCloseExposure {
|
|
317
|
+
if (!hasClose) return "none";
|
|
318
|
+
return mode === "static" ? "element" : "action";
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
export type ChipVariantProps = VariantProps<typeof chipVariants>;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
export { Chip, type ChipProps } from "./chip";
|
|
2
|
+
export { type ChipContextValue, ChipProvider, useChip, useChipContext } from "./chip.context";
|
|
3
|
+
export type { ChipSlotProps } from "./chip.types";
|
|
4
|
+
export {
|
|
5
|
+
CHIP_CLOSE_HIT_SLOP,
|
|
6
|
+
CHIP_COLORS,
|
|
7
|
+
CHIP_FOREGROUND_TOKEN,
|
|
8
|
+
CHIP_HIT_SLOP,
|
|
9
|
+
CHIP_SIZES,
|
|
10
|
+
CHIP_SURFACES,
|
|
11
|
+
CHIP_VARIANTS,
|
|
12
|
+
type ChipColor,
|
|
13
|
+
type ChipMode,
|
|
14
|
+
type ChipSize,
|
|
15
|
+
type ChipSurface,
|
|
16
|
+
type ChipVariant,
|
|
17
|
+
type ChipVariantProps,
|
|
18
|
+
chipVariants,
|
|
19
|
+
resolveChipForegroundToken,
|
|
20
|
+
resolveChipMode,
|
|
21
|
+
resolveChipSurface,
|
|
22
|
+
} from "./chip.variants";
|
|
23
|
+
export type { ChipCloseButtonProps } from "./chip-close-button";
|
|
24
|
+
export type { ChipLabelProps } from "./chip-label";
|