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:
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 { 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}
|
||||
>
|
||||
<ProgressSliderBarHandle />
|
||||
<ProgressSliderBarIndicator left={barPos.x.get() > 45 || barPos.x.get() < 0}>
|
||||
{formatter(value)}
|
||||
</ProgressSliderBarIndicator>
|
||||
</AnimatedProgressSliderBar>
|
||||
</ProgressSliderBody>
|
||||
);
|
||||
|
||||
@@ -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<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
|
||||
value={currentOverlayItem?.opacity ?? 1}
|
||||
onChange={onChangeOpacity}
|
||||
disabled={!visible}
|
||||
/>
|
||||
</Stack>
|
||||
);
|
||||
|
||||
Reference in new issue
Block a user