Skip to content

Commit 4a1c50d

Browse files
committed
add a subtle hover effect / border to the handle
only if "hoverable" is set true
1 parent 1bcaf07 commit 4a1c50d

File tree

2 files changed

+48
-8
lines changed

2 files changed

+48
-8
lines changed

src/RangeSlider.svelte

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
// state management
3939
let focus = false;
4040
let handleActivated = false;
41+
let handlePressed = false;
4142
let keyboardActive = false;
4243
let activeHandle = values.length - 1;
4344
@@ -273,20 +274,20 @@
273274
**/
274275
function moveHandle(index, value) {
275276
if (range) {
276-
// restrict the handles of a range-slider from
277+
// restrict the handles of a range-slider from
277278
// going past one-another unless "pushy" is true
278279
if (index === 0 && value > values[1]) {
279280
if (pushy) {
280281
values[1] = value;
281282
} else {
282-
value = values[1];
283+
value = values[1];
283284
}
284285
} else if (index === 1 && value < values[0]) {
285286
if (pushy) {
286287
values[0] = value;
287288
} else {
288-
value = values[0];
289-
}
289+
value = values[0];
290+
}
290291
}
291292
}
292293
// set the value for the handle, and align/clamp it
@@ -330,6 +331,7 @@
330331
if (keyboardActive) {
331332
focus = false;
332333
handleActivated = false;
334+
handlePressed = false;
333335
}
334336
}
335337
@@ -393,6 +395,7 @@
393395
// set the closest handle as active
394396
focus = true;
395397
handleActivated = true;
398+
handlePressed = true;
396399
activeHandle = getClosestHandle(p);
397400
// for touch devices we want the handle to instantly
398401
// move to the position touched for more responsive feeling
@@ -401,6 +404,15 @@
401404
}
402405
}
403406
407+
/**
408+
* function to run when the user stops touching
409+
* down on the slider element anywhere
410+
* @param {event} e the event from browser
411+
**/
412+
function sliderInteractEnd(e) {
413+
handlePressed = false;
414+
}
415+
404416
/**
405417
* unfocus the slider if the user clicked off of
406418
* it, somewhere else on the screen
@@ -442,6 +454,7 @@
442454
}
443455
}
444456
handleActivated = false;
457+
handlePressed = false;
445458
}
446459
447460
/**
@@ -451,6 +464,7 @@
451464
**/
452465
function bodyTouchEnd(e) {
453466
handleActivated = false;
467+
handlePressed = false;
454468
}
455469
456470
function bodyKeyDown(e) {
@@ -466,6 +480,7 @@
466480
--handle-inactive: var(--range-handle-inactive, #99a2a2);
467481
--handle: var(--range-handle, #838de7);
468482
--handle-focus: var(--range-handle-focus, #4a40d4);
483+
--handle-border: var(--range-handle-border, var(--handle));
469484
--range-inactive: var(--range-range-inactive, var(--handle-inactive));
470485
--range: var(--range-range, var(--handle-focus));
471486
--float-inactive: var(--range-float-inactive, var(--handle-inactive));
@@ -511,7 +526,8 @@
511526
transform: translateY(-50%) translateX(-50%);
512527
z-index: 2;
513528
}
514-
:global(.rangeSlider .rangeNub) {
529+
:global(.rangeSlider .rangeNub),
530+
:global(.rangeSlider .rangeHandle:before) {
515531
position: absolute;
516532
left: 0;
517533
top: 0;
@@ -521,6 +537,26 @@
521537
width: 100%;
522538
transition: all 0.2s ease;
523539
}
540+
:global(.rangeSlider .rangeHandle:before) {
541+
content: "";
542+
left: 1px;
543+
top: 1px;
544+
bottom: 1px;
545+
right: 1px;
546+
height: auto;
547+
width: auto;
548+
box-shadow: 0 0 0 0px var(--handle-border);
549+
opacity: 0;
550+
}
551+
:global(.rangeSlider .rangeHandle.hoverable:hover:before) {
552+
box-shadow: 0 0 0 8px var(--handle-border);
553+
opacity: 0.2;
554+
}
555+
:global(.rangeSlider .rangeHandle.hoverable.press:before),
556+
:global(.rangeSlider .rangeHandle.hoverable.press:hover:before) {
557+
box-shadow: 0 0 0 12px var(--handle-border);
558+
opacity: 0.4;
559+
}
524560
:global(.rangeSlider.range:not(.min):not(.max) .rangeNub) {
525561
border-radius: 10em 10em 10em 1.6em;
526562
}
@@ -620,14 +656,17 @@
620656
class:pips
621657
class:pip-labels={all === 'label' || first === 'label' || last === 'label' || rest === 'label'}
622658
on:touchstart|preventDefault={sliderInteractStart}
623-
on:mousedown={sliderInteractStart}>
659+
on:mousedown={sliderInteractStart}
660+
on:touchend|preventDefault={sliderInteractEnd}
661+
on:mouseup={sliderInteractEnd}>
624662
{#each values as value, index}
625663
<span
626664
role="slider"
627665
tabindex="0"
628666
class="rangeHandle"
629667
class:hoverable={hover}
630668
class:active={focus && activeHandle === index}
669+
class:press={handlePressed && activeHandle === index}
631670
on:blur={sliderBlurHandle}
632671
on:focus={sliderFocusHandle}
633672
on:keydown={sliderKeydown}

test/src/App.svelte

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,10 @@
3333
<svelte:head>
3434
<style>
3535
#test-id {
36-
--range-handle-inactive: rgb(245, 200, 230);
36+
--range-slider: rgb(245, 200, 230);
37+
--range-handle: var(--range-slider);
38+
--range-handle-inactive: var(--range-slider);
3739
--range-handle-focus: rgb(245, 0, 46);
38-
--range-slider: var(--range-handle-inactive);
3940
}
4041
#clr-test {
4142
--range-slider: rgb(195, 228, 222);

0 commit comments

Comments
 (0)