Skip to content

Commit 7af7d02

Browse files
committed
fix: shrinking button on mouse leave
1 parent 1526078 commit 7af7d02

File tree

5 files changed

+75
-8
lines changed

5 files changed

+75
-8
lines changed

packages/form/src/components/SwitchButtonField/__tests__/__snapshots__/index.test.tsx.snap

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,12 @@ exports[`SwitchButtonField > should render correctly 1`] = `
153153
border: none;
154154
}
155155
156+
.emotion-5 label {
157+
-webkit-transition: color 300ms;
158+
transition: color 300ms;
159+
color: #222638;
160+
}
161+
156162
.emotion-5[data-checked='true'] label {
157163
color: #ffffff;
158164
}
@@ -708,6 +714,12 @@ exports[`SwitchButtonField > should works with defaultValues 1`] = `
708714
border: none;
709715
}
710716
717+
.emotion-5 label {
718+
-webkit-transition: color 300ms;
719+
transition: color 300ms;
720+
color: #222638;
721+
}
722+
711723
.emotion-5[data-checked='true'] label {
712724
color: #ffffff;
713725
}

packages/ui/src/components/SwitchButton/Option.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,17 @@ const StyledSelectableCard = styled(SelectableCard)`
2929
border: none;
3030
}
3131
32+
& label {
33+
transition: color 300ms;
34+
color: ${({ theme }) => theme.colors.neutral.textStrong};
35+
}
36+
3237
&[data-checked='true'] label {
3338
color: ${({ theme }) => theme.colors.primary.textStrong};
3439
}
3540
3641
42+
3743
&:not([data-checked='true']) label {
3844
&:hover {
3945
color: ${({ theme }) => theme.colors.primary.text};

packages/ui/src/components/SwitchButton/__stories__/WithIcon.stories.tsx

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
import type { StoryFn } from '@storybook/react'
22
import { MoonIcon, SunIcon } from '@ultraviolet/icons'
33
import type { ComponentProps } from 'react'
4+
import { Stack } from '../../Stack'
45
import { SwitchButton } from '../index'
56

67
export const WithIcon: StoryFn<ComponentProps<typeof SwitchButton>> = args => (
78
<SwitchButton {...args} size="small">
89
<SwitchButton.Option value="option1">
9-
<SunIcon />
10+
<Stack direction="row" alignItems="center">
11+
<SunIcon />
12+
</Stack>
1013
</SwitchButton.Option>
1114
<SwitchButton.Option value="option2">
12-
<MoonIcon />
15+
<Stack direction="row" alignItems="center">
16+
<MoonIcon />
17+
</Stack>
1318
</SwitchButton.Option>
1419
</SwitchButton>
1520
)

packages/ui/src/components/SwitchButton/__tests__/__snapshots__/index.test.tsx.snap

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,12 @@ exports[`SwitchButton > renders correctly 1`] = `
153153
border: none;
154154
}
155155
156+
.emotion-5 label {
157+
-webkit-transition: color 300ms;
158+
transition: color 300ms;
159+
color: #222638;
160+
}
161+
156162
.emotion-5[data-checked='true'] label {
157163
color: #ffffff;
158164
}
@@ -705,6 +711,12 @@ exports[`SwitchButton > renders correctly medium 1`] = `
705711
border: none;
706712
}
707713
714+
.emotion-5 label {
715+
-webkit-transition: color 300ms;
716+
transition: color 300ms;
717+
color: #222638;
718+
}
719+
708720
.emotion-5[data-checked='true'] label {
709721
color: #ffffff;
710722
}
@@ -1257,6 +1269,12 @@ exports[`SwitchButton > renders correctly with right value 1`] = `
12571269
border: none;
12581270
}
12591271
1272+
.emotion-5 label {
1273+
-webkit-transition: color 300ms;
1274+
transition: color 300ms;
1275+
color: #222638;
1276+
}
1277+
12601278
.emotion-5[data-checked='true'] label {
12611279
color: #ffffff;
12621280
}
@@ -1809,6 +1827,12 @@ exports[`SwitchButton > renders neutral 1`] = `
18091827
border: none;
18101828
}
18111829
1830+
.emotion-5 label {
1831+
-webkit-transition: color 300ms;
1832+
transition: color 300ms;
1833+
color: #222638;
1834+
}
1835+
18121836
.emotion-5[data-checked='true'] label {
18131837
color: #ffffff;
18141838
}
@@ -2372,6 +2396,12 @@ exports[`SwitchButton > renders with icons 1`] = `
23722396
border: none;
23732397
}
23742398
2399+
.emotion-7 label {
2400+
-webkit-transition: color 300ms;
2401+
transition: color 300ms;
2402+
color: #222638;
2403+
}
2404+
23752405
.emotion-7[data-checked='true'] label {
23762406
color: #ffffff;
23772407
}
@@ -2987,6 +3017,12 @@ exports[`SwitchButton > renders with tooltip 1`] = `
29873017
border: none;
29883018
}
29893019
3020+
.emotion-7 label {
3021+
-webkit-transition: color 300ms;
3022+
transition: color 300ms;
3023+
color: #222638;
3024+
}
3025+
29903026
.emotion-7[data-checked='true'] label {
29913027
color: #ffffff;
29923028
}

packages/ui/src/components/SwitchButton/index.tsx

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -157,18 +157,26 @@ export const SwitchButton = ({
157157
onMouseDown={event => {
158158
const rect = event.currentTarget.getBoundingClientRect()
159159
const clickX = event.clientX - rect.left
160-
setMouseDownSide(
161-
clickX < getPosition(getElement(localValue)) ? 'left' : 'right',
162-
)
163-
setWidth(width + FOCUS_OVERLAY_SCALE_RATIO)
160+
const clickInCurrentElement =
161+
clickX > position && clickX < position + width
162+
if (!clickInCurrentElement) {
163+
setMouseDownSide(
164+
clickX < getPosition(getElement(localValue))
165+
? 'left'
166+
: 'right',
167+
)
168+
setWidth(width + FOCUS_OVERLAY_SCALE_RATIO)
169+
} else {
170+
setMouseDownSide(null)
171+
}
164172
}}
165173
onMouseUp={() => {
166174
setMouseDownSide(null)
167-
setWidth(width - FOCUS_OVERLAY_SCALE_RATIO)
175+
if (mouseDownSide) setWidth(width - FOCUS_OVERLAY_SCALE_RATIO)
168176
}}
169177
onMouseLeave={() => {
170178
setMouseDownSide(null)
171-
setWidth(width - FOCUS_OVERLAY_SCALE_RATIO)
179+
if (mouseDownSide) setWidth(width - FOCUS_OVERLAY_SCALE_RATIO)
172180
}}
173181
data-size={size}
174182
ref={containerRef}

0 commit comments

Comments
 (0)