Skip to content

Commit fc8c090

Browse files
authored
fix: require parent prop when hackingContainer unavailable (#177)
1 parent 0f83439 commit fc8c090

File tree

3 files changed

+31
-7
lines changed

3 files changed

+31
-7
lines changed

.changeset/thick-news-beg.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"vue-resizor": patch
3+
---
4+
5+
fix: require `parent` prop when `hackingContainer` unavailable

.vitepress/components/VueResizorDemo.vue

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<template>
2-
<div class="parent">
3-
<vue-resizor v-model:indicators="indicators">
2+
<div ref="parent" class="parent">
3+
<vue-resizor :parent="parent" v-model:indicators="indicators">
44
<div class="child">Child 1</div>
55
<div class="child">Child 2</div>
66
</vue-resizor>
@@ -12,6 +12,8 @@ import VueResizor, { Indicator } from 'vue-resizor'
1212
1313
import 'vue-resizor/styles.css'
1414
15+
const parent = ref<HTMLElement>()
16+
1517
const indicators = ref<Indicator[]>()
1618
</script>
1719
<style lang="less" scoped>

packages/vue-resizor/src/Resizor.tsx

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
import type { ComponentPublicInstance } from 'vue'
1+
import type { ComponentPublicInstance, PropType } from 'vue'
22
import {
33
defineComponent,
44
getCurrentInstance,
5+
nextTick,
56
onMounted,
67
onUnmounted,
78
ref,
@@ -29,6 +30,7 @@ const horizontalClassName = bem.modifier('horizontal').toString()
2930

3031
export default defineComponent({
3132
props: {
33+
parent: Object as PropType<HTMLElement>,
3234
indicators: Array<Indicator>,
3335
size: {
3436
type: Number,
@@ -52,16 +54,31 @@ export default defineComponent({
5254
let elements: HTMLElement[]
5355

5456
// eslint-disable-next-line sonarjs/cognitive-complexity
55-
onMounted(() => {
57+
onMounted(async () => {
5658
if (children.length <= 1) {
5759
return
5860
}
5961

60-
container =
62+
const hackingContainer =
6163
children[0].el?.parentElement ??
64+
// prettier-ignore
6265
// @ts-expect-error
63-
((getCurrentInstance()!.parent!.ctx as ComponentPublicInstance)
64-
.$el as HTMLElement) // type-coverage:ignore-line -- we can't control
66+
// type-coverage:ignore-next-line -- we can't control
67+
((getCurrentInstance()?.parent.ctx as ComponentPublicInstance | undefined)?.$el as
68+
| HTMLElement
69+
| undefined)
70+
71+
if (hackingContainer) {
72+
container = hackingContainer
73+
} else {
74+
await nextTick()
75+
if (!props.parent) {
76+
throw new Error(
77+
'No `parent` provided nor hacking container can be auto injected, please provide `parent` correctly',
78+
)
79+
}
80+
container = props.parent
81+
}
6582

6683
// eslint-disable-next-line unicorn/prefer-spread
6784
elements = (Array.from(container.children) as HTMLElement[]).filter(

0 commit comments

Comments
 (0)