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.
This commit is contained in:
Kyle Geib committed 2022-01-19 16:55:16 -05:00
1 parent daae51e06d
commit d4f8e9bffe
3 files changed
+38 -7

No files matched your search

@@ -3,19 +3,27 @@ import { useDrag } from "@use-gesture/react";
import useMeasure from "react-use-measure"; import useMeasure from "react-use-measure";
import { clamp } from "lodash"; import { clamp } from "lodash";
import { ProgressSliderBar, ProgressSliderBarHandle, ProgressSliderBody } from "./styles"; import {
ProgressSliderBar,
ProgressSliderBarHandle,
ProgressSliderBarIndicator,
ProgressSliderBody,
} from "./styles";
const MARGIN_WIDTH = 15; const MARGIN_WIDTH = 15;
const percentage = (value: number) => `${Math.floor(value * 100)}%`;
type ProgressSliderProps = { type ProgressSliderProps = {
value: number, // Fraction of 1 (0.0 to 1.0) value: number, // Fraction of 1 (0.0 to 1.0)
onChange: (value: number) => void, onChange: (value: number) => void,
disabled?: boolean,
formatter?: (value: number) => string, formatter?: (value: number) => string,
}; };
export function ProgressSlider({ export function ProgressSlider({
value, value,
onChange, onChange,
formatter = (x) => x.toString(), disabled = false,
formatter = percentage,
}: ProgressSliderProps) { }: ProgressSliderProps) {
const [bodyRef, { const [bodyRef, {
width: bodyWidth, width: bodyWidth,
@@ -31,10 +39,7 @@ export function ProgressSlider({
barPos.x.set(newPos); barPos.x.set(newPos);
onChange(newValue); onChange(newValue);
}, { enabled: !disabled });
console.log("drag", newPos, newValue);
});
const AnimatedProgressSliderBar = animated(ProgressSliderBar); const AnimatedProgressSliderBar = animated(ProgressSliderBar);
@@ -46,6 +51,9 @@ export function ProgressSlider({
bodyWidth={bodyWidth - MARGIN_WIDTH} bodyWidth={bodyWidth - MARGIN_WIDTH}
> >
<ProgressSliderBarHandle /> <ProgressSliderBarHandle />
<ProgressSliderBarIndicator left={barPos.x.get() > 45 || barPos.x.get() < 0}>
{formatter(value)}
</ProgressSliderBarIndicator>
</AnimatedProgressSliderBar> </AnimatedProgressSliderBar>
</ProgressSliderBody> </ProgressSliderBody>
); );
@@ -1,4 +1,4 @@
import styled from "styled-components"; import styled, { css } from "styled-components";
export const ProgressSliderBody = styled.div` export const ProgressSliderBody = styled.div`
position: relative; position: relative;
@@ -49,3 +49,25 @@ export const ProgressSliderBarHandle = styled.div`
} }
`; `;
ProgressSliderBarHandle.displayName = "ProgressSliderBarHandle"; ProgressSliderBarHandle.displayName = "ProgressSliderBarHandle";
type ProgressSliderBarIndicatorProps = {
left: boolean,
onBarColor?: string,
};
export const ProgressSliderBarIndicator = styled.span<ProgressSliderBarIndicatorProps>`
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";
@@ -54,6 +54,7 @@ export function OverlayControl() {
<ProgressSlider <ProgressSlider
value={currentOverlayItem?.opacity ?? 1} value={currentOverlayItem?.opacity ?? 1}
onChange={onChangeOpacity} onChange={onChangeOpacity}
disabled={!visible}
/> />
</Stack> </Stack>
); );