ISSUE-021 - Discovered the animated() wrapper solution to using Styled-Components that solves some problems.

Currently working on making the draggable activation area larger.
This commit is contained in:
Kyle Geib committed 2022-01-18 15:42:43 -05:00
1 parent 9925eae4cc
commit 8170ba3f86
2 files changed
+28 -21

No files matched your search

@@ -22,8 +22,12 @@ export function ProgressSlider({
}] = useMeasure(); }] = useMeasure();
const barPos = useSpring({ x: (bodyWidth - MARGIN_WIDTH) * value }); const barPos = useSpring({ x: (bodyWidth - MARGIN_WIDTH) * value });
const bindBarPos = useDrag((params) => { const bindBarPos = useDrag((params) => {
const newPos = clamp(barPos.x.get() + params.delta[0], MARGIN_WIDTH, bodyWidth - MARGIN_WIDTH); const newPos = clamp(
const newValue = newPos / (bodyWidth - MARGIN_WIDTH); barPos.x.get() + params.delta[0],
MARGIN_WIDTH,
bodyWidth - MARGIN_WIDTH,
);
const newValue = (newPos - MARGIN_WIDTH) / (bodyWidth - 2 * MARGIN_WIDTH);
barPos.x.set(newPos); barPos.x.set(newPos);
onChange(newValue); onChange(newValue);
@@ -31,20 +35,16 @@ export function ProgressSlider({
console.log("drag", newPos, newValue); console.log("drag", newPos, newValue);
}); });
// const
// console.log("main", bodyWidth, barPos.x.get()); const AnimatedProgressSliderBar = animated(ProgressSliderBar);
// console.log("main",barPos.x.get()); const AnimatedProgressSliderBarHandleContainer = animated(ProgressSliderBarHandleContainer);
// console.log(bodyWidth);
return ( return (
<ProgressSliderBody ref={bodyRef}> <ProgressSliderBody ref={bodyRef}>
<AnimatedProgressSliderBar style={{ x: barPos.x }} bodyWidth={bodyWidth - MARGIN_WIDTH} />
<AnimatedProgressSliderBarHandleContainer {...bindBarPos()} style={{ x: barPos.x }}>
<ProgressSliderBar style={{ x: barPos.x }} bodyWidth={bodyWidth - MARGIN_WIDTH} />
<ProgressSliderBarHandleContainer {...bindBarPos()} style={{ x: barPos.x }}>
<ProgressSliderBarHandle /> <ProgressSliderBarHandle />
</ProgressSliderBarHandleContainer> </AnimatedProgressSliderBarHandleContainer>
</ProgressSliderBody> </ProgressSliderBody>
); );
} }
@@ -1,4 +1,3 @@
import { animated } from "@react-spring/web";
import styled from "styled-components"; import styled from "styled-components";
export const ProgressSliderBody = styled.div` export const ProgressSliderBody = styled.div`
@@ -20,10 +19,9 @@ type ProgressSliderBarProps = {
bodyWidth: number, bodyWidth: number,
color?: string, color?: string,
}; };
export const ProgressSliderBar = styled(animated.div)<ProgressSliderBarProps>` export const ProgressSliderBar = styled.div<ProgressSliderBarProps>`
position: absolute; position: absolute;
left: ${({ bodyWidth }) => `${-bodyWidth}px`}; left: ${({ bodyWidth }) => `${-bodyWidth}px`};
/* left: -359px; */
height: 100%; height: 100%;
width: 100%; width: 100%;
background-color: ${({ color }) => (color ?? (p => p.theme.colors.activeGreen))}; background-color: ${({ color }) => (color ?? (p => p.theme.colors.activeGreen))};
@@ -31,21 +29,30 @@ export const ProgressSliderBar = styled(animated.div)<ProgressSliderBarProps>`
ProgressSliderBar.displayName = "ProgressSliderBar"; ProgressSliderBar.displayName = "ProgressSliderBar";
export const ProgressSliderBarHandleContainer = styled(animated.div)` export const ProgressSliderBarHandleContainer = styled.div`
position: absolute; position: relative;
left: -50%;
display: flex;
align-items: center;
justify-content: center;
width: 100%; width: 100%;
height: 100%; height: 100%;
background-color: rgba(200, 200, 0, 0.3);
`; `;
ProgressSliderBarHandleContainer.displayName = "ProgressSliderBarHandleContainer"; ProgressSliderBarHandleContainer.displayName = "ProgressSliderBarHandleContainer";
export const ProgressSliderBarHandle = styled.div` export const ProgressSliderBarHandle = styled.div`
position: relative; position: relative;
width: 1ch; width: 1ch;
height: 1.2em; height: 80%;
background-color: black; border-radius: 2px;
margin-top: 1px;
border-radius: 3px;
opacity: 0.5; opacity: 0.5;
background-color: white;
cursor: pointer; cursor: pointer;
&:hover { &:hover {