ISSUE-021 - Progress commit. Have a rough version of ProgressSlider component done. Still needs some work.

This commit is contained in:
Kyle Geib committed 2022-01-18 14:53:52 -05:00
1 parent b7ab3dbcc1
commit 97c5752102
6 files changed
+136 -8

No files matched your search

@@ -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 (
<ProgressSliderBody ref={bodyRef}>
{/* <animated.div style={{ x: barPos.x }}> */}
<ProgressSliderBar>
{/* <animated.div {...bindBarPos()} style={{ x: barPos.x }}> */}
<ProgressSliderBarHandleContainer {...bindBarPos()} style={{ x: barPos.x }}>
<ProgressSliderBarHandle />
</ProgressSliderBarHandleContainer>
{/* </animated.div> */}
</ProgressSliderBar>
{/* </animated.div> */}
</ProgressSliderBody>
);
}
@@ -0,0 +1 @@
export * from "./ProgressSlider";
@@ -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<ProgressSliderBarProps>`
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";
@@ -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 (
<Stack as={PadBox} gutter="md" padding="sm">
<Inline gutter="md" stretch="end">
<Toggle
label="Show Overlay"
toggled={visible}
onToggle={onToggle}
onToggle={onToggleVisibility}
/>
<SlotSelector
items={overlayIds}
@@ -46,9 +57,10 @@ export function OverlayControl() {
formatter={(value) => value.replace(/(\.png)|(\.jpg)$/, "")}
/>
</Inline>
<MockupBlock color="green" height="1.5rem" width="100%">
Opacity Slider
</MockupBlock>
<ProgressSlider
value={currentOverlayItem?.opacity ?? 1}
onChange={onChangeOpacity}
/>
</Stack>
);
}
@@ -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({
}}
>
<animated.div style={{ ...style, position: "absolute" }}>
<OverlayImageContainer>
<OverlayImageContainer opacity={currentOverlayItem?.opacity ?? 1}>
<OverlayImageSubContainer>
<OverlayImage
ref={overlayItemImageRef}
@@ -17,7 +17,16 @@ export const OverlayImage = styled.img.attrs<OverlayImageProps>(({
`;
OverlayImage.displayName = "OverlayImage";
export const OverlayImageContainer = styled.div`
type OverlayImageContainerProps = {
opacity: number,
};
export const OverlayImageContainer = styled.div.attrs<OverlayImageContainerProps>(({
opacity,
}) => ({
style: {
opacity,
},
}))<OverlayImageContainerProps>`
display: flex;
`;
OverlayImageContainer.displayName = "OverlayImageContainer";