Get width of slot from useSlot() #6726
Answered
by
Sight-wcg
d0peCode
asked this question in
Help/Questions
-
I'm getting the width of the HTML element which is passed as slot like this: <template>
<div>
<button @mouseover="state.show = true" @mouseleave="state.show = false">
hover me
</button>
<div v-if="state.show">
<slot name="test"></slot>
</div>
</div>
</template>
<script setup>
import { reactive, watch, useSlots, nextTick } from 'vue'
const slots = useSlots();
const state = reactive({
show: false
})
watch(() => state.show, () => {
if (state.show) {
nextTick(() => {
console.log(slots.test()[0].el.offsetWidth)
})
}
})
</script> This print correct value only on first hover of button. When hovered more than one time, it logs I also tried with <script setup>
import { reactive, watch, useSlots, nextTick, onUpdated } from 'vue'
const slots = useSlots();
const state = reactive({
show: false
})
onUpdated(() => {
if (state.show && slots.test() && slots.test()[0] && slots.test()[0].el) {
console.log(slots.test()[0].el.getBoundingClientRect())
}
})
</script> With the same result - it shows correct width only on first hover. Playground with this example |
Beta Was this translation helpful? Give feedback.
Answered by
Sight-wcg
Sep 23, 2022
Replies: 1 comment 3 replies
-
It looks like this other issue is related: #2196 Try it, you'd have to use a render function.
|
Beta Was this translation helpful? Give feedback.
3 replies
Answer selected by
d0peCode
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It looks like this other issue is related: #2196
Try it, you'd have to use a render function.
Playground example