Skip to content

Commit 6667fac

Browse files
authored
Ensure DialogPanel exposes its ref (#1404)
* ensure we expose the `el` and `$el` on `DialogPanel` * update changelog
1 parent f6f6343 commit 6667fac

File tree

3 files changed

+50
-2
lines changed

3 files changed

+50
-2
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased - @headlessui/vue]
99

10-
- Nothing yet!
10+
### Fixed
11+
12+
- Ensure `DialogPanel` exposes its ref ([#1404](https://github.com/tailwindlabs/headlessui/pull/1404))
1113

1214
## [Unreleased - @headlessui/react]
1315

packages/@headlessui-vue/src/components/dialog/dialog.test.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
h,
66
ComponentOptionsWithoutProps,
77
ConcreteComponent,
8+
onMounted,
89
} from 'vue'
910
import { render } from '../../test-utils/vue-testing-library'
1011

@@ -120,6 +121,49 @@ describe('Safe guards', () => {
120121
)
121122
})
122123

124+
describe('refs', () => {
125+
it('should be possible to access the ref on the DialogBackdrop', async () => {
126+
renderTemplate({
127+
template: `
128+
<Dialog :open="true">
129+
<DialogBackdrop ref="backdrop" />
130+
<DialogPanel>
131+
<button>element</button>
132+
</DialogPanel>
133+
</Dialog>
134+
`,
135+
setup() {
136+
let backdrop = ref<{ el: Element; $el: Element } | null>(null)
137+
onMounted(() => {
138+
expect(backdrop.value?.el).toBeInstanceOf(HTMLDivElement)
139+
expect(backdrop.value?.$el).toBeInstanceOf(HTMLDivElement)
140+
})
141+
return { backdrop }
142+
},
143+
})
144+
})
145+
146+
it('should be possible to access the ref on the DialogPanel', async () => {
147+
renderTemplate({
148+
template: `
149+
<Dialog :open="true">
150+
<DialogPanel ref="panel">
151+
<button>element</button>
152+
</DialogPanel>
153+
</Dialog>
154+
`,
155+
setup() {
156+
let panel = ref<{ el: Element; $el: Element } | null>(null)
157+
onMounted(() => {
158+
expect(panel.value?.el).toBeInstanceOf(HTMLDivElement)
159+
expect(panel.value?.$el).toBeInstanceOf(HTMLDivElement)
160+
})
161+
return { panel }
162+
},
163+
})
164+
})
165+
})
166+
123167
describe('Rendering', () => {
124168
describe('Dialog', () => {
125169
it(

packages/@headlessui-vue/src/components/dialog/dialog.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,10 +401,12 @@ export let DialogPanel = defineComponent({
401401
props: {
402402
as: { type: [Object, String], default: 'div' },
403403
},
404-
setup(props, { attrs, slots }) {
404+
setup(props, { attrs, slots, expose }) {
405405
let api = useDialogContext('DialogPanel')
406406
let id = `headlessui-dialog-panel-${useId()}`
407407

408+
expose({ el: api.panelRef, $el: api.panelRef })
409+
408410
return () => {
409411
let ourProps = {
410412
id,

0 commit comments

Comments
 (0)