Skip to content

Commit

Permalink
Add switch ui component
Browse files Browse the repository at this point in the history
  • Loading branch information
evdmatvey committed Jul 15, 2024
1 parent 9c8b959 commit 7892101
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/shared/ui/Switch/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from './ui/Switch';
43 changes: 43 additions & 0 deletions src/shared/ui/Switch/ui/Switch.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
.root {
width: 44px;
height: 24px;
margin: 0;
vertical-align: top;
background: var(--gray-300);
border-radius: 30px;
outline: none;
cursor: pointer;
appearance: none;
position: relative;
transition: all 0.3s cubic-bezier(0.23, 0.79, 0.35, 1.3);
}

[data-theme='dark'] .root {
background: var(--gray-600);
}

.root::after {
content: '';
width: 19px;
height: 19px;
left: 3px;
top: 50%;
position: absolute;
background-color: var(--white-100);
border-radius: 50%;
transform: translateX(0) translateY(-50%);
transition: all 0.3s cubic-bezier(0.23, 0.79, 0.35, 1.3);
}

.root:checked::after {
transform: translateX(18px) translateY(-50%);
}

.root:checked {
background-color: var(--success-main);
}

.root:disabled {
opacity: 0.5;
cursor: auto;
}
10 changes: 10 additions & 0 deletions src/shared/ui/Switch/ui/Switch.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { ComponentProps } from 'react';
import styles from './Switch.module.css';

interface SwitchProps extends Omit<ComponentProps<'input'>, 'type'> {}

const Switch = (props: SwitchProps) => {
return <input className={styles.root} type="checkbox" {...props} />;
};

export default Switch;
77 changes: 77 additions & 0 deletions src/shared/ui/Switch/ui/stories/Switch.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import type { Meta, StoryObj } from '@storybook/react';
import Switch from '../Switch';

const meta: Meta<typeof Switch> = {
component: Switch,
title: 'Components/Switch',
tags: ['autodocs'],
parameters: {
docs: {
subtitle: 'Switch component that includes all cases in our design layout',
},
},
argTypes: {},
};

export default meta;
type Story = StoryObj<typeof Switch>;

export const Default: Story = {
name: 'Switch',
args: {},
parameters: {
docs: {
description: {
story:
'You should use the option that is specified in the design layout',
},
},
},
render: () => (
<div
style={{
width: '50px',
display: 'flex',
gap: '10px',
flexDirection: 'column',
}}
>
<Switch />
<Switch checked />
<Switch disabled />
<Switch disabled checked />
</div>
),
};

export const DarkMode: Story = {
name: 'Switch on dark mode',
args: {},
parameters: {
docs: {
description: {
story:
'You can see what the Switch component looks like with dark mode',
},
},
backgrounds: {
default: 'dark',
},
},
render: () => (
<div
data-theme="dark"
style={{
width: '50px',
display: 'flex',
gap: '10px',
flexDirection: 'column',
}}
>
<Switch />
<Switch checked />
<Switch disabled />
<Switch disabled checked />
</div>
),
};

0 comments on commit 7892101

Please sign in to comment.