baseui 16.0.0 → 16.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/checkbox-v2/checkbox.d.ts +7 -0
- package/checkbox-v2/checkbox.js +202 -0
- package/checkbox-v2/constants.d.ts +7 -0
- package/checkbox-v2/constants.js +23 -0
- package/checkbox-v2/index.d.ts +6 -0
- package/checkbox-v2/index.js +95 -0
- package/checkbox-v2/stateful-checkbox-container.d.ts +3 -0
- package/checkbox-v2/stateful-checkbox-container.js +102 -0
- package/checkbox-v2/stateful-checkbox.d.ts +7 -0
- package/checkbox-v2/stateful-checkbox.js +26 -0
- package/checkbox-v2/styled-components.d.ts +6 -0
- package/checkbox-v2/styled-components.js +263 -0
- package/checkbox-v2/types.d.ts +163 -0
- package/checkbox-v2/types.js +1 -0
- package/package.json +1 -1
- package/switch/constants.d.ts +11 -0
- package/switch/constants.js +24 -0
- package/switch/index.d.ts +6 -0
- package/switch/index.js +102 -0
- package/switch/stateful-switch-container.d.ts +3 -0
- package/switch/stateful-switch-container.js +97 -0
- package/switch/stateful-switch.d.ts +7 -0
- package/switch/stateful-switch.js +26 -0
- package/switch/styled-components.d.ts +6 -0
- package/switch/styled-components.js +264 -0
- package/switch/switch.d.ts +4 -0
- package/switch/switch.js +199 -0
- package/switch/types.d.ts +145 -0
- package/switch/types.js +1 -0
- package/themes/dark-theme/color-semantic-tokens.js +14 -1
- package/themes/light-theme/color-semantic-tokens.js +14 -1
- package/themes/types.d.ts +12 -0
- package/tokens/color-primitive-tokens.js +23 -1
- package/tokens/types.d.ts +20 -0
- package/utils/get-shared-styles.d.ts +13 -0
- package/utils/get-shared-styles.js +37 -0
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
var React = _interopRequireWildcard(require("react"));
|
|
8
|
+
var _statefulSwitchContainer = _interopRequireDefault(require("./stateful-switch-container"));
|
|
9
|
+
var _switch = _interopRequireDefault(require("./switch"));
|
|
10
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
11
|
+
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
|
|
12
|
+
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
|
|
13
|
+
/*
|
|
14
|
+
Copyright (c) Uber Technologies, Inc.
|
|
15
|
+
|
|
16
|
+
This source code is licensed under the MIT license found in the
|
|
17
|
+
LICENSE file in the root directory of this source tree.
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
// Styled elements
|
|
21
|
+
|
|
22
|
+
const StatefulSwitch = function (props) {
|
|
23
|
+
return /*#__PURE__*/React.createElement(_statefulSwitchContainer.default, props, childrenProps => /*#__PURE__*/React.createElement(_switch.default, childrenProps, props.children));
|
|
24
|
+
};
|
|
25
|
+
StatefulSwitch.displayName = 'StatefulSwitch';
|
|
26
|
+
var _default = exports.default = StatefulSwitch;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { SharedStyleProps } from './types';
|
|
2
|
+
export declare const Root: import("styletron-react").StyletronComponent<"label", SharedStyleProps>;
|
|
3
|
+
export declare const Toggle: import("styletron-react").StyletronComponent<"div", SharedStyleProps>;
|
|
4
|
+
export declare const ToggleTrack: import("styletron-react").StyletronComponent<"div", SharedStyleProps>;
|
|
5
|
+
export declare const Label: import("styletron-react").StyletronComponent<"div", SharedStyleProps>;
|
|
6
|
+
export declare const Input: import("styletron-react").StyletronComponent<"input", {}>;
|
|
@@ -0,0 +1,264 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.ToggleTrack = exports.Toggle = exports.Root = exports.Label = exports.Input = void 0;
|
|
7
|
+
var _styles = require("../styles");
|
|
8
|
+
var _constants = require("./constants");
|
|
9
|
+
var _getSharedStyles = require("../utils/get-shared-styles");
|
|
10
|
+
/*
|
|
11
|
+
Copyright (c) Uber Technologies, Inc.
|
|
12
|
+
|
|
13
|
+
This source code is licensed under the MIT license found in the
|
|
14
|
+
LICENSE file in the root directory of this source tree.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
function getLabelPadding(props) {
|
|
18
|
+
const {
|
|
19
|
+
$labelPlacement,
|
|
20
|
+
$theme,
|
|
21
|
+
$size
|
|
22
|
+
} = props;
|
|
23
|
+
const {
|
|
24
|
+
sizing
|
|
25
|
+
} = $theme;
|
|
26
|
+
const {
|
|
27
|
+
scale0,
|
|
28
|
+
scale100,
|
|
29
|
+
scale300
|
|
30
|
+
} = sizing;
|
|
31
|
+
let paddingDirection;
|
|
32
|
+
switch ($labelPlacement) {
|
|
33
|
+
case _constants.LABEL_PLACEMENT.left:
|
|
34
|
+
paddingDirection = 'Right';
|
|
35
|
+
break;
|
|
36
|
+
case _constants.LABEL_PLACEMENT.right:
|
|
37
|
+
default:
|
|
38
|
+
paddingDirection = 'Left';
|
|
39
|
+
break;
|
|
40
|
+
}
|
|
41
|
+
if ($theme.direction === 'rtl' && paddingDirection === 'Left') {
|
|
42
|
+
paddingDirection = 'Right';
|
|
43
|
+
} else if ($theme.direction === 'rtl' && paddingDirection === 'Right') {
|
|
44
|
+
paddingDirection = 'Left';
|
|
45
|
+
}
|
|
46
|
+
return {
|
|
47
|
+
[`padding${paddingDirection}`]: scale300,
|
|
48
|
+
paddingTop: $size === _constants.SIZE.small ? scale0 : scale100
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
function getLabelColor(props) {
|
|
52
|
+
const {
|
|
53
|
+
$disabled,
|
|
54
|
+
$theme
|
|
55
|
+
} = props;
|
|
56
|
+
const {
|
|
57
|
+
colors
|
|
58
|
+
} = $theme;
|
|
59
|
+
return $disabled ? colors.contentStateDisabled : colors.contentPrimary;
|
|
60
|
+
}
|
|
61
|
+
const Root = exports.Root = (0, _styles.styled)('label', props => {
|
|
62
|
+
const {
|
|
63
|
+
$disabled,
|
|
64
|
+
$theme,
|
|
65
|
+
$size
|
|
66
|
+
} = props;
|
|
67
|
+
const {
|
|
68
|
+
sizing
|
|
69
|
+
} = $theme;
|
|
70
|
+
return {
|
|
71
|
+
flexDirection: 'row',
|
|
72
|
+
display: 'inline-flex',
|
|
73
|
+
verticalAlign: 'middle',
|
|
74
|
+
alignItems: 'flex-start',
|
|
75
|
+
cursor: $disabled ? 'not-allowed' : 'pointer',
|
|
76
|
+
userSelect: 'none',
|
|
77
|
+
'@media (pointer: coarse)': {
|
|
78
|
+
// Increase target size for touch devices to meet the minimum touch target size of 48x48dp
|
|
79
|
+
padding: $size === _constants.SIZE.small ? sizing.scale500 : sizing.scale300
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
});
|
|
83
|
+
Root.displayName = "Root";
|
|
84
|
+
Root.displayName = 'Root';
|
|
85
|
+
const Toggle = exports.Toggle = (0, _styles.styled)('div', props => {
|
|
86
|
+
const {
|
|
87
|
+
$theme,
|
|
88
|
+
$checked,
|
|
89
|
+
$disabled,
|
|
90
|
+
$size,
|
|
91
|
+
$isHovered,
|
|
92
|
+
$showIcon
|
|
93
|
+
} = props;
|
|
94
|
+
const {
|
|
95
|
+
sizing,
|
|
96
|
+
colors,
|
|
97
|
+
direction
|
|
98
|
+
} = $theme;
|
|
99
|
+
let backgroundColor = colors.contentTertiary;
|
|
100
|
+
if ($disabled) {
|
|
101
|
+
backgroundColor = colors.contentStateDisabled;
|
|
102
|
+
} else if ($checked) {
|
|
103
|
+
backgroundColor = colors.contentInversePrimary;
|
|
104
|
+
}
|
|
105
|
+
let height, width;
|
|
106
|
+
switch ($size) {
|
|
107
|
+
case _constants.SIZE.small:
|
|
108
|
+
if ($checked) {
|
|
109
|
+
// 16px
|
|
110
|
+
width = sizing.scale600;
|
|
111
|
+
height = sizing.scale600;
|
|
112
|
+
} else {
|
|
113
|
+
// 12px
|
|
114
|
+
width = sizing.scale500;
|
|
115
|
+
height = sizing.scale500;
|
|
116
|
+
}
|
|
117
|
+
break;
|
|
118
|
+
case _constants.SIZE.default:
|
|
119
|
+
default:
|
|
120
|
+
if ($checked) {
|
|
121
|
+
// 24px
|
|
122
|
+
width = sizing.scale800;
|
|
123
|
+
height = sizing.scale800;
|
|
124
|
+
} else {
|
|
125
|
+
// 16px
|
|
126
|
+
width = sizing.scale600;
|
|
127
|
+
height = sizing.scale600;
|
|
128
|
+
}
|
|
129
|
+
break;
|
|
130
|
+
}
|
|
131
|
+
const translateX = $size === _constants.SIZE.small ? sizing.scale600 : sizing.scale700;
|
|
132
|
+
const iconSize = $size === _constants.SIZE.small ? 12 : 16;
|
|
133
|
+
const checkmarkIcon = encodeURIComponent(`
|
|
134
|
+
<svg width="${iconSize}" height="${iconSize}" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
|
|
135
|
+
<path d="m8.5 19.84-6.56-6.56 2.12-2.12L8.5 15.6 19.94 4.16l2.12 2.12L8.5 19.84Z" fill="${colors.contentPrimary}"/>
|
|
136
|
+
</svg>
|
|
137
|
+
`);
|
|
138
|
+
return {
|
|
139
|
+
backgroundColor,
|
|
140
|
+
borderTopLeftRadius: '50%',
|
|
141
|
+
borderTopRightRadius: '50%',
|
|
142
|
+
borderBottomRightRadius: '50%',
|
|
143
|
+
borderBottomLeftRadius: '50%',
|
|
144
|
+
boxShadow: $isHovered && !$checked ? `inset 0 0 0 999px ${colors.hoverOverlayInverseAlpha}` : $isHovered && $checked ? `inset 0 0 0 999px ${colors.hoverOverlayAlpha}` : 'none',
|
|
145
|
+
outline: 'none',
|
|
146
|
+
height,
|
|
147
|
+
width,
|
|
148
|
+
transform: $checked ? `translateX(${direction === 'rtl' ? `-${translateX}` : translateX})` : undefined,
|
|
149
|
+
transition: 'transform 350ms cubic-bezier(0.27, 1.06, 0.18, 1.00)',
|
|
150
|
+
backgroundImage: $showIcon && $checked ? `url('data:image/svg+xml,${checkmarkIcon}');` : undefined,
|
|
151
|
+
backgroundRepeat: 'no-repeat',
|
|
152
|
+
backgroundPosition: 'center'
|
|
153
|
+
};
|
|
154
|
+
});
|
|
155
|
+
Toggle.displayName = "Toggle";
|
|
156
|
+
Toggle.displayName = 'Toggle';
|
|
157
|
+
const ToggleTrack = exports.ToggleTrack = (0, _styles.styled)('div', props => {
|
|
158
|
+
const {
|
|
159
|
+
$size,
|
|
160
|
+
$theme,
|
|
161
|
+
$checked,
|
|
162
|
+
$disabled,
|
|
163
|
+
$isHovered,
|
|
164
|
+
$isFocusVisible,
|
|
165
|
+
$isFocused
|
|
166
|
+
} = props;
|
|
167
|
+
const {
|
|
168
|
+
sizing,
|
|
169
|
+
colors
|
|
170
|
+
} = $theme;
|
|
171
|
+
let height, width;
|
|
172
|
+
switch ($size) {
|
|
173
|
+
case _constants.SIZE.small:
|
|
174
|
+
width = sizing.scale1000; //'40px';
|
|
175
|
+
height = sizing.scale800; //'24px';
|
|
176
|
+
break;
|
|
177
|
+
case _constants.SIZE.default:
|
|
178
|
+
default:
|
|
179
|
+
width = '52px';
|
|
180
|
+
height = sizing.scale900; //'32px';
|
|
181
|
+
break;
|
|
182
|
+
}
|
|
183
|
+
let backgroundColor = colors.backgroundTertiary;
|
|
184
|
+
if ($disabled) {
|
|
185
|
+
backgroundColor = colors.backgroundStateDisabled;
|
|
186
|
+
} else if ($checked) {
|
|
187
|
+
backgroundColor = colors.backgroundInversePrimary;
|
|
188
|
+
}
|
|
189
|
+
let borderColor = 'transparent';
|
|
190
|
+
let borderStyle = 'solid';
|
|
191
|
+
let borderWidth = sizing.scale0;
|
|
192
|
+
let outline = 'none';
|
|
193
|
+
let outlineOffset = '0px';
|
|
194
|
+
if ($disabled) {
|
|
195
|
+
borderColor = colors.borderStateDisabled;
|
|
196
|
+
}
|
|
197
|
+
if (!$disabled && !$checked) {
|
|
198
|
+
borderColor = colors.contentTertiary;
|
|
199
|
+
}
|
|
200
|
+
if (!$disabled && $checked) {
|
|
201
|
+
borderStyle = 'none';
|
|
202
|
+
borderWidth = '0px';
|
|
203
|
+
}
|
|
204
|
+
if (!$disabled && $isFocusVisible && $isFocused) {
|
|
205
|
+
const outlineStyles = (0, _getSharedStyles.getFocusOutlineStyle)($theme);
|
|
206
|
+
outline = outlineStyles.outline;
|
|
207
|
+
outlineOffset = outlineStyles.outlineOffset;
|
|
208
|
+
}
|
|
209
|
+
return {
|
|
210
|
+
alignItems: 'center',
|
|
211
|
+
backgroundColor,
|
|
212
|
+
borderTopLeftRadius: '999px',
|
|
213
|
+
borderTopRightRadius: '999px',
|
|
214
|
+
borderBottomRightRadius: '999px',
|
|
215
|
+
borderBottomLeftRadius: '999px',
|
|
216
|
+
borderStyle,
|
|
217
|
+
borderWidth,
|
|
218
|
+
borderColor,
|
|
219
|
+
display: 'flex',
|
|
220
|
+
flex: '0 0 auto',
|
|
221
|
+
height,
|
|
222
|
+
width,
|
|
223
|
+
outline,
|
|
224
|
+
outlineOffset,
|
|
225
|
+
paddingTop: sizing.scale100,
|
|
226
|
+
paddingBottom: sizing.scale100,
|
|
227
|
+
paddingLeft: sizing.scale100,
|
|
228
|
+
paddingRight: sizing.scale100,
|
|
229
|
+
boxSizing: 'border-box',
|
|
230
|
+
boxShadow: $isHovered && !$checked ? `inset 0 0 0 999px ${colors.hoverOverlayAlpha}` : $isHovered && $checked ? `inset 0 0 0 999px ${colors.hoverOverlayInverseAlpha}` : 'none'
|
|
231
|
+
};
|
|
232
|
+
});
|
|
233
|
+
ToggleTrack.displayName = "ToggleTrack";
|
|
234
|
+
ToggleTrack.displayName = 'ToggleTrack';
|
|
235
|
+
const Label = exports.Label = (0, _styles.styled)('div', props => {
|
|
236
|
+
const {
|
|
237
|
+
$theme,
|
|
238
|
+
$size
|
|
239
|
+
} = props;
|
|
240
|
+
const {
|
|
241
|
+
typography
|
|
242
|
+
} = $theme;
|
|
243
|
+
return {
|
|
244
|
+
verticalAlign: 'middle',
|
|
245
|
+
...getLabelPadding(props),
|
|
246
|
+
color: getLabelColor(props),
|
|
247
|
+
...($size === _constants.SIZE.small ? typography.ParagraphSmall : typography.ParagraphMedium)
|
|
248
|
+
};
|
|
249
|
+
});
|
|
250
|
+
Label.displayName = "Label";
|
|
251
|
+
Label.displayName = 'Label';
|
|
252
|
+
|
|
253
|
+
// tricky style for focus event cause display: none doesn't work
|
|
254
|
+
const Input = exports.Input = (0, _styles.styled)('input', {
|
|
255
|
+
opacity: 0,
|
|
256
|
+
width: 0,
|
|
257
|
+
height: 0,
|
|
258
|
+
overflow: 'hidden',
|
|
259
|
+
margin: 0,
|
|
260
|
+
padding: 0,
|
|
261
|
+
position: 'absolute'
|
|
262
|
+
});
|
|
263
|
+
Input.displayName = "Input";
|
|
264
|
+
Input.displayName = 'Input';
|
package/switch/switch.js
ADDED
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
var React = _interopRequireWildcard(require("react"));
|
|
8
|
+
var _overrides = require("../helpers/overrides");
|
|
9
|
+
var _styledComponents = require("./styled-components");
|
|
10
|
+
var _focusVisible = require("../utils/focusVisible");
|
|
11
|
+
var _constants = require("./constants");
|
|
12
|
+
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
|
|
13
|
+
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
|
|
14
|
+
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); } /*
|
|
15
|
+
Copyright (c) Uber Technologies, Inc.
|
|
16
|
+
|
|
17
|
+
This source code is licensed under the MIT license found in the
|
|
18
|
+
LICENSE file in the root directory of this source tree.
|
|
19
|
+
*/
|
|
20
|
+
const stopPropagation = e => e.stopPropagation();
|
|
21
|
+
const Switch = props => {
|
|
22
|
+
const {
|
|
23
|
+
overrides = {},
|
|
24
|
+
checked = false,
|
|
25
|
+
containsInteractiveElement = false,
|
|
26
|
+
disabled = false,
|
|
27
|
+
autoFocus = false,
|
|
28
|
+
showIcon = false,
|
|
29
|
+
labelPlacement = _constants.LABEL_PLACEMENT.right,
|
|
30
|
+
size = _constants.SIZE.default,
|
|
31
|
+
onChange = () => {},
|
|
32
|
+
onMouseEnter = () => {},
|
|
33
|
+
onMouseLeave = () => {},
|
|
34
|
+
onMouseDown = () => {},
|
|
35
|
+
onMouseUp = () => {},
|
|
36
|
+
onFocus = () => {},
|
|
37
|
+
onBlur = () => {},
|
|
38
|
+
onKeyDown,
|
|
39
|
+
// don't add fallback no-op to allow native keydown behavior if not customized.
|
|
40
|
+
onKeyUp,
|
|
41
|
+
// don't add fallback no-op to allow native keyup behavior if not customized.
|
|
42
|
+
value,
|
|
43
|
+
id,
|
|
44
|
+
name,
|
|
45
|
+
children,
|
|
46
|
+
required,
|
|
47
|
+
title,
|
|
48
|
+
inputRef
|
|
49
|
+
} = props;
|
|
50
|
+
const [isFocused, setIsFocused] = React.useState(autoFocus);
|
|
51
|
+
const [isFocusVisible, setIsFocusVisible] = React.useState(false);
|
|
52
|
+
const [isHovered, setIsHovered] = React.useState(false);
|
|
53
|
+
const [isActive, setIsActive] = React.useState(false);
|
|
54
|
+
const fallbackInputRef = React.useRef(null);
|
|
55
|
+
const internalInputRef = inputRef || fallbackInputRef;
|
|
56
|
+
React.useEffect(() => {
|
|
57
|
+
if (autoFocus) {
|
|
58
|
+
internalInputRef.current?.focus();
|
|
59
|
+
}
|
|
60
|
+
}, [autoFocus, internalInputRef]);
|
|
61
|
+
const onMouseEnterHandler = React.useCallback(e => {
|
|
62
|
+
setIsHovered(true);
|
|
63
|
+
onMouseEnter(e);
|
|
64
|
+
}, [onMouseEnter]);
|
|
65
|
+
const onMouseLeaveHandler = React.useCallback(e => {
|
|
66
|
+
setIsHovered(false);
|
|
67
|
+
setIsActive(false);
|
|
68
|
+
onMouseLeave(e);
|
|
69
|
+
}, [onMouseLeave]);
|
|
70
|
+
const onMouseDownHandler = React.useCallback(e => {
|
|
71
|
+
setIsActive(true);
|
|
72
|
+
onMouseDown(e);
|
|
73
|
+
}, [onMouseDown]);
|
|
74
|
+
const onMouseUpHandler = React.useCallback(e => {
|
|
75
|
+
setIsActive(false);
|
|
76
|
+
onMouseUp(e);
|
|
77
|
+
}, [onMouseUp]);
|
|
78
|
+
const onFocusHandler = React.useCallback(e => {
|
|
79
|
+
setIsFocused(true);
|
|
80
|
+
onFocus(e);
|
|
81
|
+
if ((0, _focusVisible.isFocusVisible)(e)) {
|
|
82
|
+
setIsFocusVisible(true);
|
|
83
|
+
}
|
|
84
|
+
}, [onFocus]);
|
|
85
|
+
const onBlurHandler = React.useCallback(e => {
|
|
86
|
+
setIsFocused(false);
|
|
87
|
+
onBlur(e);
|
|
88
|
+
if (!(0, _focusVisible.isFocusVisible)(e)) {
|
|
89
|
+
setIsFocusVisible(false);
|
|
90
|
+
}
|
|
91
|
+
}, [onBlur]);
|
|
92
|
+
const onKeyUpHandler = React.useCallback(event => {
|
|
93
|
+
/**
|
|
94
|
+
* Handles 'Enter' key press to toggle the switch.
|
|
95
|
+
*/
|
|
96
|
+
|
|
97
|
+
if (event.key === ' ') {
|
|
98
|
+
setIsActive(false);
|
|
99
|
+
}
|
|
100
|
+
if (event.key === 'Enter') {
|
|
101
|
+
setIsActive(false);
|
|
102
|
+
onChange?.({
|
|
103
|
+
...event,
|
|
104
|
+
currentTarget: {
|
|
105
|
+
...event.currentTarget,
|
|
106
|
+
checked: !checked
|
|
107
|
+
},
|
|
108
|
+
target: {
|
|
109
|
+
...event.target,
|
|
110
|
+
checked: !checked
|
|
111
|
+
}
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
onKeyUp?.(event);
|
|
115
|
+
}, [onKeyUp, onChange, checked]);
|
|
116
|
+
const onKeyDownHandler = React.useCallback(event => {
|
|
117
|
+
/**
|
|
118
|
+
* Handles 'Enter' key press to toggle the switch.
|
|
119
|
+
*/
|
|
120
|
+
if (event.key === 'Enter') {
|
|
121
|
+
setIsActive(true);
|
|
122
|
+
}
|
|
123
|
+
if (event.key === ' ') {
|
|
124
|
+
setIsActive(true);
|
|
125
|
+
}
|
|
126
|
+
onKeyDown?.(event);
|
|
127
|
+
}, [onKeyDown]);
|
|
128
|
+
const {
|
|
129
|
+
Root: RootOverride,
|
|
130
|
+
Toggle: ToggleOverride,
|
|
131
|
+
Label: LabelOverride,
|
|
132
|
+
Input: InputOverride,
|
|
133
|
+
ToggleTrack: ToggleTrackOverride
|
|
134
|
+
} = overrides;
|
|
135
|
+
const Root = (0, _overrides.getOverride)(RootOverride) || _styledComponents.Root;
|
|
136
|
+
const ToggleTrack = (0, _overrides.getOverride)(ToggleTrackOverride) || _styledComponents.ToggleTrack;
|
|
137
|
+
const Toggle = (0, _overrides.getOverride)(ToggleOverride) || _styledComponents.Toggle;
|
|
138
|
+
const Label = (0, _overrides.getOverride)(LabelOverride) || _styledComponents.Label;
|
|
139
|
+
const Input = (0, _overrides.getOverride)(InputOverride) || _styledComponents.Input;
|
|
140
|
+
const inputEvents = {
|
|
141
|
+
onChange,
|
|
142
|
+
onFocus: onFocusHandler,
|
|
143
|
+
onBlur: onBlurHandler,
|
|
144
|
+
onKeyDown: onKeyDownHandler,
|
|
145
|
+
onKeyUp: onKeyUpHandler
|
|
146
|
+
};
|
|
147
|
+
const mouseEvents = {
|
|
148
|
+
onMouseEnter: onMouseEnterHandler,
|
|
149
|
+
onMouseLeave: onMouseLeaveHandler,
|
|
150
|
+
onMouseDown: onMouseDownHandler,
|
|
151
|
+
onMouseUp: onMouseUpHandler
|
|
152
|
+
};
|
|
153
|
+
const sharedProps = {
|
|
154
|
+
$isFocused: isFocused,
|
|
155
|
+
$isFocusVisible: isFocusVisible,
|
|
156
|
+
$isHovered: isHovered,
|
|
157
|
+
$isActive: isActive,
|
|
158
|
+
$checked: checked,
|
|
159
|
+
$required: required,
|
|
160
|
+
$disabled: disabled,
|
|
161
|
+
$value: value,
|
|
162
|
+
$showIcon: showIcon,
|
|
163
|
+
$size: size
|
|
164
|
+
};
|
|
165
|
+
const labelComponent = children && /*#__PURE__*/React.createElement(Label, _extends({
|
|
166
|
+
$labelPlacement: labelPlacement
|
|
167
|
+
}, sharedProps, (0, _overrides.getOverrideProps)(LabelOverride)), containsInteractiveElement ?
|
|
168
|
+
/*#__PURE__*/
|
|
169
|
+
// Prevents the event from bubbling up to the label and moving focus to the switch control.
|
|
170
|
+
// eslint-disable-next-line jsx-a11y/click-events-have-key-events, jsx-a11y/no-static-element-interactions
|
|
171
|
+
React.createElement("div", {
|
|
172
|
+
onClick: e => e.preventDefault()
|
|
173
|
+
}, children) : children);
|
|
174
|
+
return /*#__PURE__*/React.createElement(Root, _extends({
|
|
175
|
+
"data-baseweb": "switch",
|
|
176
|
+
title: title || null,
|
|
177
|
+
$labelPlacement: labelPlacement
|
|
178
|
+
}, sharedProps, mouseEvents, (0, _overrides.getOverrideProps)(RootOverride)), labelPlacement === _constants.LABEL_PLACEMENT.left && labelComponent, /*#__PURE__*/React.createElement(ToggleTrack, _extends({}, sharedProps, (0, _overrides.getOverrideProps)(ToggleTrackOverride)), /*#__PURE__*/React.createElement(Toggle, _extends({}, sharedProps, (0, _overrides.getOverrideProps)(ToggleOverride)))), /*#__PURE__*/React.createElement(Input, _extends({
|
|
179
|
+
value: value,
|
|
180
|
+
id: id,
|
|
181
|
+
name: name,
|
|
182
|
+
checked: checked,
|
|
183
|
+
required: required,
|
|
184
|
+
"aria-label": props['aria-label'] || props.ariaLabel,
|
|
185
|
+
"aria-describedby": props['aria-describedby'],
|
|
186
|
+
"aria-errormessage": props['aria-errormessage'],
|
|
187
|
+
"aria-invalid": props['aria-invalid'],
|
|
188
|
+
"aria-required": required || null,
|
|
189
|
+
disabled: disabled,
|
|
190
|
+
type: "checkbox",
|
|
191
|
+
role: "switch",
|
|
192
|
+
ref: internalInputRef
|
|
193
|
+
// Prevent a second click event from firing when label is clicked.
|
|
194
|
+
// See https://github.com/uber/baseweb/issues/3847
|
|
195
|
+
,
|
|
196
|
+
onClick: stopPropagation
|
|
197
|
+
}, sharedProps, inputEvents, (0, _overrides.getOverrideProps)(InputOverride))), labelPlacement === _constants.LABEL_PLACEMENT.right && labelComponent);
|
|
198
|
+
};
|
|
199
|
+
var _default = exports.default = Switch;
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import type * as React from 'react';
|
|
2
|
+
import type { Override } from '../helpers/overrides';
|
|
3
|
+
import type { SIZE, LABEL_PLACEMENT } from './constants';
|
|
4
|
+
export type LabelPlacement = keyof typeof LABEL_PLACEMENT;
|
|
5
|
+
export type SwitchOverrides = {
|
|
6
|
+
Toggle?: Override;
|
|
7
|
+
ToggleTrack?: Override;
|
|
8
|
+
Label?: Override;
|
|
9
|
+
Root?: Override;
|
|
10
|
+
Input?: Override;
|
|
11
|
+
};
|
|
12
|
+
export type SwitchProps = {
|
|
13
|
+
/** Id of element which contains a related caption */
|
|
14
|
+
'aria-describedby'?: string;
|
|
15
|
+
/** Passed to the input element aria-label attribute. */
|
|
16
|
+
ariaLabel?: string;
|
|
17
|
+
'aria-label'?: string;
|
|
18
|
+
/** Id of element which contains an error message */
|
|
19
|
+
'aria-errormessage'?: string;
|
|
20
|
+
/** Indicates whether the switch is in an error state */
|
|
21
|
+
'aria-invalid'?: boolean;
|
|
22
|
+
/** Component or String value for label of switch. */
|
|
23
|
+
children?: React.ReactNode;
|
|
24
|
+
/** Indicates if this switch children contain an interactive element (prevents the label from moving focus from the child element to the radio button) */
|
|
25
|
+
containsInteractiveElement?: boolean;
|
|
26
|
+
overrides?: SwitchOverrides;
|
|
27
|
+
/** Check or uncheck the control. */
|
|
28
|
+
checked?: boolean;
|
|
29
|
+
/** Disable the switch from being changed. */
|
|
30
|
+
disabled?: boolean;
|
|
31
|
+
/** Marks the switch as required. */
|
|
32
|
+
required?: boolean;
|
|
33
|
+
/** Used to get a ref to the input element. Useful for programmatically focusing the input */
|
|
34
|
+
inputRef?: React.RefObject<HTMLInputElement>;
|
|
35
|
+
/** Focus the switch on initial render. */
|
|
36
|
+
autoFocus?: boolean;
|
|
37
|
+
/** Passed to the input element id attribute */
|
|
38
|
+
id?: string;
|
|
39
|
+
/** Passed to the input element name attribute */
|
|
40
|
+
name?: string;
|
|
41
|
+
/** Passed to the input element value attribute */
|
|
42
|
+
value?: string;
|
|
43
|
+
/** How to position the label relative to the switch itself. */
|
|
44
|
+
labelPlacement?: LabelPlacement;
|
|
45
|
+
/** Text to display in native OS tooltip on long hover. */
|
|
46
|
+
title?: string | null;
|
|
47
|
+
/** Handler for change events on trigger element. */
|
|
48
|
+
onChange?: (e: React.ChangeEvent<HTMLInputElement>) => unknown;
|
|
49
|
+
/** Handler for mouseenter events on trigger element. */
|
|
50
|
+
onMouseEnter?: (e: React.ChangeEvent<HTMLInputElement>) => unknown;
|
|
51
|
+
/** Handler for mouseleave events on trigger element. */
|
|
52
|
+
onMouseLeave?: (e: React.ChangeEvent<HTMLInputElement>) => unknown;
|
|
53
|
+
/** Handler for mousedown events on trigger element. */
|
|
54
|
+
onMouseDown?: (e: React.ChangeEvent<HTMLInputElement>) => unknown;
|
|
55
|
+
/** Handler for mouseup events on trigger element. */
|
|
56
|
+
onMouseUp?: (e: React.ChangeEvent<HTMLInputElement>) => unknown;
|
|
57
|
+
/** handler for focus events on trigger element. */
|
|
58
|
+
onFocus?: (e: React.ChangeEvent<HTMLInputElement>) => unknown;
|
|
59
|
+
/** handler for blur events on trigger element. */
|
|
60
|
+
onBlur?: (e: React.ChangeEvent<HTMLInputElement>) => unknown;
|
|
61
|
+
/** handler for keydown events on trigger element. */
|
|
62
|
+
onKeyDown?: (e: React.KeyboardEvent<HTMLInputElement>) => unknown;
|
|
63
|
+
/** Handler for keyup events on trigger element. */
|
|
64
|
+
onKeyUp?: (e: React.KeyboardEvent<HTMLInputElement>) => unknown;
|
|
65
|
+
/** size of switch component - both control and label(if existing) */
|
|
66
|
+
size?: keyof typeof SIZE;
|
|
67
|
+
/** whether show checkmark icon when switch is on */
|
|
68
|
+
showIcon?: boolean;
|
|
69
|
+
};
|
|
70
|
+
export type SwitchState = {
|
|
71
|
+
isFocused: boolean;
|
|
72
|
+
isFocusVisible: boolean;
|
|
73
|
+
isHovered: boolean;
|
|
74
|
+
isActive: boolean;
|
|
75
|
+
};
|
|
76
|
+
export type SwitchReducerState = {
|
|
77
|
+
checked?: boolean;
|
|
78
|
+
};
|
|
79
|
+
export type StateReducer = (stateType: string, nextState: SwitchReducerState, currentState: SwitchReducerState, event: React.ChangeEvent<HTMLInputElement> | React.KeyboardEvent<HTMLInputElement>) => SwitchReducerState;
|
|
80
|
+
export type StatefulContainerChildProps = {
|
|
81
|
+
overrides?: SwitchOverrides;
|
|
82
|
+
/** Handler for change events on trigger element. */
|
|
83
|
+
onChange?: (e: React.ChangeEvent<HTMLInputElement>) => unknown;
|
|
84
|
+
/** Handler for mouseenter events on trigger element. */
|
|
85
|
+
onMouseEnter?: (e: React.ChangeEvent<HTMLInputElement>) => unknown;
|
|
86
|
+
/** Handler for mouseleave events on trigger element. */
|
|
87
|
+
onMouseLeave?: (e: React.ChangeEvent<HTMLInputElement>) => unknown;
|
|
88
|
+
/** Handler for mousedown events on trigger element. */
|
|
89
|
+
onMouseDown?: (e: React.ChangeEvent<HTMLInputElement>) => unknown;
|
|
90
|
+
/** Handler for mouseup events on trigger element. */
|
|
91
|
+
onMouseUp?: (e: React.ChangeEvent<HTMLInputElement>) => unknown;
|
|
92
|
+
/** Handler for focus events on trigger element. */
|
|
93
|
+
onFocus?: (e: React.ChangeEvent<HTMLInputElement>) => unknown;
|
|
94
|
+
/** Handler for blur events on trigger element. */
|
|
95
|
+
onBlur?: (e: React.ChangeEvent<HTMLInputElement>) => unknown;
|
|
96
|
+
/** Handler for keydown events on trigger element. */
|
|
97
|
+
onKeyDown?: (e: React.KeyboardEvent<HTMLInputElement>) => unknown;
|
|
98
|
+
/** Handler for keyup events on trigger element. */
|
|
99
|
+
onKeyUp?: (e: React.KeyboardEvent<HTMLInputElement>) => unknown;
|
|
100
|
+
/** Focus the switch on initial render. */
|
|
101
|
+
autoFocus?: boolean;
|
|
102
|
+
} & SwitchReducerState;
|
|
103
|
+
export type StatefulContainerProps = {
|
|
104
|
+
overrides?: SwitchOverrides;
|
|
105
|
+
/** Component or String value for label of switch. */
|
|
106
|
+
children?: (a: StatefulContainerChildProps) => React.ReactNode;
|
|
107
|
+
/** Defines the components initial state value */
|
|
108
|
+
initialState?: SwitchReducerState;
|
|
109
|
+
/** A state change handler. Used to override default state transitions. */
|
|
110
|
+
stateReducer?: StateReducer;
|
|
111
|
+
/** Handler for change events on trigger element. */
|
|
112
|
+
onChange?: (e: React.ChangeEvent<HTMLInputElement>) => unknown;
|
|
113
|
+
/** Handler for mouseenter events on trigger element. */
|
|
114
|
+
onMouseEnter?: (e: React.ChangeEvent<HTMLInputElement>) => unknown;
|
|
115
|
+
/** Handler for mouseleave events on trigger element. */
|
|
116
|
+
onMouseLeave?: (e: React.ChangeEvent<HTMLInputElement>) => unknown;
|
|
117
|
+
/** Handler for mousedown events on trigger element. */
|
|
118
|
+
onMouseDown?: (e: React.ChangeEvent<HTMLInputElement>) => unknown;
|
|
119
|
+
/** Handler for mouseup events on trigger element. */
|
|
120
|
+
onMouseUp?: (e: React.ChangeEvent<HTMLInputElement>) => unknown;
|
|
121
|
+
/** Handler for focus events on trigger element. */
|
|
122
|
+
onFocus?: (e: React.ChangeEvent<HTMLInputElement>) => unknown;
|
|
123
|
+
/** Handler for blur events on trigger element. */
|
|
124
|
+
onBlur?: (e: React.ChangeEvent<HTMLInputElement>) => unknown;
|
|
125
|
+
/** Handler for keydown events on trigger element. */
|
|
126
|
+
onKeyDown?: (e: React.KeyboardEvent<HTMLInputElement>) => unknown;
|
|
127
|
+
/** Handler for keyup events on trigger element. */
|
|
128
|
+
onKeyUp?: (e: React.KeyboardEvent<HTMLInputElement>) => unknown;
|
|
129
|
+
/** Focus the switch on initial render. */
|
|
130
|
+
autoFocus?: boolean;
|
|
131
|
+
};
|
|
132
|
+
export type StatefulSwitchProps = Omit<StatefulContainerProps, 'children'> & Partial<SwitchProps>;
|
|
133
|
+
export type SharedStyleProps = {
|
|
134
|
+
$isFocused: boolean;
|
|
135
|
+
$isFocusVisible: boolean;
|
|
136
|
+
$isHovered: boolean;
|
|
137
|
+
$isActive: boolean;
|
|
138
|
+
$checked: boolean;
|
|
139
|
+
$required: boolean;
|
|
140
|
+
$disabled: boolean;
|
|
141
|
+
$value: string;
|
|
142
|
+
$labelPlacement?: LabelPlacement;
|
|
143
|
+
$showIcon?: boolean;
|
|
144
|
+
$size?: keyof typeof SIZE;
|
|
145
|
+
};
|
package/switch/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";
|
|
@@ -35,7 +35,20 @@ var _default = (foundation = _colorFoundationTokens.default) => {
|
|
|
35
35
|
borderSelected: foundation.primaryA,
|
|
36
36
|
borderInverseOpaque: _colorPrimitiveTokens.primitiveDarkColors.gray300Dark,
|
|
37
37
|
borderInverseTransparent: (0, _util.hexToRgb)(foundation.primaryB, '0.2') || '',
|
|
38
|
-
borderInverseSelected: foundation.primaryB
|
|
38
|
+
borderInverseSelected: foundation.primaryB,
|
|
39
|
+
// Brand Default
|
|
40
|
+
brandBackgroundPrimary: _colorPrimitiveTokens.primitiveDarkColors.brandDefault500Dark,
|
|
41
|
+
brandBackgroundSecondary: _colorPrimitiveTokens.primitiveDarkColors.brandDefault100Dark,
|
|
42
|
+
brandBackgroundTertiary: _colorPrimitiveTokens.primitiveDarkColors.white,
|
|
43
|
+
brandBackgroundDisabled: _colorPrimitiveTokens.primitiveDarkColors.brandDefault100Dark,
|
|
44
|
+
brandContentPrimary: _colorPrimitiveTokens.primitiveDarkColors.brandDefault600Dark,
|
|
45
|
+
brandContentOnPrimary: _colorPrimitiveTokens.primitiveDarkColors.white,
|
|
46
|
+
brandContentOnSecondary: _colorPrimitiveTokens.primitiveDarkColors.brandDefault700Dark,
|
|
47
|
+
brandContentOnTertiary: _colorPrimitiveTokens.primitiveDarkColors.black,
|
|
48
|
+
brandContentOnGradient: _colorPrimitiveTokens.primitiveDarkColors.white,
|
|
49
|
+
brandContentDisabled: _colorPrimitiveTokens.primitiveDarkColors.brandDefault400Dark,
|
|
50
|
+
brandBorderAccessible: _colorPrimitiveTokens.primitiveDarkColors.brandDefault600Dark,
|
|
51
|
+
brandBorderSubtle: _colorPrimitiveTokens.primitiveDarkColors.brandDefault400Dark
|
|
39
52
|
};
|
|
40
53
|
const coreExtensions = {
|
|
41
54
|
// Backgrounds
|