@@ -2,23 +2,21 @@ import type {MouseEventHandler} from 'react'
22import React , { useCallback , useEffect } from 'react'
33import styled , { css } from 'styled-components'
44import { variant } from 'styled-system'
5- import Box from '../Box '
5+ import { clsx } from 'clsx '
66import Spinner from '../Spinner'
7- import Text from '../Text'
87import { get } from '../constants'
98import { useProvidedStateOrCreate , useId } from '../hooks'
10- import type { BetterSystemStyleObject , SxProp } from '../sx'
11- import sx from '../sx'
129import getGlobalFocusStyles from '../internal/utils/getGlobalFocusStyles'
1310import VisuallyHidden from '../_VisuallyHidden'
1411import type { CellAlignment } from '../DataTable/column'
1512import { AriaStatus } from '../live-region'
1613import useSafeTimeout from '../hooks/useSafeTimeout'
14+ import classes from './ToggleSwitch.module.css'
1715
1816const TRANSITION_DURATION = '80ms'
1917const EASE_OUT_QUAD_CURVE = 'cubic-bezier(0.5, 1, 0.89, 1)'
2018
21- export interface ToggleSwitchProps extends Omit < React . HTMLAttributes < HTMLDivElement > , 'onChange' > , SxProp {
19+ export interface ToggleSwitchProps extends Omit < React . HTMLAttributes < HTMLDivElement > , 'onChange' > {
2220 /** The id of the DOM node that labels the switch */
2321 [ 'aria-labelledby' ] : string
2422 /** Uncontrolled - whether the switch is turned on */
@@ -67,7 +65,7 @@ type SwitchButtonProps = {
6765 disabled ?: boolean
6866 checked ?: boolean
6967 size ?: ToggleSwitchProps [ 'size' ]
70- } & SxProp
68+ }
7169
7270type InnerIconProps = { size ?: ToggleSwitchProps [ 'size' ] }
7371
@@ -188,7 +186,6 @@ const SwitchButton = styled.button<SwitchButtonProps>`
188186 }
189187 } }
190188
191- ${ sx }
192189 ${ sizeVariants }
193190`
194191const ToggleKnob = styled . div < { checked ?: boolean ; 'aria-disabled' : React . AriaAttributes [ 'aria-disabled' ] } > `
@@ -231,11 +228,6 @@ const ToggleKnob = styled.div<{checked?: boolean; 'aria-disabled': React.AriaAtt
231228 } }
232229`
233230
234- const hiddenTextStyles : BetterSystemStyleObject = {
235- visibility : 'hidden' ,
236- height : 0 ,
237- }
238-
239231const ToggleSwitch = React . forwardRef < HTMLButtonElement , React . PropsWithChildren < ToggleSwitchProps > > (
240232 function ToggleSwitch ( props , ref ) {
241233 const {
@@ -252,7 +244,7 @@ const ToggleSwitch = React.forwardRef<HTMLButtonElement, React.PropsWithChildren
252244 statusLabelPosition = 'start' ,
253245 loadingLabelDelay = 2000 ,
254246 loadingLabel = 'Loading' ,
255- sx : sxProp ,
247+ className ,
256248 ...rest
257249 } = props
258250 const isControlled = typeof checked !== 'undefined'
@@ -296,35 +288,28 @@ const ToggleSwitch = React.forwardRef<HTMLButtonElement, React.PropsWithChildren
296288 if ( ariaDescribedby ) switchButtonDescribedBy = `${ switchButtonDescribedBy } ${ ariaDescribedby } `
297289
298290 return (
299- < Box
300- display = "inline-flex"
301- alignItems = "center"
302- flexDirection = { statusLabelPosition === 'start' ? 'row' : 'row-reverse' }
303- sx = { sxProp }
304- { ...rest }
305- >
291+ < div className = { clsx ( classes . ToggleSwitch , className ) } data-status-label-position = { statusLabelPosition } { ...rest } >
306292 < VisuallyHidden >
307293 < AriaStatus announceOnShow id = { loadingLabelId } >
308294 { isLoadingLabelVisible && loadingLabel }
309295 </ AriaStatus >
310296 </ VisuallyHidden >
311297
312298 { loading ? < Spinner size = "small" srText = { null } /> : null }
313- < Text
314- color = { acceptsInteraction ? 'fg.default' : 'fg.muted' }
315- fontSize = { size === 'small' ? 0 : 1 }
316- mx = { 2 }
299+ < span
300+ className = { classes . StatusText }
301+ data-size = { size }
302+ data-disabled = { ! acceptsInteraction }
317303 aria-hidden = "true"
318- sx = { { position : 'relative' , cursor : acceptsInteraction ? 'pointer' : 'not-allowed' } }
319304 onClick = { handleToggleClick }
320305 >
321- < Box textAlign = "right" sx = { isOn ? null : hiddenTextStyles } >
306+ < div className = { classes . StatusTextItem } data-hidden = { ! isOn } >
322307 On
323- </ Box >
324- < Box textAlign = "right" sx = { isOn ? hiddenTextStyles : null } >
308+ </ div >
309+ < div className = { classes . StatusTextItem } data-hidden = { isOn } >
325310 Off
326- </ Box >
327- </ Text >
311+ </ div >
312+ </ span >
328313 < SwitchButton
329314 ref = { ref }
330315 type = { buttonType }
@@ -336,39 +321,17 @@ const ToggleSwitch = React.forwardRef<HTMLButtonElement, React.PropsWithChildren
336321 size = { size }
337322 aria-disabled = { ! acceptsInteraction }
338323 >
339- < Box aria-hidden = "true" display = "flex" alignItems = "center" width = "100%" height = "100%" overflow = "hidden" >
340- < Box
341- flexGrow = { 1 }
342- flexShrink = { 0 }
343- flexBasis = "50%"
344- color = { acceptsInteraction ? 'switchTrack.checked.fg' : 'switchTrack.checked.disabledFg' }
345- lineHeight = "0"
346- sx = { {
347- transform : `translateX(${ isOn ? '0' : '-100%' } )` ,
348- transitionProperty : 'transform' ,
349- transitionDuration : TRANSITION_DURATION ,
350- } }
351- >
324+ < div aria-hidden = "true" className = { classes . IconContainer } >
325+ < div className = { classes . IconBox } data-type = "on" data-checked = { isOn } data-disabled = { ! acceptsInteraction } >
352326 < LineIcon size = { size } />
353- </ Box >
354- < Box
355- flexGrow = { 1 }
356- flexShrink = { 0 }
357- flexBasis = "50%"
358- color = { acceptsInteraction ? 'switchTrack.fg' : 'switchTrack.disabledFg' }
359- lineHeight = "0"
360- sx = { {
361- transform : `translateX(${ isOn ? '100%' : '0' } )` ,
362- transitionProperty : 'transform' ,
363- transitionDuration : TRANSITION_DURATION ,
364- } }
365- >
327+ </ div >
328+ < div className = { classes . IconBox } data-type = "off" data-checked = { isOn } data-disabled = { ! acceptsInteraction } >
366329 < CircleIcon size = { size } />
367- </ Box >
368- </ Box >
330+ </ div >
331+ </ div >
369332 < ToggleKnob aria-hidden = "true" aria-disabled = { ! acceptsInteraction } checked = { isOn } />
370333 </ SwitchButton >
371- </ Box >
334+ </ div >
372335 )
373336 } ,
374337)
0 commit comments