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,
|
||||
} 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)}%`;
|
||||
|
||||
@@ -18,16 +23,25 @@ type ProgressSliderProps = {
|
||||
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(
|
||||
@@ -41,6 +55,8 @@ export function ProgressSlider({
|
||||
onChange(newValue);
|
||||
}, { enabled: !disabled });
|
||||
|
||||
console.log(bodyWidth, labelWidth, value, barPos.x.get());
|
||||
|
||||
const AnimatedProgressSliderBar = animated(ProgressSliderBar);
|
||||
|
||||
return (
|
||||
@@ -49,9 +65,22 @@ export function ProgressSlider({
|
||||
{...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}
|
||||
>
|
||||
<ProgressSliderBarHandle />
|
||||
<ProgressSliderBarIndicator left={barPos.x.get() > 45 || barPos.x.get() < 0}>
|
||||
{formatter(value)}
|
||||
</ProgressSliderBarIndicator>
|
||||
</AnimatedProgressSliderBar>
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import styled, { css } from "styled-components";
|
||||
import styled from "styled-components";
|
||||
|
||||
export const ProgressSliderBody = styled.div`
|
||||
position: relative;
|
||||
@@ -17,6 +17,7 @@ export const ProgressSliderBody = styled.div`
|
||||
|
||||
type ProgressSliderBarProps = {
|
||||
bodyWidth: number,
|
||||
disabled: boolean,
|
||||
color?: string,
|
||||
};
|
||||
export const ProgressSliderBar = styled.div<ProgressSliderBarProps>`
|
||||
@@ -24,11 +25,18 @@ export const ProgressSliderBar = styled.div<ProgressSliderBarProps>`
|
||||
left: ${({ bodyWidth }) => `${-bodyWidth}px`};
|
||||
height: 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";
|
||||
|
||||
export const ProgressSliderBarHandle = styled.div`
|
||||
type ProgressSliderBarHandleProps = {
|
||||
color?: string,
|
||||
};
|
||||
export const ProgressSliderBarHandle = styled.div<ProgressSliderBarHandleProps>`
|
||||
position: relative;
|
||||
float: right;
|
||||
|
||||
@@ -41,7 +49,8 @@ export const ProgressSliderBarHandle = styled.div`
|
||||
border-radius: 2px;
|
||||
|
||||
opacity: 0.5;
|
||||
background-color: white;
|
||||
background-color: ${({ color = "white" }) => color};
|
||||
transition: background-color 0.2s ease;
|
||||
cursor: pointer;
|
||||
|
||||
&:hover {
|
||||
@@ -51,18 +60,25 @@ export const ProgressSliderBarHandle = styled.div`
|
||||
ProgressSliderBarHandle.displayName = "ProgressSliderBarHandle";
|
||||
|
||||
type ProgressSliderBarIndicatorProps = {
|
||||
left: boolean,
|
||||
left: number,
|
||||
disabled: boolean,
|
||||
onBarColor?: string,
|
||||
};
|
||||
export const ProgressSliderBarIndicator = styled.span<ProgressSliderBarIndicatorProps>`
|
||||
display: block;
|
||||
position: relative;
|
||||
float: right;
|
||||
left: ${({ left }) => left ? -1 : 3}ch;
|
||||
left: ${({ left }) => left}ch;
|
||||
|
||||
font-family: ${p => p.theme.fonts.f1Regular};
|
||||
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-timing-function: ease;
|
||||
|
||||
Reference in new issue
Block a user