diff --git a/src/components/Button/index.tsx b/src/components/Button/index.tsx index 19ff3090..0b0ffd15 100644 --- a/src/components/Button/index.tsx +++ b/src/components/Button/index.tsx @@ -97,6 +97,47 @@ const ButtonText = React.forwardRef< )) ButtonText.displayName = 'ButtonText' +const BrandSlot = React.forwardRef< + HTMLElement, + React.ComponentProps & { isBrandVariant?: boolean } +>(({ isBrandVariant = false, ...slotProps }, slotRef) => { + if (!isBrandVariant) { + return + } + + const child = React.Children.only(slotProps.children) + if (!React.isValidElement(child)) { + return + } + + const brandSpan = ( + + ) + + const childWithBrand = React.cloneElement( + child as React.ReactElement>, + { + ...child.props, + children: (child.props as React.ComponentProps).children + ? [ + brandSpan, + (child.props as React.ComponentProps).children, + ] + : brandSpan, + } + ) + + return ( + + {childWithBrand} + + ) +}) +BrandSlot.displayName = 'BrandSlot' + const buttonVariants = cva( 'relative inline-flex items-center justify-center whitespace-nowrap text-sm font-mono uppercase tracking-[0.01em] transition-all select-none cursor-pointer focus-visible:outline-none focus-visible:ring-0 focus-visible:ring-[var(--border-focus)] focus-visible:ring-offset-[var(--bg-surface-primary-default)] disabled:pointer-events-none disabled:opacity-50 [&_svg]:pointer-events-none [&_svg]:shrink-0', { @@ -406,51 +447,6 @@ const Button = React.forwardRef( } }, [isBrandVariant]) - // Custom Slot wrapper for brand variant - const BrandSlot = React.forwardRef< - HTMLElement, - React.ComponentProps - >((slotProps, slotRef) => { - if (!isBrandVariant) { - return - } - - // For brand variant, we need to inject the brand span - const child = React.Children.only(slotProps.children) - if (!React.isValidElement(child)) { - return - } - - // Create brand span - const brandSpan = ( - - ) - - // Clone child with brand span injected - const childWithBrand = React.cloneElement( - child as React.ReactElement>, - { - ...child.props, - children: (child.props as React.ComponentProps).children - ? [ - brandSpan, - (child.props as React.ComponentProps).children, - ] - : brandSpan, - } - ) - - return ( - - {childWithBrand} - - ) - }) - BrandSlot.displayName = 'BrandSlot' - const Comp = asChild ? BrandSlot : 'button' const combinedRef = React.useCallback( (node: HTMLButtonElement | null) => { @@ -489,6 +485,7 @@ const Button = React.forwardRef( onMouseMove={handleMouseMove} onMouseDown={handleMouseDown} onMouseUp={handleMouseUp} + {...(asChild ? { isBrandVariant } : {})} {...props} > {asChild ? (