From 10b3b77cc90f20600ca1874139f68b6d57abcfc4 Mon Sep 17 00:00:00 2001 From: John Oshalusi Date: Thu, 30 Oct 2025 20:32:11 +0100 Subject: [PATCH] feat: add icon to toggle switch --- .../toggle-switch/toggle-switch.component.tsx | 10 ++++++ .../toggle-switch/toggle-switch.css.ts | 29 ++++++++++++++++ .../toggle-switch/toggle-switch.stories.tsx | 33 +++++++++++++++++++ src/icons/MoonComponent.tsx | 9 +++++ src/icons/SunComponent.tsx | 17 ++++++++++ src/icons/index.ts | 2 ++ 6 files changed, 100 insertions(+) create mode 100644 src/icons/MoonComponent.tsx create mode 100644 src/icons/SunComponent.tsx diff --git a/src/design-system/toggle-switch/toggle-switch.component.tsx b/src/design-system/toggle-switch/toggle-switch.component.tsx index 2f3a690..5bc1e56 100644 --- a/src/design-system/toggle-switch/toggle-switch.component.tsx +++ b/src/design-system/toggle-switch/toggle-switch.component.tsx @@ -22,6 +22,8 @@ type Props = Omit< required?: boolean; id?: string; testId?: string; + checkedChildren?: ReactNode; + unCheckedChildren?: ReactNode; }; export const ToggleSwitch = ({ @@ -30,6 +32,9 @@ export const ToggleSwitch = ({ icon, disabled, testId, + checkedChildren, + unCheckedChildren, + checked, ...props }: Readonly): JSX.Element => { return ( @@ -51,12 +56,17 @@ export const ToggleSwitch = ({ )} + {checked && {checkedChildren}} + {!checked && ( + {unCheckedChildren} + )} ); diff --git a/src/design-system/toggle-switch/toggle-switch.css.ts b/src/design-system/toggle-switch/toggle-switch.css.ts index 994f90d..4c18277 100644 --- a/src/design-system/toggle-switch/toggle-switch.css.ts +++ b/src/design-system/toggle-switch/toggle-switch.css.ts @@ -87,3 +87,32 @@ export const iconContainer = style([ overflow: 'hidden', }, ]); + +const icon = style([ + sx({ + display: 'flex', + alignItems: 'center', + justifyContent: 'center', + }), + { + position: 'absolute', + top: '50%', + transform: 'translateY(-50%)', + pointerEvents: 'none', + transition: 'opacity 0.2s', + }, +]); + +export const checkedIcon = style([ + icon, + { + left: '4px', + }, +]); + +export const uncheckedIcon = style([ + icon, + { + right: '2px', + }, +]); diff --git a/src/design-system/toggle-switch/toggle-switch.stories.tsx b/src/design-system/toggle-switch/toggle-switch.stories.tsx index b02bf03..660d51f 100644 --- a/src/design-system/toggle-switch/toggle-switch.stories.tsx +++ b/src/design-system/toggle-switch/toggle-switch.stories.tsx @@ -4,6 +4,7 @@ import InfoIcon from '@icons/InfoComponent'; import type { Meta } from '@storybook/react'; import { ThemeColorScheme, LocalThemeProvider } from '../../design-tokens'; +import { Moon, Sun } from '../../icons'; import { page, Variants, Section, UIStateTable } from '../decorators'; import { Divider } from '../divider'; import { Grid, Cell } from '../grid'; @@ -12,6 +13,25 @@ import { ToggleSwitch } from './toggle-switch.component'; const subtitle = `The toggle switch represents a physical switch that allows users to turn things on or off. Use toggle switch controls to present users with two mutually exclusive options (such as on/off), where choosing an option provides immediate results.`; +const WithCheckedAndUncheckedChildren = (): JSX.Element => { + const [checked, setChecked] = React.useState(false); + + return ( + + + } + unCheckedChildren={} + /> + + + ); +}; + export default { title: 'Basic Input/Toggle switch', component: ToggleSwitch, @@ -204,6 +224,19 @@ export const Overview = (): JSX.Element => ( + + + +
+ + + + + + + + +
); diff --git a/src/icons/MoonComponent.tsx b/src/icons/MoonComponent.tsx new file mode 100644 index 0000000..1974b38 --- /dev/null +++ b/src/icons/MoonComponent.tsx @@ -0,0 +1,9 @@ +import React, { type SVGProps } from 'react'; + +const SvgMoonComponent = (props: SVGProps) => ( + + + +); + +export default SvgMoonComponent; diff --git a/src/icons/SunComponent.tsx b/src/icons/SunComponent.tsx new file mode 100644 index 0000000..31ef2aa --- /dev/null +++ b/src/icons/SunComponent.tsx @@ -0,0 +1,17 @@ +import type { SVGProps } from 'react'; + +const SvgSunComponent = (props: SVGProps) => ( + + + + + + + + + + + +); + +export default SvgSunComponent; diff --git a/src/icons/index.ts b/src/icons/index.ts index 217efb7..f9b48e2 100644 --- a/src/icons/index.ts +++ b/src/icons/index.ts @@ -79,3 +79,5 @@ export { default as WalletComponent } from './WalletComponent'; export { default as WarningIconCircleComponent } from './WarningIconCircleComponent'; export { default as WarningIconTriangleSolidComponent } from './WarningIconTriangleSolidComponent'; export { default as WarningIconTriangleComponent } from './WarningIconTriangleComponent'; +export { default as Sun } from './SunComponent'; +export { default as Moon } from './MoonComponent';