ISSUE-021 - Added color customization props. Nice disabled look.
Okay. I'm content with what I've made. It's not really up to the level of "oh man, package this up as a library and put it out on NPM!" but it works OK for my specific situation.
This commit is contained in:
1 parent
d4f8e9bffe
commit
65a9f523d4
2 files changed
+54
-9
No files matched your search
@@ -10,6 +10,11 @@ import {
|
|||||||
ProgressSliderBody,
|
ProgressSliderBody,
|
||||||
} from "./styles";
|
} 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 MARGIN_WIDTH = 15;
|
||||||
const percentage = (value: number) => `${Math.floor(value * 100)}%`;
|
const percentage = (value: number) => `${Math.floor(value * 100)}%`;
|
||||||
|
|
||||||
@@ -18,16 +23,25 @@ type ProgressSliderProps = {
|
|||||||
onChange: (value: number) => void,
|
onChange: (value: number) => void,
|
||||||
disabled?: boolean,
|
disabled?: boolean,
|
||||||
formatter?: (value: number) => string,
|
formatter?: (value: number) => string,
|
||||||
|
barColor?: string,
|
||||||
|
labelColor?: string,
|
||||||
|
handleColor?: string,
|
||||||
};
|
};
|
||||||
export function ProgressSlider({
|
export function ProgressSlider({
|
||||||
value,
|
value,
|
||||||
onChange,
|
onChange,
|
||||||
disabled = false,
|
disabled = false,
|
||||||
formatter = percentage,
|
formatter = percentage,
|
||||||
|
barColor,
|
||||||
|
labelColor,
|
||||||
|
handleColor,
|
||||||
}: ProgressSliderProps) {
|
}: ProgressSliderProps) {
|
||||||
const [bodyRef, {
|
const [bodyRef, {
|
||||||
width: bodyWidth,
|
width: bodyWidth,
|
||||||
}] = useMeasure();
|
}] = useMeasure();
|
||||||
|
const [labelRef, {
|
||||||
|
width: labelWidth,
|
||||||
|
}] = useMeasure();
|
||||||
const barPos = useSpring({ x: ((bodyWidth - 2 * MARGIN_WIDTH) * value) + MARGIN_WIDTH });
|
const barPos = useSpring({ x: ((bodyWidth - 2 * MARGIN_WIDTH) * value) + MARGIN_WIDTH });
|
||||||
const bindBarPos = useDrag((params) => {
|
const bindBarPos = useDrag((params) => {
|
||||||
const newPos = clamp(
|
const newPos = clamp(
|
||||||
@@ -41,6 +55,8 @@ export function ProgressSlider({
|
|||||||
onChange(newValue);
|
onChange(newValue);
|
||||||
}, { enabled: !disabled });
|
}, { enabled: !disabled });
|
||||||
|
|
||||||
|
console.log(bodyWidth, labelWidth, value, barPos.x.get());
|
||||||
|
|
||||||
const AnimatedProgressSliderBar = animated(ProgressSliderBar);
|
const AnimatedProgressSliderBar = animated(ProgressSliderBar);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
@@ -49,9 +65,22 @@ export function ProgressSlider({
|
|||||||
{...bindBarPos()}
|
{...bindBarPos()}
|
||||||
style={{ x: barPos.x }}
|
style={{ x: barPos.x }}
|
||||||
bodyWidth={bodyWidth - MARGIN_WIDTH}
|
bodyWidth={bodyWidth - MARGIN_WIDTH}
|
||||||
|
disabled={disabled}
|
||||||
|
color={barColor}
|
||||||
>
|
>
|
||||||
<ProgressSliderBarHandle />
|
<ProgressSliderBarHandle color={disabled ? "black" : handleColor} />
|
||||||
<ProgressSliderBarIndicator left={barPos.x.get() > 45 || barPos.x.get() < 0}>
|
<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)}
|
{formatter(value)}
|
||||||
</ProgressSliderBarIndicator>
|
</ProgressSliderBarIndicator>
|
||||||
</AnimatedProgressSliderBar>
|
</AnimatedProgressSliderBar>
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import styled, { css } from "styled-components";
|
import styled from "styled-components";
|
||||||
|
|
||||||
export const ProgressSliderBody = styled.div`
|
export const ProgressSliderBody = styled.div`
|
||||||
position: relative;
|
position: relative;
|
||||||
@@ -17,6 +17,7 @@ export const ProgressSliderBody = styled.div`
|
|||||||
|
|
||||||
type ProgressSliderBarProps = {
|
type ProgressSliderBarProps = {
|
||||||
bodyWidth: number,
|
bodyWidth: number,
|
||||||
|
disabled: boolean,
|
||||||
color?: string,
|
color?: string,
|
||||||
};
|
};
|
||||||
export const ProgressSliderBar = styled.div<ProgressSliderBarProps>`
|
export const ProgressSliderBar = styled.div<ProgressSliderBarProps>`
|
||||||
@@ -24,11 +25,18 @@ export const ProgressSliderBar = styled.div<ProgressSliderBarProps>`
|
|||||||
left: ${({ bodyWidth }) => `${-bodyWidth}px`};
|
left: ${({ bodyWidth }) => `${-bodyWidth}px`};
|
||||||
height: 100%;
|
height: 100%;
|
||||||
width: 100%;
|
width: 100%;
|
||||||
background-color: ${({ color }) => (color ?? (p => p.theme.colors.activeGreen))};
|
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";
|
ProgressSliderBar.displayName = "ProgressSliderBar";
|
||||||
|
|
||||||
export const ProgressSliderBarHandle = styled.div`
|
type ProgressSliderBarHandleProps = {
|
||||||
|
color?: string,
|
||||||
|
};
|
||||||
|
export const ProgressSliderBarHandle = styled.div<ProgressSliderBarHandleProps>`
|
||||||
position: relative;
|
position: relative;
|
||||||
float: right;
|
float: right;
|
||||||
|
|
||||||
@@ -41,7 +49,8 @@ export const ProgressSliderBarHandle = styled.div`
|
|||||||
border-radius: 2px;
|
border-radius: 2px;
|
||||||
|
|
||||||
opacity: 0.5;
|
opacity: 0.5;
|
||||||
background-color: white;
|
background-color: ${({ color = "white" }) => color};
|
||||||
|
transition: background-color 0.2s ease;
|
||||||
cursor: pointer;
|
cursor: pointer;
|
||||||
|
|
||||||
&:hover {
|
&:hover {
|
||||||
@@ -51,18 +60,25 @@ export const ProgressSliderBarHandle = styled.div`
|
|||||||
ProgressSliderBarHandle.displayName = "ProgressSliderBarHandle";
|
ProgressSliderBarHandle.displayName = "ProgressSliderBarHandle";
|
||||||
|
|
||||||
type ProgressSliderBarIndicatorProps = {
|
type ProgressSliderBarIndicatorProps = {
|
||||||
left: boolean,
|
left: number,
|
||||||
|
disabled: boolean,
|
||||||
onBarColor?: string,
|
onBarColor?: string,
|
||||||
};
|
};
|
||||||
export const ProgressSliderBarIndicator = styled.span<ProgressSliderBarIndicatorProps>`
|
export const ProgressSliderBarIndicator = styled.span<ProgressSliderBarIndicatorProps>`
|
||||||
display: block;
|
display: block;
|
||||||
position: relative;
|
position: relative;
|
||||||
float: right;
|
float: right;
|
||||||
left: ${({ left }) => left ? -1 : 3}ch;
|
left: ${({ left }) => left}ch;
|
||||||
|
|
||||||
font-family: ${p => p.theme.fonts.f1Regular};
|
font-family: ${p => p.theme.fonts.f1Regular};
|
||||||
font-size: 0.9em;
|
font-size: 0.9em;
|
||||||
color: ${({ onBarColor, left }) => left ? onBarColor ?? "white" : "black"};
|
color: ${({ onBarColor, left, disabled }) => (
|
||||||
|
left < 0
|
||||||
|
? disabled
|
||||||
|
? (p => p.theme.colors.darkGrey)
|
||||||
|
: onBarColor ?? "white"
|
||||||
|
: (p => p.theme.colors.darkGrey)
|
||||||
|
)};
|
||||||
|
|
||||||
transition-property: left, color;
|
transition-property: left, color;
|
||||||
transition-timing-function: ease;
|
transition-timing-function: ease;
|
||||||
|
|||||||
Reference in new issue
Block a user