@vvedantb/ui 0.3.1 → 0.4.0-rc.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,49 @@
1
+ import { useEffect, useState } from "react";
2
+ import {
3
+ ghostSuggestion,
4
+ scheduleCompletion,
5
+ shouldSkipPrediction,
6
+ } from "./composerPrediction";
7
+
8
+ /**
9
+ * As-you-type ghost text for `InputArea`. The caller renders `suggestion` and
10
+ * owns accepting it. A result shows only while the draft it was requested for
11
+ * is still the current draft. Pass `getPrediction` to make the request.
12
+ */
13
+ export function useInputAreaPrediction({
14
+ value,
15
+ suspended = false,
16
+ enabled = true,
17
+ getPrediction,
18
+ }: {
19
+ value: string;
20
+ suspended?: boolean;
21
+ enabled?: boolean;
22
+ getPrediction?: (text: string) => Promise<string>;
23
+ }): { suggestion: string | undefined; dismiss: () => void } {
24
+ const [result, setResult] = useState<{ forValue: string; text: string }>();
25
+ const [dismissedFor, setDismissedFor] = useState<string>();
26
+
27
+ const idle =
28
+ !enabled ||
29
+ getPrediction === undefined ||
30
+ suspended ||
31
+ shouldSkipPrediction(value);
32
+ const settled = result?.forValue === value || dismissedFor === value;
33
+
34
+ // Debounced request: it has to react to the draft changing.
35
+ useEffect(() => {
36
+ if (idle || settled || getPrediction === undefined) return;
37
+ return scheduleCompletion(
38
+ value,
39
+ getPrediction,
40
+ (text) => setResult({ forValue: value, text }),
41
+ );
42
+ }, [value, idle, settled, getPrediction]);
43
+
44
+ const visible = !idle && dismissedFor !== value && result?.forValue === value;
45
+ return {
46
+ suggestion: visible ? ghostSuggestion(value, result.text) : undefined,
47
+ dismiss: () => setDismissedFor(value),
48
+ };
49
+ }
package/src/ui/button.tsx CHANGED
@@ -39,17 +39,26 @@ export interface ButtonProps
39
39
  React.ButtonHTMLAttributes<HTMLButtonElement>,
40
40
  VariantProps<typeof buttonVariants> {
41
41
  asChild?: boolean;
42
+ /** Rendered before the children. Ignored with `asChild`. */
43
+ startContent?: React.ReactNode;
42
44
  }
43
45
 
44
46
  const Button = React.forwardRef<HTMLButtonElement, ButtonProps>(
45
- ({ className, variant, size, asChild = false, ...props }, ref) => {
47
+ ({ className, variant, size, asChild = false, startContent, children, ...props }, ref) => {
46
48
  const Comp = asChild ? Slot : "button";
47
49
  return (
48
50
  <Comp
49
51
  className={cn(buttonVariants({ variant, size, className }))}
50
52
  ref={ref}
51
53
  {...props}
52
- />
54
+ >
55
+ {asChild ? children : (
56
+ <>
57
+ {startContent}
58
+ {children}
59
+ </>
60
+ )}
61
+ </Comp>
53
62
  );
54
63
  },
55
64
  );
@@ -75,14 +75,42 @@ const DropdownMenuItem = React.forwardRef<
75
75
  React.ComponentRef<typeof DropdownMenuPrimitive.Item>,
76
76
  React.ComponentPropsWithoutRef<typeof DropdownMenuPrimitive.Item> & {
77
77
  inset?: boolean;
78
+ /** Leading icon or mark. Ignored with `asChild`. */
79
+ startContent?: React.ReactNode;
80
+ /** Second line under the label. Ignored with `asChild`. */
81
+ description?: React.ReactNode;
78
82
  }
79
- >(({ className, inset, ...props }, ref) => (
80
- <DropdownMenuPrimitive.Item
81
- ref={ref}
82
- className={cn(menuItemClass, inset && "pl-8", className)}
83
- {...props}
84
- />
85
- ));
83
+ >(
84
+ (
85
+ { className, inset, startContent, description, children, asChild, ...props },
86
+ ref,
87
+ ) => (
88
+ <DropdownMenuPrimitive.Item
89
+ ref={ref}
90
+ asChild={asChild}
91
+ className={cn(menuItemClass, inset && "pl-8", className)}
92
+ {...props}
93
+ >
94
+ {asChild || (startContent === undefined && description === undefined) ? (
95
+ children
96
+ ) : (
97
+ <>
98
+ {startContent}
99
+ {description === undefined ? (
100
+ children
101
+ ) : (
102
+ <span className="flex min-w-0 flex-col">
103
+ <span>{children}</span>
104
+ <span className="text-xs text-muted-foreground">
105
+ {description}
106
+ </span>
107
+ </span>
108
+ )}
109
+ </>
110
+ )}
111
+ </DropdownMenuPrimitive.Item>
112
+ ),
113
+ );
86
114
  DropdownMenuItem.displayName = DropdownMenuPrimitive.Item.displayName;
87
115
 
88
116
  const DropdownMenuCheckboxItem = React.forwardRef<