-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e731361
commit 10c4c40
Showing
5 changed files
with
102 additions
and
2 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
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 @@ | ||
import type { Meta, StoryObj } from '@storybook/react'; | ||
import { useState } from 'react'; | ||
|
||
import Switch from './Switch'; | ||
|
||
const SwitchWithHooks = () => { | ||
const [isEnabled, setIsEnabled] = useState(false); | ||
|
||
return <Switch value={isEnabled} onChange={setIsEnabled} />; | ||
}; | ||
|
||
const meta: Meta<typeof Switch> = { | ||
title: 'Components/Switch', | ||
component: Switch, | ||
tags: ['autodocs'], | ||
parameters: { | ||
layout: 'centered', | ||
}, | ||
}; | ||
|
||
export default meta; | ||
type Story = StoryObj<typeof Switch>; | ||
|
||
export const Default: Story = { | ||
args: {}, | ||
render: args => { | ||
const [isEnabled, setIsEnabled] = useState(args.value); | ||
|
||
return <Switch value={isEnabled} onChange={setIsEnabled} />; | ||
}, | ||
}; | ||
|
||
export const ToggleOn: Story = { | ||
args: { | ||
value: true, | ||
}, | ||
render: args => { | ||
const [isEnabled, setIsEnabled] = useState(args.value); | ||
|
||
return <Switch value={isEnabled} onChange={setIsEnabled} />; | ||
}, | ||
}; | ||
|
||
export const ToggleOff: Story = { | ||
args: { | ||
value: false, | ||
}, | ||
render: args => { | ||
const [isEnabled, setIsEnabled] = useState(args.value); | ||
|
||
return <Switch value={isEnabled} onChange={setIsEnabled} />; | ||
}, | ||
}; |
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,40 @@ | ||
import React from 'react'; | ||
import { cn } from '@/lib/utils'; | ||
|
||
type SwitchProps = { | ||
/** | ||
* value of the toggle switch | ||
*/ | ||
value: boolean; | ||
/** | ||
* change handler for the switch | ||
*/ | ||
onChange: (value: boolean) => void; | ||
}; | ||
|
||
const Switch = (props: SwitchProps) => { | ||
const { value = false, onChange } = props; | ||
|
||
return ( | ||
<button | ||
className={cn( | ||
'box-content cursor-pointer', | ||
'w-[60px] h-[30px] rounded-[1000px] p-1 ', | ||
value ? 'bg-blue-500' : 'bg-gray-300' | ||
)} | ||
style={{ filter: 'drop-shadow( 2px 4px 8px hsl(0deg 0% 0% / 0.15))' }} | ||
onClick={() => onChange(!value)} | ||
> | ||
<span | ||
className="block h-full aspect-square rounded-full bg-white" | ||
style={{ | ||
transition: 'transform 300ms', | ||
transform: `translateX(${value ? '100%' : '0%'})`, | ||
boxShadow: '2px 4px 8px hsl(0deg 0% 0% / 0.1)', | ||
}} | ||
/> | ||
</button> | ||
); | ||
}; | ||
|
||
export default Switch; |
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 @@ | ||
export { default } from './Switch'; |