From 1ca2137a5b0a4a210325ef961be1425570f5e4e4 Mon Sep 17 00:00:00 2001 From: Kyle Geib Date: Fri, 14 Jan 2022 14:49:19 -0500 Subject: [PATCH 1/5] ISSUE-020 - Mocked up new slot selector and (since it'll happen later) opacity controls. --- src/components/Common/MenuItem.styled.ts | 2 +- .../OverlayControl/OverlayControl.tsx | 39 +++++++++++-------- .../Workspace/WorkspaceControls.tsx | 21 ++++++---- src/features/overlayTool/overlayToolSlice.ts | 5 +++ 4 files changed, 41 insertions(+), 26 deletions(-) diff --git a/src/components/Common/MenuItem.styled.ts b/src/components/Common/MenuItem.styled.ts index eb73e53..cc7bad7 100644 --- a/src/components/Common/MenuItem.styled.ts +++ b/src/components/Common/MenuItem.styled.ts @@ -6,7 +6,7 @@ const hoverRules = css` color: white; `; -export type MenuItemProps = { +type MenuItemProps = { isHovered?: boolean, isActive?: boolean, }; diff --git a/src/components/OverlayTool/OverlayControl/OverlayControl.tsx b/src/components/OverlayTool/OverlayControl/OverlayControl.tsx index 72df498..e23c8da 100644 --- a/src/components/OverlayTool/OverlayControl/OverlayControl.tsx +++ b/src/components/OverlayTool/OverlayControl/OverlayControl.tsx @@ -1,6 +1,8 @@ import { useState } from "react"; import { useSelector, useDispatch } from "react-redux"; +import { Inline } from "@bedrock-layout/inline"; import { PadBox } from "@bedrock-layout/padbox"; +import { Stack } from "@bedrock-layout/stack"; import { overlayToolSelector } from "../../../features/overlayTool/overlayToolSelector"; import { @@ -10,6 +12,7 @@ import { } from "../../../features/overlayTool/overlayToolSlice"; import { Toggle } from "../../Common/Inputs/Toggle"; +import { MockupBlock } from "../../Common/MockupBlock.styled"; export function OverlayControl() { @@ -44,22 +47,24 @@ export function OverlayControl() { }; return ( - - - - {/* For prototyping use only */} -
- - Id #{idNum}: "{overlayIds[idNum]}" - - {/* For prototyping use only */} - -
+ + + + + + Slot selector + + + + + + Opacity Slider + + + ); } diff --git a/src/components/Workspaces/Workspace/WorkspaceControls.tsx b/src/components/Workspaces/Workspace/WorkspaceControls.tsx index c0b53f3..70069a9 100644 --- a/src/components/Workspaces/Workspace/WorkspaceControls.tsx +++ b/src/components/Workspaces/Workspace/WorkspaceControls.tsx @@ -1,4 +1,5 @@ import { Grid } from "@bedrock-layout/grid"; +import { PadBox } from "@bedrock-layout/padbox"; import { useDispatch, useSelector } from "react-redux"; import { workspaceSelector } from "../../../features/workspace/workspaceSelector"; @@ -12,14 +13,18 @@ export const WorkspaceControls = () => { const { animatedBG } = useSelector(workspaceSelector); return( - - { - dispatch(setAnimatedBG(value)); - }} /> - + + + { + dispatch(setAnimatedBG(value)); + }} /> + + + + ); }; diff --git a/src/features/overlayTool/overlayToolSlice.ts b/src/features/overlayTool/overlayToolSlice.ts index 966dafc..e66140d 100644 --- a/src/features/overlayTool/overlayToolSlice.ts +++ b/src/features/overlayTool/overlayToolSlice.ts @@ -65,6 +65,10 @@ export const overlayToolSlice = createSlice({ state.currentWorkspaceId = action.payload.workspaceId; state.overlayIds = action.payload.overlayIds; }, + setOpacity: (state, action: PayloadAction) => { + if (state.currentOverlayItem == null) return; + state.currentOverlayItem.opacity = action.payload; + }, }, }); @@ -74,5 +78,6 @@ export const { setCurrentOverlayId, setPosition, newWorkspace, + setOpacity, } = overlayToolSlice.actions; export default overlayToolSlice.reducer; From df868f11260b7746e3eb2914d7b5b8165a8da131 Mon Sep 17 00:00:00 2001 From: Kyle Geib Date: Fri, 14 Jan 2022 15:17:23 -0500 Subject: [PATCH 2/5] ISSUE-025 - Partial failure. Realized that maybe the only way to get the dimensions of the loaded images is to measure them and include them with the file name definitions. To load an image at the center of the element would require writing information at the same time we read it. It's not a race condition; it's more like the chicken and the egg. I'm sure, if I really wanted to, I could find a two step process of loading at 0 opacity, finding the dimensions, and then moving it to the center and revealing it. But I don't have the desire. Still. I went ahead and added workspace properties to the central state and read from them when it makes sense. --- .../Workspaces/Workspace/Workspace.tsx | 20 +++++++++-------- .../WorkspaceList/WorkspaceList.tsx | 6 ++--- src/components/Workspaces/workspaces.ts | 12 ++++------ src/features/workspace/workspaceSlice.ts | 22 +++++++++++++++++-- src/types/state.d.ts | 6 +++++ 5 files changed, 44 insertions(+), 22 deletions(-) diff --git a/src/components/Workspaces/Workspace/Workspace.tsx b/src/components/Workspaces/Workspace/Workspace.tsx index 7a79ae0..2fac558 100644 --- a/src/components/Workspaces/Workspace/Workspace.tsx +++ b/src/components/Workspaces/Workspace/Workspace.tsx @@ -5,7 +5,7 @@ import { Center } from "@bedrock-layout/center"; import { Stack } from "@bedrock-layout/stack"; import { workspaceSelector } from "../../../features/workspace/workspaceSelector"; -import { setWorkspaceId } from "../../../features/workspace/workspaceSlice"; +import { updateWorkspace } from "../../../features/workspace/workspaceSlice"; import { overlayToolSelector } from "../../../features/overlayTool/overlayToolSelector"; import { newWorkspace } from "../../../features/overlayTool/overlayToolSlice"; @@ -14,31 +14,33 @@ import { WorkspaceControls } from "./WorkspaceControls"; import { MockupBlock } from "../../Common/MockupBlock.styled"; import { OverlayDisplay } from "../../OverlayTool"; -import { workspaceObject } from "../workspaces"; +import { workspaces } from "../workspaces"; export const Workspace = () => { const dispatch = useDispatch(); const { workspaceId } = useParams(); - const { animatedBG } = useSelector(workspaceSelector); + const { animatedBG, workspaceProperties } = useSelector(workspaceSelector); const { currentWorkspaceId: overlayWorkspaceId } = useSelector(overlayToolSelector); useEffect(() => { if (workspaceId == null) return; // Update workspace store for ID. - dispatch(setWorkspaceId(workspaceId)); + dispatch(updateWorkspace({ + workspaceId, + workspaceProperties: workspaces[workspaceId], + })); // Switching Workspace means we need to init a newWorkspace for Overlay tool. if (overlayWorkspaceId !== workspaceId) { dispatch(newWorkspace({ - overlayIds: workspaceObject[workspaceId].overlayIds, + overlayIds: workspaces[workspaceId].overlayIds, workspaceId: workspaceId, })); } }, [workspaceId]); if (workspaceId == null) return null; - const workspace = workspaceObject[workspaceId]; return ( { maxWidth="80%" centerChildren > -

{workspaceId ? workspace.name : "Unknown"}

+

{workspaceId ? workspaceProperties.name : "Unknown"}

{ width="100px" >Preview Item diff --git a/src/components/Workspaces/WorkspaceList/WorkspaceList.tsx b/src/components/Workspaces/WorkspaceList/WorkspaceList.tsx index bc5c27d..8f3e63c 100644 --- a/src/components/Workspaces/WorkspaceList/WorkspaceList.tsx +++ b/src/components/Workspaces/WorkspaceList/WorkspaceList.tsx @@ -5,13 +5,13 @@ import { WorkspaceListBox } from "./styles"; import { Stack } from "@bedrock-layout/stack"; import { MenuItem } from "../../Common/MenuItem.styled"; -import { workspaceObject } from "../workspaces"; +import { workspaces } from "../workspaces"; type WorkspaceListProps = { headerOffset?: number, }; export const WorkspaceList = ({ headerOffset }: WorkspaceListProps) => { - const workspaceList = _.keys(workspaceObject); + const workspaceList = _.keys(workspaces); return ( @@ -19,7 +19,7 @@ export const WorkspaceList = ({ headerOffset }: WorkspaceListProps) => { ))} diff --git a/src/components/Workspaces/workspaces.ts b/src/components/Workspaces/workspaces.ts index db93ab1..d4c8fde 100644 --- a/src/components/Workspaces/workspaces.ts +++ b/src/components/Workspaces/workspaces.ts @@ -1,13 +1,9 @@ -import { Dimensions, OverlayIds } from "../../types/state"; +import { WorkspaceProperties } from "../../types/state"; -export type WorkspaceObject = { - [id: string]: { - name: string, - overlayIds: OverlayIds, - previewWindowDimensions: Dimensions, - }, +type WorkspaceObject = { + [id: string]: WorkspaceProperties, }; -export const workspaceObject: WorkspaceObject = { +export const workspaces: WorkspaceObject = { chyronSimple: { name: "Chyron Simple", overlayIds: [ diff --git a/src/features/workspace/workspaceSlice.ts b/src/features/workspace/workspaceSlice.ts index 4abc2e0..9913f80 100644 --- a/src/features/workspace/workspaceSlice.ts +++ b/src/features/workspace/workspaceSlice.ts @@ -1,9 +1,19 @@ import { createSlice, PayloadAction } from "@reduxjs/toolkit"; -import { RootState, WorkspaceId } from "../../types/state"; +import { RootState, WorkspaceId, WorkspaceProperties } from "../../types/state"; const initialState: RootState["workspace"] = { workspaceId: "", animatedBG: true, + workspaceProperties: { + name: "undefined", + overlayIds: [], + previewWindowDimensions: { width: 0, height: 0 }, + }, +}; + +export type UpdateWorkspaceAction = { + workspaceId: WorkspaceId, + workspaceProperties: WorkspaceProperties, }; export const workspaceSlice = createSlice({ @@ -16,8 +26,16 @@ export const workspaceSlice = createSlice({ setAnimatedBG: (state, action: PayloadAction) => { state.animatedBG = action.payload; }, + updateWorkspace: (state, action: PayloadAction) => { + state.workspaceId = action.payload.workspaceId; + state.workspaceProperties = action.payload.workspaceProperties; + }, }, }); -export const { setWorkspaceId, setAnimatedBG } = workspaceSlice.actions; +export const { + setWorkspaceId, + setAnimatedBG, + updateWorkspace, +} = workspaceSlice.actions; export default workspaceSlice.reducer; diff --git a/src/types/state.d.ts b/src/types/state.d.ts index 0d7d47f..de6fb3f 100644 --- a/src/types/state.d.ts +++ b/src/types/state.d.ts @@ -16,9 +16,15 @@ export type Dimensions = { width: number, height: number }; export type Workspace = { workspaceId: WorkspaceId, animatedBG: boolean, + workspaceProperties: WorkspaceProperties, }; export type WorkspaceId = string; +export type WorkspaceProperties = { + name: string, + overlayIds: OverlayIds, + previewWindowDimensions: Dimensions, +}; // Overlay Tool export type OverlayTool = { From 35ca424dd6a1c0d9fe89c013032b3b4cd74d6034 Mon Sep 17 00:00:00 2001 From: Kyle Geib Date: Fri, 14 Jan 2022 17:16:27 -0500 Subject: [PATCH 3/5] ISSUE-020 - Plain, static SlotSelector component defined. Not too fancy looking. To save space and reduce redundant data I created a utility function called findPrefixCount() and wrote a suite of tests for it. --- .../Inputs/SlotSelector/SlotSelector.tsx | 49 +++++++++++++++++ .../Common/Inputs/SlotSelector/index.ts | 1 + .../Common/Inputs/SlotSelector/styles.ts | 27 ++++++++++ .../OverlayControl/OverlayControl.tsx | 28 +++------- src/utils/strings.spec.ts | 53 +++++++++++++++++++ src/utils/strings.ts | 25 +++++++++ 6 files changed, 163 insertions(+), 20 deletions(-) create mode 100644 src/components/Common/Inputs/SlotSelector/SlotSelector.tsx create mode 100644 src/components/Common/Inputs/SlotSelector/index.ts create mode 100644 src/components/Common/Inputs/SlotSelector/styles.ts create mode 100644 src/utils/strings.spec.ts create mode 100644 src/utils/strings.ts diff --git a/src/components/Common/Inputs/SlotSelector/SlotSelector.tsx b/src/components/Common/Inputs/SlotSelector/SlotSelector.tsx new file mode 100644 index 0000000..9030949 --- /dev/null +++ b/src/components/Common/Inputs/SlotSelector/SlotSelector.tsx @@ -0,0 +1,49 @@ +import { useMemo, useState } from "react"; +import { findPrefixCount } from "../../../../utils/strings"; +import { SlotText, SlotWindow } from "./styles"; + +type SlotSelectorProps = { + items: Array, + onChange: (value: string) => void, // eslint-disable-line no-unused-vars + removePrefix?: boolean, + formatter?: (value: string) => string, // eslint-disable-line no-unused-vars + initialIndex?: number, +} +export function SlotSelector({ + items, + onChange, + removePrefix = false, + formatter = (x) => x, + initialIndex = 0, +}: SlotSelectorProps) { + const [index, setIndex] = useState(initialIndex); + const [prefixMask, setPrefixMask] = useState(0); + const [formattedItems, setFormattedItems] = useState>([]); + + useMemo(() => { + const tempFormattedItems = items.map(item => formatter(item)); + setFormattedItems(tempFormattedItems); + setPrefixMask(findPrefixCount(tempFormattedItems)); + }, [items]); + + const onClick = () => { + const nextIndex = (index + 1) % items.length; + setIndex(nextIndex); + onChange(items[nextIndex]); + }; + + return ( + + + {formattedItems.length && + removePrefix && formattedItems[0].length !== prefixMask ? + formattedItems[index].slice(prefixMask) + : + formattedItems[index] + } + + + ); +} diff --git a/src/components/Common/Inputs/SlotSelector/index.ts b/src/components/Common/Inputs/SlotSelector/index.ts new file mode 100644 index 0000000..31ed247 --- /dev/null +++ b/src/components/Common/Inputs/SlotSelector/index.ts @@ -0,0 +1 @@ +export * from "./SlotSelector"; diff --git a/src/components/Common/Inputs/SlotSelector/styles.ts b/src/components/Common/Inputs/SlotSelector/styles.ts new file mode 100644 index 0000000..c4fe8c4 --- /dev/null +++ b/src/components/Common/Inputs/SlotSelector/styles.ts @@ -0,0 +1,27 @@ +import styled from "styled-components"; + +export const SlotWindow = styled.div` + position: relative; + height: 1.2em; + height: 100%; + width: 200px; + + border-radius: 0.6em; + background-color: white; + box-shadow: inset 0 0 50px -50px ${p => p.theme.colors.darkGrey}; + border: 1px solid ${p => p.theme.colors.darkGrey}; + + cursor: pointer; + + overflow: hidden; + white-space: nowrap; + text-overflow: ellipsis; +`; + +export const SlotText = styled.span` + margin-left: .4em; + position: relative; + font-family: ${p => p.theme.fonts.f1Regular}; + color: ${p => p.theme.colors.darkGrey}; + user-select: none; +`; diff --git a/src/components/OverlayTool/OverlayControl/OverlayControl.tsx b/src/components/OverlayTool/OverlayControl/OverlayControl.tsx index e23c8da..d4babc1 100644 --- a/src/components/OverlayTool/OverlayControl/OverlayControl.tsx +++ b/src/components/OverlayTool/OverlayControl/OverlayControl.tsx @@ -1,4 +1,3 @@ -import { useState } from "react"; import { useSelector, useDispatch } from "react-redux"; import { Inline } from "@bedrock-layout/inline"; import { PadBox } from "@bedrock-layout/padbox"; @@ -13,31 +12,17 @@ import { import { Toggle } from "../../Common/Inputs/Toggle"; import { MockupBlock } from "../../Common/MockupBlock.styled"; +import { SlotSelector } from "../../Common/Inputs/SlotSelector"; export function OverlayControl() { const dispatch = useDispatch(); const { overlayIds, visible, currentOverlayItem } = useSelector(overlayToolSelector); - - - - /* For prototyping use only */ - const [idNum, setIdNum] = useState(0); - - const nextIdNum = () => { - const nextIdNum = (idNum + 1) % overlayIds.length; - setIdNum(nextIdNum); - dispatch(setCurrentOverlayId(overlayIds[nextIdNum])); - }; - /* For prototyping use only */ - - - const onToggle = (value: boolean) => { if (currentOverlayItem == null) { dispatch(initNewOverlayItem({ - overlayId: overlayIds[idNum], + overlayId: overlayIds[0], initialPosition: { x: 0, y: 0 }, setVisible: true, })); @@ -55,9 +40,12 @@ export function OverlayControl() { onToggle={onToggle} /> - - Slot selector - + dispatch(setCurrentOverlayId(value))} + removePrefix + formatter={(value) => value.replace(/(\.png)|(\.jpg)$/, "")} + /> diff --git a/src/utils/strings.spec.ts b/src/utils/strings.spec.ts new file mode 100644 index 0000000..d7c4935 --- /dev/null +++ b/src/utils/strings.spec.ts @@ -0,0 +1,53 @@ +import { findPrefixCount } from "./strings"; + +describe("strings", () => { + describe("findPrefixCount", () => { + describe("finds matches", () => { + it("gets correct count all the same", () => { + expect(findPrefixCount([ + "hello", + "hello", + "hello", + "hello", + ], true)).toBe(5); + }); + it("gets correct count for hello prefix", () => { + expect(findPrefixCount([ + "hello world", + "hello mother", + "hello father", + "hello!", + ], true)).toBe(5); + }); + it("gets zero when nothing matches", () => { + expect(findPrefixCount([ + "cat", + "dog", + "rat", + ], true)).toBe(0); + }); + it("returns 0 when only given a single string", () => { + expect(findPrefixCount([ + "by myself", + ], true)).toBe(0); + }); + it("returns 0 when given no strings", () => { + expect(findPrefixCount([], true)).toBe(0); + }); + it("is case sensitive", () => { + expect(findPrefixCount([ + "Hello", + "HELLO", + "Hello", + ], true)).toBe(1); + }); + it("is correctly case-insensitive", () => { + expect(findPrefixCount([ + "Hello", + "HELLO", + "Hello", + ], false)).toBe(5); + }); + }); + }); +}); diff --git a/src/utils/strings.ts b/src/utils/strings.ts new file mode 100644 index 0000000..19652ba --- /dev/null +++ b/src/utils/strings.ts @@ -0,0 +1,25 @@ +/** + * Gives you the number of characters that prefix all the given strings. + * I use this to remove redundant repeated characters shared between file names. + * Basically, this is a method to do the " you find in a list. + * @param strings + * @returns number of characters that all strings share from the start + */ +export function findPrefixCount(strings: Array, caseSensitive = true): number { + if (strings.length <= 1) { + return 0; // Cannot find anything. So, most neutral return is 0. + } + + let maskLength = strings[0].length; + for (let x = 1; x < strings.length; ++x) { + for (let c = 0; c < maskLength; c++) { + const left = caseSensitive ? strings[0][c] : strings[0][c].toLowerCase(); + const right = caseSensitive ? strings[x][c] : strings[x][c].toLowerCase(); + if (left !== right) { + maskLength = c; + break; + } + } + } + return maskLength; +} From 9b3ae2dae287b0250efcc557aa38d8a67a3fc53b Mon Sep 17 00:00:00 2001 From: Kyle Geib Date: Fri, 14 Jan 2022 17:39:48 -0500 Subject: [PATCH 4/5] ISSUE-020 - Making a checkpoint in my work. SlotSelector written a little better. Animation work coming next. --- .../Inputs/SlotSelector/SlotSelector.tsx | 25 ++++++++++++------- .../Common/Inputs/SlotSelector/styles.ts | 4 +-- 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/src/components/Common/Inputs/SlotSelector/SlotSelector.tsx b/src/components/Common/Inputs/SlotSelector/SlotSelector.tsx index 9030949..7c20ab5 100644 --- a/src/components/Common/Inputs/SlotSelector/SlotSelector.tsx +++ b/src/components/Common/Inputs/SlotSelector/SlotSelector.tsx @@ -26,24 +26,31 @@ export function SlotSelector({ setPrefixMask(findPrefixCount(tempFormattedItems)); }, [items]); + const getNextIndex = (i: number) => (i + 1) % items.length; + const getPreviousIndex = (i: number) => i - 1 >= 0 ? i - 1 : items.length - 1; + const onClick = () => { - const nextIndex = (index + 1) % items.length; + const nextIndex = getNextIndex(index); setIndex(nextIndex); onChange(items[nextIndex]); }; + const getItem = (i: number) => + formattedItems.length && + removePrefix && + formattedItems[0].length !== prefixMask ? + formattedItems[i].slice(prefixMask) : + formattedItems[i]; + + const currentItem = getItem(index); + const nextItem = getItem(getNextIndex(index)); + return ( - - {formattedItems.length && - removePrefix && formattedItems[0].length !== prefixMask ? - formattedItems[index].slice(prefixMask) - : - formattedItems[index] - } - + {currentItem} + {nextItem} ); } diff --git a/src/components/Common/Inputs/SlotSelector/styles.ts b/src/components/Common/Inputs/SlotSelector/styles.ts index c4fe8c4..efc24e5 100644 --- a/src/components/Common/Inputs/SlotSelector/styles.ts +++ b/src/components/Common/Inputs/SlotSelector/styles.ts @@ -2,8 +2,7 @@ import styled from "styled-components"; export const SlotWindow = styled.div` position: relative; - height: 1.2em; - height: 100%; + height: 1.7em; width: 200px; border-radius: 0.6em; @@ -19,6 +18,7 @@ export const SlotWindow = styled.div` `; export const SlotText = styled.span` + display: block; margin-left: .4em; position: relative; font-family: ${p => p.theme.fonts.f1Regular}; From 3fce5d05b90e9a3f71dccd0defcb5f99414a708e Mon Sep 17 00:00:00 2001 From: Kyle Geib Date: Mon, 17 Jan 2022 13:26:38 -0500 Subject: [PATCH 5/5] ISSUE-020 - Transitions in place and working. Also fixed an issue with eslinter that was reporting some incorrect unused var warnings. Removed some ignore rules with it being fixed. --- .eslintrc.json | 3 +- .../Inputs/SlotSelector/SlotSelector.tsx | 39 ++++++++++++------- .../Common/Inputs/SlotSelector/styles.ts | 4 +- .../Common/Inputs/Toggle/Toggle.tsx | 2 +- 4 files changed, 29 insertions(+), 19 deletions(-) diff --git a/.eslintrc.json b/.eslintrc.json index 893b410..7fc714a 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -50,7 +50,8 @@ "error", "always-multiline" ], - "no-unused-vars": [ + "no-unused-vars": "off", + "@typescript-eslint/no-unused-vars": [ "warn", { "varsIgnorePattern": "^_", diff --git a/src/components/Common/Inputs/SlotSelector/SlotSelector.tsx b/src/components/Common/Inputs/SlotSelector/SlotSelector.tsx index 7c20ab5..5c72800 100644 --- a/src/components/Common/Inputs/SlotSelector/SlotSelector.tsx +++ b/src/components/Common/Inputs/SlotSelector/SlotSelector.tsx @@ -1,14 +1,16 @@ -import { useMemo, useState } from "react"; +import { useEffect, useMemo, useState } from "react"; +import { animated, useSpringRef, useTransition } from "@react-spring/web"; + import { findPrefixCount } from "../../../../utils/strings"; import { SlotText, SlotWindow } from "./styles"; type SlotSelectorProps = { items: Array, - onChange: (value: string) => void, // eslint-disable-line no-unused-vars + onChange: (value: string) => void, removePrefix?: boolean, - formatter?: (value: string) => string, // eslint-disable-line no-unused-vars + formatter?: (value: string) => string, initialIndex?: number, -} +}; export function SlotSelector({ items, onChange, @@ -20,17 +22,15 @@ export function SlotSelector({ const [prefixMask, setPrefixMask] = useState(0); const [formattedItems, setFormattedItems] = useState>([]); + // useMemo makes these functions only run once when mounting. Not ever re-render. useMemo(() => { const tempFormattedItems = items.map(item => formatter(item)); setFormattedItems(tempFormattedItems); setPrefixMask(findPrefixCount(tempFormattedItems)); }, [items]); - const getNextIndex = (i: number) => (i + 1) % items.length; - const getPreviousIndex = (i: number) => i - 1 >= 0 ? i - 1 : items.length - 1; - const onClick = () => { - const nextIndex = getNextIndex(index); + const nextIndex = (index + 1) % items.length; setIndex(nextIndex); onChange(items[nextIndex]); }; @@ -42,15 +42,24 @@ export function SlotSelector({ formattedItems[i].slice(prefixMask) : formattedItems[i]; - const currentItem = getItem(index); - const nextItem = getItem(getNextIndex(index)); + const transRef = useSpringRef(); + const transitions = useTransition(index, { + ref: transRef, + keys: null, + from: { opacity: 0, transform: "translate3d(0, 100%, 0" }, + enter: { opacity: 1, transform: "translate3d(0, 0%, 0" }, + leave: { opacity: 0, transform: "translate3d(0, -100%, 0" }, + }); + + useEffect(() => { + transRef.start(); + }, [index]); return ( - - {currentItem} - {nextItem} + + {transitions((style, i) => ( + {getItem(i)} + ))} ); } diff --git a/src/components/Common/Inputs/SlotSelector/styles.ts b/src/components/Common/Inputs/SlotSelector/styles.ts index efc24e5..8af82f4 100644 --- a/src/components/Common/Inputs/SlotSelector/styles.ts +++ b/src/components/Common/Inputs/SlotSelector/styles.ts @@ -3,7 +3,7 @@ import styled from "styled-components"; export const SlotWindow = styled.div` position: relative; height: 1.7em; - width: 200px; + width: auto; border-radius: 0.6em; background-color: white; @@ -18,9 +18,9 @@ export const SlotWindow = styled.div` `; export const SlotText = styled.span` + position: absolute; display: block; margin-left: .4em; - position: relative; font-family: ${p => p.theme.fonts.f1Regular}; color: ${p => p.theme.colors.darkGrey}; user-select: none; diff --git a/src/components/Common/Inputs/Toggle/Toggle.tsx b/src/components/Common/Inputs/Toggle/Toggle.tsx index 43b7876..3d496ba 100644 --- a/src/components/Common/Inputs/Toggle/Toggle.tsx +++ b/src/components/Common/Inputs/Toggle/Toggle.tsx @@ -12,7 +12,7 @@ import { export type ToggleProps = { label: string, toggled: boolean, - onToggle: (value: boolean) => void, // eslint-disable-line no-unused-vars + onToggle: (value: boolean) => void, showIndicators?: boolean, }; /**