ISSUE-005 - Emulating an iOS toggle switch and hooked it all up. Good practice.
Think there is some improvements I can make, though. The toggle circle on iOS has the drop shadow - not the body (with an inset shadow). But because I'm using absolute positioning the shadow overflows outside the the toggle body. So. I'll need to switch to using some other positioning rule
This commit is contained in:
1 parent
7c2654ba51
commit
3151c58d9d
14 files changed
+212
-14
No files matched your search
@@ -0,0 +1,46 @@
|
||||
import { useState } from "react";
|
||||
import { AnimatedBG } from "../../../../types/state";
|
||||
import {
|
||||
ToggleContainer,
|
||||
ToggleBody,
|
||||
ToggleInput,
|
||||
ToggleLabel,
|
||||
ToggleBackground,
|
||||
ToggleCircle,
|
||||
} from "./styles";
|
||||
|
||||
export type ToggleProps = {
|
||||
label: string,
|
||||
toggled: boolean,
|
||||
onToggle: (value: AnimatedBG) => void,
|
||||
};
|
||||
/**
|
||||
* This is my attempt at making a iOS-style toggle switch.
|
||||
* @param param0
|
||||
* @returns
|
||||
*/
|
||||
export const Toggle = ({ label, toggled, onToggle }: ToggleProps) => {
|
||||
const [hovered, setHovered] = useState(false);
|
||||
return (
|
||||
<ToggleContainer>
|
||||
{label}{" "}
|
||||
<ToggleBody>
|
||||
<ToggleInput
|
||||
type="checkbox"
|
||||
name={label}
|
||||
id={label}
|
||||
checked={toggled}
|
||||
onChange={e => onToggle(e.target.checked)}
|
||||
/>
|
||||
<ToggleLabel
|
||||
htmlFor={label}
|
||||
onMouseDown={() => setHovered(true)}
|
||||
onMouseUp={() => setHovered(false)}
|
||||
>
|
||||
<ToggleBackground toggled={toggled} />
|
||||
<ToggleCircle toggled={toggled} stretched={hovered} />
|
||||
</ToggleLabel>
|
||||
</ToggleBody>
|
||||
</ToggleContainer>
|
||||
);
|
||||
};
|
||||
@@ -0,0 +1 @@
|
||||
export * from "./Toggle";
|
||||
@@ -0,0 +1,72 @@
|
||||
import styled, { css } from "styled-components";
|
||||
|
||||
type ToggleContainerProps = {
|
||||
fontSize?: string;
|
||||
}
|
||||
export const ToggleContainer = styled.div<ToggleContainerProps>`
|
||||
text-align: center;
|
||||
font-size: ${({ fontSize = "inherit" }) => fontSize};
|
||||
`;
|
||||
ToggleContainer.displayName = "ToggleContainer";
|
||||
|
||||
export const ToggleBody = styled.div`
|
||||
position: relative;
|
||||
display: inline-block;
|
||||
text-align: left;
|
||||
top: 0.5em;
|
||||
`;
|
||||
ToggleBody.displayName = "ToggleBody";
|
||||
|
||||
export const ToggleInput = styled.input`
|
||||
display: none;
|
||||
`;
|
||||
ToggleInput.displayName = "ToggleInput";
|
||||
|
||||
export const ToggleLabel = styled.label`
|
||||
display: block;
|
||||
overflow: hidden;
|
||||
border-radius: .75em;
|
||||
height: 1.5em;
|
||||
width: 2.7em;
|
||||
cursor: pointer;
|
||||
`;
|
||||
ToggleLabel.displayName = "ToggleLabel";
|
||||
|
||||
type ToggleBackgroundProps = {
|
||||
toggled: boolean,
|
||||
toggledColor?: string,
|
||||
};
|
||||
export const ToggleBackground = styled.div<ToggleBackgroundProps>`
|
||||
background-color: ${({
|
||||
toggledColor,
|
||||
toggled,
|
||||
}) => (
|
||||
toggled ?
|
||||
(toggledColor ?? (p => p.theme.colors.activeGreen)) :
|
||||
p => p.theme.colors.lightGrey
|
||||
)};
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
|
||||
${({ toggled }) => toggled && css`
|
||||
box-shadow: inset 0 0 24px -16px black;
|
||||
`}
|
||||
`;
|
||||
ToggleBackground.displayName = "ToggleBackground";
|
||||
|
||||
type ToggleCircleProps = {
|
||||
toggled: boolean,
|
||||
stretched: boolean,
|
||||
};
|
||||
export const ToggleCircle = styled.div<ToggleCircleProps>`
|
||||
position: absolute;
|
||||
height: 1.3em;
|
||||
width: ${({ stretched }) => stretched ? "1.6em": "1.3em"};
|
||||
border-radius: 0.65em;
|
||||
margin: 0.1em;
|
||||
background-color: white;
|
||||
top: 0;
|
||||
${({ toggled }) => toggled ? css`right: 0;` : css`left: 0;`}
|
||||
`;
|
||||
ToggleCircle.displayName = "ToggleCircle";
|
||||
|
||||
Reference in new issue
Block a user