6 files changed
+204
-9
No files matched your search
@@ -0,0 +1,89 @@
|
|||||||
|
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,
|
||||||
|
ProgressSliderBarIndicator,
|
||||||
|
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)}%`;
|
||||||
|
|
||||||
|
type ProgressSliderProps = {
|
||||||
|
value: number, // Fraction of 1 (0.0 to 1.0)
|
||||||
|
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(
|
||||||
|
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);
|
||||||
|
}, { enabled: !disabled });
|
||||||
|
|
||||||
|
console.log(bodyWidth, labelWidth, value, barPos.x.get());
|
||||||
|
|
||||||
|
const AnimatedProgressSliderBar = animated(ProgressSliderBar);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<ProgressSliderBody ref={bodyRef}>
|
||||||
|
<AnimatedProgressSliderBar
|
||||||
|
{...bindBarPos()}
|
||||||
|
style={{ x: barPos.x }}
|
||||||
|
bodyWidth={bodyWidth - MARGIN_WIDTH}
|
||||||
|
disabled={disabled}
|
||||||
|
color={barColor}
|
||||||
|
>
|
||||||
|
<ProgressSliderBarHandle color={disabled ? "black" : handleColor} />
|
||||||
|
<ProgressSliderBarIndicator
|
||||||
|
ref={labelRef}
|
||||||
|
left={
|
||||||
|
(
|
||||||
|
barPos.x.get() > labelWidth * 1.5
|
||||||
|
|| barPos.x.get() < 0
|
||||||
|
|| value === 1
|
||||||
|
) ? -1 : value > 0.1 ? 4 : 3 // Tuned fitting considerations.
|
||||||
|
}
|
||||||
|
disabled={disabled}
|
||||||
|
onBarColor={labelColor}
|
||||||
|
>
|
||||||
|
{formatter(value)}
|
||||||
|
</ProgressSliderBarIndicator>
|
||||||
|
</AnimatedProgressSliderBar>
|
||||||
|
</ProgressSliderBody>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
export * from "./ProgressSlider";
|
||||||
@@ -0,0 +1,89 @@
|
|||||||
|
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 = {
|
||||||
|
bodyWidth: number,
|
||||||
|
disabled: boolean,
|
||||||
|
color?: string,
|
||||||
|
};
|
||||||
|
export const ProgressSliderBar = styled.div<ProgressSliderBarProps>`
|
||||||
|
position: absolute;
|
||||||
|
left: ${({ bodyWidth }) => `${-bodyWidth}px`};
|
||||||
|
height: 100%;
|
||||||
|
width: 100%;
|
||||||
|
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";
|
||||||
|
|
||||||
|
type ProgressSliderBarHandleProps = {
|
||||||
|
color?: string,
|
||||||
|
};
|
||||||
|
export const ProgressSliderBarHandle = styled.div<ProgressSliderBarHandleProps>`
|
||||||
|
position: relative;
|
||||||
|
float: right;
|
||||||
|
|
||||||
|
width: 0.8ch;
|
||||||
|
right: 0.5ch;
|
||||||
|
|
||||||
|
height: 70%;
|
||||||
|
top: 15%;
|
||||||
|
|
||||||
|
border-radius: 2px;
|
||||||
|
|
||||||
|
opacity: 0.5;
|
||||||
|
background-color: ${({ color = "white" }) => color};
|
||||||
|
transition: background-color 0.2s ease;
|
||||||
|
cursor: pointer;
|
||||||
|
|
||||||
|
&:hover {
|
||||||
|
opacity: 0.8;
|
||||||
|
}
|
||||||
|
`;
|
||||||
|
ProgressSliderBarHandle.displayName = "ProgressSliderBarHandle";
|
||||||
|
|
||||||
|
type ProgressSliderBarIndicatorProps = {
|
||||||
|
left: number,
|
||||||
|
disabled: boolean,
|
||||||
|
onBarColor?: string,
|
||||||
|
};
|
||||||
|
export const ProgressSliderBarIndicator = styled.span<ProgressSliderBarIndicatorProps>`
|
||||||
|
display: block;
|
||||||
|
position: relative;
|
||||||
|
float: right;
|
||||||
|
left: ${({ left }) => left}ch;
|
||||||
|
|
||||||
|
font-family: ${p => p.theme.fonts.f1Regular};
|
||||||
|
font-size: 0.9em;
|
||||||
|
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;
|
||||||
|
transition-duration: 0.2s;
|
||||||
|
|
||||||
|
user-select: none;
|
||||||
|
`;
|
||||||
|
ProgressSliderBarIndicator.displayName = "ProgressSliderBarIndicator";
|
||||||
@@ -7,19 +7,20 @@ import { overlayToolSelector } from "../../../features/overlayTool/overlayToolSe
|
|||||||
import {
|
import {
|
||||||
initNewOverlayItem,
|
initNewOverlayItem,
|
||||||
setCurrentOverlayId,
|
setCurrentOverlayId,
|
||||||
|
setOpacity,
|
||||||
setVisibility,
|
setVisibility,
|
||||||
} from "../../../features/overlayTool/overlayToolSlice";
|
} from "../../../features/overlayTool/overlayToolSlice";
|
||||||
|
|
||||||
import { Toggle } from "../../Common/Inputs/Toggle";
|
import { Toggle } from "../../Common/Inputs/Toggle";
|
||||||
import { MockupBlock } from "../../Common/MockupBlock.styled";
|
|
||||||
import { SlotSelector } from "../../Common/Inputs/SlotSelector";
|
import { SlotSelector } from "../../Common/Inputs/SlotSelector";
|
||||||
|
import { ProgressSlider } from "../../Common/Inputs/ProgressSlider";
|
||||||
|
|
||||||
|
|
||||||
export function OverlayControl() {
|
export function OverlayControl() {
|
||||||
const dispatch = useDispatch();
|
const dispatch = useDispatch();
|
||||||
const { overlayIds, visible, currentOverlayItem } = useSelector(overlayToolSelector);
|
const { overlayIds, visible, currentOverlayItem } = useSelector(overlayToolSelector);
|
||||||
|
|
||||||
const onToggle = (value: boolean) => {
|
const onToggleVisibility = (value: boolean) => {
|
||||||
if (currentOverlayItem == null) {
|
if (currentOverlayItem == null) {
|
||||||
dispatch(initNewOverlayItem({
|
dispatch(initNewOverlayItem({
|
||||||
overlayId: overlayIds[0],
|
overlayId: overlayIds[0],
|
||||||
@@ -31,13 +32,17 @@ export function OverlayControl() {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const onChangeOpacity = (value: number) => {
|
||||||
|
dispatch(setOpacity(value));
|
||||||
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Stack as={PadBox} gutter="md" padding="sm">
|
<Stack as={PadBox} gutter="md" padding="sm">
|
||||||
<Inline gutter="md" stretch="end">
|
<Inline gutter="md" stretch="end">
|
||||||
<Toggle
|
<Toggle
|
||||||
label="Show Overlay"
|
label="Show Overlay"
|
||||||
toggled={visible}
|
toggled={visible}
|
||||||
onToggle={onToggle}
|
onToggle={onToggleVisibility}
|
||||||
/>
|
/>
|
||||||
<SlotSelector
|
<SlotSelector
|
||||||
items={overlayIds}
|
items={overlayIds}
|
||||||
@@ -46,9 +51,11 @@ export function OverlayControl() {
|
|||||||
formatter={(value) => value.replace(/(\.png)|(\.jpg)$/, "")}
|
formatter={(value) => value.replace(/(\.png)|(\.jpg)$/, "")}
|
||||||
/>
|
/>
|
||||||
</Inline>
|
</Inline>
|
||||||
<MockupBlock color="green" height="1.5rem" width="100%">
|
<ProgressSlider
|
||||||
Opacity Slider
|
value={currentOverlayItem?.opacity ?? 1}
|
||||||
</MockupBlock>
|
onChange={onChangeOpacity}
|
||||||
|
disabled={!visible}
|
||||||
|
/>
|
||||||
</Stack>
|
</Stack>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -53,7 +53,7 @@ export function OverlayDisplay({
|
|||||||
from: { top: 50, opacity: 0 },
|
from: { top: 50, opacity: 0 },
|
||||||
enter: {
|
enter: {
|
||||||
top: 0,
|
top: 0,
|
||||||
opacity: currentOverlayItem?.opacity ?? 1,
|
opacity: 1,
|
||||||
},
|
},
|
||||||
leave: { top: 50, opacity: 0 },
|
leave: { top: 50, opacity: 0 },
|
||||||
});
|
});
|
||||||
@@ -77,7 +77,7 @@ export function OverlayDisplay({
|
|||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<animated.div style={{ ...style, position: "absolute" }}>
|
<animated.div style={{ ...style, position: "absolute" }}>
|
||||||
<OverlayImageContainer>
|
<OverlayImageContainer opacity={currentOverlayItem?.opacity ?? 1}>
|
||||||
<OverlayImageSubContainer>
|
<OverlayImageSubContainer>
|
||||||
<OverlayImage
|
<OverlayImage
|
||||||
ref={overlayItemImageRef}
|
ref={overlayItemImageRef}
|
||||||
|
|||||||
@@ -17,7 +17,16 @@ export const OverlayImage = styled.img.attrs<OverlayImageProps>(({
|
|||||||
`;
|
`;
|
||||||
OverlayImage.displayName = "OverlayImage";
|
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;
|
display: flex;
|
||||||
`;
|
`;
|
||||||
OverlayImageContainer.displayName = "OverlayImageContainer";
|
OverlayImageContainer.displayName = "OverlayImageContainer";
|
||||||
|
|||||||
Reference in new issue
Block a user