This repository has been archived by the owner on Dec 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
ToggleSwitch.tsx
107 lines (100 loc) · 2.93 KB
/
ToggleSwitch.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import { css } from '@emotion/react';
import type { EmotionJSX } from '@emotion/react/types/jsx-namespace';
import { neutral } from '@guardian/src-foundations';
import { textSans } from '@guardian/src-foundations/typography';
import type { Props } from '@guardian/src-helpers';
import { mediumStyles, slimStyles, toggleSwitchStyles } from './styles';
export type Size = 'medium' | 'slim';
export interface ToggleSwitchProps extends Props {
/**
* different color theme is used whether the component is used
* in light or dark background
*/
isDarkBackground?: boolean;
/**
* Whether the ToggleSwitch is checked. This is necessary when using the
* [controlled approach](https://reactjs.org/docs/forms.html#controlled-components)
* (recommended) to form state management.
*
* _Note: if you pass the `checked` prop, you MUST also pass an `onClick`
* handler, or the field will be rendered as read-only._
*/
checked?: boolean;
/**
* When using the [uncontrolled approach](https://reactjs.org/docs/uncontrolled-components.html),
* use defaultChecked to indicate the whether the ToggleSwitch is checked intially.
*/
defaultChecked?: boolean;
/**
* Appears to the right of the switch.
*
*/
label?: string;
/**
* slim or medium toggle would be different in size and colors
*
*/
size?: Size;
/**
* A callback function called when the component is opened or closed.
* Receives the click event as an argument.
*/
onClick?: (event: React.MouseEvent<HTMLButtonElement>) => void;
}
/**
* [Storybook](https://guardian.github.io/source/?path=/docs/kitchen-source-react-components-development-kitchen-toggle-switch--playground) •
* [Design System](https://theguardian.design) •
* [GitHub](https://github.com/guardian/source/tree/main/packages/@guardian/source-react-components-development-kitchen/components/toggle-switch) •
* [NPM](https://www.npmjs.com/package/@guardian/source-react-components-development-kitchen)
*
* Displays an on/off toggle switch.
*
*/
const labelStyles = (isDarkBackground: boolean) => css`
${textSans.small()};
display: inline-block;
transform: translateY(1px);
color: ${isDarkBackground ? neutral[100] : neutral[7]};
`;
export const ToggleSwitch = ({
isDarkBackground = false,
checked,
label,
defaultChecked,
cssOverrides,
size = 'medium',
onClick = () => undefined,
...props
}: ToggleSwitchProps): EmotionJSX.Element => {
const isChecked = (): boolean => {
if (checked != undefined) {
return checked;
}
return !!defaultChecked;
};
const isSlim = size === 'slim';
return (
<div
css={[
toggleSwitchStyles,
isSlim
? slimStyles(isDarkBackground)
: mediumStyles(isDarkBackground),
cssOverrides,
]}
{...props}
>
<span>
<button
role="switch"
aria-checked={isChecked()}
aria-labelledby="notify"
onClick={onClick}
></button>
</span>
<span css={labelStyles(isDarkBackground)} id="notify">
{label}
</span>
</div>
);
};