File tree Expand file tree Collapse file tree 4 files changed +4539
-3188
lines changed
packages/firebaseui-react Expand file tree Collapse file tree 4 files changed +4539
-3188
lines changed Original file line number Diff line number Diff line change 36
36
},
37
37
"dependencies" : {
38
38
"@nanostores/react" : " ^0.8.4" ,
39
+ "@radix-ui/react-slot" : " ^1.2.3" ,
39
40
"@tanstack/react-form" : " ^0.41.3" ,
40
41
"clsx" : " ^2.1.1" ,
41
42
"firebase" : " ^11.2.0" ,
Original file line number Diff line number Diff line change 15
15
*/
16
16
17
17
import { ButtonHTMLAttributes } from "react" ;
18
+ import { Slot } from "@radix-ui/react-slot"
18
19
import { cn } from "~/utils/cn" ;
19
20
20
21
const buttonVariants = {
@@ -26,21 +27,21 @@ type ButtonVariant = keyof typeof buttonVariants;
26
27
27
28
interface ButtonProps extends ButtonHTMLAttributes < HTMLButtonElement > {
28
29
variant ?: ButtonVariant ;
30
+ asChild ?: boolean ;
29
31
}
30
32
31
33
export function Button ( {
32
- children,
33
34
className,
34
35
variant = "primary" ,
36
+ asChild,
35
37
...props
36
38
} : ButtonProps ) {
39
+ const Comp = asChild ? Slot : "button" ;
40
+
37
41
return (
38
- < button
39
- type = "button"
42
+ < Comp
40
43
className = { cn ( buttonVariants [ variant ] , className ) }
41
44
{ ...props }
42
- >
43
- { children }
44
- </ button >
45
+ />
45
46
) ;
46
47
}
Original file line number Diff line number Diff line change @@ -62,4 +62,28 @@ describe("Button Component", () => {
62
62
63
63
expect ( button ) . toBeDisabled ( ) ;
64
64
} ) ;
65
+
66
+ it ( "renders as a Slot component when asChild is true" , ( ) => {
67
+ render (
68
+ < Button asChild >
69
+ < a href = "/test" > Link Button</ a >
70
+ </ Button >
71
+ ) ;
72
+ const link = screen . getByRole ( "link" , { name : / l i n k b u t t o n / i } ) ;
73
+
74
+ expect ( link ) . toBeInTheDocument ( ) ;
75
+ expect ( link ) . toHaveClass ( "fui-button" ) ;
76
+ expect ( link . tagName ) . toBe ( "A" ) ;
77
+ expect ( link ) . toHaveAttribute ( "href" , "/test" ) ;
78
+ } ) ;
79
+
80
+ it ( "renders as a button element when asChild is false or undefined" , ( ) => {
81
+ const { rerender } = render ( < Button > Regular Button</ Button > ) ;
82
+ let button = screen . getByRole ( "button" , { name : / r e g u l a r b u t t o n / i } ) ;
83
+ expect ( button . tagName ) . toBe ( "BUTTON" ) ;
84
+
85
+ rerender ( < Button asChild = { false } > Regular Button</ Button > ) ;
86
+ button = screen . getByRole ( "button" , { name : / r e g u l a r b u t t o n / i } ) ;
87
+ expect ( button . tagName ) . toBe ( "BUTTON" ) ;
88
+ } ) ;
65
89
} ) ;
You can’t perform that action at this time.
0 commit comments