ente/web/packages/base/components/EnteSwitch.tsx
Manav Rathi e3c09d7af0
ns
2025-02-04 18:28:07 +05:30

64 lines
1.9 KiB
TypeScript

import { Switch, styled, type SwitchProps } from "@mui/material";
import React from "react";
/**
* A custom variant of the MUI {@link Switch}, styled per our designs.
*/
export const EnteSwitch: React.FC<SwitchProps> = styled((props) => (
<Switch disableRipple {...props} />
))(({ theme }) => ({
width: 40,
height: 24,
padding: 0,
"& .MuiSwitch-switchBase": {
padding: 0,
margin: 2,
transitionDuration: "300ms",
"&.Mui-checked": {
transform: "translateX(16px)",
color: "white",
"& + .MuiSwitch-track": {
opacity: 1,
border: 0,
backgroundColor: theme.vars.palette.fixed.switchOn,
},
"&.Mui-disabled + .MuiSwitch-track": {
opacity: 0.5,
},
},
"&.Mui-disabled .MuiSwitch-thumb": {
color: theme.vars.palette.grey[600],
...theme.applyStyles("light", {
color: theme.vars.palette.grey[100],
}),
},
"&.Mui-disabled + .MuiSwitch-track": {
opacity: 0.3,
},
},
"& .MuiSwitch-thumb": {
boxSizing: "border-box",
width: 20,
height: 20,
},
"& .MuiSwitch-track": {
borderRadius: 22 / 2,
backgroundColor: theme.vars.palette.fill.muted,
opacity: 1,
transition: theme.transitions.create(["background-color"], {
duration: 500,
}),
},
// Use an alternative affordance to indicate focusVisible as the ripple
// effect is disabled.
".MuiSwitch-switchBase.Mui-focusVisible + .MuiSwitch-track": {
outline: `2px solid ${theme.vars.palette.stroke.muted}`,
outlineOffset: "-2px",
},
// Same for when the switch is actived.
".MuiSwitch-switchBase:active + .MuiSwitch-track": {
outline: `1px solid ${theme.vars.palette.stroke.faint}`,
outlineOffset: "-1px",
},
}));