@vvedantb/ui 0.59.4 → 0.61.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,148 @@
1
+ import * as React from "react";
2
+ import { Input } from "./input";
3
+ import { Label } from "./label";
4
+ import { useCn } from "./ui-config";
5
+
6
+ interface FieldContextValue {
7
+ controlId: string;
8
+ descriptionId: string;
9
+ errorId: string;
10
+ hasDescription: boolean;
11
+ hasError: boolean;
12
+ setHasDescription: (present: boolean) => void;
13
+ setHasError: (present: boolean) => void;
14
+ }
15
+
16
+ const FieldContext = React.createContext<FieldContextValue | null>(null);
17
+
18
+ function useField(part: string): FieldContextValue {
19
+ const value = React.useContext(FieldContext);
20
+ if (!value) throw new Error(`${part} must be used inside <Field>.`);
21
+ return value;
22
+ }
23
+
24
+ export interface FieldProps extends React.ComponentPropsWithoutRef<"div"> {
25
+ /** The `id` of the control, which `FieldLabel` points at with `htmlFor`. Default: a generated id. */
26
+ controlId?: string;
27
+ }
28
+
29
+ /**
30
+ * Stack for one labelled control. It generates the control `id` and the ids of
31
+ * the description and error, so `FieldLabel` (`htmlFor`) and `FieldControl`
32
+ * (`id`, `aria-describedby`) link up without any manual wiring.
33
+ *
34
+ * A plain `<Input />` does not need it. Use it when a field has a label, a
35
+ * hint or an error message.
36
+ */
37
+ const Field = React.forwardRef<HTMLDivElement, FieldProps>(
38
+ ({ className, controlId, ...props }, ref) => {
39
+ const merge = useCn();
40
+ const generated = React.useId();
41
+ const [hasDescription, setHasDescription] = React.useState(false);
42
+ const [hasError, setHasError] = React.useState(false);
43
+ const id = controlId ?? generated;
44
+ const context = React.useMemo<FieldContextValue>(
45
+ () => ({
46
+ controlId: id,
47
+ descriptionId: `${id}-description`,
48
+ errorId: `${id}-error`,
49
+ hasDescription,
50
+ hasError,
51
+ setHasDescription,
52
+ setHasError,
53
+ }),
54
+ [id, hasDescription, hasError],
55
+ );
56
+ return (
57
+ <FieldContext.Provider value={context}>
58
+ <div ref={ref} className={merge("grid gap-2", className)} {...props} />
59
+ </FieldContext.Provider>
60
+ );
61
+ },
62
+ );
63
+ Field.displayName = "Field";
64
+
65
+ /** `Label` with `htmlFor` set to the control id of the nearest `Field`. */
66
+ const FieldLabel = React.forwardRef<
67
+ React.ComponentRef<typeof Label>,
68
+ React.ComponentPropsWithoutRef<typeof Label>
69
+ >((props, ref) => {
70
+ const { controlId } = useField("FieldLabel");
71
+ return <Label ref={ref} htmlFor={controlId} {...props} />;
72
+ });
73
+ FieldLabel.displayName = "FieldLabel";
74
+
75
+ /**
76
+ * `Input` with the `id` of the nearest `Field`. `aria-describedby` lists the
77
+ * description and error ids that are rendered, and nothing when there are none.
78
+ * `aria-invalid` is set while a `FieldError` is rendered. Both merge with props
79
+ * you pass.
80
+ */
81
+ const FieldControl = React.forwardRef<HTMLInputElement, React.ComponentProps<"input">>(
82
+ ({ "aria-describedby": describedBy, "aria-invalid": invalid, ...props }, ref) => {
83
+ const field = useField("FieldControl");
84
+ const ids = [
85
+ describedBy,
86
+ field.hasDescription ? field.descriptionId : undefined,
87
+ field.hasError ? field.errorId : undefined,
88
+ ]
89
+ .filter(Boolean)
90
+ .join(" ");
91
+ return (
92
+ <Input
93
+ {...props}
94
+ ref={ref}
95
+ id={field.controlId}
96
+ aria-describedby={ids || undefined}
97
+ aria-invalid={invalid ?? (field.hasError ? true : undefined)}
98
+ />
99
+ );
100
+ },
101
+ );
102
+ FieldControl.displayName = "FieldControl";
103
+
104
+ function useRegister(set: (present: boolean) => void) {
105
+ React.useLayoutEffect(() => {
106
+ set(true);
107
+ return () => set(false);
108
+ }, [set]);
109
+ }
110
+
111
+ /** Muted hint under the control. Its id joins the control's `aria-describedby`. */
112
+ const FieldDescription = React.forwardRef<HTMLParagraphElement, React.ComponentPropsWithoutRef<"p">>(
113
+ ({ className, ...props }, ref) => {
114
+ const field = useField("FieldDescription");
115
+ const merge = useCn();
116
+ useRegister(field.setHasDescription);
117
+ return (
118
+ <p
119
+ ref={ref}
120
+ id={field.descriptionId}
121
+ className={merge("text-sm text-muted-foreground", className)}
122
+ {...props}
123
+ />
124
+ );
125
+ },
126
+ );
127
+ FieldDescription.displayName = "FieldDescription";
128
+
129
+ /** Error message under the control. Its id joins the control's `aria-describedby`, and the control becomes `aria-invalid`. */
130
+ const FieldError = React.forwardRef<HTMLParagraphElement, React.ComponentPropsWithoutRef<"p">>(
131
+ ({ className, ...props }, ref) => {
132
+ const field = useField("FieldError");
133
+ const merge = useCn();
134
+ useRegister(field.setHasError);
135
+ return (
136
+ <p
137
+ ref={ref}
138
+ id={field.errorId}
139
+ role="alert"
140
+ className={merge("text-sm text-danger", className)}
141
+ {...props}
142
+ />
143
+ );
144
+ },
145
+ );
146
+ FieldError.displayName = "FieldError";
147
+
148
+ export { Field, FieldControl, FieldDescription, FieldError, FieldLabel };
@@ -7,6 +7,8 @@ import { useCn, useUiConfig } from "./ui-config";
7
7
  const sizeClasses = {
8
8
  sm: { input: "h-8 pl-8 text-base sm:pointer-fine:text-sm", icon: "left-2" },
9
9
  md: { input: "h-10 pl-9", icon: "left-3" },
10
+ // Alias of `md`.
11
+ default: { input: "h-10 pl-9", icon: "left-3" },
10
12
  };
