@postenbring/hedwig-react 4.6.1 → 4.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,266 @@
1
+ import {
2
+ forwardRef,
3
+ useState,
4
+ type ButtonHTMLAttributes,
5
+ type InputHTMLAttributes,
6
+ type MouseEvent,
7
+ type ReactElement,
8
+ type ReactNode,
9
+ type Ref,
10
+ } from "react";
11
+ import { clsx } from "@postenbring/hedwig-css/typed-classname";
12
+ import { Slot } from "@radix-ui/react-slot";
13
+
14
+ interface ChipBaseProps {
15
+ /**
16
+ * `primary`: Colored by the inherited brand theme.
17
+ * `secondary`: White/grey chip, the checked indicator keeps the brand color.
18
+ *
19
+ * @default "primary"
20
+ */
21
+ variant?: "primary" | "secondary";
22
+ children: ReactNode;
23
+ }
24
+
25
+ /**
26
+ * Native button props, without `type`.
27
+ * The `type` prop of the Chip selects the kind of chip, so the native button `type` can not be set.
28
+ * Buttons are always rendered with `type="button"` to avoid accidentally submitting forms.
29
+ */
30
+ type NativeButtonProps = Omit<ButtonHTMLAttributes<HTMLButtonElement>, "type" | "children">;
31
+
32
+ type NativeInputProps = Omit<InputHTMLAttributes<HTMLInputElement>, "type" | "children" | "size">;
33
+
34
+ export type ChipRadioProps = ChipBaseProps & NativeInputProps & { type: "radio" };
35
+
36
+ export type ChipCheckboxProps = ChipBaseProps & NativeInputProps & { type: "checkbox" };
37
+
38
+ export type ChipRemovableProps = ChipBaseProps &
39
+ NativeButtonProps & {
40
+ type: "removable";
41
+ /**
42
+ * Called when the chip is clicked, i.e. the user wants to remove it.
43
+ * A `onClick` handler is also called, if provided.
44
+ */
45
+ onRemove?: (event: MouseEvent<HTMLButtonElement>) => void;
46
+ /**
47
+ * Text prepended to the label to form the accessible name, e.g. "Fjern Oslo".
48
+ * Only used when `children` is a string or number, otherwise provide an `aria-label`.
49
+ *
50
+ * @default "Fjern"
51
+ */
52
+ removeLabel?: string;
53
+ };
54
+
55
+ export type ChipButtonProps = ChipBaseProps &
56
+ NativeButtonProps & {
57
+ type: "button";
58
+ /**
59
+ * Change the default rendered element for the one passed as a child, merging their props and behavior.
60
+ *
61
+ * @default false
62
+ */
63
+ asChild?: boolean;
64
+ };
65
+
66
+ export type ChipToggleProps = ChipBaseProps &
67
+ NativeButtonProps & {
68
+ type: "toggle";
69
+ /** Icon shown in the chip. Should be an `svg`, it is hidden from assistive technology. */
70
+ icon: ReactNode;
71
+ /** Icon shown instead of `icon` when pressed. */
72
+ pressedIcon?: ReactNode;
73
+ /** Controlled pressed state. */
74
+ pressed?: boolean;
75
+ /** Initial pressed state when uncontrolled. */
76
+ defaultPressed?: boolean;
77
+ /** Called when the user toggles the chip. */
78
+ onPressedChange?: (pressed: boolean) => void;
79
+ };
80
+
81
+ export type ChipProps =
82
+ ChipRadioProps | ChipCheckboxProps | ChipRemovableProps | ChipButtonProps | ChipToggleProps;
83
+
84
+ function chipClassName(type: ChipProps["type"], variant: ChipProps["variant"], className?: string) {
85
+ return clsx(
86
+ "hds-chip",
87
+ // The button type has no type-specific styles, so it has no modifier class
88
+ type !== "button" && `hds-chip--${type}`,
89
+ { "hds-chip--secondary": variant === "secondary" },
90
+ className as undefined,
91
+ );
92
+ }
93
+
94
+ /**
95
+ * One component per type of chip, `Chip` picks the right one
96
+ */
97
+
98
+ const InputChip = forwardRef<HTMLInputElement, ChipRadioProps | ChipCheckboxProps>(
99
+ function InputChip(
100
+ { type, variant = "primary", className, style, children, ...inputProps },
101
+ ref,
102
+ ) {
103
+ return (
104
+ <label className={chipClassName(type, variant, className)} style={style}>
105
+ <input {...inputProps} ref={ref} type={type} className="hds-chip__input" />
106
+ <span className="hds-chip__indicator" aria-hidden="true" />
107
+ {children}
108
+ </label>
109
+ );
110
+ },
111
+ );
112
+
113
+ const RemovableChip = forwardRef<HTMLButtonElement, ChipRemovableProps>(function RemovableChip(
114
+ {
115
+ type,
116
+ variant = "primary",
117
+ className,
118
+ onRemove,
119
+ removeLabel = "Fjern",
120
+ onClick,
121
+ children,
122
+ ...buttonProps
123
+ },
124
+ ref,
125
+ ) {
126
+ // E.g. "Fjern Oslo". For other children than text the consumer must provide an `aria-label`
127
+ const ariaLabel =
128
+ typeof children === "string" || typeof children === "number"
129
+ ? `${removeLabel} ${children}`
130
+ : undefined;
131
+
132
+ return (
133
+ <button
134
+ aria-label={ariaLabel}
135
+ {...buttonProps}
136
+ ref={ref}
137
+ type="button"
138
+ className={chipClassName(type, variant, className)}
139
+ onClick={(event) => {
140
+ onClick?.(event);
141
+ if (!event.defaultPrevented) onRemove?.(event);
142
+ }}
143
+ >
144
+ {children}
145
+ <span className="hds-chip__remove" aria-hidden="true" />
146
+ </button>
147
+ );
148
+ });
149
+
150
+ const ButtonChip = forwardRef<HTMLButtonElement, ChipButtonProps>(function ButtonChip(
151
+ { type, variant = "primary", className, asChild, ...buttonProps },
152
+ ref,
153
+ ) {
154
+ const classNames = chipClassName(type, variant, className);
155
+
156
+ // With `asChild` the child decides the element, e.g. an anchor, so no `type="button"`
157
+ if (asChild) return <Slot {...buttonProps} ref={ref} className={classNames} />;
158
+
159
+ return <button {...buttonProps} ref={ref} type="button" className={classNames} />;
160
+ });
161
+
162
+ const ToggleChip = forwardRef<HTMLButtonElement, ChipToggleProps>(function ToggleChip(
163
+ {
164
+ type,
165
+ variant = "primary",
166
+ className,
167
+ icon,
168
+ pressedIcon,
169
+ pressed: pressedProp,
170
+ defaultPressed = false,
171
+ onPressedChange,
172
+ onClick,
173
+ children,
174
+ ...buttonProps
175
+ },
176
+ ref,
177
+ ) {
178
+ const [pressedState, setPressedState] = useState(defaultPressed);
179
+ const isControlled = pressedProp !== undefined;
180
+ const pressed = isControlled ? pressedProp : pressedState;
181
+
182
+ return (
183
+ <button
184
+ {...buttonProps}
185
+ ref={ref}
186
+ type="button"
187
+ aria-pressed={pressed}
188
+ className={chipClassName(type, variant, className)}
189
+ onClick={(event) => {
190
+ onClick?.(event);
191
+ if (event.defaultPrevented) return;
192
+ if (!isControlled) setPressedState(!pressed);
193
+ onPressedChange?.(!pressed);
194
+ }}
195
+ >
196
+ <span className="hds-chip__icon" aria-hidden="true">
197
+ {pressed && pressedIcon ? pressedIcon : icon}
198
+ </span>
199
+ {children}
200
+ </button>
201
+ );
202
+ });
203
+
204
+ /**
205
+ * Gives `ref` the right element type for each chip type:
206
+ * `HTMLInputElement` for radio and checkbox, `HTMLButtonElement` for the rest.
207
+ */
208
+ interface ChipComponent {
209
+ (
210
+ props:
211
+ | ((ChipRadioProps | ChipCheckboxProps) & { ref?: Ref<HTMLInputElement> })
212
+ | ((ChipRemovableProps | ChipButtonProps | ChipToggleProps) & {
213
+ ref?: Ref<HTMLButtonElement>;
214
+ }),
215
+ ): ReactElement | null;
216
+ displayName?: string;
217
+ }
218
+
219
+ /**
220
+ * Chips let the user select one or more options, toggle something, or remove an item.
221
+ *
222
+ * Choose the kind of chip with the `type` prop:
223
+ * - `radio`: Select one among several options. Use the same `name` for the options.
224
+ * - `checkbox`: Select one or more among several options.
225
+ * - `removable`: Represents a selected value the user can remove.
226
+ * - `button`: Triggers an action. Use `asChild` with an anchor for navigation.
227
+ * - `toggle`: A pressable chip with an icon, e.g. to mark something as favorite.
228
+ *
229
+ * Note that the `type` prop is the chip type, not the native button `type`.
230
+ * Buttons are always rendered with `type="button"`.
231
+ *
232
+ * For `radio` and `checkbox` `className` and `style` are set on the root `label`,
233
+ * all other props are set on the `input`.
234
+ *
235
+ * Wrap multiple chips in a `ChipGroup`.
236
+ *
237
+ * @example
238
+ * <Chip type="radio" name="weight" value="0-2">0-2 kg</Chip>
239
+ * <Chip type="checkbox" defaultChecked>Sporing</Chip>
240
+ * <Chip type="removable" onRemove={() => removeCity("oslo")}>Oslo</Chip>
241
+ * <Chip type="button" onClick={openFilter}>Filter</Chip>
242
+ * <Chip variant="secondary" type="toggle" icon={<StarOutline />} pressedIcon={<StarSolid />}>
243
+ * Favoritt
244
+ * </Chip>
245
+ *
246
+ * @example
247
+ * // For navigation use `asChild` with an anchor element.
248
+ * <Chip type="button" asChild><a href="/tracking">Sporing</a></Chip>
249
+ */
250
+ export const Chip = forwardRef<HTMLInputElement | HTMLButtonElement, ChipProps>((props, ref) => {
251
+ const inputRef = ref as Ref<HTMLInputElement>;
252
+ const buttonRef = ref as Ref<HTMLButtonElement>;
253
+
254
+ switch (props.type) {
255
+ case "radio":
256
+ case "checkbox":
257
+ return <InputChip {...props} ref={inputRef} />;
258
+ case "removable":
259
+ return <RemovableChip {...props} ref={buttonRef} />;
260
+ case "button":
261
+ return <ButtonChip {...props} ref={buttonRef} />;
262
+ case "toggle":
263
+ return <ToggleChip {...props} ref={buttonRef} />;
264
+ }
265
+ }) as ChipComponent;
266
+ Chip.displayName = "Chip";
@@ -0,0 +1,4 @@
1
+ export { Chip } from "./chip";
2
+ export type * from "./chip";
3
+ export { ChipGroup } from "./chip-group";
4
+ export type * from "./chip-group";
package/src/index.ts CHANGED
@@ -7,6 +7,7 @@ export * from "./breadcrumbs";
7
7
  export * from "./button";
8
8
  export * from "./button-list";
9
9
  export * from "./card";
10
+ export * from "./chip";
10
11
  export * from "./description-list";
11
12
  export * from "./figure";
12
13
  export * from "./form";