-
Notifications
You must be signed in to change notification settings - Fork 112
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* [sparkle] Add split button * bump * review comments
- Loading branch information
Showing
4 changed files
with
148 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import { cva } from "class-variance-authority"; | ||
import React, { useState } from "react"; | ||
|
||
import { | ||
Button, | ||
ButtonSizeType, | ||
MetaButtonProps, | ||
} from "@sparkle/components/Button"; | ||
import { | ||
DropdownMenu, | ||
DropdownMenuContent, | ||
DropdownMenuItem, | ||
DropdownMenuTrigger, | ||
} from "@sparkle/components/Dropdown"; | ||
import { Separator } from "@sparkle/components/Separator"; | ||
import { ChevronDownIcon } from "@sparkle/icons"; | ||
import { cn } from "@sparkle/lib"; | ||
|
||
const separatorSizeVariants: Record<ButtonSizeType, string> = { | ||
xs: "s-h-3", | ||
sm: "s-h-5", | ||
md: "s-h-7", | ||
}; | ||
|
||
const separatorVariants = cva("s--ml-[1px]", { | ||
variants: { | ||
size: separatorSizeVariants, | ||
}, | ||
}); | ||
|
||
interface SplitButtonActionProps { | ||
label: string; | ||
icon?: React.ComponentType; | ||
tooltip?: string; | ||
disabled?: boolean; | ||
onClick?: (event: React.MouseEvent<HTMLButtonElement, MouseEvent>) => void; | ||
} | ||
|
||
export interface SplitButtonProps extends Omit<MetaButtonProps, "children"> { | ||
actions: SplitButtonActionProps[]; | ||
defaultAction: SplitButtonActionProps; | ||
} | ||
|
||
export const SplitButton = React.forwardRef< | ||
HTMLButtonElement, | ||
SplitButtonProps | ||
>(({ actions, className, size, variant, disabled, ...props }, ref) => { | ||
const [current, setCurrent] = useState(props.defaultAction); | ||
return ( | ||
<div className="s-flex s-items-center"> | ||
<Button | ||
{...props} | ||
size={size} | ||
variant={variant} | ||
label={current.label} | ||
icon={current.icon} | ||
disabled={disabled || current.disabled} | ||
onClick={(e) => current.onClick && current.onClick(e)} | ||
ref={ref} | ||
className={cn("s-rounded-r-none s-border-r-0", className)} | ||
/> | ||
<div className={separatorVariants({ size })}> | ||
<Separator orientation="vertical" /> | ||
</div> | ||
<DropdownMenu> | ||
<DropdownMenuTrigger asChild> | ||
<Button | ||
{...props} | ||
size={size} | ||
variant={variant} | ||
icon={ChevronDownIcon} | ||
disabled={disabled} | ||
className={cn("s-rounded-l-none s-border-l-0", className)} | ||
/> | ||
</DropdownMenuTrigger> | ||
<DropdownMenuContent align="end"> | ||
{actions.map((action, index) => ( | ||
<DropdownMenuItem | ||
key={index} | ||
label={action.label} | ||
icon={action.icon} | ||
disabled={action.disabled} | ||
onClick={() => setCurrent(action)} | ||
/> | ||
))} | ||
</DropdownMenuContent> | ||
</DropdownMenu> | ||
</div> | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import type { Meta, StoryObj } from "@storybook/react"; | ||
import React from "react"; | ||
|
||
import { BUTTON_SIZES, BUTTON_VARIANTS } from "@sparkle/components/Button"; | ||
|
||
import { PlusIcon, RobotIcon, SplitButton } from "../index_with_tw_base"; | ||
|
||
const meta: Meta<React.ComponentProps<typeof SplitButton>> = { | ||
title: "Primitives/SplitButton", | ||
component: SplitButton, | ||
argTypes: { | ||
variant: { | ||
description: "The visual style variant of the button", | ||
options: BUTTON_VARIANTS, | ||
control: { type: "select" }, | ||
}, | ||
size: { | ||
description: "The size of the button", | ||
options: BUTTON_SIZES, | ||
control: { type: "select" }, | ||
}, | ||
}, | ||
}; | ||
|
||
export default meta; | ||
type Story = StoryObj<typeof meta>; | ||
|
||
export const ExampleButton: Story = { | ||
args: { | ||
variant: "outline", | ||
size: "md", | ||
defaultAction: { | ||
label: "First", | ||
icon: PlusIcon, | ||
tooltip: "First tooltip", | ||
}, | ||
actions: [ | ||
{ | ||
label: "First", | ||
icon: PlusIcon, | ||
tooltip: "First tooltip", | ||
}, | ||
{ | ||
label: "Second", | ||
icon: RobotIcon, | ||
tooltip: "Second tooltip", | ||
disabled: true, | ||
}, | ||
{ | ||
label: "Third", | ||
icon: PlusIcon, | ||
tooltip: "Third tooltip", | ||
}, | ||
], | ||
}, | ||
}; |