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,263 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.Root = exports.Label = exports.Input = exports.CheckmarkContainer = exports.Checkmark = 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 getBorderColor(props) {
|
|
18
|
+
const {
|
|
19
|
+
$disabled,
|
|
20
|
+
$checked,
|
|
21
|
+
$error,
|
|
22
|
+
$isIndeterminate,
|
|
23
|
+
$theme,
|
|
24
|
+
$isFocusVisible
|
|
25
|
+
} = props;
|
|
26
|
+
const {
|
|
27
|
+
colors
|
|
28
|
+
} = $theme;
|
|
29
|
+
if ($disabled) {
|
|
30
|
+
return colors.contentStateDisabled;
|
|
31
|
+
}
|
|
32
|
+
if ($checked || $isIndeterminate) {
|
|
33
|
+
return 'transparent';
|
|
34
|
+
}
|
|
35
|
+
if ($error) {
|
|
36
|
+
return colors.tagRedContentSecondary;
|
|
37
|
+
}
|
|
38
|
+
if ($isFocusVisible) {
|
|
39
|
+
return colors.borderSelected;
|
|
40
|
+
}
|
|
41
|
+
return colors.contentTertiary;
|
|
42
|
+
}
|
|
43
|
+
function getLabelPadding(props) {
|
|
44
|
+
const {
|
|
45
|
+
$labelPlacement = '',
|
|
46
|
+
$theme
|
|
47
|
+
} = props;
|
|
48
|
+
const {
|
|
49
|
+
sizing
|
|
50
|
+
} = $theme;
|
|
51
|
+
const {
|
|
52
|
+
scale100
|
|
53
|
+
} = sizing;
|
|
54
|
+
let paddingDirection;
|
|
55
|
+
switch ($labelPlacement) {
|
|
56
|
+
case _constants.LABEL_PLACEMENT.left:
|
|
57
|
+
paddingDirection = 'Right';
|
|
58
|
+
break;
|
|
59
|
+
default:
|
|
60
|
+
case _constants.LABEL_PLACEMENT.right:
|
|
61
|
+
paddingDirection = 'Left';
|
|
62
|
+
break;
|
|
63
|
+
}
|
|
64
|
+
if ($theme.direction === 'rtl' && paddingDirection === 'Left') {
|
|
65
|
+
paddingDirection = 'Right';
|
|
66
|
+
} else if ($theme.direction === 'rtl' && paddingDirection === 'Right') {
|
|
67
|
+
paddingDirection = 'Left';
|
|
68
|
+
}
|
|
69
|
+
return {
|
|
70
|
+
[`padding${paddingDirection}`]: scale100
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
function getBackgroundColor(props) {
|
|
74
|
+
const {
|
|
75
|
+
$disabled,
|
|
76
|
+
$checked,
|
|
77
|
+
$isIndeterminate,
|
|
78
|
+
$error,
|
|
79
|
+
$isHovered,
|
|
80
|
+
$isActive,
|
|
81
|
+
$theme
|
|
82
|
+
} = props;
|
|
83
|
+
const {
|
|
84
|
+
colors
|
|
85
|
+
} = $theme;
|
|
86
|
+
if ($disabled) {
|
|
87
|
+
return $checked || $isIndeterminate ? colors.contentStateDisabled : 'transparent';
|
|
88
|
+
}
|
|
89
|
+
if ($checked || $isIndeterminate) {
|
|
90
|
+
return $error ? colors.tagRedContentSecondary : colors.contentPrimary;
|
|
91
|
+
}
|
|
92
|
+
if ($isHovered) {
|
|
93
|
+
return $error ? colors.hoverNegativeAlpha : colors.hoverOverlayAlpha;
|
|
94
|
+
}
|
|
95
|
+
if ($isActive) {
|
|
96
|
+
return $error ? colors.pressedNegativeAlpha : colors.pressedOverlayAlpha;
|
|
97
|
+
}
|
|
98
|
+
return 'transparent';
|
|
99
|
+
}
|
|
100
|
+
function getLabelColor(props) {
|
|
101
|
+
const {
|
|
102
|
+
$disabled,
|
|
103
|
+
$theme
|
|
104
|
+
} = props;
|
|
105
|
+
const {
|
|
106
|
+
colors
|
|
107
|
+
} = $theme;
|
|
108
|
+
return $disabled ? colors.contentStateDisabled : colors.contentPrimary;
|
|
109
|
+
}
|
|
110
|
+
const Root = exports.Root = (0, _styles.styled)('label', props => {
|
|
111
|
+
const {
|
|
112
|
+
$disabled,
|
|
113
|
+
$theme
|
|
114
|
+
} = props;
|
|
115
|
+
const {
|
|
116
|
+
sizing
|
|
117
|
+
} = $theme;
|
|
118
|
+
return {
|
|
119
|
+
flexDirection: 'row',
|
|
120
|
+
display: 'inline-flex',
|
|
121
|
+
verticalAlign: 'middle',
|
|
122
|
+
alignItems: 'flex-start',
|
|
123
|
+
cursor: $disabled ? 'not-allowed' : 'pointer',
|
|
124
|
+
userSelect: 'none',
|
|
125
|
+
'@media (pointer: coarse)': {
|
|
126
|
+
// Increase target size for touch devices to meet the minimum touch target size of 48x48dp
|
|
127
|
+
padding: sizing.scale300
|
|
128
|
+
}
|
|
129
|
+
};
|
|
130
|
+
});
|
|
131
|
+
Root.displayName = "Root";
|
|
132
|
+
Root.displayName = 'Root';
|
|
133
|
+
|
|
134
|
+
// Styled checkmark container as the state layer, backplate container
|
|
135
|
+
const CheckmarkContainer = exports.CheckmarkContainer = (0, _styles.styled)('span', props => {
|
|
136
|
+
const {
|
|
137
|
+
$theme
|
|
138
|
+
} = props;
|
|
139
|
+
const {
|
|
140
|
+
sizing
|
|
141
|
+
} = $theme;
|
|
142
|
+
const {
|
|
143
|
+
hoveredColor,
|
|
144
|
+
pressedColor
|
|
145
|
+
} = (0, _getSharedStyles.getOverlayColor)(props);
|
|
146
|
+
return {
|
|
147
|
+
display: 'flex',
|
|
148
|
+
alignItems: 'center',
|
|
149
|
+
justifyContent: 'center',
|
|
150
|
+
boxSizing: 'border-box',
|
|
151
|
+
minHeight: sizing.scale900,
|
|
152
|
+
minWidth: sizing.scale900,
|
|
153
|
+
borderRadius: sizing.scale300,
|
|
154
|
+
paddingTop: sizing.scale300,
|
|
155
|
+
paddingBottom: sizing.scale300,
|
|
156
|
+
paddingLeft: sizing.scale300,
|
|
157
|
+
paddingRight: sizing.scale300,
|
|
158
|
+
'@media (hover: hover)': {
|
|
159
|
+
':hover': {
|
|
160
|
+
backgroundColor: hoveredColor
|
|
161
|
+
}
|
|
162
|
+
},
|
|
163
|
+
':active': {
|
|
164
|
+
backgroundColor: pressedColor
|
|
165
|
+
}
|
|
166
|
+
};
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
// @ts-ignore
|
|
170
|
+
CheckmarkContainer.displayName = "CheckmarkContainer";
|
|
171
|
+
const Checkmark = exports.Checkmark = (0, _styles.styled)('span', props => {
|
|
172
|
+
const {
|
|
173
|
+
$checked,
|
|
174
|
+
$isIndeterminate,
|
|
175
|
+
$theme,
|
|
176
|
+
$isFocusVisible,
|
|
177
|
+
$isFocused
|
|
178
|
+
} = props;
|
|
179
|
+
const {
|
|
180
|
+
sizing,
|
|
181
|
+
animation
|
|
182
|
+
} = $theme;
|
|
183
|
+
const tickColor = $theme.colors.contentInversePrimary;
|
|
184
|
+
const indeterminate = encodeURIComponent(`
|
|
185
|
+
<svg width="17" height="17" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
|
|
186
|
+
<path d="M18 10.5H6v3h12v-3Z" fill="${tickColor}"/>
|
|
187
|
+
</svg>
|
|
188
|
+
`);
|
|
189
|
+
const check = encodeURIComponent(`
|
|
190
|
+
<svg width="17" height="17" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
|
|
191
|
+
<path d="m10 17.34-4.56-4.56 2.12-2.12L10 13.1l6.44-6.44 2.12 2.12L10 17.34Z" fill="${tickColor}"/>
|
|
192
|
+
</svg>
|
|
193
|
+
`);
|
|
194
|
+
const borderRadius = sizing.scale100;
|
|
195
|
+
const borderColor = getBorderColor(props);
|
|
196
|
+
return {
|
|
197
|
+
flex: '0 0 auto',
|
|
198
|
+
transitionDuration: animation.timing200,
|
|
199
|
+
transitionTimingFunction: animation.easeOutCurve,
|
|
200
|
+
transitionProperty: 'background-image, border-color, background-color',
|
|
201
|
+
width: '17px',
|
|
202
|
+
height: '17px',
|
|
203
|
+
boxSizing: 'border-box',
|
|
204
|
+
borderLeftStyle: 'solid',
|
|
205
|
+
borderRightStyle: 'solid',
|
|
206
|
+
borderTopStyle: 'solid',
|
|
207
|
+
borderBottomStyle: 'solid',
|
|
208
|
+
borderLeftWidth: sizing.scale0,
|
|
209
|
+
borderRightWidth: sizing.scale0,
|
|
210
|
+
borderTopWidth: sizing.scale0,
|
|
211
|
+
borderBottomWidth: sizing.scale0,
|
|
212
|
+
borderLeftColor: borderColor,
|
|
213
|
+
borderRightColor: borderColor,
|
|
214
|
+
borderTopColor: borderColor,
|
|
215
|
+
borderBottomColor: borderColor,
|
|
216
|
+
borderTopLeftRadius: borderRadius,
|
|
217
|
+
borderTopRightRadius: borderRadius,
|
|
218
|
+
borderBottomRightRadius: borderRadius,
|
|
219
|
+
borderBottomLeftRadius: borderRadius,
|
|
220
|
+
// Apply focus outline style if the checkbox is focused and focus is visible(focused by Tab)
|
|
221
|
+
...($isFocusVisible && $isFocused ? (0, _getSharedStyles.getFocusOutlineStyle)($theme) : {}),
|
|
222
|
+
display: 'inline-block',
|
|
223
|
+
verticalAlign: 'middle',
|
|
224
|
+
backgroundImage: $isIndeterminate ? `url('data:image/svg+xml,${indeterminate}');` : $checked ? `url('data:image/svg+xml,${check}');` : null,
|
|
225
|
+
backgroundColor: getBackgroundColor(props),
|
|
226
|
+
backgroundRepeat: 'no-repeat',
|
|
227
|
+
backgroundPosition: 'center'
|
|
228
|
+
};
|
|
229
|
+
});
|
|
230
|
+
Checkmark.displayName = "Checkmark";
|
|
231
|
+
Checkmark.displayName = 'Checkmark';
|
|
232
|
+
const Label = exports.Label = (0, _styles.styled)('div', props => {
|
|
233
|
+
const {
|
|
234
|
+
$theme
|
|
235
|
+
} = props;
|
|
236
|
+
const {
|
|
237
|
+
typography,
|
|
238
|
+
sizing
|
|
239
|
+
} = $theme;
|
|
240
|
+
return {
|
|
241
|
+
verticalAlign: 'middle',
|
|
242
|
+
paddingTop: sizing.scale200,
|
|
243
|
+
// top padding to make checkbox aligned with first row of the label
|
|
244
|
+
...getLabelPadding(props),
|
|
245
|
+
color: getLabelColor(props),
|
|
246
|
+
...typography.ParagraphSmall
|
|
247
|
+
};
|
|
248
|
+
});
|
|
249
|
+
Label.displayName = "Label";
|
|
250
|
+
Label.displayName = 'Label';
|
|
251
|
+
|
|
252
|
+
// tricky style for focus event cause display: none doesn't work
|
|
253
|
+
const Input = exports.Input = (0, _styles.styled)('input', {
|
|
254
|
+
opacity: 0,
|
|
255
|
+
width: 0,
|
|
256
|
+
height: 0,
|
|
257
|
+
overflow: 'hidden',
|
|
258
|
+
margin: 0,
|
|
259
|
+
padding: 0,
|
|
260
|
+
position: 'absolute'
|
|
261
|
+
});
|
|
262
|
+
Input.displayName = "Input";
|
|
263
|
+
Input.displayName = 'Input';
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
import type * as React from 'react';
|
|
2
|
+
import type { Override } from '../helpers/overrides';
|
|
3
|
+
export type LabelPlacement = 'left' | 'right';
|
|
4
|
+
export type CheckboxOverrides = {
|
|
5
|
+
Checkmark?: Override;
|
|
6
|
+
CheckmarkContainer?: Override;
|
|
7
|
+
Label?: Override;
|
|
8
|
+
Root?: Override;
|
|
9
|
+
Input?: Override;
|
|
10
|
+
};
|
|
11
|
+
export type DefaultProps = {
|
|
12
|
+
overrides?: any;
|
|
13
|
+
children?: React.ReactNode;
|
|
14
|
+
checked: boolean;
|
|
15
|
+
disabled: boolean;
|
|
16
|
+
error: boolean;
|
|
17
|
+
autoFocus: boolean;
|
|
18
|
+
isIndeterminate: boolean;
|
|
19
|
+
labelPlacement: LabelPlacement;
|
|
20
|
+
onChange: (e: React.ChangeEvent<HTMLInputElement>) => unknown;
|
|
21
|
+
onMouseEnter: (e: React.ChangeEvent<HTMLInputElement>) => unknown;
|
|
22
|
+
onMouseLeave: (e: React.ChangeEvent<HTMLInputElement>) => unknown;
|
|
23
|
+
onMouseDown: (e: React.ChangeEvent<HTMLInputElement>) => unknown;
|
|
24
|
+
onMouseUp: (e: React.ChangeEvent<HTMLInputElement>) => unknown;
|
|
25
|
+
onFocus: (e: React.ChangeEvent<HTMLInputElement>) => unknown;
|
|
26
|
+
onBlur: (e: React.ChangeEvent<HTMLInputElement>) => unknown;
|
|
27
|
+
containsInteractiveElement?: boolean;
|
|
28
|
+
};
|
|
29
|
+
export type CheckboxProps = {
|
|
30
|
+
/** Id of element which contains a related caption */
|
|
31
|
+
'aria-describedby'?: string;
|
|
32
|
+
/** Id of element which contains a related error message */
|
|
33
|
+
'aria-errormessage'?: string;
|
|
34
|
+
/** Passed to the input element aria-label attribute. */
|
|
35
|
+
ariaLabel?: string;
|
|
36
|
+
'aria-label'?: string;
|
|
37
|
+
/** Ids of element which this checkbox controls, may be useful when there is a master checkbox controlling multiple child checkboxes - https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Reference/Attributes/aria-controls */
|
|
38
|
+
'aria-controls'?: string;
|
|
39
|
+
/** Component or String value for label of checkbox. */
|
|
40
|
+
children?: React.ReactNode;
|
|
41
|
+
/** Indicates if this checkbox children contain an interactive element (prevents the label from moving focus from the child element to the radio button) */
|
|
42
|
+
containsInteractiveElement?: boolean;
|
|
43
|
+
overrides?: CheckboxOverrides;
|
|
44
|
+
/** Check or uncheck the control. */
|
|
45
|
+
checked?: boolean;
|
|
46
|
+
/** Disable the checkbox from being changed. */
|
|
47
|
+
disabled?: boolean;
|
|
48
|
+
/** Marks the checkbox as required. */
|
|
49
|
+
required?: boolean;
|
|
50
|
+
/** Renders checkbox in errored state. */
|
|
51
|
+
error?: boolean;
|
|
52
|
+
/** Used to get a ref to the input element. Useful for programmatically focusing the input */
|
|
53
|
+
inputRef?: React.RefObject<HTMLInputElement>;
|
|
54
|
+
/** Focus the checkbox on initial render. */
|
|
55
|
+
autoFocus?: boolean;
|
|
56
|
+
/** Passed to the input element id attribute */
|
|
57
|
+
id?: string;
|
|
58
|
+
/** Passed to the input element name attribute */
|
|
59
|
+
name?: string;
|
|
60
|
+
/** Passed to the input element value attribute */
|
|
61
|
+
value?: string;
|
|
62
|
+
/** Indicates a 'half' state for the checkmark. In this case, `checked` is ignored. */
|
|
63
|
+
isIndeterminate?: boolean;
|
|
64
|
+
/** How to position the label relative to the checkbox itself. */
|
|
65
|
+
labelPlacement?: LabelPlacement;
|
|
66
|
+
/** Text to display in native OS tooltip on long hover. */
|
|
67
|
+
title?: string | null;
|
|
68
|
+
/** Handler for change events on trigger element. */
|
|
69
|
+
onChange?: (e: React.ChangeEvent<HTMLInputElement>) => unknown;
|
|
70
|
+
/** Handler for mouseenter events on trigger element. */
|
|
71
|
+
onMouseEnter?: (e: React.ChangeEvent<HTMLInputElement>) => unknown;
|
|
72
|
+
/** Handler for mouseleave events on trigger element. */
|
|
73
|
+
onMouseLeave?: (e: React.ChangeEvent<HTMLInputElement>) => unknown;
|
|
74
|
+
/** Handler for mousedown events on trigger element. */
|
|
75
|
+
onMouseDown?: (e: React.ChangeEvent<HTMLInputElement>) => unknown;
|
|
76
|
+
/** Handler for mouseup events on trigger element. */
|
|
77
|
+
onMouseUp?: (e: React.ChangeEvent<HTMLInputElement>) => unknown;
|
|
78
|
+
/** handler for focus events on trigger element. */
|
|
79
|
+
onFocus?: (e: React.ChangeEvent<HTMLInputElement>) => unknown;
|
|
80
|
+
/** handler for blur events on trigger element. */
|
|
81
|
+
onBlur?: (e: React.ChangeEvent<HTMLInputElement>) => unknown;
|
|
82
|
+
/** Handler for keydown events on trigger element. */
|
|
83
|
+
onKeyDown?: (e: React.KeyboardEvent<HTMLInputElement>) => unknown;
|
|
84
|
+
/** Handler for keyup events on trigger element. */
|
|
85
|
+
onKeyUp?: (e: React.KeyboardEvent<HTMLInputElement>) => unknown;
|
|
86
|
+
};
|
|
87
|
+
export type CheckboxState = {
|
|
88
|
+
isFocused: boolean;
|
|
89
|
+
isFocusVisible: boolean;
|
|
90
|
+
isHovered: boolean;
|
|
91
|
+
isActive: boolean;
|
|
92
|
+
};
|
|
93
|
+
export type CheckboxReducerState = {
|
|
94
|
+
checked?: boolean;
|
|
95
|
+
isIndeterminate?: boolean;
|
|
96
|
+
};
|
|
97
|
+
export type StateReducer = (stateType: string, nextState: CheckboxReducerState, currentState: CheckboxReducerState, event: React.ChangeEvent<HTMLInputElement>) => CheckboxReducerState;
|
|
98
|
+
export type StatefulContainerChildProps = {
|
|
99
|
+
overrides?: CheckboxOverrides;
|
|
100
|
+
/** Handler for change events on trigger element. */
|
|
101
|
+
onChange?: (e: React.ChangeEvent<HTMLInputElement>) => unknown;
|
|
102
|
+
/** Handler for mouseenter events on trigger element. */
|
|
103
|
+
onMouseEnter?: (e: React.ChangeEvent<HTMLInputElement>) => unknown;
|
|
104
|
+
/** Handler for mouseleave events on trigger element. */
|
|
105
|
+
onMouseLeave?: (e: React.ChangeEvent<HTMLInputElement>) => unknown;
|
|
106
|
+
/** Handler for mousedown events on trigger element. */
|
|
107
|
+
onMouseDown?: (e: React.ChangeEvent<HTMLInputElement>) => unknown;
|
|
108
|
+
/** Handler for mouseup events on trigger element. */
|
|
109
|
+
onMouseUp?: (e: React.ChangeEvent<HTMLInputElement>) => unknown;
|
|
110
|
+
/** Handler for focus events on trigger element. */
|
|
111
|
+
onFocus?: (e: React.ChangeEvent<HTMLInputElement>) => unknown;
|
|
112
|
+
/** Handler for blur events on trigger element. */
|
|
113
|
+
onBlur?: (e: React.ChangeEvent<HTMLInputElement>) => unknown;
|
|
114
|
+
/** Handler for keydown events on trigger element. */
|
|
115
|
+
onKeyDown?: (e: React.KeyboardEvent<HTMLInputElement>) => unknown;
|
|
116
|
+
/** Handler for keyup events on trigger element. */
|
|
117
|
+
onKeyUp?: (e: React.KeyboardEvent<HTMLInputElement>) => unknown;
|
|
118
|
+
/** Focus the checkbox on initial render. */
|
|
119
|
+
autoFocus?: boolean;
|
|
120
|
+
} & CheckboxReducerState;
|
|
121
|
+
export type StatefulContainerProps = {
|
|
122
|
+
overrides?: CheckboxOverrides;
|
|
123
|
+
/** Component or String value for label of checkbox. */
|
|
124
|
+
children?: (a: StatefulContainerChildProps) => React.ReactNode;
|
|
125
|
+
/** Defines the components initial state value */
|
|
126
|
+
initialState?: CheckboxReducerState;
|
|
127
|
+
/** A state change handler. Used to override default state transitions. */
|
|
128
|
+
stateReducer?: StateReducer;
|
|
129
|
+
/** Handler for change events on trigger element. */
|
|
130
|
+
onChange?: (e: React.ChangeEvent<HTMLInputElement>) => unknown;
|
|
131
|
+
/** Handler for mouseenter events on trigger element. */
|
|
132
|
+
onMouseEnter?: (e: React.ChangeEvent<HTMLInputElement>) => unknown;
|
|
133
|
+
/** Handler for mouseleave events on trigger element. */
|
|
134
|
+
onMouseLeave?: (e: React.ChangeEvent<HTMLInputElement>) => unknown;
|
|
135
|
+
/** Handler for mousedown events on trigger element. */
|
|
136
|
+
onMouseDown?: (e: React.ChangeEvent<HTMLInputElement>) => unknown;
|
|
137
|
+
/** Handler for mouseup events on trigger element. */
|
|
138
|
+
onMouseUp?: (e: React.ChangeEvent<HTMLInputElement>) => unknown;
|
|
139
|
+
/** Handler for focus events on trigger element. */
|
|
140
|
+
onFocus?: (e: React.ChangeEvent<HTMLInputElement>) => unknown;
|
|
141
|
+
/** Handler for blur events on trigger element. */
|
|
142
|
+
onBlur?: (e: React.ChangeEvent<HTMLInputElement>) => unknown;
|
|
143
|
+
/** Handler for keydown events on trigger element. */
|
|
144
|
+
onKeyDown?: (e: React.KeyboardEvent<HTMLInputElement>) => unknown;
|
|
145
|
+
/** Handler for keyup events on trigger element. */
|
|
146
|
+
onKeyUp?: (e: React.KeyboardEvent<HTMLInputElement>) => unknown;
|
|
147
|
+
/** Focus the checkbox on initial render. */
|
|
148
|
+
autoFocus?: boolean;
|
|
149
|
+
};
|
|
150
|
+
export type StatefulCheckboxProps = Omit<StatefulContainerProps, 'children'> & Omit<CheckboxProps, 'value' | keyof DefaultProps> & Partial<DefaultProps>;
|
|
151
|
+
export type SharedStyleProps = {
|
|
152
|
+
$isFocused: boolean;
|
|
153
|
+
$isFocusVisible: boolean;
|
|
154
|
+
$isHovered: boolean;
|
|
155
|
+
$isActive: boolean;
|
|
156
|
+
$error: boolean;
|
|
157
|
+
$checked: boolean;
|
|
158
|
+
$isIndeterminate: boolean;
|
|
159
|
+
$required: boolean;
|
|
160
|
+
$disabled: boolean;
|
|
161
|
+
$value: string;
|
|
162
|
+
$labelPlacement?: LabelPlacement;
|
|
163
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";
|
package/package.json
CHANGED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export declare const STATE_TYPE: Readonly<{
|
|
2
|
+
readonly change: "CHANGE";
|
|
3
|
+
}>;
|
|
4
|
+
export declare const LABEL_PLACEMENT: Readonly<{
|
|
5
|
+
readonly left: "left";
|
|
6
|
+
readonly right: "right";
|
|
7
|
+
}>;
|
|
8
|
+
export declare const SIZE: Readonly<{
|
|
9
|
+
readonly default: "default";
|
|
10
|
+
readonly small: "small";
|
|
11
|
+
}>;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.STATE_TYPE = exports.SIZE = exports.LABEL_PLACEMENT = void 0;
|
|
7
|
+
/*
|
|
8
|
+
Copyright (c) Uber Technologies, Inc.
|
|
9
|
+
|
|
10
|
+
This source code is licensed under the MIT license found in the
|
|
11
|
+
LICENSE file in the root directory of this source tree.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
const STATE_TYPE = exports.STATE_TYPE = Object.freeze({
|
|
15
|
+
change: 'CHANGE'
|
|
16
|
+
});
|
|
17
|
+
const LABEL_PLACEMENT = exports.LABEL_PLACEMENT = Object.freeze({
|
|
18
|
+
left: 'left',
|
|
19
|
+
right: 'right'
|
|
20
|
+
});
|
|
21
|
+
const SIZE = exports.SIZE = Object.freeze({
|
|
22
|
+
default: 'default',
|
|
23
|
+
small: 'small'
|
|
24
|
+
});
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { default as StatefulSwitch } from './stateful-switch';
|
|
2
|
+
export { default as StatefulContainer } from './stateful-switch-container';
|
|
3
|
+
export { default as Switch } from './switch';
|
|
4
|
+
export { Root as StyledRoot, Toggle as StyledToggle, ToggleTrack as StyledToggleTrack, Label as StyledLabel, Input as StyledInput, } from './styled-components';
|
|
5
|
+
export { STATE_TYPE, LABEL_PLACEMENT, SIZE } from './constants';
|
|
6
|
+
export * from './types';
|
package/switch/index.js
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
var _exportNames = {
|
|
7
|
+
StatefulSwitch: true,
|
|
8
|
+
StatefulContainer: true,
|
|
9
|
+
Switch: true,
|
|
10
|
+
StyledRoot: true,
|
|
11
|
+
StyledToggle: true,
|
|
12
|
+
StyledToggleTrack: true,
|
|
13
|
+
StyledLabel: true,
|
|
14
|
+
StyledInput: true,
|
|
15
|
+
STATE_TYPE: true,
|
|
16
|
+
LABEL_PLACEMENT: true,
|
|
17
|
+
SIZE: true
|
|
18
|
+
};
|
|
19
|
+
Object.defineProperty(exports, "LABEL_PLACEMENT", {
|
|
20
|
+
enumerable: true,
|
|
21
|
+
get: function () {
|
|
22
|
+
return _constants.LABEL_PLACEMENT;
|
|
23
|
+
}
|
|
24
|
+
});
|
|
25
|
+
Object.defineProperty(exports, "SIZE", {
|
|
26
|
+
enumerable: true,
|
|
27
|
+
get: function () {
|
|
28
|
+
return _constants.SIZE;
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
Object.defineProperty(exports, "STATE_TYPE", {
|
|
32
|
+
enumerable: true,
|
|
33
|
+
get: function () {
|
|
34
|
+
return _constants.STATE_TYPE;
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
Object.defineProperty(exports, "StatefulContainer", {
|
|
38
|
+
enumerable: true,
|
|
39
|
+
get: function () {
|
|
40
|
+
return _statefulSwitchContainer.default;
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
Object.defineProperty(exports, "StatefulSwitch", {
|
|
44
|
+
enumerable: true,
|
|
45
|
+
get: function () {
|
|
46
|
+
return _statefulSwitch.default;
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
Object.defineProperty(exports, "StyledInput", {
|
|
50
|
+
enumerable: true,
|
|
51
|
+
get: function () {
|
|
52
|
+
return _styledComponents.Input;
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
Object.defineProperty(exports, "StyledLabel", {
|
|
56
|
+
enumerable: true,
|
|
57
|
+
get: function () {
|
|
58
|
+
return _styledComponents.Label;
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
Object.defineProperty(exports, "StyledRoot", {
|
|
62
|
+
enumerable: true,
|
|
63
|
+
get: function () {
|
|
64
|
+
return _styledComponents.Root;
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
Object.defineProperty(exports, "StyledToggle", {
|
|
68
|
+
enumerable: true,
|
|
69
|
+
get: function () {
|
|
70
|
+
return _styledComponents.Toggle;
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
Object.defineProperty(exports, "StyledToggleTrack", {
|
|
74
|
+
enumerable: true,
|
|
75
|
+
get: function () {
|
|
76
|
+
return _styledComponents.ToggleTrack;
|
|
77
|
+
}
|
|
78
|
+
});
|
|
79
|
+
Object.defineProperty(exports, "Switch", {
|
|
80
|
+
enumerable: true,
|
|
81
|
+
get: function () {
|
|
82
|
+
return _switch.default;
|
|
83
|
+
}
|
|
84
|
+
});
|
|
85
|
+
var _statefulSwitch = _interopRequireDefault(require("./stateful-switch"));
|
|
86
|
+
var _statefulSwitchContainer = _interopRequireDefault(require("./stateful-switch-container"));
|
|
87
|
+
var _switch = _interopRequireDefault(require("./switch"));
|
|
88
|
+
var _styledComponents = require("./styled-components");
|
|
89
|
+
var _constants = require("./constants");
|
|
90
|
+
var _types = require("./types");
|
|
91
|
+
Object.keys(_types).forEach(function (key) {
|
|
92
|
+
if (key === "default" || key === "__esModule") return;
|
|
93
|
+
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
|
|
94
|
+
if (key in exports && exports[key] === _types[key]) return;
|
|
95
|
+
Object.defineProperty(exports, key, {
|
|
96
|
+
enumerable: true,
|
|
97
|
+
get: function () {
|
|
98
|
+
return _types[key];
|
|
99
|
+
}
|
|
100
|
+
});
|
|
101
|
+
});
|
|
102
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
@@ -0,0 +1,97 @@
|
|
|
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 _constants = require("./constants");
|
|
9
|
+
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); }
|
|
10
|
+
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; }
|
|
11
|
+
/*
|
|
12
|
+
Copyright (c) Uber Technologies, Inc.
|
|
13
|
+
|
|
14
|
+
This source code is licensed under the MIT license found in the
|
|
15
|
+
LICENSE file in the root directory of this source tree.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
const defaultStateReducer = (type, nextState, currentState) => ({
|
|
19
|
+
...currentState,
|
|
20
|
+
...nextState
|
|
21
|
+
});
|
|
22
|
+
const StatefulSwitchContainer = props => {
|
|
23
|
+
const {
|
|
24
|
+
initialState = {
|
|
25
|
+
checked: false
|
|
26
|
+
},
|
|
27
|
+
stateReducer = defaultStateReducer,
|
|
28
|
+
onChange = () => {},
|
|
29
|
+
onMouseEnter = () => {},
|
|
30
|
+
onMouseLeave = () => {},
|
|
31
|
+
onMouseDown = () => {},
|
|
32
|
+
onMouseUp = () => {},
|
|
33
|
+
onFocus = () => {},
|
|
34
|
+
onBlur = () => {},
|
|
35
|
+
onKeyDown = () => {},
|
|
36
|
+
onKeyUp = () => {},
|
|
37
|
+
children = childProps => null,
|
|
38
|
+
...restProps
|
|
39
|
+
} = props;
|
|
40
|
+
const [checked, setChecked] = React.useState(initialState.checked);
|
|
41
|
+
const updateState = React.useCallback((type, e) => {
|
|
42
|
+
let nextState = {};
|
|
43
|
+
switch (type) {
|
|
44
|
+
case _constants.STATE_TYPE.change:
|
|
45
|
+
nextState = {
|
|
46
|
+
checked: e.target.checked
|
|
47
|
+
};
|
|
48
|
+
break;
|
|
49
|
+
}
|
|
50
|
+
const newState = stateReducer(type, nextState, {
|
|
51
|
+
checked
|
|
52
|
+
}, e);
|
|
53
|
+
setChecked(newState.checked);
|
|
54
|
+
}, [checked, stateReducer]);
|
|
55
|
+
const onChangeHandler = React.useCallback(e => {
|
|
56
|
+
updateState(_constants.STATE_TYPE.change, e);
|
|
57
|
+
onChange && onChange(e);
|
|
58
|
+
}, [updateState, onChange]);
|
|
59
|
+
const onMouseEnterHandler = React.useCallback(e => {
|
|
60
|
+
onMouseEnter && onMouseEnter(e);
|
|
61
|
+
}, [onMouseEnter]);
|
|
62
|
+
const onMouseLeaveHandler = React.useCallback(e => {
|
|
63
|
+
onMouseLeave && onMouseLeave(e);
|
|
64
|
+
}, [onMouseLeave]);
|
|
65
|
+
const onFocusHandler = React.useCallback(e => {
|
|
66
|
+
onFocus && onFocus(e);
|
|
67
|
+
}, [onFocus]);
|
|
68
|
+
const onBlurHandler = React.useCallback(e => {
|
|
69
|
+
onBlur && onBlur(e);
|
|
70
|
+
}, [onBlur]);
|
|
71
|
+
const onMouseDownHandler = React.useCallback(e => {
|
|
72
|
+
onMouseDown && onMouseDown(e);
|
|
73
|
+
}, [onMouseDown]);
|
|
74
|
+
const onMouseUpHandler = React.useCallback(e => {
|
|
75
|
+
onMouseUp && onMouseUp(e);
|
|
76
|
+
}, [onMouseUp]);
|
|
77
|
+
const onKeyDownHandler = React.useCallback(event => {
|
|
78
|
+
onKeyDown && onKeyDown(event);
|
|
79
|
+
}, [onKeyDown]);
|
|
80
|
+
const onKeyUpHandler = React.useCallback(event => {
|
|
81
|
+
onKeyUp && onKeyUp(event);
|
|
82
|
+
}, [onKeyUp]);
|
|
83
|
+
return children({
|
|
84
|
+
...restProps,
|
|
85
|
+
checked,
|
|
86
|
+
onChange: onChangeHandler,
|
|
87
|
+
onMouseEnter: onMouseEnterHandler,
|
|
88
|
+
onMouseLeave: onMouseLeaveHandler,
|
|
89
|
+
onMouseDown: onMouseDownHandler,
|
|
90
|
+
onMouseUp: onMouseUpHandler,
|
|
91
|
+
onFocus: onFocusHandler,
|
|
92
|
+
onBlur: onBlurHandler,
|
|
93
|
+
onKeyDown: onKeyDownHandler,
|
|
94
|
+
onKeyUp: onKeyUpHandler
|
|
95
|
+
});
|
|
96
|
+
};
|
|
97
|
+
var _default = exports.default = StatefulSwitchContainer;
|