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
@@ -8,6 +8,7 @@
|
||||
"@bedrock-layout/columns": "^2.3.4",
|
||||
"@bedrock-layout/cover": "^2.2.5",
|
||||
"@bedrock-layout/frame": "^2.2.4",
|
||||
"@bedrock-layout/grid": "^2.2.5",
|
||||
"@bedrock-layout/inline": "^2.3.3",
|
||||
"@bedrock-layout/inline-cluster": "^2.4.2",
|
||||
"@bedrock-layout/padbox": "^2.1.4",
|
||||
|
||||
@@ -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";
|
||||
|
||||
@@ -5,6 +5,7 @@ import styled from "styled-components";
|
||||
export const Title = styled.h3`
|
||||
font-family: ${p => p.theme.fonts.f1Bold};
|
||||
`;
|
||||
Title.displayName = "Title";
|
||||
|
||||
export const MenuBar = styled(InlineCluster).attrs(() => ({
|
||||
as: PadBox,
|
||||
@@ -16,3 +17,4 @@ export const MenuBar = styled(InlineCluster).attrs(() => ({
|
||||
background-image: linear-gradient(to right, #f0f0f0, #fff);
|
||||
border-block-end: 1px solid ${p => p.theme.colors.darkGrey};
|
||||
`;
|
||||
MenuBar.displayName = "MenuBar";
|
||||
@@ -1,9 +1,11 @@
|
||||
import { useParams } from "react-router-dom";
|
||||
import { Cover } from "@bedrock-layout/cover";
|
||||
import { useSelector } from "react-redux";
|
||||
import { Center } from "@bedrock-layout/center";
|
||||
import { Stack } from "@bedrock-layout/stack";
|
||||
|
||||
import { PreviewWindow, H2 } from "./styles";
|
||||
import { workspaceSelector } from "../../../features/workspace/workspaceSelector";
|
||||
|
||||
import { PreviewWindow, H2, ControlsContainer } from "./styles";
|
||||
import { WorkspaceControls } from "./WorkspaceControls";
|
||||
import { MockupBlock } from "../../Common/MockupBlock.styled";
|
||||
|
||||
@@ -11,6 +13,7 @@ import { workspaceObject } from "../workspaces";
|
||||
|
||||
export const Workspace = () => {
|
||||
const { workspaceId } = useParams();
|
||||
const { animatedBG } = useSelector(workspaceSelector);
|
||||
return (
|
||||
<Stack
|
||||
gutter="lg"
|
||||
@@ -19,14 +22,16 @@ export const Workspace = () => {
|
||||
centerChildren
|
||||
>
|
||||
<H2>{workspaceId ? workspaceObject[workspaceId] : "Unknown"}</H2>
|
||||
<PreviewWindow height={600} width={800} animatedBG>
|
||||
<PreviewWindow height={600} width={800} animatedBG={animatedBG}>
|
||||
<MockupBlock
|
||||
color="#415bad"
|
||||
height="300px"
|
||||
width="400px"
|
||||
>Preview Item</MockupBlock>
|
||||
</PreviewWindow>
|
||||
<WorkspaceControls />
|
||||
<ControlsContainer>
|
||||
<WorkspaceControls />
|
||||
</ControlsContainer>
|
||||
</Stack>
|
||||
);
|
||||
};
|
||||
@@ -1,11 +1,24 @@
|
||||
import { MockupBlock } from "../../Common/MockupBlock.styled";
|
||||
import { Grid } from "@bedrock-layout/grid";
|
||||
import { useDispatch, useSelector } from "react-redux";
|
||||
|
||||
export const WorkspaceControls = () => (
|
||||
<MockupBlock
|
||||
color="#aa5a5a"
|
||||
width="400px"
|
||||
height="80px"
|
||||
>
|
||||
WorkspaceControls
|
||||
</MockupBlock>
|
||||
);
|
||||
import { workspaceSelector } from "../../../features/workspace/workspaceSelector";
|
||||
import { setAnimatedBG } from "../../../features/workspace/workspaceSlice";
|
||||
import { AnimatedBG } from "../../../types/state";
|
||||
|
||||
import { Toggle } from "../../Common/Inputs/Toggle";
|
||||
|
||||
export const WorkspaceControls = () => {
|
||||
const dispatch = useDispatch();
|
||||
const { animatedBG } = useSelector(workspaceSelector);
|
||||
|
||||
return(
|
||||
<Grid gutter="sm">
|
||||
<Toggle
|
||||
label="Animate BG"
|
||||
toggled={animatedBG}
|
||||
onToggle={(value: AnimatedBG) => {
|
||||
dispatch(setAnimatedBG(value));
|
||||
}} />
|
||||
</Grid>
|
||||
);
|
||||
};
|
||||
@@ -33,6 +33,7 @@ export const PreviewWindow = styled(PadBox).attrs(() => ({
|
||||
animation: ${scrollAnimation} 5s linear infinite;
|
||||
`}
|
||||
`;
|
||||
PreviewWindow.displayName = "PreviewWindow";
|
||||
|
||||
export const H2 = styled.h2`
|
||||
margin: 0.2em;
|
||||
@@ -40,3 +41,12 @@ export const H2 = styled.h2`
|
||||
font-family: ${p => p.theme.fonts.f1Regular};
|
||||
max-inline-size: 600px;
|
||||
`;
|
||||
H2.displayName = "H2";
|
||||
|
||||
export const ControlsContainer = styled(PadBox).attrs(() => ({
|
||||
padding: "lg",
|
||||
}))`
|
||||
background-color: ${p => p.theme.colors.faintGrey};
|
||||
border-radius: 1em;
|
||||
`;
|
||||
ControlsContainer.displayName = "ControlsContainer";
|
||||
@@ -0,0 +1,3 @@
|
||||
import { RootState } from "../../types/state";
|
||||
|
||||
export const workspaceSelector = (state: RootState) => state.workspace;
|
||||
@@ -0,0 +1,23 @@
|
||||
import { createSlice, PayloadAction } from "@reduxjs/toolkit";
|
||||
import { RootState, WorkspaceId, AnimatedBG } from "../../types/state";
|
||||
|
||||
const initialState: RootState["workspace"] = {
|
||||
workspaceId: "",
|
||||
animatedBG: true,
|
||||
};
|
||||
|
||||
export const workspaceSlice = createSlice({
|
||||
name: "workspace",
|
||||
initialState,
|
||||
reducers: {
|
||||
setWorkspaceId: (state, action: PayloadAction<WorkspaceId>) => {
|
||||
state.workspaceId = action.payload;
|
||||
},
|
||||
setAnimatedBG: (state, action: PayloadAction<AnimatedBG>) => {
|
||||
state.animatedBG = action.payload;
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
export const { setWorkspaceId, setAnimatedBG } = workspaceSlice.actions;
|
||||
export default workspaceSlice.reducer;
|
||||
@@ -14,8 +14,10 @@ export const theme = {
|
||||
orange: "#f4ae40",
|
||||
blue: "#387af5",
|
||||
pink: "#eb57a3",
|
||||
activeGreen: "#65c466",
|
||||
grey: "#aaa",
|
||||
lightGrey: "#ddd",
|
||||
faintGrey: "#eee",
|
||||
darkGrey: "#303030",
|
||||
videoFrameBG: "#333",
|
||||
},
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
import { configureStore, combineReducers } from "@reduxjs/toolkit";
|
||||
|
||||
import pageDimensions from "./features/pageDimensions/pageDimensionsSlice";
|
||||
import workspace from "./features/workspace/workspaceSlice";
|
||||
|
||||
export const store = configureStore({
|
||||
reducer: combineReducers({
|
||||
pageDimensions,
|
||||
workspace,
|
||||
}),
|
||||
});
|
||||
Vendored
+11
@@ -1,7 +1,9 @@
|
||||
export type RootState = {
|
||||
pageDimensions: PageDimensions,
|
||||
workspace: Workspace,
|
||||
};
|
||||
|
||||
// Page Dimensions
|
||||
export type PageDimensions = {
|
||||
menu: MenuDimensions,
|
||||
};
|
||||
@@ -9,3 +11,12 @@ export type PageDimensions = {
|
||||
export type MenuDimensions = {
|
||||
height: number,
|
||||
};
|
||||
|
||||
// Workspace
|
||||
export type Workspace = {
|
||||
workspaceId: WorkspaceId,
|
||||
animatedBG: AnimatedBG,
|
||||
};
|
||||
|
||||
export type WorkspaceId = string;
|
||||
export type AnimatedBG = boolean;
|
||||
@@ -1113,6 +1113,13 @@
|
||||
dependencies:
|
||||
"@bedrock-layout/spacing-constants" "^2.5.1"
|
||||
|
||||
"@bedrock-layout/grid@^2.2.5":
|
||||
version "2.2.5"
|
||||
resolved "https://registry.yarnpkg.com/@bedrock-layout/grid/-/grid-2.2.5.tgz#36b622481c5d8b3bfb5a0fd76e5e0986ed69b098"
|
||||
integrity sha512-5HteSi7BEWU7RTEBG9dRanqsKxf18aOcFj0V12S1muGki6Xc1yxw1V4O/GH/wGppvU1NH5ICxGuUBBqmXMPoyQ==
|
||||
dependencies:
|
||||
"@bedrock-layout/spacing-constants" "^2.5.1"
|
||||
|
||||
"@bedrock-layout/inline-cluster@^2.4.2":
|
||||
version "2.4.2"
|
||||
resolved "https://registry.yarnpkg.com/@bedrock-layout/inline-cluster/-/inline-cluster-2.4.2.tgz#0019482153b61f63a85cc0c22133f3ecac1e2821"
|
||||
|
||||
Reference in new issue
Block a user