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 3636 },
3737 "dependencies" : {
3838 "@nanostores/react" : " ^0.8.4" ,
39+ "@radix-ui/react-slot" : " ^1.2.3" ,
3940 "@tanstack/react-form" : " ^0.41.3" ,
4041 "clsx" : " ^2.1.1" ,
4142 "firebase" : " ^11.2.0" ,
Original file line number Diff line number Diff line change 1515 */
1616
1717import { ButtonHTMLAttributes } from "react" ;
18+ import { Slot } from "@radix-ui/react-slot"
1819import { cn } from "~/utils/cn" ;
1920
2021const buttonVariants = {
@@ -26,21 +27,21 @@ type ButtonVariant = keyof typeof buttonVariants;
2627
2728interface ButtonProps extends ButtonHTMLAttributes < HTMLButtonElement > {
2829 variant ?: ButtonVariant ;
30+ asChild ?: boolean ;
2931}
3032
3133export function Button ( {
32- children,
3334 className,
3435 variant = "primary" ,
36+ asChild,
3537 ...props
3638} : ButtonProps ) {
39+ const Comp = asChild ? Slot : "button" ;
40+
3741 return (
38- < button
39- type = "button"
42+ < Comp
4043 className = { cn ( buttonVariants [ variant ] , className ) }
4144 { ...props }
42- >
43- { children }
44- </ button >
45+ />
4546 ) ;
4647}
Original file line number Diff line number Diff line change @@ -62,4 +62,28 @@ describe("Button Component", () => {
6262
6363 expect ( button ) . toBeDisabled ( ) ;
6464 } ) ;
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+ } ) ;
6589} ) ;
You can’t perform that action at this time.
0 commit comments