From 3fce5d05b90e9a3f71dccd0defcb5f99414a708e Mon Sep 17 00:00:00 2001 From: Kyle Geib Date: Mon, 17 Jan 2022 13:26:38 -0500 Subject: [PATCH] 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, }; /**