11
13
 
12
14
  const variantClasses = {
@@ -42,7 +44,7 @@ export interface SearchInputProps extends Omit<ClearInputProps, "leading" | "siz
42
44
  label?: string;
43
45
  /** Key hint shown on the right while the value is empty, such as `"/"` or `"⌘K"`. */
44
46
  shortcut?: string;
45
- /** `sm` is `h-8`, `md` is `h-10`. Default `"sm"`. */
47
+ /** `sm` is `h-8`; `md` and its alias `default` are `h-10`. Default `"sm"`. */
46
48
  size?: keyof typeof sizeClasses;
47
49
  /** `toolbar`: transparent until hover or focus, fixed width from `sm`. Default `"default"`. */
48
50
  variant?: keyof typeof variantClasses;
@@ -5,6 +5,7 @@ import { useCn, useUiConfig } from "./ui-config";
5
5
  const sizeClasses = {
6
6
  sm: "size-4",
7
7
  md: "size-6",
8
+ default: "size-6",
8
9
  lg: "size-8",
9
10
  };
10
11
 
@@ -54,8 +55,8 @@ export type SpinnerProps = Omit<React.ComponentPropsWithoutRef<"svg">, "size" |
54
55
  * carry the state); `size`, `tone`, `stroke` and `decorative` do not apply.
55
56
  */
56
57
  variant?: "icon" | "circle" | "ring";
57
- /** Named size (a `size-*` class) or a pixel size (`width`/`height`, no size class). Default `"md"`. Not used with `"ring"`. */
58
- size?: "sm" | "md" | "lg" | number;
58
+ /** Named size (a `size-*` class; `default` is an alias of `md`) or a pixel size (`width`/`height`, no size class). Default `"md"`. Not used with `"ring"`. */
59
+ size?: "sm" | "md" | "default" | "lg" | number;
59
60
  /** Icon stroke width. Default the icon's own (2). Only for `"icon"`. */
60
61
  stroke?: number | string;
61
62
  /** `"muted"` adds `text-muted-foreground`; `"inherit"` adds no colour class, so the icon uses `currentColor`. Default `"muted"`. Only for `"icon"`. */
@@ -225,7 +226,7 @@ function RingSpinner({
225
226
  return (
226
227
  <svg className={merge("h-4 w-4 animate-spin", className)} xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" {...props}>
227
228
  <circle className="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" strokeWidth="4" />
228
- <path className="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c3.042 1.135 5.824 3 7.938l3-2.647z" />
229
+ <path className="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z" />
229
230
  </svg>
230
231
  );
231
232
  }
package/src/ui/tabs.tsx CHANGED
@@ -195,7 +195,8 @@ const TabsContent = React.forwardRef<
195
195
  });
196
196
  TabsContent.displayName = TabsPrimitive.Content.displayName;
197
197
 
198
- const tabsBarVariants = cva(
198
+ /** The class recipe of `TabsBar`. Ignores `UiConfig.Tabs` overrides. */
199
+ export const tabsBarVariants = cva(
199
200
  "flex shrink-0 flex-wrap items-center gap-2 border-b border-border",
200
201
  {
201
202
  variants: {