-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ui): ToggleGroup component (#81)
* chore: add deps for togglegroup component * feat: ToggleGroup component * feat: ToggleGroup story
- Loading branch information
Showing
6 changed files
with
185 additions
and
0 deletions.
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,63 @@ | ||
import type { Meta, StoryObj } from "@storybook/react"; | ||
|
||
import { ToggleGroup } from "./ToggleGroup"; | ||
|
||
// More on how to set up stories at: https://storybook.js.org/docs/writing-stories#default-export | ||
const meta = { | ||
title: "Common/ToggleGroup", | ||
component: ToggleGroup, | ||
// This component will have an automatically generated Autodocs entry: https://storybook.js.org/docs/writing-docs/autodocs | ||
tags: ["autodocs"], | ||
args: { | ||
type: "single", | ||
size: "md", | ||
disabled: false, | ||
}, | ||
// More on argTypes: https://storybook.js.org/docs/api/argtypes | ||
} satisfies Meta<typeof ToggleGroup>; | ||
|
||
export default meta; | ||
type Story = StoryObj<typeof meta>; | ||
|
||
export const Default: Story = { | ||
render: (args) => { | ||
console.log(args); | ||
return ( | ||
<ToggleGroup {...args}> | ||
<ToggleGroup.Item value="down" aria-label="Toggle down"> | ||
Down | ||
</ToggleGroup.Item> | ||
<ToggleGroup.Item value="check" aria-label="Toggle check"> | ||
Check | ||
</ToggleGroup.Item> | ||
<ToggleGroup.Item value="lock" aria-label="Toggle lock"> | ||
Lock | ||
</ToggleGroup.Item> | ||
</ToggleGroup> | ||
); | ||
}, | ||
}; | ||
|
||
// More on writing stories with args: https://storybook.js.org/docs/writing-stories/args | ||
export const Multiple: Story = { | ||
...Default, | ||
args: { | ||
type: "multiple", | ||
}, | ||
}; | ||
|
||
export const Small: Story = { | ||
...Default, | ||
args: { | ||
...Default.args, | ||
size: "sm", | ||
}, | ||
}; | ||
|
||
export const Disabled: Story = { | ||
...Default, | ||
args: { | ||
...Default.args, | ||
disabled: true, | ||
}, | ||
}; |
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,41 @@ | ||
import { tv, type VariantProps } from "tailwind-variants"; | ||
|
||
export const toggleGroupTheme = tv({ | ||
slots: { | ||
root: [ | ||
"inline-flex", | ||
"items-start", | ||
"rounded-lg", | ||
"border", | ||
"border-solid", | ||
"text-border-default", | ||
"overflow-hidden", | ||
], | ||
item: [ | ||
"inline-flex", | ||
"items-center", | ||
"justify-center", | ||
"py-2", | ||
"px-5", | ||
"flex-shrink-0", | ||
"transition-colors", | ||
"bg-surface-base", | ||
"text-text-em-high", | ||
"focus-ring", | ||
"hover:bg-bg-alt", | ||
"data-[state=on]:bg-primary-default", | ||
"data-[state=on]:text-text-on-primary", | ||
], | ||
}, | ||
variants: { | ||
size: { | ||
sm: { | ||
item: ["h-8", "text-sm"], | ||
}, | ||
md: { | ||
item: ["h-10", "text-base"], | ||
}, | ||
}, | ||
}, | ||
}); | ||
export type ToggleGroupVariants = VariantProps<typeof toggleGroupTheme>; |
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 @@ | ||
"use client"; | ||
|
||
import * as React from "react"; | ||
import * as ToggleGroupPrimitive from "@radix-ui/react-toggle-group"; | ||
|
||
import { | ||
toggleGroupTheme, | ||
type ToggleGroupVariants, | ||
} from "./ToggleGroup.theme"; | ||
|
||
const ToggleGroupContext = React.createContext<ToggleGroupVariants>({ | ||
size: "md", | ||
}); | ||
|
||
const ToggleGroupRoot = React.forwardRef< | ||
React.ElementRef<typeof ToggleGroupPrimitive.Root>, | ||
React.ComponentPropsWithoutRef<typeof ToggleGroupPrimitive.Root> & | ||
ToggleGroupVariants | ||
>(({ className, size = "md", children, ...props }, ref) => { | ||
const { root } = toggleGroupTheme(); | ||
return ( | ||
<ToggleGroupContext.Provider value={{ size }}> | ||
<ToggleGroupPrimitive.Root | ||
ref={ref} | ||
className={root({ className, size })} | ||
{...props} | ||
> | ||
{children} | ||
</ToggleGroupPrimitive.Root> | ||
</ToggleGroupContext.Provider> | ||
); | ||
}); | ||
ToggleGroupRoot.displayName = ToggleGroupPrimitive.Root.displayName; | ||
|
||
const ToggleGroupItem = React.forwardRef< | ||
React.ElementRef<typeof ToggleGroupPrimitive.Item>, | ||
React.ComponentPropsWithoutRef<typeof ToggleGroupPrimitive.Item> & | ||
ToggleGroupVariants | ||
>(({ className, children, size = "md", ...props }, ref) => { | ||
const context = React.useContext(ToggleGroupContext); | ||
const { item } = toggleGroupTheme(); | ||
return ( | ||
<ToggleGroupPrimitive.Item | ||
ref={ref} | ||
className={item({ className, size: context.size || size })} | ||
{...props} | ||
> | ||
{children} | ||
</ToggleGroupPrimitive.Item> | ||
); | ||
}); | ||
ToggleGroupItem.displayName = ToggleGroupPrimitive.Item.displayName; | ||
|
||
export const ToggleGroup = Object.assign({}, ToggleGroupRoot, { | ||
Item: ToggleGroupItem, | ||
}); |
Empty file.
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