Skip to content

Commit b263a1d

Browse files
committed
feat: add hover color support for Switch component and refactor styles
1 parent 95faa76 commit b263a1d

File tree

4 files changed

+51
-3
lines changed

4 files changed

+51
-3
lines changed

src/Shared/Components/Switch/Switch.component.tsx

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,13 @@ import {
1313
getSwitchIconColor,
1414
getSwitchThumbClass,
1515
getSwitchTrackColor,
16+
getSwitchTrackHoverColor,
1617
getThumbPadding,
1718
getThumbPosition,
1819
} from './utils'
1920

21+
import './switch.scss'
22+
2023
const Switch = ({
2124
ariaLabel,
2225
dataTestId,
@@ -56,14 +59,14 @@ const Switch = ({
5659
{isLoading ? (
5760
<motion.span
5861
transition={{ ease: 'easeInOut', duration: 0.2 }}
59-
layoutId="loader"
62+
layoutId={`${name}-loader`}
6063
className="flex-grow-1 h-100 dc__fill-available-space dc__no-shrink"
6164
>
6265
<Icon key="progressing" name="ic-circle-loader" color={LOADING_COLOR_MAP[variant]} size={null} />
6366
</motion.span>
6467
) : (
6568
<motion.span
66-
layoutId="thumb"
69+
layoutId={`${name}-thumb`}
6770
className={getSwitchThumbClass({ shape, size, showIndeterminateIcon })}
6871
layout
6972
transition={{ ease: 'easeInOut', duration: 0.2 }}
@@ -129,8 +132,16 @@ const Switch = ({
129132
data-testid={dataTestId}
130133
disabled={isDisabled || isLoading}
131134
aria-disabled={isDisabled}
132-
className={`p-0-imp h-100 flex flex-grow-1 dc__no-border ${shape === 'rounded' ? 'br-12' : 'br-4'} ${getSwitchTrackColor({ shape, variant, isChecked, isLoading })} ${isDisabled ? 'dc__disabled' : ''} dc__fill-available-space`}
135+
className={`p-0-imp h-100 flex flex-grow-1 dc__no-border dt-switch__track ${shape === 'rounded' ? 'br-12' : 'br-4'} ${getSwitchTrackColor({ shape, variant, isChecked, isLoading })} ${isDisabled ? 'dc__disabled' : ''} dc__fill-available-space`}
133136
onClick={onChange}
137+
style={{
138+
// Adding hover styles directly to the button
139+
['--switch-track-hover-color' as string]: getSwitchTrackHoverColor({
140+
shape,
141+
variant,
142+
isChecked,
143+
}),
144+
}}
134145
>
135146
{renderContent()}
136147
</button>

src/Shared/Components/Switch/constants.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,21 @@ export const ROUNDED_SWITCH_TRACK_COLOR_MAP: Record<DTSwitchProps['variant'], st
3838
positive: 'bcg-5',
3939
}
4040

41+
export const ROUNDED_SWITCH_TRACK_HOVER_COLOR_MAP: Record<DTSwitchProps['variant'], `var(--${IconBaseColorType})`> = {
42+
theme: 'var(--B600)',
43+
positive: 'var(--G600)',
44+
}
45+
4146
export const SQUARE_SWITCH_TRACK_COLOR_MAP: typeof ROUNDED_SWITCH_TRACK_COLOR_MAP = {
4247
theme: 'bcb-3',
4348
positive: 'bcg-3',
4449
}
4550

51+
export const SQUARE_SWITCH_TRACK_HOVER_COLOR_MAP: typeof ROUNDED_SWITCH_TRACK_HOVER_COLOR_MAP = {
52+
theme: 'var(--B400)',
53+
positive: 'var(--G400)',
54+
}
55+
4656
export const ROUNDED_SWITCH_THUMB_SIZE_MAP: Record<DTSwitchProps['size'], string> = {
4757
[ComponentSizeType.medium]: 'icon-dim-16',
4858
[ComponentSizeType.small]: 'icon-dim-12',
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
.dt-switch {
2+
&__track {
3+
--switch-track-hover-color: 'transparent';
4+
5+
&:hover {
6+
background-color: var(--switch-track-hover-color);
7+
}
8+
}
9+
}

src/Shared/Components/Switch/utils.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ import {
44
ROUNDED_SWITCH_SIZE_MAP,
55
ROUNDED_SWITCH_THUMB_SIZE_MAP,
66
ROUNDED_SWITCH_TRACK_COLOR_MAP,
7+
ROUNDED_SWITCH_TRACK_HOVER_COLOR_MAP,
78
SQUARE_SWITCH_SIZE_MAP,
89
SQUARE_SWITCH_TRACK_COLOR_MAP,
10+
SQUARE_SWITCH_TRACK_HOVER_COLOR_MAP,
911
SWITCH_HEIGHT_MAP,
1012
SWITCH_THUMB_PADDING_MAP,
1113
THUMB_OUTER_PADDING_MAP,
@@ -32,6 +34,22 @@ export const getSwitchTrackColor = ({
3234
return shape === 'rounded' ? ROUNDED_SWITCH_TRACK_COLOR_MAP[variant] : SQUARE_SWITCH_TRACK_COLOR_MAP[variant]
3335
}
3436

37+
export const getSwitchTrackHoverColor = ({
38+
shape,
39+
variant,
40+
isChecked,
41+
}: Required<
42+
Pick<DTSwitchProps, 'shape' | 'variant' | 'isChecked'>
43+
>): (typeof ROUNDED_SWITCH_TRACK_HOVER_COLOR_MAP)[DTSwitchProps['variant']] => {
44+
if (!isChecked) {
45+
return 'var(--N300)'
46+
}
47+
48+
return shape === 'rounded'
49+
? ROUNDED_SWITCH_TRACK_HOVER_COLOR_MAP[variant]
50+
: SQUARE_SWITCH_TRACK_HOVER_COLOR_MAP[variant]
51+
}
52+
3553
export const getSwitchThumbClass = ({
3654
shape,
3755
size,

0 commit comments

Comments
 (0)