Skip to content

Commit

Permalink
feat: remove container ref
Browse files Browse the repository at this point in the history
  • Loading branch information
wiidede committed Oct 20, 2023
1 parent 796ca00 commit 91a89a5
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 13 deletions.
1 change: 0 additions & 1 deletion src/Range.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import type { InjectionKey, Ref } from 'vue'

export const RangeTrackRefKey: InjectionKey<Ref<HTMLElement | undefined>> = Symbol('RangeTrackRefKey')
export const RangeContainerRefKey: InjectionKey<Ref<HTMLElement | undefined>> = Symbol('RangeContainerRefKey')
5 changes: 1 addition & 4 deletions src/Range.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<script lang="ts" setup generic="T = any, U = RangeValueType<T>">
import { computed, nextTick, provide, ref, watch } from 'vue'
import { RangeContainerRefKey, RangeTrackRefKey } from './Range'
import { RangeTrackRefKey } from './Range'
import RangeThumb from './RangeThumb.vue'
import type { RangeData, RangeMarks, RangeRenderFn, RangeValue, RangeValueType } from './type'
import { percentage2value, swap, value2percentage } from './utils'
Expand Down Expand Up @@ -126,7 +126,6 @@ watch(model, (val) => {
})
const trackRef = ref<HTMLElement>()
const containerRef = ref<HTMLElement>()
function getValue(percentage: number) {
return percentage2value(percentage, props.min, props.max, props.step)
Expand Down Expand Up @@ -210,12 +209,10 @@ function addThumb(e: MouseEvent) {
}
provide(RangeTrackRefKey, trackRef)
provide(RangeContainerRefKey, containerRef)
</script>

<template>
<div
ref="containerRef"
class="dark:m-range-theme-dark m-range-theme m-range"
:class="[`m-range-${vertical ? 'v-' : ''}${size}`, `m-range-${vertical ? 'v-' : ''}thumb-${thumbSize}`]"
>
Expand Down
14 changes: 6 additions & 8 deletions src/RangeThumb.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<script lang="ts" setup generic="T = any, U = RangeValueType<T>">
import { computed, inject, nextTick, ref } from 'vue'
import { RangeContainerRefKey, RangeTrackRefKey } from './Range'
import { RangeTrackRefKey } from './Range'
import Render from './Render.vue'
import type { RangeData, RangeRenderFn, RangeValueType } from './type'
Expand Down Expand Up @@ -36,7 +36,6 @@ defineSlots<{
const thumbRef = ref<HTMLElement>()
const trackRef = inject(RangeTrackRefKey)
const containerRef = inject(RangeContainerRefKey)
const removable = computed(() => props.addable && !props.unremovable)
const cursor = computed(() => props.disabled ? 'cursor-not-allowed' : removable.value ? 'cursor-move' : props.vertical ? 'cursor-ns-resize' : 'cursor-ew-resize')
Expand All @@ -53,18 +52,17 @@ function setTrackCursor(cursor: string | undefined) {
}
function shouldDelete(offset: number) {
if (!containerRef?.value || !trackRef?.value)
if (!trackRef?.value)
return false
const containerRect = containerRef.value.getBoundingClientRect()
const trackRect = trackRef.value.getBoundingClientRect()
if (props.vertical) {
const left = Math.min(containerRect.left, trackRect.left - 32)
const right = Math.max(containerRect.right, trackRect.right + 32)
const left = trackRect.left - 32
const right = trackRect.right + 32
return offset < left || offset > right
}
else {
const top = Math.min(containerRect.top, trackRect.top - 32)
const bottom = Math.max(containerRect.bottom, trackRect.bottom + 32)
const top = trackRect.top - 32
const bottom = trackRect.bottom + 32
return offset < top || offset > bottom
}
}
Expand Down

0 comments on commit 91a89a5

Please sign in to comment.