Skip to content
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
34 changes: 32 additions & 2 deletions core/src/components/range/range-interface.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export type KnobName = 'A' | 'B' | undefined;
export type RangeKnobName = 'A' | 'B';

export type KnobPosition = 'lower' | 'upper' | undefined;
export type RangeKnobPosition = 'lower' | 'upper';

export type RangeValue = number | { lower: number; upper: number };

Expand All @@ -12,13 +12,43 @@ export interface RangeChangeEventDetail {

export interface RangeKnobMoveStartEventDetail {
value: RangeValue;
/**
* The knob being moved. Each knob keeps its name even if the knobs
* cross. A single knob range always reports `A`.
*/
knob: RangeKnobName;
/**
* Whether the moved knob holds the `lower` or `upper` value before it moves.
* A single knob range always reports `lower`.
*/
position: RangeKnobPosition;
}

export interface RangeKnobMoveEndEventDetail {
value: RangeValue;
/**
* The knob that was moved. Each knob keeps its name even if the knobs
* cross. A single knob range always reports `A`.
*/
knob: RangeKnobName;
/**
* Whether the moved knob holds the `lower` or `upper` value after it moves.
* A single knob range always reports `lower`.
*/
position: RangeKnobPosition;
}

export interface RangeCustomEvent extends CustomEvent {
detail: RangeChangeEventDetail | RangeKnobMoveStartEventDetail | RangeKnobMoveEndEventDetail;
target: HTMLIonRangeElement;
}

export interface RangeKnobMoveStartCustomEvent extends CustomEvent {
detail: RangeKnobMoveStartEventDetail;
target: HTMLIonRangeElement;
}

export interface RangeKnobMoveEndCustomEvent extends CustomEvent {
detail: RangeKnobMoveEndEventDetail;
target: HTMLIonRangeElement;
}
136 changes: 57 additions & 79 deletions core/src/components/range/range.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import type { Color, Gesture, GestureDetail } from '../../interface';
import { roundToMaxDecimalPlaces } from '../../utils/floating-point';

import type {
KnobName,
KnobPosition,
RangeKnobName,
RangeKnobPosition,
RangeChangeEventDetail,
RangeKnobMoveEndEventDetail,
RangeKnobMoveStartEventDetail,
Expand Down Expand Up @@ -93,10 +93,10 @@ export class Range implements ComponentInterface {

@State() private ratioA = 0;
@State() private ratioB = 0;
@State() private activatedKnob: KnobName;
@State() private focusedKnob: KnobName;
@State() private hoveredKnob: KnobName;
@State() private pressedKnob: KnobName;
@State() private activatedKnob: RangeKnobName | undefined;
@State() private focusedKnob: RangeKnobName | undefined;
@State() private hoveredKnob: RangeKnobName | undefined;
@State() private pressedKnob: RangeKnobName | undefined;

/**
* The color to use from your application's color palette.
Expand Down Expand Up @@ -359,7 +359,7 @@ export class Range implements ComponentInterface {
* should not move.
*/
threshold: 10,
onStart: () => this.onStart(),
onStart: (ev) => this.onStart(ev.startX),
onMove: (ev) => this.onMove(ev),
onEnd: (ev) => this.onEnd(ev),
});
Expand Down Expand Up @@ -452,25 +452,24 @@ export class Range implements ComponentInterface {
}
}

