Skip to content

Commit 6fc5e15

Browse files
committed
feat(range): apply range-pressed classes to host based on the knob
1 parent 0a920f7 commit 6fc5e15

File tree

2 files changed

+57
-5
lines changed

2 files changed

+57
-5
lines changed

core/src/components/range/range.tsx

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ import type {
5050
* @part knob-b - The visual knob element for the second knob. Only available when `dualKnobs` is `true`.
5151
* @part knob-lower - The visual knob element for the lower knob. Only available when `dualKnobs` is `true`.
5252
* @part knob-upper - The visual knob element for the upper knob. Only available when `dualKnobs` is `true`.
53-
* @part pressed - Added to the knob-handle, knob, and pin that is currently being pressed to drag. Only one set has this part at a time when `dualKnobs` is `true`.
5453
* @part focused - Added to the knob-handle, knob, and pin that currently has focus. Only one set has this part at a time when `dualKnobs` is `true`.
55-
* @part hover - Added to the knob-handle, knob, and pin when the knob has hover. Only one set has this part at a time.
54+
* @part hover - Added to the knob-handle, knob, and pin when the knob has hover. Only one set has this part at a time when `dualKnobs` is `true`.
55+
* @part pressed - Added to the knob-handle, knob, and pin that is currently being pressed to drag. Only one set has this part at a time when `dualKnobs` is `true`.
5656
*/
5757
@Component({
5858
tag: 'ion-range',
@@ -878,7 +878,7 @@ export class Range implements ComponentInterface {
878878

879879
{renderKnob(rtl, {
880880
knob: 'A',
881-
position: this.dualKnobs ? (this.ratioA <= this.ratioB ? 'lower' : 'upper') : 'lower',
881+
position: getKnobPosition('A', this.ratioA, this.ratioB, this.dualKnobs),
882882
dualKnobs: this.dualKnobs,
883883
focused: focusedKnob === 'A',
884884
hovered: hoveredKnob === 'A',
@@ -901,7 +901,7 @@ export class Range implements ComponentInterface {
901901
{this.dualKnobs &&
902902
renderKnob(rtl, {
903903
knob: 'B',
904-
position: this.ratioB <= this.ratioA ? 'lower' : 'upper',
904+
position: getKnobPosition('B', this.ratioA, this.ratioB, this.dualKnobs),
905905
dualKnobs: this.dualKnobs,
906906
focused: focusedKnob === 'B',
907907
hovered: hoveredKnob === 'B',
@@ -948,6 +948,14 @@ export class Range implements ComponentInterface {
948948

949949
const mode = getIonMode(this);
950950

951+
/**
952+
* Determine the name and position of the pressed knob to apply
953+
* Host classes for styling.
954+
*/
955+
const pressedKnobName = dualKnobs ? pressedKnob?.toLowerCase() : undefined;
956+
const pressedKnobPosition =
957+
dualKnobs && pressedKnob ? getKnobPosition(pressedKnob, this.ratioA, this.ratioB, dualKnobs) : undefined;
958+
951959
/**
952960
* Determine if any knob is at the min or max value to
953961
* apply Host classes for styling.
@@ -968,6 +976,8 @@ export class Range implements ComponentInterface {
968976
'range-disabled': disabled,
969977
'range-dual-knobs': dualKnobs,
970978
'range-pressed': pressedKnob !== undefined,
979+
[`range-pressed-${pressedKnobName}`]: pressedKnob !== undefined && pressedKnobName !== undefined,
980+
[`range-pressed-${pressedKnobPosition}`]: pressedKnob !== undefined && pressedKnobPosition !== undefined,
971981
'range-has-pin': pin,
972982
[`range-label-placement-${labelPlacement}`]: true,
973983
'range-item-start-adjustment': needsStartAdjustment,
@@ -1030,9 +1040,9 @@ const renderKnob = (
10301040
min,
10311041
max,
10321042
disabled,
1033-
pressed,
10341043
focused,
10351044
hovered,
1045+
pressed,
10361046
pin,
10371047
handleKeyboard,
10381048
pinFormatter,
@@ -1147,6 +1157,20 @@ const renderKnob = (
11471157
);
11481158
};
11491159

1160+
/**
1161+
* Returns whether the given knob is at the lower or upper position based
1162+
* on current ratios for the given knob.
1163+
*/
1164+
const getKnobPosition = (knob: 'A' | 'B', ratioA: number, ratioB: number, dualKnobs: boolean): 'lower' | 'upper' => {
1165+
if (dualKnobs) {
1166+
if (knob === 'A') {
1167+
return ratioA <= ratioB ? 'lower' : 'upper';
1168+
}
1169+
return ratioB <= ratioA ? 'lower' : 'upper';
1170+
}
1171+
return 'lower';
1172+
};
1173+
11501174
const ratioToValue = (ratio: number, min: number, max: number, step: number): number => {
11511175
let value = (max - min) * ratio;
11521176

core/src/components/range/test/custom/index.html

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,20 @@
194194
* -----------------------------------
195195
*/
196196

197+
/* Style the start slot knob value when the lower knob is pressed */
198+
#lower-upper-dual-knobs-range.range-pressed-lower [slot='start'] {
199+
background: var(--color-blue-light);
200+
border-color: var(--color-blue-light);
201+
color: #fff;
202+
}
203+
204+
/* Style the end slot knob value when the upper knob is pressed */
205+
#lower-upper-dual-knobs-range.range-pressed-upper [slot='end'] {
206+
background: var(--color-blue-dark);
207+
border-color: var(--color-blue-dark);
208+
color: #fff;
209+
}
210+
197211
/* Knob lower */
198212
#lower-upper-dual-knobs-range::part(knob-lower) {
199213
border-radius: 50% 0 0 50%;
@@ -242,6 +256,20 @@
242256
* -----------------------------------
243257
*/
244258

259+
/* Style the start slot knob value when the A knob is pressed */
260+
#a-b-dual-knobs-range.range-pressed-a [slot='start'] {
261+
background: var(--color-blue-light);
262+
border-color: var(--color-blue-light);
263+
color: #fff;
264+
}
265+
266+
/* Style the end slot knob value when the B knob is pressed */
267+
#a-b-dual-knobs-range.range-pressed-b [slot='end'] {
268+
background: var(--color-blue-dark);
269+
border-color: var(--color-blue-dark);
270+
color: #fff;
271+
}
272+
245273
/* Hovered knob A */
246274
#a-b-dual-knobs-range::part(knob-a hover) {
247275
background: rgba(var(--color-blue-light-rgb), 0.5);

0 commit comments

Comments
 (0)