From 97c5752102cc69c8f5ae6c7fed96e75c8ce35c2e Mon Sep 17 00:00:00 2001 From: Kyle Geib Date: Tue, 18 Jan 2022 14:53:52 -0500 Subject: [PATCH 1/6] ISSUE-021 - Progress commit. Have a rough version of ProgressSlider component done. Still needs some work. --- .../Inputs/ProgressSlider/ProgressSlider.tsx | 53 +++++++++++++++++++ .../Common/Inputs/ProgressSlider/index.ts | 1 + .../Common/Inputs/ProgressSlider/styles.ts | 53 +++++++++++++++++++ .../OverlayControl/OverlayControl.tsx | 22 ++++++-- .../OverlayDisplay/OverlayDisplay.tsx | 4 +- .../OverlayTool/OverlayDisplay/styles.ts | 11 +++- 6 files changed, 136 insertions(+), 8 deletions(-) create mode 100644 src/components/Common/Inputs/ProgressSlider/ProgressSlider.tsx create mode 100644 src/components/Common/Inputs/ProgressSlider/index.ts create mode 100644 src/components/Common/Inputs/ProgressSlider/styles.ts diff --git a/src/components/Common/Inputs/ProgressSlider/ProgressSlider.tsx b/src/components/Common/Inputs/ProgressSlider/ProgressSlider.tsx new file mode 100644 index 0000000..65ab607 --- /dev/null +++ b/src/components/Common/Inputs/ProgressSlider/ProgressSlider.tsx @@ -0,0 +1,53 @@ +import { animated, useSpring } from "@react-spring/web"; +import { useDrag } from "@use-gesture/react"; +import useMeasure from "react-use-measure"; +import { clamp } from "lodash"; + +import { ProgressSliderBar, ProgressSliderBarHandle, ProgressSliderBarHandleContainer, ProgressSliderBody } from "./styles"; + +const MARGIN_WIDTH = 30; + +type ProgressSliderProps = { + value: number, // Fraction of 1 (0.0 to 1.0) + onChange: (value: number) => void, + formatter?: (value: number) => string, +}; +export function ProgressSlider({ + value, + onChange, + formatter = (x) => x.toString(), +}: ProgressSliderProps) { + const [bodyRef, { + width: bodyWidth, + }] = useMeasure(); + const barPos = useSpring({ x: (bodyWidth - 2 * MARGIN_WIDTH) * value }); + const bindBarPos = useDrag((params) => { + const newPos = clamp(barPos.x.get() + params.delta[0], MARGIN_WIDTH, bodyWidth - MARGIN_WIDTH); + const newValue = newPos / (bodyWidth - MARGIN_WIDTH); + + barPos.x.set(newPos); + onChange(newValue); + + console.log("drag", newPos, newValue); + }); + + // const + // console.log("main", bodyWidth, barPos.x.get()); + console.log("main",barPos.x.get()); + + return ( + + {/* */} + + + {/* */} + + + + {/* */} + + {/* */} + + + ); +} diff --git a/src/components/Common/Inputs/ProgressSlider/index.ts b/src/components/Common/Inputs/ProgressSlider/index.ts new file mode 100644 index 0000000..0dbb390 --- /dev/null +++ b/src/components/Common/Inputs/ProgressSlider/index.ts @@ -0,0 +1 @@ +export * from "./ProgressSlider"; diff --git a/src/components/Common/Inputs/ProgressSlider/styles.ts b/src/components/Common/Inputs/ProgressSlider/styles.ts new file mode 100644 index 0000000..dcf57cb --- /dev/null +++ b/src/components/Common/Inputs/ProgressSlider/styles.ts @@ -0,0 +1,53 @@ +import { animated } from "@react-spring/web"; +import styled from "styled-components"; + +export const ProgressSliderBody = styled.div` + position: relative; + height: 1.5em; + width: auto; + + 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; +`; + +type ProgressSliderBarProps = { + color?: string, +}; +export const ProgressSliderBar = styled.div` + position: absolute; + height: 100%; + width: 100%; + background-color: ${({ color }) => (color ?? (p => p.theme.colors.activeGreen))}; +`; +ProgressSliderBar.displayName = "ProgressSliderBar"; + + +export const ProgressSliderBarHandleContainer = styled(animated.div)` + width: 100%; + height: 100%; + position: absolute; +`; +ProgressSliderBarHandleContainer.displayName = "ProgressSliderBarHandleContainer"; + +export const ProgressSliderBarHandle = styled.div` + position: relative; + width: 1ch; + height: 1.2em; + background-color: white; + /* margin: 2px; */ + margin-top: 1px; + border-radius: 3px; + opacity: 0.5; + cursor: pointer; + + &:hover { + opacity: 0.9; + } +`; +ProgressSliderBarHandle.displayName = "ProgressSliderBarHandle"; diff --git a/src/components/OverlayTool/OverlayControl/OverlayControl.tsx b/src/components/OverlayTool/OverlayControl/OverlayControl.tsx index ccb977b..c9141da 100644 --- a/src/components/OverlayTool/OverlayControl/OverlayControl.tsx +++ b/src/components/OverlayTool/OverlayControl/OverlayControl.tsx @@ -7,19 +7,22 @@ import { overlayToolSelector } from "../../../features/overlayTool/overlayToolSe import { initNewOverlayItem, setCurrentOverlayId, + setOpacity, setVisibility, } from "../../../features/overlayTool/overlayToolSlice"; import { Toggle } from "../../Common/Inputs/Toggle"; import { MockupBlock } from "../../Common/MockupBlock.styled"; import { SlotSelector } from "../../Common/Inputs/SlotSelector"; +import { ProgressSlider } from "../../Common/Inputs/ProgressSlider"; +import { useState } from "react"; export function OverlayControl() { const dispatch = useDispatch(); const { overlayIds, visible, currentOverlayItem } = useSelector(overlayToolSelector); - const onToggle = (value: boolean) => { + const onToggleVisibility = (value: boolean) => { if (currentOverlayItem == null) { dispatch(initNewOverlayItem({ overlayId: overlayIds[0], @@ -31,13 +34,21 @@ export function OverlayControl() { } }; + const onChangeOpacity = (value: number) => { + dispatch(setOpacity(value)); + }; + + /* TEMP */ + const [barValue, setBarValue] = useState(0.3); + /* TEMP */ + return ( value.replace(/(\.png)|(\.jpg)$/, "")} /> - - Opacity Slider - + ); } diff --git a/src/components/OverlayTool/OverlayDisplay/OverlayDisplay.tsx b/src/components/OverlayTool/OverlayDisplay/OverlayDisplay.tsx index 4fca404..516ce92 100644 --- a/src/components/OverlayTool/OverlayDisplay/OverlayDisplay.tsx +++ b/src/components/OverlayTool/OverlayDisplay/OverlayDisplay.tsx @@ -53,7 +53,7 @@ export function OverlayDisplay({ from: { top: 50, opacity: 0 }, enter: { top: 0, - opacity: currentOverlayItem?.opacity ?? 1, + opacity: 1, }, leave: { top: 50, opacity: 0 }, }); @@ -77,7 +77,7 @@ export function OverlayDisplay({ }} > - + (({ `; OverlayImage.displayName = "OverlayImage"; -export const OverlayImageContainer = styled.div` +type OverlayImageContainerProps = { + opacity: number, +}; +export const OverlayImageContainer = styled.div.attrs(({ + opacity, +}) => ({ + style: { + opacity, + }, +}))` display: flex; `; OverlayImageContainer.displayName = "OverlayImageContainer"; From 9925eae4cca195006e416c829cbfe4974a2cd742 Mon Sep 17 00:00:00 2001 From: Kyle Geib Date: Tue, 18 Jan 2022 15:18:39 -0500 Subject: [PATCH 2/6] ISSUE-021 - Another progress commit. Have issues with combining spring animated components and Styled-Components. --- .../Inputs/ProgressSlider/ProgressSlider.tsx | 21 ++++++++----------- .../Common/Inputs/ProgressSlider/styles.ts | 12 ++++++----- 2 files changed, 16 insertions(+), 17 deletions(-) diff --git a/src/components/Common/Inputs/ProgressSlider/ProgressSlider.tsx b/src/components/Common/Inputs/ProgressSlider/ProgressSlider.tsx index 65ab607..9d0712c 100644 --- a/src/components/Common/Inputs/ProgressSlider/ProgressSlider.tsx +++ b/src/components/Common/Inputs/ProgressSlider/ProgressSlider.tsx @@ -5,7 +5,7 @@ import { clamp } from "lodash"; import { ProgressSliderBar, ProgressSliderBarHandle, ProgressSliderBarHandleContainer, ProgressSliderBody } from "./styles"; -const MARGIN_WIDTH = 30; +const MARGIN_WIDTH = 15; type ProgressSliderProps = { value: number, // Fraction of 1 (0.0 to 1.0) @@ -20,7 +20,7 @@ export function ProgressSlider({ const [bodyRef, { width: bodyWidth, }] = useMeasure(); - const barPos = useSpring({ x: (bodyWidth - 2 * MARGIN_WIDTH) * value }); + const barPos = useSpring({ x: (bodyWidth - MARGIN_WIDTH) * value }); const bindBarPos = useDrag((params) => { const newPos = clamp(barPos.x.get() + params.delta[0], MARGIN_WIDTH, bodyWidth - MARGIN_WIDTH); const newValue = newPos / (bodyWidth - MARGIN_WIDTH); @@ -33,20 +33,17 @@ export function ProgressSlider({ // const // console.log("main", bodyWidth, barPos.x.get()); - console.log("main",barPos.x.get()); + // console.log("main",barPos.x.get()); + // console.log(bodyWidth); return ( - {/* */} - - {/* */} - - - - {/* */} - - {/* */} + + + + + ); diff --git a/src/components/Common/Inputs/ProgressSlider/styles.ts b/src/components/Common/Inputs/ProgressSlider/styles.ts index dcf57cb..ff3447d 100644 --- a/src/components/Common/Inputs/ProgressSlider/styles.ts +++ b/src/components/Common/Inputs/ProgressSlider/styles.ts @@ -17,10 +17,13 @@ export const ProgressSliderBody = styled.div` `; type ProgressSliderBarProps = { + bodyWidth: number, color?: string, }; -export const ProgressSliderBar = styled.div` +export const ProgressSliderBar = styled(animated.div)` position: absolute; + left: ${({ bodyWidth }) => `${-bodyWidth}px`}; + /* left: -359px; */ height: 100%; width: 100%; background-color: ${({ color }) => (color ?? (p => p.theme.colors.activeGreen))}; @@ -29,9 +32,9 @@ ProgressSliderBar.displayName = "ProgressSliderBar"; export const ProgressSliderBarHandleContainer = styled(animated.div)` + position: absolute; width: 100%; height: 100%; - position: absolute; `; ProgressSliderBarHandleContainer.displayName = "ProgressSliderBarHandleContainer"; @@ -39,15 +42,14 @@ export const ProgressSliderBarHandle = styled.div` position: relative; width: 1ch; height: 1.2em; - background-color: white; - /* margin: 2px; */ + background-color: black; margin-top: 1px; border-radius: 3px; opacity: 0.5; cursor: pointer; &:hover { - opacity: 0.9; + opacity: 0.8; } `; ProgressSliderBarHandle.displayName = "ProgressSliderBarHandle"; From 8170ba3f8689ecbcd63c72be259367b8cb35d929 Mon Sep 17 00:00:00 2001 From: Kyle Geib Date: Tue, 18 Jan 2022 15:42:43 -0500 Subject: [PATCH 3/6] ISSUE-021 - Discovered the animated() wrapper solution to using Styled-Components that solves some problems. Currently working on making the draggable activation area larger. --- .../Inputs/ProgressSlider/ProgressSlider.tsx | 26 +++++++++---------- .../Common/Inputs/ProgressSlider/styles.ts | 25 +++++++++++------- 2 files changed, 29 insertions(+), 22 deletions(-) diff --git a/src/components/Common/Inputs/ProgressSlider/ProgressSlider.tsx b/src/components/Common/Inputs/ProgressSlider/ProgressSlider.tsx index 9d0712c..81543c2 100644 --- a/src/components/Common/Inputs/ProgressSlider/ProgressSlider.tsx +++ b/src/components/Common/Inputs/ProgressSlider/ProgressSlider.tsx @@ -20,10 +20,14 @@ export function ProgressSlider({ const [bodyRef, { width: bodyWidth, }] = useMeasure(); - const barPos = useSpring({ x: (bodyWidth - MARGIN_WIDTH) * value }); + const barPos = useSpring({ x: (bodyWidth - MARGIN_WIDTH) * value }); const bindBarPos = useDrag((params) => { - const newPos = clamp(barPos.x.get() + params.delta[0], MARGIN_WIDTH, bodyWidth - MARGIN_WIDTH); - const newValue = newPos / (bodyWidth - MARGIN_WIDTH); + const newPos = clamp( + barPos.x.get() + params.delta[0], + MARGIN_WIDTH, + bodyWidth - MARGIN_WIDTH, + ); + const newValue = (newPos - MARGIN_WIDTH) / (bodyWidth - 2 * MARGIN_WIDTH); barPos.x.set(newPos); onChange(newValue); @@ -31,20 +35,16 @@ export function ProgressSlider({ console.log("drag", newPos, newValue); }); - // const - // console.log("main", bodyWidth, barPos.x.get()); - // console.log("main",barPos.x.get()); - // console.log(bodyWidth); + + const AnimatedProgressSliderBar = animated(ProgressSliderBar); + const AnimatedProgressSliderBarHandleContainer = animated(ProgressSliderBarHandleContainer); return ( - - - - + + - - + ); } diff --git a/src/components/Common/Inputs/ProgressSlider/styles.ts b/src/components/Common/Inputs/ProgressSlider/styles.ts index ff3447d..706322a 100644 --- a/src/components/Common/Inputs/ProgressSlider/styles.ts +++ b/src/components/Common/Inputs/ProgressSlider/styles.ts @@ -1,4 +1,3 @@ -import { animated } from "@react-spring/web"; import styled from "styled-components"; export const ProgressSliderBody = styled.div` @@ -20,10 +19,9 @@ type ProgressSliderBarProps = { bodyWidth: number, color?: string, }; -export const ProgressSliderBar = styled(animated.div)` +export const ProgressSliderBar = styled.div` position: absolute; left: ${({ bodyWidth }) => `${-bodyWidth}px`}; - /* left: -359px; */ height: 100%; width: 100%; background-color: ${({ color }) => (color ?? (p => p.theme.colors.activeGreen))}; @@ -31,21 +29,30 @@ export const ProgressSliderBar = styled(animated.div)` ProgressSliderBar.displayName = "ProgressSliderBar"; -export const ProgressSliderBarHandleContainer = styled(animated.div)` - position: absolute; +export const ProgressSliderBarHandleContainer = styled.div` + position: relative; + left: -50%; + + display: flex; + align-items: center; + justify-content: center; width: 100%; height: 100%; + + + background-color: rgba(200, 200, 0, 0.3); `; ProgressSliderBarHandleContainer.displayName = "ProgressSliderBarHandleContainer"; export const ProgressSliderBarHandle = styled.div` position: relative; width: 1ch; - height: 1.2em; - background-color: black; - margin-top: 1px; - border-radius: 3px; + height: 80%; + border-radius: 2px; + opacity: 0.5; + background-color: white; + cursor: pointer; &:hover { From daae51e06d06337d9265caa091a7b475d03e6659 Mon Sep 17 00:00:00 2001 From: Kyle Geib Date: Tue, 18 Jan 2022 16:49:45 -0500 Subject: [PATCH 4/6] ISSUE-021 - Simplified the ProgressSlider, dropping the container component and instead letting the entire progress bar be dragged. --- .../Inputs/ProgressSlider/ProgressSlider.tsx | 14 +++++----- .../Common/Inputs/ProgressSlider/styles.ts | 27 ++++++------------- .../OverlayControl/OverlayControl.tsx | 6 ----- 3 files changed, 16 insertions(+), 31 deletions(-) diff --git a/src/components/Common/Inputs/ProgressSlider/ProgressSlider.tsx b/src/components/Common/Inputs/ProgressSlider/ProgressSlider.tsx index 81543c2..6d398ae 100644 --- a/src/components/Common/Inputs/ProgressSlider/ProgressSlider.tsx +++ b/src/components/Common/Inputs/ProgressSlider/ProgressSlider.tsx @@ -3,7 +3,7 @@ import { useDrag } from "@use-gesture/react"; import useMeasure from "react-use-measure"; import { clamp } from "lodash"; -import { ProgressSliderBar, ProgressSliderBarHandle, ProgressSliderBarHandleContainer, ProgressSliderBody } from "./styles"; +import { ProgressSliderBar, ProgressSliderBarHandle, ProgressSliderBody } from "./styles"; const MARGIN_WIDTH = 15; @@ -20,7 +20,7 @@ export function ProgressSlider({ const [bodyRef, { width: bodyWidth, }] = useMeasure(); - const barPos = useSpring({ x: (bodyWidth - MARGIN_WIDTH) * value }); + const barPos = useSpring({ x: ((bodyWidth - 2 * MARGIN_WIDTH) * value) + MARGIN_WIDTH }); const bindBarPos = useDrag((params) => { const newPos = clamp( barPos.x.get() + params.delta[0], @@ -37,14 +37,16 @@ export function ProgressSlider({ const AnimatedProgressSliderBar = animated(ProgressSliderBar); - const AnimatedProgressSliderBarHandleContainer = animated(ProgressSliderBarHandleContainer); return ( - - + - + ); } diff --git a/src/components/Common/Inputs/ProgressSlider/styles.ts b/src/components/Common/Inputs/ProgressSlider/styles.ts index 706322a..98f4e11 100644 --- a/src/components/Common/Inputs/ProgressSlider/styles.ts +++ b/src/components/Common/Inputs/ProgressSlider/styles.ts @@ -28,31 +28,20 @@ export const ProgressSliderBar = styled.div` `; ProgressSliderBar.displayName = "ProgressSliderBar"; - -export const ProgressSliderBarHandleContainer = styled.div` - position: relative; - left: -50%; - - display: flex; - align-items: center; - justify-content: center; - width: 100%; - height: 100%; - - - background-color: rgba(200, 200, 0, 0.3); -`; -ProgressSliderBarHandleContainer.displayName = "ProgressSliderBarHandleContainer"; - export const ProgressSliderBarHandle = styled.div` position: relative; - width: 1ch; - height: 80%; + float: right; + + width: 0.8ch; + right: 0.5ch; + + height: 70%; + top: 15%; + border-radius: 2px; opacity: 0.5; background-color: white; - cursor: pointer; &:hover { diff --git a/src/components/OverlayTool/OverlayControl/OverlayControl.tsx b/src/components/OverlayTool/OverlayControl/OverlayControl.tsx index c9141da..f95aa24 100644 --- a/src/components/OverlayTool/OverlayControl/OverlayControl.tsx +++ b/src/components/OverlayTool/OverlayControl/OverlayControl.tsx @@ -12,10 +12,8 @@ import { } from "../../../features/overlayTool/overlayToolSlice"; import { Toggle } from "../../Common/Inputs/Toggle"; -import { MockupBlock } from "../../Common/MockupBlock.styled"; import { SlotSelector } from "../../Common/Inputs/SlotSelector"; import { ProgressSlider } from "../../Common/Inputs/ProgressSlider"; -import { useState } from "react"; export function OverlayControl() { @@ -38,10 +36,6 @@ export function OverlayControl() { dispatch(setOpacity(value)); }; - /* TEMP */ - const [barValue, setBarValue] = useState(0.3); - /* TEMP */ - return ( From d4f8e9bffede58d36f3549ea323aef7bb1fc724d Mon Sep 17 00:00:00 2001 From: Kyle Geib Date: Wed, 19 Jan 2022 16:55:16 -0500 Subject: [PATCH 5/6] ISSUE-021 - Percentage indicator has been added. Slider can now be disabled. I ran into too many issues with the percentage value being buggy when the overlay wasn't visible. I'm still running into issues where useMeasure isn't populating fast enough and the left prop isn't being set correctly. --- .../Inputs/ProgressSlider/ProgressSlider.tsx | 20 +++++++++++----- .../Common/Inputs/ProgressSlider/styles.ts | 24 ++++++++++++++++++- .../OverlayControl/OverlayControl.tsx | 1 + 3 files changed, 38 insertions(+), 7 deletions(-) diff --git a/src/components/Common/Inputs/ProgressSlider/ProgressSlider.tsx b/src/components/Common/Inputs/ProgressSlider/ProgressSlider.tsx index 6d398ae..d7183b0 100644 --- a/src/components/Common/Inputs/ProgressSlider/ProgressSlider.tsx +++ b/src/components/Common/Inputs/ProgressSlider/ProgressSlider.tsx @@ -3,19 +3,27 @@ import { useDrag } from "@use-gesture/react"; import useMeasure from "react-use-measure"; import { clamp } from "lodash"; -import { ProgressSliderBar, ProgressSliderBarHandle, ProgressSliderBody } from "./styles"; +import { + ProgressSliderBar, + ProgressSliderBarHandle, + ProgressSliderBarIndicator, + ProgressSliderBody, +} from "./styles"; const MARGIN_WIDTH = 15; +const percentage = (value: number) => `${Math.floor(value * 100)}%`; type ProgressSliderProps = { value: number, // Fraction of 1 (0.0 to 1.0) onChange: (value: number) => void, + disabled?: boolean, formatter?: (value: number) => string, }; export function ProgressSlider({ value, onChange, - formatter = (x) => x.toString(), + disabled = false, + formatter = percentage, }: ProgressSliderProps) { const [bodyRef, { width: bodyWidth, @@ -31,10 +39,7 @@ export function ProgressSlider({ barPos.x.set(newPos); onChange(newValue); - - console.log("drag", newPos, newValue); - }); - + }, { enabled: !disabled }); const AnimatedProgressSliderBar = animated(ProgressSliderBar); @@ -46,6 +51,9 @@ export function ProgressSlider({ bodyWidth={bodyWidth - MARGIN_WIDTH} > + 45 || barPos.x.get() < 0}> + {formatter(value)} + ); diff --git a/src/components/Common/Inputs/ProgressSlider/styles.ts b/src/components/Common/Inputs/ProgressSlider/styles.ts index 98f4e11..76d0c58 100644 --- a/src/components/Common/Inputs/ProgressSlider/styles.ts +++ b/src/components/Common/Inputs/ProgressSlider/styles.ts @@ -1,4 +1,4 @@ -import styled from "styled-components"; +import styled, { css } from "styled-components"; export const ProgressSliderBody = styled.div` position: relative; @@ -49,3 +49,25 @@ export const ProgressSliderBarHandle = styled.div` } `; ProgressSliderBarHandle.displayName = "ProgressSliderBarHandle"; + +type ProgressSliderBarIndicatorProps = { + left: boolean, + onBarColor?: string, +}; +export const ProgressSliderBarIndicator = styled.span` + display: block; + position: relative; + float: right; + left: ${({ left }) => left ? -1 : 3}ch; + + font-family: ${p => p.theme.fonts.f1Regular}; + font-size: 0.9em; + color: ${({ onBarColor, left }) => left ? onBarColor ?? "white" : "black"}; + + transition-property: left, color; + transition-timing-function: ease; + transition-duration: 0.2s; + + user-select: none; +`; +ProgressSliderBarIndicator.displayName = "ProgressSliderBarIndicator"; diff --git a/src/components/OverlayTool/OverlayControl/OverlayControl.tsx b/src/components/OverlayTool/OverlayControl/OverlayControl.tsx index f95aa24..261dd2d 100644 --- a/src/components/OverlayTool/OverlayControl/OverlayControl.tsx +++ b/src/components/OverlayTool/OverlayControl/OverlayControl.tsx @@ -54,6 +54,7 @@ export function OverlayControl() { ); From 65a9f523d4ca27306cda9bbd161b2146eb1830ec Mon Sep 17 00:00:00 2001 From: Kyle Geib Date: Wed, 19 Jan 2022 17:37:05 -0500 Subject: [PATCH 6/6] ISSUE-021 - Added color customization props. Nice disabled look. Okay. I'm content with what I've made. It's not really up to the level of "oh man, package this up as a library and put it out on NPM!" but it works OK for my specific situation. --- .../Inputs/ProgressSlider/ProgressSlider.tsx | 33 +++++++++++++++++-- .../Common/Inputs/ProgressSlider/styles.ts | 30 +++++++++++++---- 2 files changed, 54 insertions(+), 9 deletions(-) diff --git a/src/components/Common/Inputs/ProgressSlider/ProgressSlider.tsx b/src/components/Common/Inputs/ProgressSlider/ProgressSlider.tsx index d7183b0..47881b2 100644 --- a/src/components/Common/Inputs/ProgressSlider/ProgressSlider.tsx +++ b/src/components/Common/Inputs/ProgressSlider/ProgressSlider.tsx @@ -10,6 +10,11 @@ import { ProgressSliderBody, } from "./styles"; +/** + * This component isn't 100% amazing. Consider that the number indicator + * component may be bugged when loading with a value that isn't 1. + */ + const MARGIN_WIDTH = 15; const percentage = (value: number) => `${Math.floor(value * 100)}%`; @@ -18,16 +23,25 @@ type ProgressSliderProps = { onChange: (value: number) => void, disabled?: boolean, formatter?: (value: number) => string, + barColor?: string, + labelColor?: string, + handleColor?: string, }; export function ProgressSlider({ value, onChange, disabled = false, formatter = percentage, + barColor, + labelColor, + handleColor, }: ProgressSliderProps) { const [bodyRef, { width: bodyWidth, }] = useMeasure(); + const [labelRef, { + width: labelWidth, + }] = useMeasure(); const barPos = useSpring({ x: ((bodyWidth - 2 * MARGIN_WIDTH) * value) + MARGIN_WIDTH }); const bindBarPos = useDrag((params) => { const newPos = clamp( @@ -41,6 +55,8 @@ export function ProgressSlider({ onChange(newValue); }, { enabled: !disabled }); + console.log(bodyWidth, labelWidth, value, barPos.x.get()); + const AnimatedProgressSliderBar = animated(ProgressSliderBar); return ( @@ -49,9 +65,22 @@ export function ProgressSlider({ {...bindBarPos()} style={{ x: barPos.x }} bodyWidth={bodyWidth - MARGIN_WIDTH} + disabled={disabled} + color={barColor} > - - 45 || barPos.x.get() < 0}> + + labelWidth * 1.5 + || barPos.x.get() < 0 + || value === 1 + ) ? -1 : value > 0.1 ? 4 : 3 // Tuned fitting considerations. + } + disabled={disabled} + onBarColor={labelColor} + > {formatter(value)} diff --git a/src/components/Common/Inputs/ProgressSlider/styles.ts b/src/components/Common/Inputs/ProgressSlider/styles.ts index 76d0c58..1b1df95 100644 --- a/src/components/Common/Inputs/ProgressSlider/styles.ts +++ b/src/components/Common/Inputs/ProgressSlider/styles.ts @@ -1,4 +1,4 @@ -import styled, { css } from "styled-components"; +import styled from "styled-components"; export const ProgressSliderBody = styled.div` position: relative; @@ -17,6 +17,7 @@ export const ProgressSliderBody = styled.div` type ProgressSliderBarProps = { bodyWidth: number, + disabled: boolean, color?: string, }; export const ProgressSliderBar = styled.div` @@ -24,11 +25,18 @@ export const ProgressSliderBar = styled.div` left: ${({ bodyWidth }) => `${-bodyWidth}px`}; height: 100%; width: 100%; - background-color: ${({ color }) => (color ?? (p => p.theme.colors.activeGreen))}; + background-color: ${({ color, disabled }) => ( + disabled ? (p => p.theme.colors.lightGrey) : + color ?? (p => p.theme.colors.activeGreen) + )}; + transition: background-color 0.2s ease; `; ProgressSliderBar.displayName = "ProgressSliderBar"; -export const ProgressSliderBarHandle = styled.div` +type ProgressSliderBarHandleProps = { + color?: string, +}; +export const ProgressSliderBarHandle = styled.div` position: relative; float: right; @@ -41,7 +49,8 @@ export const ProgressSliderBarHandle = styled.div` border-radius: 2px; opacity: 0.5; - background-color: white; + background-color: ${({ color = "white" }) => color}; + transition: background-color 0.2s ease; cursor: pointer; &:hover { @@ -51,18 +60,25 @@ export const ProgressSliderBarHandle = styled.div` ProgressSliderBarHandle.displayName = "ProgressSliderBarHandle"; type ProgressSliderBarIndicatorProps = { - left: boolean, + left: number, + disabled: boolean, onBarColor?: string, }; export const ProgressSliderBarIndicator = styled.span` display: block; position: relative; float: right; - left: ${({ left }) => left ? -1 : 3}ch; + left: ${({ left }) => left}ch; font-family: ${p => p.theme.fonts.f1Regular}; font-size: 0.9em; - color: ${({ onBarColor, left }) => left ? onBarColor ?? "white" : "black"}; + color: ${({ onBarColor, left, disabled }) => ( + left < 0 + ? disabled + ? (p => p.theme.colors.darkGrey) + : onBarColor ?? "white" + : (p => p.theme.colors.darkGrey) + )}; transition-property: left, color; transition-timing-function: ease;