@toptal/picasso-switch 1.0.10-alpha-fx-4618-migrate-section-bdcbfe499.9 → 1.0.11-alpha-FX-4613-delete-input-label-724841a39.1
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/dist-package/src/Switch/Switch.d.ts +2 -0
- package/dist-package/src/Switch/Switch.d.ts.map +1 -1
- package/dist-package/src/Switch/Switch.js +10 -25
- package/dist-package/src/Switch/Switch.js.map +1 -1
- package/dist-package/src/Switch/styles.d.ts +3 -0
- package/dist-package/src/Switch/styles.d.ts.map +1 -0
- package/dist-package/src/Switch/styles.js +79 -0
- package/dist-package/src/Switch/styles.js.map +1 -0
- package/package.json +7 -9
- package/src/Switch/Switch.tsx +15 -42
- package/src/Switch/__snapshots__/test.tsx.snap +61 -36
- package/src/Switch/styles.ts +86 -0
|
@@ -10,6 +10,8 @@ export interface Props extends BaseProps, TextLabelProps, Omit<ButtonHTMLAttribu
|
|
|
10
10
|
label?: ReactNode;
|
|
11
11
|
/** Callback invoked when `Switch` changed its value */
|
|
12
12
|
onChange?: (event: React.ChangeEvent<HTMLInputElement>, checked: boolean) => void;
|
|
13
|
+
/** Value of the `Switch` (applicable only for controlled component) */
|
|
14
|
+
value?: string;
|
|
13
15
|
}
|
|
14
16
|
export declare const Switch: React.ForwardRefExoticComponent<Props & React.RefAttributes<HTMLButtonElement>>;
|
|
15
17
|
export default Switch;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Switch.d.ts","sourceRoot":"","sources":["../../../src/Switch/Switch.tsx"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"Switch.d.ts","sourceRoot":"","sources":["../../../src/Switch/Switch.tsx"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,SAAS,EAAE,cAAc,EAAE,MAAM,wBAAwB,CAAA;AACvE,OAAO,KAAK,EAAE,oBAAoB,EAAE,SAAS,EAAE,MAAM,OAAO,CAAA;AAC5D,OAAO,KAAqB,MAAM,OAAO,CAAA;AAOzC,MAAM,WAAW,KACf,SAAQ,SAAS,EACf,cAAc,EACd,IAAI,CAAC,oBAAoB,CAAC,iBAAiB,CAAC,EAAE,UAAU,GAAG,MAAM,CAAC;IACpE,uCAAuC;IACvC,OAAO,CAAC,EAAE,OAAO,CAAA;IACjB,sCAAsC;IACtC,QAAQ,CAAC,EAAE,OAAO,CAAA;IAClB,kCAAkC;IAClC,KAAK,CAAC,EAAE,SAAS,CAAA;IACjB,uDAAuD;IACvD,QAAQ,CAAC,EAAE,CACT,KAAK,EAAE,KAAK,CAAC,WAAW,CAAC,gBAAgB,CAAC,EAC1C,OAAO,EAAE,OAAO,KACb,IAAI,CAAA;IACT,uEAAuE;IACvE,KAAK,CAAC,EAAE,MAAM,CAAA;CACf;AAED,eAAO,MAAM,MAAM,iFAsDjB,CAAA;AASF,eAAe,MAAM,CAAA"}
|
|
@@ -9,38 +9,23 @@ var __rest = (this && this.__rest) || function (s, e) {
|
|
|
9
9
|
}
|
|
10
10
|
return t;
|
|
11
11
|
};
|
|
12
|
-
import { Switch as MUISwitch } from '@
|
|
12
|
+
import { Switch as MUISwitch } from '@material-ui/core';
|
|
13
|
+
import { makeStyles } from '@material-ui/core/styles';
|
|
13
14
|
import React, { forwardRef } from 'react';
|
|
14
15
|
import { FormControlLabel } from '@toptal/picasso-form';
|
|
15
|
-
import
|
|
16
|
+
import styles from './styles';
|
|
17
|
+
const useStyles = makeStyles(styles, { name: 'PicassoSwitch' });
|
|
16
18
|
export const Switch = forwardRef(function Switch(props, ref) {
|
|
17
|
-
const { label, id, className, style, disabled, onChange, checked, titleCase, color, // eslint-disable-line
|
|
18
|
-
'data-testid': dataTestId } = props, rest = __rest(props, ["label", "id", "className", "style", "disabled", "onChange", "checked", "titleCase", "color", 'data-testid']);
|
|
19
|
-
const
|
|
20
|
-
|
|
21
|
-
onChange(event, event.target.checked);
|
|
22
|
-
}
|
|
23
|
-
};
|
|
24
|
-
const switchElement = (React.createElement(MUISwitch, Object.assign({}, rest, { color: 'primary', ref: ref, checked: checked, className: className, style: style, disabled: disabled, id: id, onChange: onChangeCallback, "data-testid": label ? undefined : dataTestId, slotProps: {
|
|
25
|
-
root: {
|
|
26
|
-
className: 'w-[40px] h-[24px] p-0 relative inline-flex z-0 overflow-visible shrink-0 align-middle group',
|
|
27
|
-
},
|
|
28
|
-
track: {
|
|
29
|
-
className: cx('w-full h-full border border-solid bg-gray-600 border-gray-600 opacity-100 rounded-[12px]', 'transition-colors duration-300 ease-out', 'group-[.base--checked]:bg-blue-500 group-[.base--checked]:border-blue-500', 'group-[.base--disabled]:opacity-40', 'group-[.base--disabled:not(.base--checked)]:bg-black'),
|
|
30
|
-
},
|
|
31
|
-
thumb: {
|
|
32
|
-
className: cx('w-[22px] h-[22px] bg-current text-white block rounded-full shadow-1 absolute z-10 p-0 top-[1px] left-[1px]', 'transition-transform duration-150 ease-out', 'group-[:not(.base--disabled):hover]:shadow-[0_0_0_4px_rgba(32,78,207,0.48)]', 'group-[.base--focusVisible]:shadow-[0_0_0_4px_rgba(32,78,207,0.48)]', 'group-[.base--checked]:translate-x-[16px]'),
|
|
33
|
-
},
|
|
34
|
-
input: {
|
|
35
|
-
className: cx('w-full h-full m-0 p-0 opacity-0 absolute top-0 left-0 cursor-pointer z-20', 'group-[.base--disabled]:cursor-default'),
|
|
36
|
-
},
|
|
37
|
-
} })));
|
|
19
|
+
const { label, id, className, style, disabled, onChange, value, checked, titleCase, color, // eslint-disable-line
|
|
20
|
+
'data-testid': dataTestId } = props, rest = __rest(props, ["label", "id", "className", "style", "disabled", "onChange", "value", "checked", "titleCase", "color", 'data-testid']);
|
|
21
|
+
const classes = useStyles();
|
|
22
|
+
const switchElement = (React.createElement(MUISwitch, Object.assign({}, rest, { color: 'primary', ref: ref, checked: checked, className: className, style: style, disabled: disabled, id: id, onChange: onChange, value: value, "data-testid": label ? undefined : dataTestId })));
|
|
38
23
|
if (!label) {
|
|
39
24
|
return switchElement;
|
|
40
25
|
}
|
|
41
26
|
return (React.createElement(FormControlLabel, { classes: {
|
|
42
|
-
root:
|
|
43
|
-
label:
|
|
27
|
+
root: classes.root,
|
|
28
|
+
label: classes.label,
|
|
44
29
|
}, control: switchElement, disabled: disabled, label: label, titleCase: titleCase, "data-testid": dataTestId }));
|
|
45
30
|
});
|
|
46
31
|
Switch.defaultProps = {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Switch.js","sourceRoot":"","sources":["../../../src/Switch/Switch.tsx"],"names":[],"mappings":";;;;;;;;;;;AAAA,OAAO,EAAE,MAAM,IAAI,SAAS,EAAE,MAAM,
|
|
1
|
+
{"version":3,"file":"Switch.js","sourceRoot":"","sources":["../../../src/Switch/Switch.tsx"],"names":[],"mappings":";;;;;;;;;;;AAAA,OAAO,EAAE,MAAM,IAAI,SAAS,EAAE,MAAM,mBAAmB,CAAA;AAEvD,OAAO,EAAE,UAAU,EAAE,MAAM,0BAA0B,CAAA;AAGrD,OAAO,KAAK,EAAE,EAAE,UAAU,EAAE,MAAM,OAAO,CAAA;AACzC,OAAO,EAAE,gBAAgB,EAAE,MAAM,sBAAsB,CAAA;AAEvD,OAAO,MAAM,MAAM,UAAU,CAAA;AAE7B,MAAM,SAAS,GAAG,UAAU,CAAQ,MAAM,EAAE,EAAE,IAAI,EAAE,eAAe,EAAE,CAAC,CAAA;AAqBtE,MAAM,CAAC,MAAM,MAAM,GAAG,UAAU,CAA2B,SAAS,MAAM,CACxE,KAAK,EACL,GAAG;IAEH,MAAM,EACJ,KAAK,EACL,EAAE,EACF,SAAS,EACT,KAAK,EACL,QAAQ,EACR,QAAQ,EACR,KAAK,EACL,OAAO,EACP,SAAS,EACT,KAAK,EAAE,sBAAsB;IAC7B,aAAa,EAAE,UAAU,KAEvB,KAAK,EADJ,IAAI,UACL,KAAK,EAbH,sHAaL,CAAQ,CAAA;IAET,MAAM,OAAO,GAAG,SAAS,EAAE,CAAA;IAE3B,MAAM,aAAa,GAAG,CACpB,oBAAC,SAAS,oBACJ,IAAI,IACR,KAAK,EAAC,SAAS,EACf,GAAG,EAAE,GAAG,EACR,OAAO,EAAE,OAAO,EAChB,SAAS,EAAE,SAAS,EACpB,KAAK,EAAE,KAAK,EACZ,QAAQ,EAAE,QAAQ,EAClB,EAAE,EAAE,EAAE,EACN,QAAQ,EAAE,QAAQ,EAClB,KAAK,EAAE,KAAK,iBACC,KAAK,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,UAAU,IAC3C,CACH,CAAA;IAED,IAAI,CAAC,KAAK,EAAE;QACV,OAAO,aAAa,CAAA;KACrB;IAED,OAAO,CACL,oBAAC,gBAAgB,IACf,OAAO,EAAE;YACP,IAAI,EAAE,OAAO,CAAC,IAAI;YAClB,KAAK,EAAE,OAAO,CAAC,KAAK;SACrB,EACD,OAAO,EAAE,aAAa,EACtB,QAAQ,EAAE,QAAQ,EAClB,KAAK,EAAE,KAAK,EACZ,SAAS,EAAE,SAAS,iBACP,UAAU,GACvB,CACH,CAAA;AACH,CAAC,CAAC,CAAA;AAEF,MAAM,CAAC,YAAY,GAAG;IACpB,QAAQ,EAAE,KAAK;IACf,QAAQ,EAAE,GAAG,EAAE,GAAE,CAAC;CACnB,CAAA;AAED,MAAM,CAAC,WAAW,GAAG,QAAQ,CAAA;AAE7B,eAAe,MAAM,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"styles.d.ts","sourceRoot":"","sources":["../../../src/Switch/styles.ts"],"names":[],"mappings":";AA4EA,wBASI"}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { createStyles } from '@material-ui/core/styles';
|
|
2
|
+
import { outline } from '@toptal/picasso-shared';
|
|
3
|
+
import { PicassoProvider } from '@toptal/picasso-provider';
|
|
4
|
+
const CONTROL_WIDTH = '1em';
|
|
5
|
+
const LABEL_LEFT_MARGIN = '0.5em';
|
|
6
|
+
const LABEL_TOP_MARGIN = '0.25em';
|
|
7
|
+
const THUMB_SIZE = 22;
|
|
8
|
+
const TRACK_HEIGHT = THUMB_SIZE + 2;
|
|
9
|
+
const TRACK_WIDTH = 40;
|
|
10
|
+
PicassoProvider.override(({ palette, transitions }) => ({
|
|
11
|
+
MuiSwitch: {
|
|
12
|
+
root: {
|
|
13
|
+
width: TRACK_WIDTH,
|
|
14
|
+
height: TRACK_HEIGHT,
|
|
15
|
+
padding: 0,
|
|
16
|
+
overflow: 'visible',
|
|
17
|
+
},
|
|
18
|
+
switchBase: {
|
|
19
|
+
top: 1,
|
|
20
|
+
left: 1,
|
|
21
|
+
padding: 0,
|
|
22
|
+
color: palette.common.white,
|
|
23
|
+
'&$checked': {
|
|
24
|
+
transform: 'translateX(16px)',
|
|
25
|
+
color: palette.common.white,
|
|
26
|
+
'& + $track': {
|
|
27
|
+
backgroundColor: palette.primary.main,
|
|
28
|
+
borderColor: palette.primary.main,
|
|
29
|
+
opacity: 1,
|
|
30
|
+
},
|
|
31
|
+
'&$disabled + $track': {
|
|
32
|
+
opacity: 0.4,
|
|
33
|
+
backgroundColor: palette.primary.main,
|
|
34
|
+
borderColor: palette.primary.main,
|
|
35
|
+
},
|
|
36
|
+
},
|
|
37
|
+
'&:hover, &.Mui-focusVisible': Object.assign({}, outline(palette.primary.main, 4)),
|
|
38
|
+
'&$disabled': {
|
|
39
|
+
'& + $track': {
|
|
40
|
+
opacity: 0.4,
|
|
41
|
+
backgroundColor: palette.grey.main2,
|
|
42
|
+
borderColor: palette.grey.main2,
|
|
43
|
+
},
|
|
44
|
+
},
|
|
45
|
+
},
|
|
46
|
+
thumb: {
|
|
47
|
+
width: THUMB_SIZE,
|
|
48
|
+
height: THUMB_SIZE,
|
|
49
|
+
color: palette.common.white,
|
|
50
|
+
// fix for bad subpixel rendering on 150% displays
|
|
51
|
+
'@media (-webkit-min-device-pixel-ratio: 1.5) and (-webkit-max-device-pixel-ratio: 1.5)': {
|
|
52
|
+
transform: 'translate(-0.2px, -0.3px)',
|
|
53
|
+
},
|
|
54
|
+
},
|
|
55
|
+
track: {
|
|
56
|
+
borderRadius: TRACK_HEIGHT / 2,
|
|
57
|
+
border: `1px solid ${palette.grey.main2}`,
|
|
58
|
+
backgroundColor: palette.grey.main2,
|
|
59
|
+
borderColor: palette.grey.main2,
|
|
60
|
+
opacity: 1,
|
|
61
|
+
transition: transitions.create(['background-color', 'border']),
|
|
62
|
+
},
|
|
63
|
+
disabled: {
|
|
64
|
+
'& + $track': {
|
|
65
|
+
opacity: 0.4,
|
|
66
|
+
},
|
|
67
|
+
},
|
|
68
|
+
},
|
|
69
|
+
}));
|
|
70
|
+
export default () => createStyles({
|
|
71
|
+
root: { alignItems: 'flex-start', fontSize: '1rem' },
|
|
72
|
+
label: {
|
|
73
|
+
marginLeft: LABEL_LEFT_MARGIN,
|
|
74
|
+
marginTop: LABEL_TOP_MARGIN,
|
|
75
|
+
// 1px is needed for safari
|
|
76
|
+
maxWidth: `calc(100% - ${CONTROL_WIDTH} - ${LABEL_LEFT_MARGIN} + 1px)`,
|
|
77
|
+
},
|
|
78
|
+
});
|
|
79
|
+
//# sourceMappingURL=styles.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"styles.js","sourceRoot":"","sources":["../../../src/Switch/styles.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,0BAA0B,CAAA;AACvD,OAAO,EAAE,OAAO,EAAE,MAAM,wBAAwB,CAAA;AAChD,OAAO,EAAE,eAAe,EAAE,MAAM,0BAA0B,CAAA;AAE1D,MAAM,aAAa,GAAG,KAAK,CAAA;AAC3B,MAAM,iBAAiB,GAAG,OAAO,CAAA;AACjC,MAAM,gBAAgB,GAAG,QAAQ,CAAA;AAEjC,MAAM,UAAU,GAAG,EAAE,CAAA;AACrB,MAAM,YAAY,GAAG,UAAU,GAAG,CAAC,CAAA;AACnC,MAAM,WAAW,GAAG,EAAE,CAAA;AAEtB,eAAe,CAAC,QAAQ,CAAC,CAAC,EAAE,OAAO,EAAE,WAAW,EAAE,EAAE,EAAE,CAAC,CAAC;IACtD,SAAS,EAAE;QACT,IAAI,EAAE;YACJ,KAAK,EAAE,WAAW;YAClB,MAAM,EAAE,YAAY;YACpB,OAAO,EAAE,CAAC;YACV,QAAQ,EAAE,SAAS;SACpB;QACD,UAAU,EAAE;YACV,GAAG,EAAE,CAAC;YACN,IAAI,EAAE,CAAC;YACP,OAAO,EAAE,CAAC;YACV,KAAK,EAAE,OAAO,CAAC,MAAM,CAAC,KAAK;YAC3B,WAAW,EAAE;gBACX,SAAS,EAAE,kBAAkB;gBAC7B,KAAK,EAAE,OAAO,CAAC,MAAM,CAAC,KAAK;gBAC3B,YAAY,EAAE;oBACZ,eAAe,EAAE,OAAO,CAAC,OAAO,CAAC,IAAI;oBACrC,WAAW,EAAE,OAAO,CAAC,OAAO,CAAC,IAAI;oBACjC,OAAO,EAAE,CAAC;iBACX;gBACD,qBAAqB,EAAE;oBACrB,OAAO,EAAE,GAAG;oBACZ,eAAe,EAAE,OAAO,CAAC,OAAO,CAAC,IAAI;oBACrC,WAAW,EAAE,OAAO,CAAC,OAAO,CAAC,IAAI;iBAClC;aACF;YACD,6BAA6B,oBACxB,OAAO,CAAC,OAAO,CAAC,OAAO,CAAC,IAAI,EAAE,CAAC,CAAC,CACpC;YACD,YAAY,EAAE;gBACZ,YAAY,EAAE;oBACZ,OAAO,EAAE,GAAG;oBACZ,eAAe,EAAE,OAAO,CAAC,IAAI,CAAC,KAAK;oBACnC,WAAW,EAAE,OAAO,CAAC,IAAI,CAAC,KAAK;iBAChC;aACF;SACF;QACD,KAAK,EAAE;YACL,KAAK,EAAE,UAAU;YACjB,MAAM,EAAE,UAAU;YAClB,KAAK,EAAE,OAAO,CAAC,MAAM,CAAC,KAAK;YAC3B,kDAAkD;YAClD,wFAAwF,EACtF;gBACE,SAAS,EAAE,2BAA2B;aACvC;SACJ;QACD,KAAK,EAAE;YACL,YAAY,EAAE,YAAY,GAAG,CAAC;YAC9B,MAAM,EAAE,aAAa,OAAO,CAAC,IAAI,CAAC,KAAK,EAAE;YACzC,eAAe,EAAE,OAAO,CAAC,IAAI,CAAC,KAAK;YACnC,WAAW,EAAE,OAAO,CAAC,IAAI,CAAC,KAAK;YAC/B,OAAO,EAAE,CAAC;YACV,UAAU,EAAE,WAAW,CAAC,MAAM,CAAC,CAAC,kBAAkB,EAAE,QAAQ,CAAC,CAAC;SAC/D;QACD,QAAQ,EAAE;YACR,YAAY,EAAE;gBACZ,OAAO,EAAE,GAAG;aACb;SACF;KACF;CACF,CAAC,CAAC,CAAA;AAEH,eAAe,GAAG,EAAE,CAClB,YAAY,CAAC;IACX,IAAI,EAAE,EAAE,UAAU,EAAE,YAAY,EAAE,QAAQ,EAAE,MAAM,EAAE;IACpD,KAAK,EAAE;QACL,UAAU,EAAE,iBAAiB;QAC7B,SAAS,EAAE,gBAAgB;QAC3B,2BAA2B;QAC3B,QAAQ,EAAE,eAAe,aAAa,MAAM,iBAAiB,SAAS;KACvE;CACF,CAAC,CAAA"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@toptal/picasso-switch",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.11-alpha-FX-4613-delete-input-label-724841a39.1+724841a39",
|
|
4
4
|
"description": "Toptal UI components library - Switch",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
@@ -22,31 +22,29 @@
|
|
|
22
22
|
},
|
|
23
23
|
"homepage": "https://github.com/toptal/picasso/tree/master/packages/picasso#readme",
|
|
24
24
|
"dependencies": {
|
|
25
|
-
"@
|
|
26
|
-
"@toptal/picasso-
|
|
27
|
-
"@toptal/picasso-shared": "15.0.1-alpha-fx-4618-migrate-section-bdcbfe499.9+bdcbfe499",
|
|
28
|
-
"classnames": "^2.5.1"
|
|
25
|
+
"@toptal/picasso-form": "3.0.3-alpha-FX-4613-delete-input-label-724841a39.1+724841a39",
|
|
26
|
+
"@toptal/picasso-shared": "15.0.1-alpha-FX-4613-delete-input-label-724841a39.7+724841a39"
|
|
29
27
|
},
|
|
30
28
|
"sideEffects": [
|
|
31
29
|
"**/styles.ts",
|
|
32
30
|
"**/styles.js"
|
|
33
31
|
],
|
|
34
32
|
"peerDependencies": {
|
|
33
|
+
"@material-ui/core": "4.12.4",
|
|
35
34
|
"@toptal/picasso-provider": "*",
|
|
36
|
-
"@toptal/picasso-tailwind": "^2.5.0",
|
|
37
35
|
"react": ">=16.12.0 < 19.0.0"
|
|
38
36
|
},
|
|
39
37
|
"exports": {
|
|
40
38
|
".": "./dist-package/src/index.js"
|
|
41
39
|
},
|
|
42
40
|
"devDependencies": {
|
|
43
|
-
"@toptal/picasso-provider": "4.2.2-alpha-
|
|
44
|
-
"@toptal/picasso-test-utils": "1.1.2-alpha-
|
|
41
|
+
"@toptal/picasso-provider": "4.2.2-alpha-FX-4613-delete-input-label-724841a39.15+724841a39",
|
|
42
|
+
"@toptal/picasso-test-utils": "1.1.2-alpha-FX-4613-delete-input-label-724841a39.7+724841a39"
|
|
45
43
|
},
|
|
46
44
|
"files": [
|
|
47
45
|
"dist-package/**",
|
|
48
46
|
"!dist-package/tsconfig.tsbuildinfo",
|
|
49
47
|
"src"
|
|
50
48
|
],
|
|
51
|
-
"gitHead": "
|
|
49
|
+
"gitHead": "724841a39519e07955691ee671f265665081d5e1"
|
|
52
50
|
}
|
package/src/Switch/Switch.tsx
CHANGED
|
@@ -1,9 +1,14 @@
|
|
|
1
|
-
import { Switch as MUISwitch } from '@
|
|
1
|
+
import { Switch as MUISwitch } from '@material-ui/core'
|
|
2
|
+
import type { Theme } from '@material-ui/core/styles'
|
|
3
|
+
import { makeStyles } from '@material-ui/core/styles'
|
|
2
4
|
import type { BaseProps, TextLabelProps } from '@toptal/picasso-shared'
|
|
3
5
|
import type { ButtonHTMLAttributes, ReactNode } from 'react'
|
|
4
6
|
import React, { forwardRef } from 'react'
|
|
5
7
|
import { FormControlLabel } from '@toptal/picasso-form'
|
|
6
|
-
|
|
8
|
+
|
|
9
|
+
import styles from './styles'
|
|
10
|
+
|
|
11
|
+
const useStyles = makeStyles<Theme>(styles, { name: 'PicassoSwitch' })
|
|
7
12
|
|
|
8
13
|
export interface Props
|
|
9
14
|
extends BaseProps,
|
|
@@ -20,6 +25,8 @@ export interface Props
|
|
|
20
25
|
event: React.ChangeEvent<HTMLInputElement>,
|
|
21
26
|
checked: boolean
|
|
22
27
|
) => void
|
|
28
|
+
/** Value of the `Switch` (applicable only for controlled component) */
|
|
29
|
+
value?: string
|
|
23
30
|
}
|
|
24
31
|
|
|
25
32
|
export const Switch = forwardRef<HTMLButtonElement, Props>(function Switch(
|
|
@@ -33,6 +40,7 @@ export const Switch = forwardRef<HTMLButtonElement, Props>(function Switch(
|
|
|
33
40
|
style,
|
|
34
41
|
disabled,
|
|
35
42
|
onChange,
|
|
43
|
+
value,
|
|
36
44
|
checked,
|
|
37
45
|
titleCase,
|
|
38
46
|
color, // eslint-disable-line
|
|
@@ -40,13 +48,7 @@ export const Switch = forwardRef<HTMLButtonElement, Props>(function Switch(
|
|
|
40
48
|
...rest
|
|
41
49
|
} = props
|
|
42
50
|
|
|
43
|
-
const
|
|
44
|
-
HTMLInputElement
|
|
45
|
-
> = event => {
|
|
46
|
-
if (onChange) {
|
|
47
|
-
onChange(event, event.target.checked)
|
|
48
|
-
}
|
|
49
|
-
}
|
|
51
|
+
const classes = useStyles()
|
|
50
52
|
|
|
51
53
|
const switchElement = (
|
|
52
54
|
<MUISwitch
|
|
@@ -58,38 +60,9 @@ export const Switch = forwardRef<HTMLButtonElement, Props>(function Switch(
|
|
|
58
60
|
style={style}
|
|
59
61
|
disabled={disabled}
|
|
60
62
|
id={id}
|
|
61
|
-
onChange={
|
|
63
|
+
onChange={onChange}
|
|
64
|
+
value={value}
|
|
62
65
|
data-testid={label ? undefined : dataTestId}
|
|
63
|
-
slotProps={{
|
|
64
|
-
root: {
|
|
65
|
-
className:
|
|
66
|
-
'w-[40px] h-[24px] p-0 relative inline-flex z-0 overflow-visible shrink-0 align-middle group',
|
|
67
|
-
},
|
|
68
|
-
track: {
|
|
69
|
-
className: cx(
|
|
70
|
-
'w-full h-full border border-solid bg-gray-600 border-gray-600 opacity-100 rounded-[12px]',
|
|
71
|
-
'transition-colors duration-300 ease-out',
|
|
72
|
-
'group-[.base--checked]:bg-blue-500 group-[.base--checked]:border-blue-500',
|
|
73
|
-
'group-[.base--disabled]:opacity-40',
|
|
74
|
-
'group-[.base--disabled:not(.base--checked)]:bg-black'
|
|
75
|
-
),
|
|
76
|
-
},
|
|
77
|
-
thumb: {
|
|
78
|
-
className: cx(
|
|
79
|
-
'w-[22px] h-[22px] bg-current text-white block rounded-full shadow-1 absolute z-10 p-0 top-[1px] left-[1px]',
|
|
80
|
-
'transition-transform duration-150 ease-out',
|
|
81
|
-
'group-[:not(.base--disabled):hover]:shadow-[0_0_0_4px_rgba(32,78,207,0.48)]',
|
|
82
|
-
'group-[.base--focusVisible]:shadow-[0_0_0_4px_rgba(32,78,207,0.48)]',
|
|
83
|
-
'group-[.base--checked]:translate-x-[16px]'
|
|
84
|
-
),
|
|
85
|
-
},
|
|
86
|
-
input: {
|
|
87
|
-
className: cx(
|
|
88
|
-
'w-full h-full m-0 p-0 opacity-0 absolute top-0 left-0 cursor-pointer z-20',
|
|
89
|
-
'group-[.base--disabled]:cursor-default'
|
|
90
|
-
),
|
|
91
|
-
},
|
|
92
|
-
}}
|
|
93
66
|
/>
|
|
94
67
|
)
|
|
95
68
|
|
|
@@ -100,8 +73,8 @@ export const Switch = forwardRef<HTMLButtonElement, Props>(function Switch(
|
|
|
100
73
|
return (
|
|
101
74
|
<FormControlLabel
|
|
102
75
|
classes={{
|
|
103
|
-
root:
|
|
104
|
-
label:
|
|
76
|
+
root: classes.root,
|
|
77
|
+
label: classes.label,
|
|
105
78
|
}}
|
|
106
79
|
control={switchElement}
|
|
107
80
|
disabled={disabled}
|
|
@@ -2,27 +2,35 @@
|
|
|
2
2
|
|
|
3
3
|
exports[`Switch behaves correctly when interacting 1`] = `
|
|
4
4
|
<label
|
|
5
|
-
class="PicassoFormControlLabel-root
|
|
5
|
+
class="PicassoFormControlLabel-root PicassoSwitch-root"
|
|
6
6
|
data-testid="switch"
|
|
7
7
|
>
|
|
8
8
|
<span
|
|
9
|
-
class="
|
|
10
|
-
color="primary"
|
|
9
|
+
class="MuiSwitch-root"
|
|
11
10
|
>
|
|
12
11
|
<span
|
|
13
|
-
|
|
14
|
-
|
|
12
|
+
aria-disabled="false"
|
|
13
|
+
class="MuiButtonBase-root MuiIconButton-root PrivateSwitchBase-root MuiSwitch-switchBase MuiSwitch-colorPrimary PrivateSwitchBase-checked Mui-checked"
|
|
14
|
+
>
|
|
15
|
+
<span
|
|
16
|
+
class="MuiIconButton-label"
|
|
17
|
+
>
|
|
18
|
+
<input
|
|
19
|
+
class="PrivateSwitchBase-input MuiSwitch-input"
|
|
20
|
+
type="checkbox"
|
|
21
|
+
value=""
|
|
22
|
+
/>
|
|
23
|
+
<span
|
|
24
|
+
class="MuiSwitch-thumb"
|
|
25
|
+
/>
|
|
26
|
+
</span>
|
|
27
|
+
</span>
|
|
15
28
|
<span
|
|
16
|
-
class="
|
|
17
|
-
/>
|
|
18
|
-
<input
|
|
19
|
-
class="base-Switch w-full h-full m-0 p-0 opacity-0 absolute top-0 left-0 cursor-pointer z-20 group-[.base--disabled]:cursor"
|
|
20
|
-
role="switch"
|
|
21
|
-
type="checkbox"
|
|
29
|
+
class="MuiSwitch-track"
|
|
22
30
|
/>
|
|
23
31
|
</span>
|
|
24
32
|
<span
|
|
25
|
-
class="PicassoFormLabel-root PicassoFormLabel-inline PicassoFormControlLabel-label
|
|
33
|
+
class="PicassoFormLabel-root PicassoFormLabel-inline PicassoFormControlLabel-label PicassoSwitch-label"
|
|
26
34
|
>
|
|
27
35
|
<span
|
|
28
36
|
class="PicassoFormLabel-medium"
|
|
@@ -39,20 +47,28 @@ exports[`Switch renders default Switch without label 1`] = `
|
|
|
39
47
|
class="Picasso-root"
|
|
40
48
|
>
|
|
41
49
|
<span
|
|
42
|
-
class="
|
|
43
|
-
color="primary"
|
|
44
|
-
data-testid="switch"
|
|
50
|
+
class="MuiSwitch-root"
|
|
45
51
|
>
|
|
46
52
|
<span
|
|
47
|
-
|
|
48
|
-
|
|
53
|
+
aria-disabled="false"
|
|
54
|
+
class="MuiButtonBase-root MuiIconButton-root PrivateSwitchBase-root MuiSwitch-switchBase MuiSwitch-colorPrimary"
|
|
55
|
+
data-testid="switch"
|
|
56
|
+
>
|
|
57
|
+
<span
|
|
58
|
+
class="MuiIconButton-label"
|
|
59
|
+
>
|
|
60
|
+
<input
|
|
61
|
+
class="PrivateSwitchBase-input MuiSwitch-input"
|
|
62
|
+
type="checkbox"
|
|
63
|
+
value=""
|
|
64
|
+
/>
|
|
65
|
+
<span
|
|
66
|
+
class="MuiSwitch-thumb"
|
|
67
|
+
/>
|
|
68
|
+
</span>
|
|
69
|
+
</span>
|
|
49
70
|
<span
|
|
50
|
-
class="
|
|
51
|
-
/>
|
|
52
|
-
<input
|
|
53
|
-
class="base-Switch w-full h-full m-0 p-0 opacity-0 absolute top-0 left-0 cursor-pointer z-20 group-[.base--disabled]:cursor"
|
|
54
|
-
role="switch"
|
|
55
|
-
type="checkbox"
|
|
71
|
+
class="MuiSwitch-track"
|
|
56
72
|
/>
|
|
57
73
|
</span>
|
|
58
74
|
</div>
|
|
@@ -61,28 +77,37 @@ exports[`Switch renders default Switch without label 1`] = `
|
|
|
61
77
|
|
|
62
78
|
exports[`Switch renders disabled state 1`] = `
|
|
63
79
|
<label
|
|
64
|
-
class="PicassoFormControlLabel-root
|
|
80
|
+
class="PicassoFormControlLabel-root PicassoSwitch-root PicassoFormControlLabel-disabled"
|
|
65
81
|
data-testid="switch"
|
|
66
82
|
>
|
|
67
83
|
<span
|
|
68
|
-
class="
|
|
69
|
-
color="primary"
|
|
84
|
+
class="MuiSwitch-root"
|
|
70
85
|
>
|
|
71
86
|
<span
|
|
72
|
-
|
|
73
|
-
|
|
87
|
+
aria-disabled="true"
|
|
88
|
+
class="MuiButtonBase-root MuiIconButton-root PrivateSwitchBase-root MuiSwitch-switchBase MuiSwitch-colorPrimary PrivateSwitchBase-disabled Mui-disabled Mui-disabled Mui-disabled"
|
|
89
|
+
tabindex="-1"
|
|
90
|
+
>
|
|
91
|
+
<span
|
|
92
|
+
class="MuiIconButton-label"
|
|
93
|
+
>
|
|
94
|
+
<input
|
|
95
|
+
class="PrivateSwitchBase-input MuiSwitch-input"
|
|
96
|
+
disabled=""
|
|
97
|
+
type="checkbox"
|
|
98
|
+
value=""
|
|
99
|
+
/>
|
|
100
|
+
<span
|
|
101
|
+
class="MuiSwitch-thumb"
|
|
102
|
+
/>
|
|
103
|
+
</span>
|
|
104
|
+
</span>
|
|
74
105
|
<span
|
|
75
|
-
class="
|
|
76
|
-
/>
|
|
77
|
-
<input
|
|
78
|
-
class="base-Switch w-full h-full m-0 p-0 opacity-0 absolute top-0 left-0 cursor-pointer z-20 group-[.base--disabled]:cursor"
|
|
79
|
-
disabled=""
|
|
80
|
-
role="switch"
|
|
81
|
-
type="checkbox"
|
|
106
|
+
class="MuiSwitch-track"
|
|
82
107
|
/>
|
|
83
108
|
</span>
|
|
84
109
|
<span
|
|
85
|
-
class="PicassoFormLabel-root PicassoFormLabel-disabled PicassoFormLabel-inline PicassoFormControlLabel-label
|
|
110
|
+
class="PicassoFormLabel-root PicassoFormLabel-disabled PicassoFormLabel-inline PicassoFormControlLabel-label PicassoSwitch-label"
|
|
86
111
|
>
|
|
87
112
|
<span
|
|
88
113
|
class="PicassoFormLabel-medium"
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { createStyles } from '@material-ui/core/styles'
|
|
2
|
+
import { outline } from '@toptal/picasso-shared'
|
|
3
|
+
import { PicassoProvider } from '@toptal/picasso-provider'
|
|
4
|
+
|
|
5
|
+
const CONTROL_WIDTH = '1em'
|
|
6
|
+
const LABEL_LEFT_MARGIN = '0.5em'
|
|
7
|
+
const LABEL_TOP_MARGIN = '0.25em'
|
|
8
|
+
|
|
9
|
+
const THUMB_SIZE = 22
|
|
10
|
+
const TRACK_HEIGHT = THUMB_SIZE + 2
|
|
11
|
+
const TRACK_WIDTH = 40
|
|
12
|
+
|
|
13
|
+
PicassoProvider.override(({ palette, transitions }) => ({
|
|
14
|
+
MuiSwitch: {
|
|
15
|
+
root: {
|
|
16
|
+
width: TRACK_WIDTH,
|
|
17
|
+
height: TRACK_HEIGHT,
|
|
18
|
+
padding: 0,
|
|
19
|
+
overflow: 'visible',
|
|
20
|
+
},
|
|
21
|
+
switchBase: {
|
|
22
|
+
top: 1,
|
|
23
|
+
left: 1,
|
|
24
|
+
padding: 0,
|
|
25
|
+
color: palette.common.white,
|
|
26
|
+
'&$checked': {
|
|
27
|
+
transform: 'translateX(16px)',
|
|
28
|
+
color: palette.common.white,
|
|
29
|
+
'& + $track': {
|
|
30
|
+
backgroundColor: palette.primary.main,
|
|
31
|
+
borderColor: palette.primary.main,
|
|
32
|
+
opacity: 1,
|
|
33
|
+
},
|
|
34
|
+
'&$disabled + $track': {
|
|
35
|
+
opacity: 0.4,
|
|
36
|
+
backgroundColor: palette.primary.main,
|
|
37
|
+
borderColor: palette.primary.main,
|
|
38
|
+
},
|
|
39
|
+
},
|
|
40
|
+
'&:hover, &.Mui-focusVisible': {
|
|
41
|
+
...outline(palette.primary.main, 4),
|
|
42
|
+
},
|
|
43
|
+
'&$disabled': {
|
|
44
|
+
'& + $track': {
|
|
45
|
+
opacity: 0.4,
|
|
46
|
+
backgroundColor: palette.grey.main2,
|
|
47
|
+
borderColor: palette.grey.main2,
|
|
48
|
+
},
|
|
49
|
+
},
|
|
50
|
+
},
|
|
51
|
+
thumb: {
|
|
52
|
+
width: THUMB_SIZE,
|
|
53
|
+
height: THUMB_SIZE,
|
|
54
|
+
color: palette.common.white,
|
|
55
|
+
// fix for bad subpixel rendering on 150% displays
|
|
56
|
+
'@media (-webkit-min-device-pixel-ratio: 1.5) and (-webkit-max-device-pixel-ratio: 1.5)':
|
|
57
|
+
{
|
|
58
|
+
transform: 'translate(-0.2px, -0.3px)',
|
|
59
|
+
},
|
|
60
|
+
},
|
|
61
|
+
track: {
|
|
62
|
+
borderRadius: TRACK_HEIGHT / 2,
|
|
63
|
+
border: `1px solid ${palette.grey.main2}`,
|
|
64
|
+
backgroundColor: palette.grey.main2,
|
|
65
|
+
borderColor: palette.grey.main2,
|
|
66
|
+
opacity: 1,
|
|
67
|
+
transition: transitions.create(['background-color', 'border']),
|
|
68
|
+
},
|
|
69
|
+
disabled: {
|
|
70
|
+
'& + $track': {
|
|
71
|
+
opacity: 0.4,
|
|
72
|
+
},
|
|
73
|
+
},
|
|
74
|
+
},
|
|
75
|
+
}))
|
|
76
|
+
|
|
77
|
+
export default () =>
|
|
78
|
+
createStyles({
|
|
79
|
+
root: { alignItems: 'flex-start', fontSize: '1rem' },
|
|
80
|
+
label: {
|
|
81
|
+
marginLeft: LABEL_LEFT_MARGIN,
|
|
82
|
+
marginTop: LABEL_TOP_MARGIN,
|
|
83
|
+
// 1px is needed for safari
|
|
84
|
+
maxWidth: `calc(100% - ${CONTROL_WIDTH} - ${LABEL_LEFT_MARGIN} + 1px)`,
|
|
85
|
+
},
|
|
86
|
+
})
|