Skip to content

Commit 147dc44

Browse files
committed
fix: only imports what necessary from React/radix primitive
1 parent ed07255 commit 147dc44

File tree

15 files changed

+303
-315
lines changed

15 files changed

+303
-315
lines changed

packages/shadcn/src/components/ui/alert.tsx

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import * as React from 'react';
21
import { cva, type VariantProps } from 'class-variance-authority';
2+
import { forwardRef, HTMLAttributes } from 'react';
33

44
import { cn } from '../../lib/utils';
55

@@ -18,26 +18,25 @@ const alertVariants = cva(
1818
}
1919
);
2020

21-
const Alert = React.forwardRef<
22-
HTMLDivElement,
23-
React.HTMLAttributes<HTMLDivElement> & VariantProps<typeof alertVariants>
24-
>(({ className, variant, ...props }, ref) => (
25-
<div ref={ref} role='alert' className={cn(alertVariants({ variant }), className)} {...props} />
26-
));
21+
const Alert = forwardRef<HTMLDivElement, HTMLAttributes<HTMLDivElement> & VariantProps<typeof alertVariants>>(
22+
({ className, variant, ...props }, ref) => (
23+
<div ref={ref} role='alert' className={cn(alertVariants({ variant }), className)} {...props} />
24+
)
25+
);
2726
Alert.displayName = 'Alert';
2827

