Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve range slider callbacks #472

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 22 additions & 5 deletions src/components/RangeSlider/RangeSlider.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ import styled from 'styled-components';
import { Story, Meta } from '@storybook/react';
import { readableColor, toColorString } from 'polished';

import { action } from '@storybook/addon-actions';
import { withFoundryContext } from '../../../.storybook/decorators';
import fonts from '../../enums/fonts';
import colors from '../../enums/colors';
import RangeSlider, { SlideRail } from './RangeSlider';
import { RangeSliderProps, ValueProp } from './types';

import Card from '../Card';

const Row = styled.div`
Expand Down Expand Up @@ -102,7 +104,10 @@ export const Default: Story<DefaultProps> = ({
min={min}
max={max}
debounceInterval={debounceInterval}
onDebounceChange={newVal => setVal(Math.round(newVal))}
onDebounceChange={newVal => {
action('onDebounceChange')(newVal);
setVal(Math.round(newVal));
}}
dragHandleAttachment={dragHandleAttachment}
values={[{ value: val, label: val }]}
markers={markersArray as RangeSliderProps['markers']}
Expand Down Expand Up @@ -152,7 +157,10 @@ export const Rating: Story<RatingProps> = ({
springOnRelease={springOnRelease}
min={min}
max={max}
onChange={newVal => setVal(Math.round(newVal))}
onChange={newVal => {
action('onChange')(newVal);
setVal(Math.round(newVal));
}}
values={[
{
value: val,
Expand Down Expand Up @@ -250,7 +258,10 @@ export const ColorPicker: Story<ColorPickerProps> = ({
min={0}
max={360}
debounceInterval={debounceInterval}
onDebounceChange={val => setHue(Math.round(val))}
onDebounceChange={val => {
action('onDebounceChange hue')(val);
setHue(Math.round(val));
}}
values={[
{
value: hue_,
Expand All @@ -275,7 +286,10 @@ export const ColorPicker: Story<ColorPickerProps> = ({
min={0}
max={100}
debounceInterval={debounceInterval}
onDebounceChange={val => setSat(Math.round(val))}
onDebounceChange={val => {
action('onDebounceChange saturation')(val);
setSat(Math.round(val));
}}
showDomainLabels={false}
showSelectedRange={false}
values={[
Expand Down Expand Up @@ -304,7 +318,10 @@ export const ColorPicker: Story<ColorPickerProps> = ({
min={0}
max={100}
debounceInterval={debounceInterval}
onDebounceChange={val => setLight(Math.round(val))}
onDebounceChange={val => {
action('onDebounceChange lightness')(val);
setLight(Math.round(val));
}}
showDomainLabels={false}
showSelectedRange={false}
values={[
Expand Down
15 changes: 12 additions & 3 deletions src/components/RangeSlider/RangeSlider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export const Container = styled.div`
`;

export const DragHandle = styled(a.div)`
${({ $beingDragged = false, color, readonly }: HandleProps) => {
${({ $beingDragged = false, color, $readonly }: HandleProps) => {
const { colors } = useTheme();
const handleColor = color || colors.primary;
return `
Expand All @@ -88,7 +88,7 @@ export const DragHandle = styled(a.div)`
touch-action: none;
filter: url(#blur);
cursor: ${$beingDragged ? 'grabbing' : 'grab'};
cursor: ${readonly ? 'default' : ''};
cursor: ${$readonly ? 'default' : ''};
z-index: 2;
`;
}}
Expand Down Expand Up @@ -379,6 +379,8 @@ export const RangeSlider = ({

const bind = useDrag(
({ down, movement: [deltaX] }) => {
if (readonly) return;

const delta = (deltaX / sliderBounds.width) * domain;
valueBuffer.current = clamp(delta, min, max);
setDraggedHandle(down ? 0 : -1);
Expand Down Expand Up @@ -426,6 +428,13 @@ export const RangeSlider = ({
}
}, [springRef, pixelPositions, draggedHandle, prefersReducedMotion, sliderBounds]);

useEffect(() => {
return () => {
debouncedOnChange.cancel();
debouncedOnRelease.cancel();
};
}, [debouncedOnChange, debouncedOnRelease]);

return (
<StyledContainer
data-test-id={['hs-ui-range-slider', testId].join('-')}
Expand Down Expand Up @@ -479,7 +488,7 @@ export const RangeSlider = ({
key={`handle${i}`}
ref={dragHandleRef}
onMouseUp={() => debouncedOnRelease(value)}
readonly={readonly}
$readonly={readonly}
{...dragHandleProps}
>
{showHandleLabels && (
Expand Down
4 changes: 2 additions & 2 deletions src/components/RangeSlider/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export type ContainerProps = {
export type HandleProps = {
$beingDragged?: boolean;
color: string;
readonly: boolean;
$readonly: boolean;
};

export type HandleLabelProps = { velocity?: number; showHandleLabels?: boolean };
Expand Down Expand Up @@ -84,7 +84,7 @@ export type RangeSliderProps = {
testId?: string;
markers?: number[] | ValueProp[];
/**
* Whether the drag handle should follow the passed-in valule, or the mouse.
* Whether the drag handle should follow the passed-in value, or the mouse.
*/
dragHandleAttachment?: 'value' | 'mouse';

Expand Down
Loading