Skip to content

Commit

Permalink
add switch input
Browse files Browse the repository at this point in the history
  • Loading branch information
vassbence committed Jul 12, 2024
1 parent d0e4408 commit 5234ddd
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 1 deletion.
22 changes: 22 additions & 0 deletions src/components/SwitchInput/SwitchInput.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import React, { useState } from 'react'
import { SwitchInput } from './index.js'

export default {
title: 'Components/SwitchInput',
component: SwitchInput,
}

export const Default = () => {
const [value, setValue] = useState(false)
return <SwitchInput value={value} onChange={setValue} />
}

export const Error = () => {
const [value, setValue] = useState(true)
return <SwitchInput error value={value} onChange={setValue} />
}

export const Disabled = () => {
const [value, setValue] = useState(true)
return <SwitchInput value={value} onChange={setValue} disabled />
}
67 changes: 67 additions & 0 deletions src/components/SwitchInput/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { borderRadius } from '../../utils/border.js'
import { color } from '../../utils/colors.js'

type SwitchInputProps = {
value: boolean
onChange: (value: boolean) => void
disabled?: boolean
error?: boolean
}

function SwitchInput({ value, onChange, disabled, error }: SwitchInputProps) {
return (
<div
style={{
width: 24,
height: 16,
borderRadius: borderRadius('full'),
border: `1px solid ${color('neutral-20-adjusted')}`,
background: color('neutral-10-adjusted'),
...(value && {
border: `1px solid ${color('neutral-100')}`,
background: color('neutral-100'),
}),
...(error && {
border: `1px solid ${color('destructive-100')}`,
background: color('neutral-10-adjusted'),
...(value && {
border: `1px solid ${color('destructive-100')}`,
background: color('destructive-100'),
}),
}),
...(disabled && {
border: `1px solid transparent`,
background: color('neutral-20'),
}),
}}
tabIndex={0}
onClick={() => {
if (disabled) return

onChange(!value)
}}
>
<div
style={{
width: 14,
height: 14,
borderRadius: borderRadius('full'),
background: color('neutral-100'),
...(value && {
marginLeft: 'auto',
background: color('neutral-inverted-100'),
}),
...(error && {
background: color('neutral-100'),
}),
...(disabled && {
background: color('neutral-inverted-80'),
}),
}}
/>
</div>
)
}

export type { SwitchInputProps }
export { SwitchInput }
1 change: 0 additions & 1 deletion src/components/TextInput/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,6 @@ function TextInput({
bottom: -5,
padding: '4px 6px',
borderRadius: borderRadius(4),

background: color('neutral-100'),
color: color('neutral-inverted-100'),
transform: 'translateY(100%)',
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ export * from './components/Icon/index.js'
export * from './components/Button/index.js'
export * from './components/IconButton/index.js'
export * from './components/TextInput/index.js'
export * from './components/SwitchInput/index.js'
export * from './utils/colors.js'
export * from './utils/border.js'

0 comments on commit 5234ddd

Please sign in to comment.