Skip to content

Commit

Permalink
fix: correct range input behavior, fixes #64
Browse files Browse the repository at this point in the history
  • Loading branch information
theKashey committed Apr 16, 2022
1 parent 24fcd4b commit b405438
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 17 deletions.
30 changes: 17 additions & 13 deletions src/SideEffect.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import * as React from 'react';
import { TouchEvent } from 'react';
import { RemoveScrollBar } from 'react-remove-scroll-bar';
import { styleSingleton } from 'react-style-singleton';
import { handleScroll, locationCouldBeScrolled } from './handleScroll';
import { nonPassive } from './aggresiveCapture';
import { Axis, IRemoveScrollEffectProps } from './types';
import {TouchEvent} from 'react';
import {RemoveScrollBar} from 'react-remove-scroll-bar';
import {styleSingleton} from 'react-style-singleton';
import {handleScroll, locationCouldBeScrolled} from './handleScroll';
import {nonPassive} from './aggresiveCapture';
import {Axis, IRemoveScrollEffectProps} from './types';

export const getTouchXY = (event: TouchEvent | WheelEvent) =>
'changedTouches' in event
Expand All @@ -28,9 +28,7 @@ let idCounter = 0;
let lockStack: any[] = [];

export function RemoveScrollSideCar(props: IRemoveScrollEffectProps) {
const shouldPreventQueue = React.useRef<
Array<{ name: string; delta: number[]; target: any; should: boolean }>
>([]);
const shouldPreventQueue = React.useRef<Array<{ name: string; delta: number[]; target: any; should: boolean }>>([]);
const touchStartRef = React.useRef([0, 0]);
const activeAxis = React.useRef<Axis | undefined>();
const [id] = React.useState(idCounter++);
Expand Down Expand Up @@ -86,6 +84,12 @@ export function RemoveScrollSideCar(props: IRemoveScrollEffectProps) {
const moveDirection: Axis =
Math.abs(deltaX) > Math.abs(deltaY) ? 'h' : 'v';

// allow horizontal touch move on Range inputs. They will not cause any scroll
if ('touches' in event && moveDirection === 'h' && (target as HTMLInputElement).type === 'range') {
return false;
}


let canBeScrolledInMainDirection = locationCouldBeScrolled(
moveDirection,
target
Expand Down Expand Up @@ -174,7 +178,7 @@ export function RemoveScrollSideCar(props: IRemoveScrollEffectProps) {

const shouldCancel = React.useCallback(
(name: string, delta: number[], target: any, should: boolean) => {
const event = { name, delta, target, should };
const event = {name, delta, target, should};
shouldPreventQueue.current.push(event);
setTimeout(() => {
shouldPreventQueue.current = shouldPreventQueue.current.filter(
Expand Down Expand Up @@ -240,12 +244,12 @@ export function RemoveScrollSideCar(props: IRemoveScrollEffectProps) {
};
}, []);

const { removeScrollBar, inert } = props;
const {removeScrollBar, inert} = props;

return (
<React.Fragment>
{inert ? <Style styles={generateStyle(id)} /> : null}
{removeScrollBar ? <RemoveScrollBar gapMode="margin" /> : null}
{inert ? <Style styles={generateStyle(id)}/> : null}
{removeScrollBar ? <RemoveScrollBar gapMode="margin"/> : null}
</React.Fragment>
);
}
5 changes: 1 addition & 4 deletions src/handleScroll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@ const elementCouldBeVScrolled = (node: HTMLElement): boolean => {

const elementCouldBeHScrolled = (node: HTMLElement): boolean => {
const styles = window.getComputedStyle(node);
// we allow horizontal scroll on range elements
if ((node as HTMLInputElement).type === "range") {
return true;
}

return (
styles.overflowX !== 'hidden' && // not-not-scrollable
!(styles.overflowY === styles.overflowX && styles.overflowX === 'visible') // scrollable
Expand Down

0 comments on commit b405438

Please sign in to comment.