Skip to content

Commit

Permalink
fix: require parent prop when hackingContainer unavailable (#177)
Browse files Browse the repository at this point in the history
  • Loading branch information
JounQin authored Jan 24, 2024
1 parent 0f83439 commit fc8c090
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/thick-news-beg.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"vue-resizor": patch
---

fix: require `parent` prop when `hackingContainer` unavailable
6 changes: 4 additions & 2 deletions .vitepress/components/VueResizorDemo.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<template>
<div class="parent">
<vue-resizor v-model:indicators="indicators">
<div ref="parent" class="parent">
<vue-resizor :parent="parent" v-model:indicators="indicators">
<div class="child">Child 1</div>
<div class="child">Child 2</div>
</vue-resizor>
Expand All @@ -12,6 +12,8 @@ import VueResizor, { Indicator } from 'vue-resizor'
import 'vue-resizor/styles.css'
const parent = ref<HTMLElement>()
const indicators = ref<Indicator[]>()
</script>
<style lang="less" scoped>
Expand Down
27 changes: 22 additions & 5 deletions packages/vue-resizor/src/Resizor.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import type { ComponentPublicInstance } from 'vue'
import type { ComponentPublicInstance, PropType } from 'vue'
import {
defineComponent,
getCurrentInstance,
nextTick,
onMounted,
onUnmounted,
ref,
Expand Down Expand Up @@ -29,6 +30,7 @@ const horizontalClassName = bem.modifier('horizontal').toString()

export default defineComponent({
props: {
parent: Object as PropType<HTMLElement>,
indicators: Array<Indicator>,
size: {
type: Number,
Expand All @@ -52,16 +54,31 @@ export default defineComponent({
let elements: HTMLElement[]

// eslint-disable-next-line sonarjs/cognitive-complexity
onMounted(() => {
onMounted(async () => {
if (children.length <= 1) {
return
}

container =
const hackingContainer =
children[0].el?.parentElement ??
// prettier-ignore
// @ts-expect-error
((getCurrentInstance()!.parent!.ctx as ComponentPublicInstance)
.$el as HTMLElement) // type-coverage:ignore-line -- we can't control
// type-coverage:ignore-next-line -- we can't control
((getCurrentInstance()?.parent.ctx as ComponentPublicInstance | undefined)?.$el as
| HTMLElement
| undefined)

if (hackingContainer) {
container = hackingContainer
} else {
await nextTick()
if (!props.parent) {
throw new Error(
'No `parent` provided nor hacking container can be auto injected, please provide `parent` correctly',
)
}
container = props.parent
}

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

1 comment on commit fc8c090

@vercel
Copy link

@vercel vercel bot commented on fc8c090 Jan 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

vue – ./

rx-vue.vercel.app
vue-jounqin.vercel.app
vue-git-master-jounqin.vercel.app
vue-rx.vercel.app

Please sign in to comment.