-
Notifications
You must be signed in to change notification settings - Fork 11
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 #1599 from public-convenience-ltd/add-switch-to-de…
…sign-system Add switch to design system
- Loading branch information
Showing
13 changed files
with
99 additions
and
84 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
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,31 @@ | ||
.switch { | ||
--switch-height: 16px; | ||
--switch-width: 27px; | ||
--switch-thumb-padding: 1px; | ||
--switch-thumb-size: calc(var(--switch-height) - var(--switch-thumb-padding) * 2); | ||
|
||
position: relative; | ||
background-color: var(--color-blue); | ||
border-radius: calc(var(--switch-height) + var(--switch-thumb-padding) * 2); | ||
width: var(--switch-width); | ||
height: var(--switch-height); | ||
} | ||
|
||
.switch[aria-checked=true] { | ||
background-color: var(--color-pink); | ||
} | ||
|
||
.switch .switch-thumb { | ||
transition: left 0.2s ease; | ||
position:absolute; | ||
width: var(--switch-thumb-size); | ||
height: var(--switch-thumb-size); | ||
top: var(--switch-thumb-padding); | ||
left: var(--switch-thumb-padding); | ||
border-radius: 50%; | ||
background-color: white; | ||
} | ||
|
||
.switch[aria-checked=true] .switch-thumb { | ||
left: calc(100% - var(--switch-thumb-size) - var(--switch-thumb-padding)); | ||
} |
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 { Meta, StoryObj } from '@storybook/react'; | ||
|
||
import Switch from './Switch'; | ||
import { useState } from 'react'; | ||
|
||
const meta: Meta<typeof Switch> = { | ||
title: 'Design-System/Form Elements/Switch', | ||
component: Switch, | ||
}; | ||
|
||
export default meta; | ||
|
||
const SwitchWithState = () => { | ||
const [checked, setChecked] = useState(false); | ||
return <Switch onChange={setChecked} checked={checked} />; | ||
}; | ||
|
||
export const Primary: StoryObj<typeof Switch> = { | ||
render: () => <SwitchWithState />, | ||
}; |
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,38 @@ | ||
import { ComponentPropsWithoutRef, forwardRef } from 'react'; | ||
|
||
interface SwitchProps | ||
extends Omit<ComponentPropsWithoutRef<'button'>, 'onClick' | 'onChange'> { | ||
/** Determines whether the switch is on */ | ||
checked: boolean; | ||
onClick?: () => void; | ||
onChange?: (checked: boolean) => void; | ||
} | ||
|
||
const Switch = forwardRef<HTMLButtonElement, SwitchProps>( | ||
({ checked, onClick, onChange, ...props }, ref) => { | ||
return ( | ||
<button | ||
type="button" | ||
role="switch" | ||
ref={ref} | ||
{...props} | ||
className="switch" | ||
aria-checked={checked} | ||
onClick={() => { | ||
if (onClick) { | ||
onClick(); | ||
} | ||
if (onChange) { | ||
onChange(!checked); | ||
} | ||
}} | ||
> | ||
<span className="switch-thumb" /> | ||
</button> | ||
); | ||
}, | ||
); | ||
|
||
Switch.displayName = 'Switch'; | ||
|
||
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'; |
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
8074a19
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
toiletmap – ./
toiletmap.org.uk
toiletmap-public-convenience-ltd.vercel.app
www.toiletmap.org.uk
*.toiletmap.org.uk
toiletmap-git-main-public-convenience-ltd.vercel.app