File size: 2,558 Bytes
4d70170 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
<script lang="ts">
import {
computed,
defineComponent,
nextTick,
onMounted,
provide,
ref,
useSlots,
watch,
} from 'vue'
export default defineComponent({
props: {
indicator: {
type: Boolean,
default: false,
},
modelValue: {
type: [String, Object],
default: '',
},
},
emits: ['update:modelValue'],
setup(props, { emit }) {
const indicatorStyle = ref<null | {
top: number
left: number
width: number
height: number
}>(null)
const model = computed({
get: () => props.modelValue,
set: value => emit('update:modelValue', value),
})
provide('VueGroup', {
data: model,
setValue: value => (model.value = value),
})
const root = ref<null | HTMLElement>(null)
const updateIndicator = async () => {
await nextTick()
const el = root.value?.querySelector('.selected') as HTMLElement
if (!el) {
indicatorStyle.value = null
return
}
const offset = {
top: el.offsetTop,
left: el.offsetLeft,
width: el.offsetWidth,
height: el.offsetHeight,
}
let parent = el.offsetParent as HTMLElement
while (parent && parent !== root.value) {
offset.top += parent.offsetTop
offset.left += parent.offsetLeft
parent = parent.offsetParent as HTMLElement
}
indicatorStyle.value = offset
}
watch(
() => model.value,
(value, oldValue) => {
if (value !== oldValue) {
updateIndicator()
}
},
)
const slot = useSlots()
watch(
() => slot.length,
() => {
updateIndicator()
},
)
onMounted(() => {
updateIndicator()
})
return {
root,
indicatorStyle,
updateIndicator,
}
},
})
</script>
<template>
<div
ref="root"
class="vue-ui-group"
:class="{
'has-indicator': indicator,
}"
>
<div class="content-wrapper">
<div class="content">
<slot />
</div>
<resize-observer
v-if="indicator"
@notify="updateIndicator()"
/>
</div>
<div
v-if="indicator && indicatorStyle"
class="indicator"
:style="{
top: `${indicatorStyle.top}px`,
left: `${indicatorStyle.left}px`,
width: `${indicatorStyle.width}px`,
height: `${indicatorStyle.height}px`,
}"
>
<div class="content">
<slot name="indicator" />
</div>
</div>
</div>
</template>
|