From 65e043904aa9e2d53fddddc3ffc4cc6a0a36a3f1 Mon Sep 17 00:00:00 2001 From: Vitaliy Malyshev Date: Sun, 30 Jun 2024 22:01:09 -0500 Subject: [PATCH] feat: adding size property to the Switch component --- .changeset/poor-singers-dress.md | 6 +++ apps/website/docs/inputs/switch.mdx | 10 +++++ .../src/components/Switch/Switch.styles.ts | 39 +++++++++++++++++-- .../react/src/components/Switch/Switch.tsx | 20 +++++++++- .../Switch/story/Switch.stories.tsx | 7 ++++ 5 files changed, 76 insertions(+), 6 deletions(-) create mode 100644 .changeset/poor-singers-dress.md diff --git a/.changeset/poor-singers-dress.md b/.changeset/poor-singers-dress.md new file mode 100644 index 000000000..35df7ff07 --- /dev/null +++ b/.changeset/poor-singers-dress.md @@ -0,0 +1,6 @@ +--- +'website': minor +'@project44-manifest/react': minor +--- + +switch component size property diff --git a/apps/website/docs/inputs/switch.mdx b/apps/website/docs/inputs/switch.mdx index 4e7238345..36c15f72a 100644 --- a/apps/website/docs/inputs/switch.mdx +++ b/apps/website/docs/inputs/switch.mdx @@ -21,6 +21,16 @@ Pass in a `string` as a child to render a label for the switch. Label ``` +## Size + +### Small + +Set the component size to `small` + +```jsx live +Label +``` + ### Controlled A switch's state can be controlled by a parent React component by setting the `isSelected` prop and diff --git a/packages/react/src/components/Switch/Switch.styles.ts b/packages/react/src/components/Switch/Switch.styles.ts index fd2279c04..e89ccc610 100644 --- a/packages/react/src/components/Switch/Switch.styles.ts +++ b/packages/react/src/components/Switch/Switch.styles.ts @@ -18,21 +18,19 @@ export const useStyles = css({ color: '$palette-white', cursor: 'pointer', display: 'inline-flex', - height: pxToRem(24), margin: 0, padding: 0, position: 'relative', - width: pxToRem(44), + transition: 'background 200ms cubic-bezier(0.4, 0.14, 0.3, 1) 0ms', }, '.manifest-switch__indicator': { backgroundColor: '$palette-white', borderRadius: '$full', display: 'block', - size: pxToRem(18), transform: 'translateX(3px)', - transition: '$transform', willChange: 'transform', + transition: 'transform 200ms cubic-bezier(0.4, 0.14, 0.3, 1) 0ms', }, '.manifest-switch__input': { @@ -52,6 +50,26 @@ export const useStyles = css({ }, variants: { + size: { + medium: { + '.manifest-switch__control': { + height: pxToRem(24), + width: pxToRem(44), + }, + '.manifest-switch__indicator': { + size: pxToRem(18), + }, + }, + small: { + '.manifest-switch__control': { + height: pxToRem(16), + width: pxToRem(30), + }, + '.manifest-switch__indicator': { + size: pxToRem(12), + }, + }, + }, isChecked: { true: { $$switchBackgroundColor: '$colors$primary-default', @@ -113,5 +131,18 @@ export const useStyles = css({ $$switchBackgroundColor: '$colors$primary-active', }, }, + { + isChecked: true, + size: 'small', + css: { + '.manifest-switch__indicator': { + transform: 'translateX(15px)', + }, + }, + }, ], + + defaultVariants: { + size: 'medium', + }, }); diff --git a/packages/react/src/components/Switch/Switch.tsx b/packages/react/src/components/Switch/Switch.tsx index 9e76f878a..d7eb1408c 100644 --- a/packages/react/src/components/Switch/Switch.tsx +++ b/packages/react/src/components/Switch/Switch.tsx @@ -12,8 +12,18 @@ import { Typography } from '../Typography'; import { useStyles } from './Switch.styles'; export type SwitchElement = 'label'; -export type SwitchOptions = AriaSwitchProps & Options & StyleProps; export type SwitchProps = Props>; +export interface SwitchOptions + extends AriaSwitchProps, + Options, + StyleProps { + /** + * The size of the component + * + * @default medium + */ + size?: 'medium' | 'small'; +} export const Switch = createComponent((props, forwardedRef) => { const { @@ -23,6 +33,7 @@ export const Switch = createComponent((props, forwardedRef) => { className: classNameProp, css, isDisabled, + size = 'medium', } = props; const inputRef = React.useRef(null); @@ -38,6 +49,7 @@ export const Switch = createComponent((props, forwardedRef) => { const { className } = useStyles({ css, + size, isChecked, isDisabled, isFocusVisible, @@ -49,6 +61,7 @@ export const Switch = createComponent((props, forwardedRef) => { 'manifest-switch': true, 'manifest-switch--checked': isChecked, 'manifest-switch--disabled': isDisabled, + [`manifest-switch--${size}`]: size, }); return ( @@ -64,7 +77,10 @@ export const Switch = createComponent((props, forwardedRef) => { {children && ( - + {children} )} diff --git a/packages/react/src/components/Switch/story/Switch.stories.tsx b/packages/react/src/components/Switch/story/Switch.stories.tsx index 04a494663..67ce63fee 100644 --- a/packages/react/src/components/Switch/story/Switch.stories.tsx +++ b/packages/react/src/components/Switch/story/Switch.stories.tsx @@ -15,6 +15,13 @@ Default.args = { children: 'Switch', }; +export const Small = Template.bind({}); + +Small.args = { + children: 'Switch', + size: 'small', +}; + export const Disabled = Template.bind({}); Disabled.args = {