29-
const AlertTitle = React.forwardRef<HTMLParagraphElement, React.HTMLAttributes<HTMLHeadingElement>>(
28+
const AlertTitle = forwardRef<HTMLParagraphElement, HTMLAttributes<HTMLHeadingElement>>(
3029
({ className, ...props }, ref) => (
3130
<h5 ref={ref} className={cn('mb-1 font-medium leading-none tracking-tight', className)} {...props} />
3231
)
3332
);
3433
AlertTitle.displayName = 'AlertTitle';
3534

36-
const AlertDescription = React.forwardRef<HTMLParagraphElement, React.HTMLAttributes<HTMLParagraphElement>>(
35+
const AlertDescription = forwardRef<HTMLParagraphElement, HTMLAttributes<HTMLParagraphElement>>(
3736
({ className, ...props }, ref) => (
3837
<div ref={ref} className={cn('text-sm [&_p]:leading-relaxed', className)} {...props} />
3938
)
4039
);
4140
AlertDescription.displayName = 'AlertDescription';
4241

43-
export { Alert, AlertTitle, AlertDescription };
42+
export { Alert, AlertDescription, AlertTitle };

packages/shadcn/src/components/ui/badge.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import * as React from 'react';
21
import { cva, type VariantProps } from 'class-variance-authority';
2+
import { HTMLAttributes } from 'react';
33

44
import { cn } from '../../lib/utils';
55

@@ -20,7 +20,7 @@ const badgeVariants = cva(
2020
}
2121
);
2222

23-
export interface BadgeProps extends React.HTMLAttributes<HTMLDivElement>, VariantProps<typeof badgeVariants> {}
23+
export interface BadgeProps extends HTMLAttributes<HTMLDivElement>, VariantProps<typeof badgeVariants> {}
2424

2525
function Badge({ className, variant, ...props }: BadgeProps) {
2626
return <div className={cn(badgeVariants({ variant }), className)} {...props} />;

packages/shadcn/src/components/ui/button.tsx

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import * as React from 'react';
21
import { Slot } from '@radix-ui/react-slot';
32
import { cva, type VariantProps } from 'class-variance-authority';
4-
3+
import { ButtonHTMLAttributes, forwardRef } from 'react';
54
import { cn } from '../../lib/utils';
65

76
const buttonVariants = cva(
@@ -30,13 +29,11 @@ const buttonVariants = cva(
3029
}
3130
);
3231

33-
export interface ButtonProps
34-
extends React.ButtonHTMLAttributes<HTMLButtonElement>,
35-
VariantProps<typeof buttonVariants> {
32+
export interface ButtonProps extends ButtonHTMLAttributes<HTMLButtonElement>, VariantProps<typeof buttonVariants> {
3633
asChild?: boolean;
3734
}
3835

39-
const Button = React.forwardRef<HTMLButtonElement, ButtonProps>(
36+
const Button = forwardRef<HTMLButtonElement, ButtonProps>(
4037
({ className, variant, size, asChild = false, ...props }, ref) => {
4138
const Comp = asChild ? Slot : 'button';
4239
return <Comp className={cn(buttonVariants({ variant, size, className }))} ref={ref} {...props} />;
Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,26 @@
11
'use client';
22

3-
import * as React from 'react';
4-
import * as CheckboxPrimitive from '@radix-ui/react-checkbox';
3+
import { Indicator, Root } from '@radix-ui/react-checkbox';
54
import { CheckIcon } from '@radix-ui/react-icons';
6-
5+
import { ComponentPropsWithoutRef, ElementRef, forwardRef } from 'react';
76
import { cn } from '../../lib/utils';
87

9-
const Checkbox = React.forwardRef<
10-
React.ElementRef<typeof CheckboxPrimitive.Root>,
11-
React.ComponentPropsWithoutRef<typeof CheckboxPrimitive.Root>
12-
>(({ className, ...props }, ref) => (
13-
<CheckboxPrimitive.Root
14-
ref={ref}
15-
className={cn(
16-
'peer h-4 w-4 shrink-0 rounded-sm border border-primary shadow focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring disabled:cursor-not-allowed disabled:opacity-50 data-[state=checked]:bg-primary data-[state=checked]:text-primary-foreground',
17-
className
18-
)}
19-
{...props}
20-
>
21-
<CheckboxPrimitive.Indicator className={cn('flex items-center justify-center text-current')}>
22-
<CheckIcon className='h-4 w-4' />
23-
</CheckboxPrimitive.Indicator>
24-
</CheckboxPrimitive.Root>
25-
));
26-
Checkbox.displayName = CheckboxPrimitive.Root.displayName;
8+
const Checkbox = forwardRef<ElementRef<typeof Root>, ComponentPropsWithoutRef<typeof Root>>(
9+
({ className, ...props }, ref) => (
10+
<Root
11+
ref={ref}
12+
className={cn(
13+
'peer h-4 w-4 shrink-0 rounded-sm border border-primary shadow focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring disabled:cursor-not-allowed disabled:opacity-50 data-[state=checked]:bg-primary data-[state=checked]:text-primary-foreground',
14+
className
15+
)}
16+
{...props}
17+
>
18+
<Indicator className={cn('flex items-center justify-center text-current')}>
19+
<CheckIcon className='h-4 w-4' />
20+
</Indicator>
21+
</Root>
22+
)
23+
);
24+
Checkbox.displayName = Root.displayName;
2725

2826
export { Checkbox };
Lines changed: 36 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,23 @@
11
'use client';
22

3-
import * as React from 'react';
4-
import { type DialogProps } from '@radix-ui/react-dialog';
3+
import { Dialog, DialogContent, type DialogProps } from '@radix-ui/react-dialog';
54
import { Command as CommandPrimitive } from 'cmdk';
65
import { Search } from 'lucide-react';
7-
6+
import { ComponentPropsWithoutRef, ElementRef, forwardRef, HTMLAttributes } from 'react';
87
import { cn } from '../../lib/utils';
9-
import { Dialog, DialogContent } from './dialog';
108

11-
const Command = React.forwardRef<
12-
React.ElementRef<typeof CommandPrimitive>,
13-
React.ComponentPropsWithoutRef<typeof CommandPrimitive>
14-
>(({ className, ...props }, ref) => (
15-
<CommandPrimitive
16-
ref={ref}
17-
className={cn(
18-
'flex h-full w-full flex-col overflow-hidden rounded-md bg-popover text-popover-foreground',
19-
className
20-
)}
21-
{...props}
22-
/>
23-
));
9+
const Command = forwardRef<ElementRef<typeof CommandPrimitive>, ComponentPropsWithoutRef<typeof CommandPrimitive>>(
10+
({ className, ...props }, ref) => (
11+
<CommandPrimitive
12+
ref={ref}
13+
className={cn(
14+
'flex h-full w-full flex-col overflow-hidden rounded-md bg-popover text-popover-foreground',
15+
className
16+
)}
17+
{...props}
18+
/>
19+
)
20+
);
2421
Command.displayName = CommandPrimitive.displayName;
2522

2623
const CommandDialog = ({ children, ...props }: DialogProps) => {
@@ -35,9 +32,9 @@ const CommandDialog = ({ children, ...props }: DialogProps) => {
3532
);
3633
};
3734

38-
const CommandInput = React.forwardRef<
39-
React.ElementRef<typeof CommandPrimitive.Input>,
40-
React.ComponentPropsWithoutRef<typeof CommandPrimitive.Input>
35+
const CommandInput = forwardRef<
36+
ElementRef<typeof CommandPrimitive.Input>,
37+
ComponentPropsWithoutRef<typeof CommandPrimitive.Input>
4138
>(({ className, ...props }, ref) => (
4239
<div className='flex items-center border-b px-3' {...{ 'cmdk-input-wrapper': '' }}>
4340
<Search className='mr-2 h-4 w-4 shrink-0 opacity-50' />
@@ -54,9 +51,9 @@ const CommandInput = React.forwardRef<
5451

5552
CommandInput.displayName = CommandPrimitive.Input.displayName;
5653

57-
const CommandList = React.forwardRef<
58-
React.ElementRef<typeof CommandPrimitive.List>,
59-
React.ComponentPropsWithoutRef<typeof CommandPrimitive.List>
54+
const CommandList = forwardRef<
55+
ElementRef<typeof CommandPrimitive.List>,
56+
ComponentPropsWithoutRef<typeof CommandPrimitive.List>
6057
>(({ className, ...props }, ref) => (
6158
<CommandPrimitive.List
6259
ref={ref}
@@ -67,16 +64,16 @@ const CommandList = React.forwardRef<
6764

6865
CommandList.displayName = CommandPrimitive.List.displayName;
6966

70-
const CommandEmpty = React.forwardRef<
71-
React.ElementRef<typeof CommandPrimitive.Empty>,
72-
React.ComponentPropsWithoutRef<typeof CommandPrimitive.Empty>
67+
const CommandEmpty = forwardRef<
68+
ElementRef<typeof CommandPrimitive.Empty>,
69+
ComponentPropsWithoutRef<typeof CommandPrimitive.Empty>
7370
>((props, ref) => <CommandPrimitive.Empty ref={ref} className='py-6 text-center text-sm' {...props} />);
7471

7572
CommandEmpty.displayName = CommandPrimitive.Empty.displayName;
7673

77-
const CommandGroup = React.forwardRef<
78-
React.ElementRef<typeof CommandPrimitive.Group>,
79-
React.ComponentPropsWithoutRef<typeof CommandPrimitive.Group>
74+
const CommandGroup = forwardRef<
75+
ElementRef<typeof CommandPrimitive.Group>,
76+
ComponentPropsWithoutRef<typeof CommandPrimitive.Group>
8077
>(({ className, ...props }, ref) => (
8178
<CommandPrimitive.Group
8279
ref={ref}
@@ -90,17 +87,17 @@ const CommandGroup = React.forwardRef<
9087

9188
CommandGroup.displayName = CommandPrimitive.Group.displayName;
9289

93-
const CommandSeparator = React.forwardRef<
94-
React.ElementRef<typeof CommandPrimitive.Separator>,
95-
React.ComponentPropsWithoutRef<typeof CommandPrimitive.Separator>
90+
const CommandSeparator = forwardRef<
91+
ElementRef<typeof CommandPrimitive.Separator>,
92+
ComponentPropsWithoutRef<typeof CommandPrimitive.Separator>
9693
>(({ className, ...props }, ref) => (
9794
<CommandPrimitive.Separator ref={ref} className={cn('-mx-1 h-px bg-border', className)} {...props} />
9895
));
9996
CommandSeparator.displayName = CommandPrimitive.Separator.displayName;
10097

101-
const CommandItem = React.forwardRef<
102-
React.ElementRef<typeof CommandPrimitive.Item>,
103-
React.ComponentPropsWithoutRef<typeof CommandPrimitive.Item>
98+
const CommandItem = forwardRef<
99+
ElementRef<typeof CommandPrimitive.Item>,
100+
ComponentPropsWithoutRef<typeof CommandPrimitive.Item>
104101
>(({ className, ...props }, ref) => (
105102
<CommandPrimitive.Item
106103
ref={ref}
@@ -114,19 +111,19 @@ const CommandItem = React.forwardRef<
114111

115112
CommandItem.displayName = CommandPrimitive.Item.displayName;
116113

117-
const CommandShortcut = ({ className, ...props }: React.HTMLAttributes<HTMLSpanElement>) => {
114+
const CommandShortcut = ({ className, ...props }: HTMLAttributes<HTMLSpanElement>) => {
118115
return <span className={cn('ms-auto text-xs tracking-widest text-muted-foreground', className)} {...props} />;
119116
};
120117
CommandShortcut.displayName = 'CommandShortcut';
121118

122119
export {
123120
Command,
124121
CommandDialog,
125-
CommandInput,
126-
CommandList,
127122
CommandEmpty,
128123
CommandGroup,
124+
CommandInput,
129125
CommandItem,
130-
CommandShortcut,
126+
CommandList,
131127
CommandSeparator,
128+
CommandShortcut,
132129
};

0 commit comments

Comments
 (0)