Skip to content

Commit

Permalink
add animations for Radio, Checkbox
Browse files Browse the repository at this point in the history
  • Loading branch information
notsidney committed Sep 1, 2021
1 parent 62a6028 commit 44214cc
Show file tree
Hide file tree
Showing 5 changed files with 260 additions and 2 deletions.
19 changes: 18 additions & 1 deletion src/pages/Test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
Tooltip,
FormControlLabel,
Checkbox,
RadioGroup,
Radio,
Switch,
TextField,
Expand Down Expand Up @@ -192,6 +193,7 @@ export default function TestView() {
<Button
// startIcon={<SparkIcon />}
// endIcon={<SparkIcon />}
color="primary"
variant="text"
size="small"
>
Expand All @@ -200,6 +202,7 @@ export default function TestView() {
<Button
// startIcon={<SparkIcon />}
// endIcon={<SparkIcon />}
color="primary"
variant="text"
size="medium"
>
Expand All @@ -208,6 +211,7 @@ export default function TestView() {
<Button
// startIcon={<SparkIcon />}
// endIcon={<SparkIcon />}
color="primary"
variant="text"
size="large"
>
Expand Down Expand Up @@ -275,6 +279,7 @@ export default function TestView() {
<Button
// startIcon={<SparkIcon />}
// endIcon={<SparkIcon />}
color="primary"
variant="outlined"
size="small"
>
Expand All @@ -283,6 +288,7 @@ export default function TestView() {
<Button
// startIcon={<SparkIcon />}
// endIcon={<SparkIcon />}
color="primary"
variant="outlined"
size="medium"
>
Expand All @@ -291,6 +297,7 @@ export default function TestView() {
<Button
// startIcon={<SparkIcon />}
// endIcon={<SparkIcon />}
color="primary"
variant="outlined"
size="large"
>
Expand Down Expand Up @@ -357,6 +364,7 @@ export default function TestView() {
<Button
// startIcon={<SparkIcon />}
// endIcon={<SparkIcon />}
color="primary"
variant="contained"
size="small"
>
Expand All @@ -365,6 +373,7 @@ export default function TestView() {
<Button
// startIcon={<SparkIcon />}
// endIcon={<SparkIcon />}
color="primary"
variant="contained"
size="medium"
>
Expand All @@ -373,6 +382,7 @@ export default function TestView() {
<Button
// startIcon={<SparkIcon />}
// endIcon={<SparkIcon />}
color="primary"
variant="contained"
size="large"
>
Expand Down Expand Up @@ -747,7 +757,14 @@ export default function TestView() {

<Stack>
<FormControlLabel control={<Checkbox />} label="Label" />
<FormControlLabel control={<Radio />} label="Label" />
<FormControlLabel
control={<Checkbox indeterminate />}
label="Label indeterminate"
/>
<RadioGroup>
<FormControlLabel control={<Radio />} value="1" label="Label 1" />
<FormControlLabel control={<Radio />} value="2" label="Label 2" />
</RadioGroup>
</Stack>

<div>
Expand Down
81 changes: 81 additions & 0 deletions src/theme/CheckboxIcon.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import { Box } from "@material-ui/core";
import { toRem } from "./typography";

export default function CheckboxIcon() {
return (
<Box
component="span"
sx={{
width: toRem(18),
height: toRem(18),
margin: toRem((24 - 18) / 2),
borderRadius: 1,

position: "relative",

bgcolor: "action.input",
border: "1px solid",
borderColor: "text.disabled",
color: "primary.main",

transition: (theme) =>
theme.transitions.create(["background-color", "border-color"], {
easing: theme.transitions.easing.easeIn,
duration: theme.transitions.duration.shortest,
delay: theme.transitions.duration.shortest,
}),

"& svg": {
position: "absolute",
top: -1,
right: -1,
bottom: -1,
left: -1,
color: "inherit",
},
"& .tick": {
stroke: (theme) => theme.palette.primary.contrastText,
strokeDasharray: 18,
strokeDashoffset: 18,
transition: (theme) =>
theme.transitions.create(["stroke-dashoffset"], {
easing: theme.transitions.easing.easeIn,
duration: theme.transitions.duration.shortest,
}),

boxShadow: 1,
},

".Mui-checked &": {
backgroundColor: "currentColor",
borderColor: "currentColor",

transition: (theme) =>
theme.transitions.create(["background-color", "border-color"], {
easing: theme.transitions.easing.easeOut,
duration: theme.transitions.duration.shortest,
}),

"& .tick": {
strokeDashoffset: 0,
transition: (theme) =>
theme.transitions.create(["stroke-dashoffset"], {
easing: theme.transitions.easing.easeOut,
duration: theme.transitions.duration.shortest,
delay: theme.transitions.duration.shortest,
}),
},
},
}}
>
<svg viewBox="0 0 18 18">
<polyline
stroke-width="2"
points="2.705 8.29 7 12.585 15.295 4.29"
fill="none"
className="tick"
/>
</svg>
</Box>
);
}
76 changes: 76 additions & 0 deletions src/theme/CheckboxIndeterminateIcon.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { Box } from "@material-ui/core";
import { toRem } from "./typography";

export default function CheckboxIndeterminateIcon() {
return (
<Box
component="span"
sx={{
width: toRem(18),
height: toRem(18),
margin: toRem((24 - 18) / 2),
borderRadius: 1,

position: "relative",

bgcolor: "action.input",
border: "1px solid",
borderColor: "text.disabled",
color: "primary.main",

transition: (theme) =>
theme.transitions.create(["background-color", "border-color"], {
easing: theme.transitions.easing.easeIn,
duration: theme.transitions.duration.shortest,
delay: theme.transitions.duration.shortest,
}),

"& svg": {
position: "absolute",
top: -1,
right: -1,
bottom: -1,
left: -1,
color: "inherit",
},
"& .tick": {
stroke: (theme) => theme.palette.primary.contrastText,
strokeDasharray: 10,
strokeDashoffset: 10,
transition: (theme) =>
theme.transitions.create(["stroke-dashoffset"], {
easing: theme.transitions.easing.easeIn,
duration: theme.transitions.duration.shortest,
}),

boxShadow: 1,
},

".Mui-checked &": {
backgroundColor: "currentColor",
borderColor: "currentColor",

transition: (theme) =>
theme.transitions.create(["background-color", "border-color"], {
easing: theme.transitions.easing.easeOut,
duration: theme.transitions.duration.shortest,
}),

"& .tick": {
strokeDashoffset: 0,
transition: (theme) =>
theme.transitions.create(["stroke-dashoffset"], {
easing: theme.transitions.easing.easeOut,
duration: theme.transitions.duration.shortest,
delay: theme.transitions.duration.shortest,
}),
},
},
}}
>
<svg viewBox="0 0 18 18">
<line x1="4" y1="9" x2="14" y2="9" stroke-width="2" className="tick" />
</svg>
</Box>
);
}
65 changes: 65 additions & 0 deletions src/theme/RadioIcon.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { Box } from "@material-ui/core";
import { toRem } from "./typography";

export default function RadioIcon() {
return (
<Box
component="span"
sx={{
width: toRem(20),
height: toRem(20),
margin: toRem((24 - 20) / 2),
borderRadius: "50%",

backgroundColor: "transparent",
border: "1px solid",
borderColor: "text.disabled",
color: "primary.main",

transition: (theme) =>
theme.transitions.create(["background-color", "border-color"], {
easing: theme.transitions.easing.easeIn,
duration: theme.transitions.duration.shortest,
}),

"&::before": {
content: '""',
width: "100%",
height: "100%",
borderRadius: "50%",
display: "block",

bgcolor: "action.input",
transition: (theme) =>
theme.transitions.create(["transform", "background-color"], {
easing: theme.transitions.easing.easeIn,
duration: theme.transitions.duration.shortest,
}),
},

".Mui-checked &": {
backgroundColor: "currentColor",
borderColor: "currentColor",

transition: (theme) =>
theme.transitions.create(["background-color", "border-color"], {
easing: theme.transitions.easing.easeOut,
duration: theme.transitions.duration.shortest,
}),

"&::before": {
bgcolor: "primary.contrastText",
boxShadow: 1,
transform: `scale(${12 / 20})`,

transition: (theme) =>
theme.transitions.create(["transform", "background-color"], {
easing: theme.transitions.easing.easeOut,
duration: theme.transitions.duration.shortest,
}),
},
},
}}
/>
);
}
21 changes: 20 additions & 1 deletion src/theme/components.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { Theme, ThemeOptions } from "@material-ui/core/styles";

import RadioIcon from "theme/RadioIcon";
import CheckboxIcon from "theme/CheckboxIcon";
import CheckboxIndeterminateIcon from "theme/CheckboxIndeterminateIcon";

import { colord, extend } from "colord";
import mixPlugin from "colord/plugins/mix";
extend([mixPlugin]);
Expand Down Expand Up @@ -503,7 +507,7 @@ export const components = (theme: Theme): ThemeOptions => {

track: {
borderRadius: 32 / 2,
backgroundColor: "transparent",
backgroundColor: theme.palette.action.input,
boxShadow: `0 0 0 1px ${theme.palette.text.disabled} inset`,
".Mui-disabled + &": {
backgroundColor: theme.palette.text.disabled,
Expand Down Expand Up @@ -585,6 +589,21 @@ export const components = (theme: Theme): ThemeOptions => {
},
} as any,
},

MuiRadio: {
defaultProps: {
icon: <RadioIcon />,
checkedIcon: <RadioIcon />,
},
},
MuiCheckbox: {
defaultProps: {
icon: <CheckboxIcon />,
checkedIcon: <CheckboxIcon />,
indeterminateIcon: <CheckboxIndeterminateIcon />,
},
},

MuiSlider: {
styleOverrides: {
thumb: {
Expand Down

0 comments on commit 44214cc

Please sign in to comment.