-
-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #974 from tszhong0411/pack-33-add-toggle-group
Add Toggle Group
- Loading branch information
Showing
13 changed files
with
261 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@tszhong0411/ui': patch | ||
--- | ||
|
||
Add Toggle Group |
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,46 @@ | ||
--- | ||
title: Toggle Group | ||
description: A set of two-state buttons that can be toggled on or off. | ||
--- | ||
|
||
<ComponentPreview name='toggle-group/toggle-group' /> | ||
|
||
## Usage | ||
|
||
```tsx | ||
import { ToggleGroup, ToggleGroupItem } from '@tszhong0411/ui' | ||
``` | ||
|
||
```tsx | ||
<ToggleGroup type='single'> | ||
<ToggleGroupItem value='a'>A</ToggleGroupItem> | ||
<ToggleGroupItem value='b'>B</ToggleGroupItem> | ||
<ToggleGroupItem value='c'>C</ToggleGroupItem> | ||
</ToggleGroup> | ||
``` | ||
|
||
## Examples | ||
|
||
### Default | ||
|
||
<ComponentPreview name='toggle-group/toggle-group' /> | ||
|
||
### Outline | ||
|
||
<ComponentPreview name='toggle-group/outline' /> | ||
|
||
### Single | ||
|
||
<ComponentPreview name='toggle-group/single' /> | ||
|
||
### Small | ||
|
||
<ComponentPreview name='toggle-group/small' /> | ||
|
||
### Large | ||
|
||
<ComponentPreview name='toggle-group/large' /> | ||
|
||
### Disabled | ||
|
||
<ComponentPreview name='toggle-group/disabled' /> |
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,20 @@ | ||
import { ToggleGroup, ToggleGroupItem } from '@tszhong0411/ui' | ||
import { BoldIcon, ItalicIcon, UnderlineIcon } from 'lucide-react' | ||
|
||
const ToggleGroupDisabledDemo = () => { | ||
return ( | ||
<ToggleGroup type='multiple' disabled> | ||
<ToggleGroupItem value='bold' aria-label='Toggle bold'> | ||
<BoldIcon className='size-4' /> | ||
</ToggleGroupItem> | ||
<ToggleGroupItem value='italic' aria-label='Toggle italic'> | ||
<ItalicIcon className='size-4' /> | ||
</ToggleGroupItem> | ||
<ToggleGroupItem value='underline' aria-label='Toggle underline'> | ||
<UnderlineIcon className='size-4' /> | ||
</ToggleGroupItem> | ||
</ToggleGroup> | ||
) | ||
} | ||
|
||
export default ToggleGroupDisabledDemo |
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,20 @@ | ||
import { ToggleGroup, ToggleGroupItem } from '@tszhong0411/ui' | ||
import { BoldIcon, ItalicIcon, UnderlineIcon } from 'lucide-react' | ||
|
||
const ToggleGroupLargeDemo = () => { | ||
return ( | ||
<ToggleGroup size='lg' type='multiple'> | ||
<ToggleGroupItem value='bold' aria-label='Toggle bold'> | ||
<BoldIcon className='size-4' /> | ||
</ToggleGroupItem> | ||
<ToggleGroupItem value='italic' aria-label='Toggle italic'> | ||
<ItalicIcon className='size-4' /> | ||
</ToggleGroupItem> | ||
<ToggleGroupItem value='underline' aria-label='Toggle underline'> | ||
<UnderlineIcon className='size-4' /> | ||
</ToggleGroupItem> | ||
</ToggleGroup> | ||
) | ||
} | ||
|
||
export default ToggleGroupLargeDemo |
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,20 @@ | ||
import { ToggleGroup, ToggleGroupItem } from '@tszhong0411/ui' | ||
import { BoldIcon, ItalicIcon, UnderlineIcon } from 'lucide-react' | ||
|
||
const ToggleGroupOutlineDemo = () => { | ||
return ( | ||
<ToggleGroup variant='outline' type='multiple'> | ||
<ToggleGroupItem value='bold' aria-label='Toggle bold'> | ||
<BoldIcon className='size-4' /> | ||
</ToggleGroupItem> | ||
<ToggleGroupItem value='italic' aria-label='Toggle italic'> | ||
<ItalicIcon className='size-4' /> | ||
</ToggleGroupItem> | ||
<ToggleGroupItem value='underline' aria-label='Toggle underline'> | ||
<UnderlineIcon className='size-4' /> | ||
</ToggleGroupItem> | ||
</ToggleGroup> | ||
) | ||
} | ||
|
||
export default ToggleGroupOutlineDemo |
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,20 @@ | ||
import { ToggleGroup, ToggleGroupItem } from '@tszhong0411/ui' | ||
import { BoldIcon, ItalicIcon, UnderlineIcon } from 'lucide-react' | ||
|
||
const ToggleGroupSingleDemo = () => { | ||
return ( | ||
<ToggleGroup type='single'> | ||
<ToggleGroupItem value='bold' aria-label='Toggle bold'> | ||
<BoldIcon className='size-4' /> | ||
</ToggleGroupItem> | ||
<ToggleGroupItem value='italic' aria-label='Toggle italic'> | ||
<ItalicIcon className='size-4' /> | ||
</ToggleGroupItem> | ||
<ToggleGroupItem value='underline' aria-label='Toggle underline'> | ||
<UnderlineIcon className='size-4' /> | ||
</ToggleGroupItem> | ||
</ToggleGroup> | ||
) | ||
} | ||
|
||
export default ToggleGroupSingleDemo |
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,20 @@ | ||
import { ToggleGroup, ToggleGroupItem } from '@tszhong0411/ui' | ||
import { BoldIcon, ItalicIcon, UnderlineIcon } from 'lucide-react' | ||
|
||
const ToggleGroupSmallDemo = () => { | ||
return ( | ||
<ToggleGroup size='sm' type='multiple'> | ||
<ToggleGroupItem value='bold' aria-label='Toggle bold'> | ||
<BoldIcon className='size-4' /> | ||
</ToggleGroupItem> | ||
<ToggleGroupItem value='italic' aria-label='Toggle italic'> | ||
<ItalicIcon className='size-4' /> | ||
</ToggleGroupItem> | ||
<ToggleGroupItem value='underline' aria-label='Toggle underline'> | ||
<UnderlineIcon className='size-4' /> | ||
</ToggleGroupItem> | ||
</ToggleGroup> | ||
) | ||
} | ||
|
||
export default ToggleGroupSmallDemo |
20 changes: 20 additions & 0 deletions
20
apps/docs/src/components/demos/toggle-group/toggle-group.tsx
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,20 @@ | ||
import { ToggleGroup, ToggleGroupItem } from '@tszhong0411/ui' | ||
import { BoldIcon, ItalicIcon, UnderlineIcon } from 'lucide-react' | ||
|
||
const ToggleGroupDemo = () => { | ||
return ( | ||
<ToggleGroup type='multiple'> | ||
<ToggleGroupItem value='bold' aria-label='Toggle bold'> | ||
<BoldIcon className='size-4' /> | ||
</ToggleGroupItem> | ||
<ToggleGroupItem value='italic' aria-label='Toggle italic'> | ||
<ItalicIcon className='size-4' /> | ||
</ToggleGroupItem> | ||
<ToggleGroupItem value='underline' aria-label='Toggle underline'> | ||
<UnderlineIcon className='size-4' /> | ||
</ToggleGroupItem> | ||
</ToggleGroup> | ||
) | ||
} | ||
|
||
export default ToggleGroupDemo |
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
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,53 @@ | ||
'use client' | ||
|
||
import * as ToggleGroupPrimitive from '@radix-ui/react-toggle-group' | ||
import { cn } from '@tszhong0411/utils' | ||
import { type VariantProps } from 'class-variance-authority' | ||
import { createContext, useContext } from 'react' | ||
|
||
import { toggleVariants } from './toggle' | ||
|
||
const ToggleGroupContext = createContext<VariantProps<typeof toggleVariants>>({ | ||
size: 'default', | ||
variant: 'default' | ||
}) | ||
|
||
type ToggleGroupProps = React.ComponentProps<typeof ToggleGroupPrimitive.Root> & | ||
VariantProps<typeof toggleVariants> | ||
|
||
export const ToggleGroup = (props: ToggleGroupProps) => { | ||
const { className, variant, size, children, ...rest } = props | ||
|
||
return ( | ||
<ToggleGroupPrimitive.Root | ||
className={cn('flex items-center justify-center gap-1', className)} | ||
{...rest} | ||
> | ||
<ToggleGroupContext value={{ variant, size }}>{children}</ToggleGroupContext> | ||
</ToggleGroupPrimitive.Root> | ||
) | ||
} | ||
|
||
type ToggleGroupItemProps = React.ComponentProps<typeof ToggleGroupPrimitive.Item> & | ||
VariantProps<typeof toggleVariants> | ||
|
||
export const ToggleGroupItem = (props: ToggleGroupItemProps) => { | ||
const { className, children, variant, size, ...rest } = props | ||
|
||
const context = useContext(ToggleGroupContext) | ||
|
||
return ( | ||
<ToggleGroupPrimitive.Item | ||
className={cn( | ||
toggleVariants({ | ||
variant: context.variant ?? variant, | ||
size: context.size ?? size | ||
}), | ||
className | ||
)} | ||
{...rest} | ||
> | ||
{children} | ||
</ToggleGroupPrimitive.Item> | ||
) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.