private handleKeyboard = (knob: KnobName, isIncrease: boolean) => {
const { ensureValueInBounds } = this;

private handleKeyboard = (knob: RangeKnobName, isIncrease: boolean) => {
let step = this.step;
step = step > 0 ? step : 1;
step = step / (this.max - this.min);
if (!isIncrease) {
step *= -1;
}
this.ionKnobMoveStart.emit(this.getKnobMoveDetail(knob));

if (knob === 'A') {
this.ratioA = clamp(0, this.ratioA + step, 1);
} else {
this.ratioB = clamp(0, this.ratioB + step, 1);
}

this.ionKnobMoveStart.emit({ value: ensureValueInBounds(this.value) });
this.updateValue();
this.emitValueChange();
this.ionKnobMoveEnd.emit({ value: ensureValueInBounds(this.value) });
this.ionKnobMoveEnd.emit(this.getKnobMoveDetail(knob));
};
private getValue(): RangeValue {
const value = this.value ?? 0;
Expand Down Expand Up @@ -502,59 +501,32 @@ export class Range implements ComponentInterface {
}

/**
* The value should be updated on touch end or
* when the component is being dragged.
* This follows the native behavior of mobile devices.
* Called when the user starts dragging the bar or knob,
* or taps the bar. The view doesn't scroll during a drag
* so the user doesn't get disoriented.
*
* For example: When the user lifts their finger from the
* screen after tapping the bar or dragging the bar or knob.
* @param startX The x coordinate when the drag started, or where the bar was tapped.
*/
private onStart() {
this.ionKnobMoveStart.emit({ value: this.ensureValueInBounds(this.value) });
private onStart(startX: number) {
const { contentEl } = this;

if (contentEl) {
this.initialContentScrollY = disableContentScrollY(contentEl);
Comment thread
brandyscarney marked this conversation as resolved.
Resolved
}

const knob = this.setPressedKnob(startX);

this.ionKnobMoveStart.emit(this.getKnobMoveDetail(knob));
}

/**
* The value should be updated while dragging the
* bar or knob.
*
* While the user is dragging, the view
* should not scroll. This is to prevent the user from
* feeling disoriented while dragging.
*
* The user can scroll on the view if the knob or
* bar is not being dragged.
*
* @param detail The details of the gesture event.
*/
private onMove(detail: GestureDetail) {
const { contentEl, pressedKnob } = this;
const currentX = detail.currentX;

/**
* Since the user is dragging on the bar or knob, the view should not scroll.
*
* This only needs to be done once.
*/
if (contentEl && this.pressedKnob === undefined) {
this.initialContentScrollY = disableContentScrollY(contentEl);
}

/**
* The `pressedKnob` can be undefined if the user just
* started dragging the knob.
*
* This is necessary to determine which knob the user is dragging,
* especially when using dual knobs.
* Plus, it determines when to apply certain styles.
*
* This only needs to be done once since the knob won't change
* while the user is dragging.
*/
if (pressedKnob === undefined) {
this.setPressedKnob(currentX);
}

this.update(currentX);
this.update(detail.currentX);
}

/**
Expand All @@ -568,39 +540,30 @@ export class Range implements ComponentInterface {
const { contentEl, initialContentScrollY } = this;
const currentX = (detail as GestureDetail).currentX ?? (detail as MouseEvent).clientX;

/**
* The `pressedKnob` can be undefined if the user never
* dragged the knob. They just tapped on the bar.
*
* This is necessary to determine which knob the user is changing,
* especially when using dual knobs.
* Plus, it determines when to apply certain styles.
*/
if (this.pressedKnob === undefined) {
this.setPressedKnob(currentX);
}

/**
* The user is no longer dragging the bar or
* knob (if they were dragging it).
*
* The user can now scroll on the view in the next gesture event.
*/
if (contentEl && this.pressedKnob !== undefined) {
if (contentEl) {
resetContentScrollY(contentEl, initialContentScrollY);
}

// update the active knob's position
this.update(currentX);

// The pressed knob is always set because `onStart` runs first.
const knob = this.pressedKnob!;

/**
* Reset the pressed knob to undefined since the user
* may start dragging a different knob in the next gesture event.
*/
this.pressedKnob = undefined;

this.emitValueChange();
this.ionKnobMoveEnd.emit({ value: this.ensureValueInBounds(this.value) });
this.ionKnobMoveEnd.emit(this.getKnobMoveDetail(knob));
}

private update(currentX: number) {
Expand Down Expand Up @@ -628,15 +591,25 @@ export class Range implements ComponentInterface {
this.updateValue();
}

private setPressedKnob(currentX: number) {
private getKnobMoveDetail(knob: RangeKnobName): RangeKnobMoveStartEventDetail & RangeKnobMoveEndEventDetail {
return {
value: this.ensureValueInBounds(this.value),
knob,
position: getKnobPosition(knob, this.ratioA, this.ratioB, this.dualKnobs),
};
}

private setPressedKnob(currentX: number): RangeKnobName {
const rect = (this.rect = this.rangeSlider!.getBoundingClientRect() as any);

// figure out which knob they started closer to
let ratio = clamp(0, (currentX - rect.left) / rect.width, 1);
if (isRTL(this.el)) {
ratio = 1 - ratio;
}
this.pressedKnob = !this.dualKnobs || Math.abs(this.ratioA - ratio) < Math.abs(this.ratioB - ratio) ? 'A' : 'B';
const knob = !this.dualKnobs || Math.abs(this.ratioA - ratio) < Math.abs(this.ratioB - ratio) ? 'A' : 'B';
this.pressedKnob = knob;
return knob;
}

private get valA() {
Expand Down Expand Up @@ -722,7 +695,7 @@ export class Range implements ComponentInterface {
}
};

private onKnobFocus = (knob: KnobName) => {
private onKnobFocus = (knob: RangeKnobName) => {
// Clicking focuses the range which is needed for the keyboard,
// but we only want to add the ion-focused class when focused via Tab.
if (!this.focusFromPointer) {
Expand Down Expand Up @@ -756,7 +729,7 @@ export class Range implements ComponentInterface {
}, 0);
};

private onKnobMouseEnter = (knob: KnobName) => {
private onKnobMouseEnter = (knob: RangeKnobName) => {
this.hoveredKnob = knob;
};

Expand Down Expand Up @@ -903,7 +876,7 @@ export class Range implements ComponentInterface {
* dragged, the pressedKnob will be defined.
*/
if (this.pressedKnob === undefined) {
this.onStart();
this.onStart(ev.clientX);
this.onEnd(ev);
}
}}
Expand Down Expand Up @@ -1068,8 +1041,8 @@ export class Range implements ComponentInterface {
}

interface RangeKnob {
knob: KnobName;
position: KnobPosition;
knob: RangeKnobName;
position: RangeKnobPosition;
dualKnobs: boolean;
value: number;
ratio: number;
Expand All @@ -1083,10 +1056,10 @@ interface RangeKnob {
pin: boolean;
pinFormatter: PinFormatter;
inheritedAttributes: Attributes;
handleKeyboard: (name: KnobName, isIncrease: boolean) => void;
onKnobFocus: (knob: KnobName) => void;
handleKeyboard: (name: RangeKnobName, isIncrease: boolean) => void;
onKnobFocus: (knob: RangeKnobName) => void;
onKnobBlur: () => void;
onKnobMouseEnter: (knob: KnobName) => void;
onKnobMouseEnter: (knob: RangeKnobName) => void;
onKnobMouseLeave: () => void;
}

Expand Down Expand Up @@ -1231,7 +1204,12 @@ const renderKnob = (
* the same knob. In that case, we treat knob "A" as the lower position and
* knob "B" as the upper position.
*/
const getKnobPosition = (knob: 'A' | 'B', ratioA: number, ratioB: number, dualKnobs: boolean): 'lower' | 'upper' => {
const getKnobPosition = (
knob: RangeKnobName,
ratioA: number,
ratioB: number,
dualKnobs: boolean
): RangeKnobPosition => {
if (!dualKnobs) {
return 'lower';
}
Expand Down
Loading
Loading