Skip to content

Commit 1c871a0

Browse files
committed
feat(components): Move the sub-component in Switch outside.
To imporve its render speed.
1 parent 5159bbd commit 1c871a0

File tree

1 file changed

+71
-71
lines changed

1 file changed

+71
-71
lines changed

packages/components/src/Switch.js

Lines changed: 71 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -5,85 +5,85 @@ import { Label } from './Label'
55
const GUTTER = 2
66
const SIZE = 18
77

8-
export const Switch = React.forwardRef(function Switch(
9-
{ className, label, labelPosition = 'end', sx, variant = 'switch', ...props },
10-
ref
11-
) {
12-
// Inner really checkbox (but hidden).
13-
const ReallyHiddenCheckbox = ({ ref, label, ...props }) => {
14-
return (
15-
<Box
16-
ref={ref}
17-
as="input"
18-
type="checkbox"
19-
__themeKey="forms"
20-
aria-label={label}
21-
{...props}
22-
sx={{
23-
position: 'absolute',
24-
opacity: 0,
25-
zIndex: -1,
26-
width: 1,
27-
height: 1,
28-
overflow: 'hidden',
29-
}}
30-
/>
31-
);
32-
};
8+
// Inner really checkbox (but hidden).
9+
const ReallyHiddenCheckbox = ({ ref, label, ...props }) => {
10+
return (
11+
<Box
12+
ref={ref}
13+
as="input"
14+
type="checkbox"
15+
__themeKey="forms"
16+
aria-label={label}
17+
{...props}
18+
sx={{
19+
position: 'absolute',
20+
opacity: 0,
21+
zIndex: -1,
22+
width: 1,
23+
height: 1,
24+
overflow: 'hidden',
25+
}}
26+
/>
27+
)
28+
}
3329

34-
// Switch just for show
35-
const SwitchForShow = ({ variant, className, sx }) => {
36-
return (
37-
<Box
38-
css={{
39-
padding: GUTTER,
40-
}}
41-
__themeKey="forms"
42-
variant={variant}
43-
className={className}
44-
sx={sx}
45-
__css={{
30+
// Switch just for show
31+
const SwitchForShow = ({ variant, className, sx }) => {
32+
return (
33+
<Box
34+
css={{
35+
padding: GUTTER,
36+
}}
37+
__themeKey="forms"
38+
variant={variant}
39+
className={className}
40+
sx={sx}
41+
__css={{
42+
position: 'relative',
43+
flexShrink: 0,
44+
bg: 'gray',
45+
borderRadius: SIZE,
46+
height: SIZE + GUTTER * 2,
47+
width: SIZE * 2 + GUTTER * 2,
48+
'input:disabled ~ &': {
49+
opacity: 0.5,
50+
cursor: 'not-allowed',
51+
},
52+
'& > div': {
53+
display: 'flex',
54+
alignItems: 'center',
55+
borderRadius: '50%',
56+
height: SIZE,
57+
width: SIZE,
58+
bg: 'white',
59+
boxShadow: '0 1px 2px rgba(0, 0, 0, 0.1)',
4660
position: 'relative',
47-
flexShrink: 0,
48-
bg: 'gray',
49-
borderRadius: SIZE,
50-
height: SIZE + GUTTER * 2,
51-
width: SIZE * 2 + GUTTER * 2,
52-
'input:disabled ~ &': {
53-
opacity: 0.5,
54-
cursor: 'not-allowed',
61+
transform: 'translateX(0%)',
62+
transition: `transform 240ms cubic-bezier(0.165, 0.840, 0.440, 1.000)`,
63+
},
64+
'input:checked ~ &': {
65+
bg: 'primary',
66+
'> div': {
67+
transform: 'translateX(100%)',
5568
},
56-
'& > div': {
57-
display: 'flex',
58-
alignItems: 'center',
59-
borderRadius: '50%',
60-
height: SIZE,
61-
width: SIZE,
62-
bg: 'white',
63-
boxShadow: '0 1px 2px rgba(0, 0, 0, 0.1)',
64-
position: 'relative',
65-
transform: 'translateX(0%)',
66-
transition: `transform 240ms cubic-bezier(0.165, 0.840, 0.440, 1.000)`,
67-
},
68-
'input:checked ~ &': {
69-
bg: 'primary',
70-
'> div': {
71-
transform: 'translateX(100%)',
72-
},
73-
},
74-
}}
75-
>
76-
<Box />
77-
</Box>
78-
)
79-
};
69+
},
70+
}}
71+
>
72+
<Box />
73+
</Box>
74+
)
75+
}
8076

77+
export const Switch = React.forwardRef(function Switch(
78+
{ className, label, labelPosition = 'end', sx, variant = 'switch', ...props },
79+
ref
80+
) {
8181
return (
8282
<Label sx={{ cursor: 'pointer', gap: 2 }}>
83-
{ labelPosition === 'start' && label ? <span>{label}</span> : null }
83+
{labelPosition === 'start' && label ? <span>{label}</span> : null}
8484
<ReallyHiddenCheckbox ref={ref} label={label} {...props} />
8585
<SwitchForShow variant={variant} className={className} sx={sx} />
86-
{ labelPosition === 'end' && label ? <span>{label}</span> : null }
86+
{labelPosition === 'end' && label ? <span>{label}</span> : null}
8787
</Label>
8888
)
8989
})

0 commit comments

Comments
 (0)