-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(time-layer): add new component for date slider, range mode
- Loading branch information
1 parent
840938f
commit a9a49bd
Showing
10 changed files
with
355 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
<script setup lang="ts"> | ||
import { onUnmounted, Ref, computed, ref } from 'vue' | ||
const props = defineProps<{ | ||
ariaLabel?: string | ||
minValue: number | ||
maxValue: number | ||
totalSteps: number | ||
selectedValue: number | ||
}>() | ||
const emit = defineEmits<{ | ||
(e: 'change', value: number): void | ||
}>() | ||
const sliderBarElement: Ref<HTMLElement | null> = ref(null) | ||
const thumbElement: Ref<HTMLElement | null> = ref(null) | ||
const thumbElementWidth = computed(() => thumbElement.value?.offsetWidth || 40) | ||
const currentLeftOffset = computed(() => { | ||
const offset = sliderBarElement.value?.offsetWidth | ||
? (sliderBarElement.value?.offsetWidth * props.selectedValue) / | ||
(props.totalSteps - 1) | ||
: 0 | ||
return Math.round(offset - thumbElementWidth.value / 2) | ||
}) | ||
const styleObject = computed(() => ({ left: `${currentLeftOffset.value}px` })) | ||
let isDragging = false | ||
onUnmounted(() => { | ||
document.removeEventListener('mousemove', onMouseMove) | ||
document.removeEventListener('mouseup', onMouseUp) | ||
}) | ||
function onMoveThumb(value: number) { | ||
emit('change', Math.min(Math.max(value, props.minValue), props.maxValue)) | ||
} | ||
function onKeyDownLeft() { | ||
onMoveThumb(props.selectedValue - 1) | ||
} | ||
function onKeyDownRight() { | ||
onMoveThumb(props.selectedValue + 1) | ||
} | ||
function onMouseDown() { | ||
isDragging = true | ||
document.addEventListener('mousemove', onMouseMove) | ||
document.addEventListener('mouseup', onMouseUp) | ||
} | ||
function onMouseMove(payload: MouseEvent) { | ||
if (!isDragging) { | ||
return | ||
} | ||
const value = sliderBarElement.value?.offsetWidth | ||
? ((payload.clientX - thumbElementWidth.value / 2) * | ||
(props.totalSteps - 1)) / | ||
sliderBarElement.value?.offsetWidth | ||
: 0 | ||
onMoveThumb(Math.floor(value)) | ||
} | ||
function onMouseUp() { | ||
isDragging = false | ||
document.removeEventListener('mousemove', onMouseMove) | ||
document.removeEventListener('mouseup', onMouseUp) | ||
} | ||
</script> | ||
|
||
<template> | ||
<div class="w-full" role="slider" ref="sliderBarElement"> | ||
<button | ||
class="lux-slidebar-thumb" | ||
ref="thumbElement" | ||
:style="styleObject" | ||
@keydown.space="onKeyDownRight" | ||
@keydown.right="onKeyDownRight" | ||
@keydown.left="onKeyDownLeft" | ||
@keydown.delete="onKeyDownLeft" | ||
@mousedown="onMouseDown" | ||
@mousemove="onMouseMove" | ||
@mouseup="onMouseUp" | ||
:aria-label="ariaLabel" | ||
:title="ariaLabel" | ||
></button> | ||
</div> | ||
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<script setup lang="ts"> | ||
import { Ref, computed, ref } from 'vue' | ||
const props = defineProps<{ | ||
selectedMinValue: number | ||
selectedMaxValue: number | ||
totalSteps: number | ||
}>() | ||
const sliderTrackElement: Ref<HTMLElement | null> = ref(null) | ||
const sliderTrackSelectionElement: Ref<HTMLElement | null> = ref(null) | ||
const currentLeftOffset = computed(() => { | ||
const offsetLeft = sliderTrackElement.value?.offsetWidth | ||
? (sliderTrackElement.value?.offsetWidth * props.selectedMinValue) / | ||
(props.totalSteps - 1) | ||
: 0 | ||
return Math.round(offsetLeft) | ||
}) | ||
const currentWidthOffset = computed(() => { | ||
let offsetWidth = 0 | ||
if (sliderTrackElement.value?.offsetWidth) { | ||
const offsetRight = | ||
(sliderTrackElement.value?.offsetWidth * props.selectedMaxValue) / | ||
(props.totalSteps - 1) | ||
offsetWidth = offsetRight - currentLeftOffset.value | ||
} | ||
return Math.round(offsetWidth) | ||
}) | ||
const styleObject = computed(() => ({ | ||
left: `${currentLeftOffset.value}px`, | ||
width: `${currentWidthOffset.value}px`, | ||
})) | ||
</script> | ||
|
||
<template> | ||
<div ref="sliderTrackElement" class="lux-slidebar-track"> | ||
<div | ||
ref="sliderTrackSelectionElement" | ||
class="lux-slidebar-track-selection" | ||
:style="styleObject" | ||
></div> | ||
<div class="lux-slidebar-track-full"></div> | ||
</div> | ||
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<script setup lang="ts"> | ||
import SliderRangeThumb from './slider-range-thumb.vue' | ||
import SliderRangeTrack from './slider-range-track.vue' | ||
const props = defineProps<{ | ||
minValue: number | ||
maxValue: number | ||
selectedMinValue: number | ||
selectedMaxValue: number | ||
totalSteps: number | ||
ariaLabelMin?: string | ||
ariaLabelMax?: string | ||
}>() | ||
const emit = defineEmits<{ | ||
(e: 'change', min: number, max: number): void | ||
}>() | ||
function onChangeMin(value: number) { | ||
emit('change', value, props.selectedMaxValue) | ||
} | ||
function onChangeMax(value: number) { | ||
emit('change', props.selectedMinValue, value) | ||
} | ||
</script> | ||
|
||
<template> | ||
<div class="lux-slidebar-fake"> | ||
<slider-range-thumb | ||
:ariaLabel="ariaLabelMin" | ||
:minValue="minValue" | ||
:maxValue="selectedMaxValue" | ||
:selectedValue="selectedMinValue" | ||
:totalSteps="totalSteps" | ||
@change="onChangeMin" | ||
/> | ||
<slider-range-thumb | ||
:totalSteps="totalSteps" | ||
:ariaLabel="ariaLabelMax" | ||
:minValue="selectedMinValue" | ||
:maxValue="maxValue" | ||
:selectedValue="selectedMaxValue" | ||
@change="onChangeMax" | ||
/> | ||
<slider-range-track | ||
:selectedMinValue="selectedMinValue" | ||
:selectedMaxValue="selectedMaxValue" | ||
:totalSteps="totalSteps" | ||
/> | ||
</div> | ||
</template> |
Oops, something went wrong.