Skip to content

Commit aa0056f

Browse files
authored
Only render the Dialog on the client (#1566)
* only render the Dialog on the client * update changelog
1 parent f2a813e commit aa0056f

File tree

3 files changed

+30
-14
lines changed

3 files changed

+30
-14
lines changed

packages/@headlessui-vue/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1717
- Support `<slot>` children when using `as="template"` ([#1548](https://github.com/tailwindlabs/headlessui/pull/1548))
1818
- Improve outside click of `Dialog` component ([#1546](https://github.com/tailwindlabs/headlessui/pull/1546))
1919
- Detect outside clicks from within `<iframe>` elements ([#1552](https://github.com/tailwindlabs/headlessui/pull/1552))
20+
- Only render the `Dialog` on the client ([#1566](https://github.com/tailwindlabs/headlessui/pull/1566))
2021

2122
## [1.6.4] - 2022-05-29
2223

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

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,4 @@
1-
import {
2-
defineComponent,
3-
ref,
4-
nextTick,
5-
h,
6-
ComponentOptionsWithoutProps,
7-
ConcreteComponent,
8-
onMounted,
9-
} from 'vue'
1+
import { defineComponent, ref, nextTick, h, ConcreteComponent, onMounted } from 'vue'
102
import { createRenderTemplate, render } from '../../test-utils/vue-testing-library'
113

124
import {
@@ -109,6 +101,7 @@ describe('Safe guards', () => {
109101

110102
describe('refs', () => {
111103
it('should be possible to access the ref on the DialogBackdrop', async () => {
104+
expect.assertions(2)
112105
renderTemplate({
113106
template: `
114107
<Dialog :open="true">
@@ -121,15 +114,18 @@ describe('refs', () => {
121114
setup() {
122115
let backdrop = ref<{ el: Element; $el: Element } | null>(null)
123116
onMounted(() => {
124-
expect(backdrop.value?.el).toBeInstanceOf(HTMLDivElement)
125-
expect(backdrop.value?.$el).toBeInstanceOf(HTMLDivElement)
117+
nextTick(() => {
118+
expect(backdrop.value?.el).toBeInstanceOf(HTMLDivElement)
119+
expect(backdrop.value?.$el).toBeInstanceOf(HTMLDivElement)
120+
})
126121
})
127122
return { backdrop }
128123
},
129124
})
130125
})
131126

132127
it('should be possible to access the ref on the DialogPanel', async () => {
128+
expect.assertions(2)
133129
renderTemplate({
134130
template: `
135131
<Dialog :open="true">
@@ -141,8 +137,10 @@ describe('refs', () => {
141137
setup() {
142138
let panel = ref<{ el: Element; $el: Element } | null>(null)
143139
onMounted(() => {
144-
expect(panel.value?.el).toBeInstanceOf(HTMLDivElement)
145-
expect(panel.value?.$el).toBeInstanceOf(HTMLDivElement)
140+
nextTick(() => {
141+
expect(panel.value?.el).toBeInstanceOf(HTMLDivElement)
142+
expect(panel.value?.$el).toBeInstanceOf(HTMLDivElement)
143+
})
146144
})
147145
return { panel }
148146
},
@@ -1153,6 +1151,8 @@ describe('Mouse interactions', () => {
11531151
},
11541152
})
11551153

1154+
await new Promise<void>(nextTick)
1155+
11561156
// Verify it is open
11571157
assertDialog({ state: DialogState.Visible })
11581158

@@ -1196,6 +1196,8 @@ describe('Mouse interactions', () => {
11961196
},
11971197
})
11981198

1199+
await new Promise<void>(nextTick)
1200+
11991201
// Verify it is open
12001202
assertDialog({ state: DialogState.Visible })
12011203

@@ -1233,6 +1235,8 @@ describe('Mouse interactions', () => {
12331235
},
12341236
})
12351237

1238+
await new Promise<void>(nextTick)
1239+
12361240
// Verify it is open
12371241
assertDialog({ state: DialogState.Visible })
12381242

@@ -1277,6 +1281,8 @@ describe('Mouse interactions', () => {
12771281
},
12781282
})
12791283

1284+
await new Promise<void>(nextTick)
1285+
12801286
// Verify it is open
12811287
assertDialog({ state: DialogState.Visible })
12821288

@@ -1327,6 +1333,8 @@ describe('Mouse interactions', () => {
13271333
},
13281334
})
13291335

1336+
await new Promise<void>(nextTick)
1337+
13301338
// Verify it is open
13311339
assertDialog({ state: DialogState.Visible })
13321340

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@ export let Dialog = defineComponent({
7878
},
7979
emits: { close: (_close: boolean) => true },
8080
setup(props, { emit, attrs, slots, expose }) {
81+
let ready = ref(false)
82+
onMounted(() => {
83+
ready.value = true
84+
})
85+
8186
let nestedDialogCount = ref(0)
8287

8388
let usesOpenClosedState = useOpenClosed()
@@ -117,7 +122,9 @@ export let Dialog = defineComponent({
117122
)
118123
}
119124

120-
let dialogState = computed(() => (open.value ? DialogStates.Open : DialogStates.Closed))
125+
let dialogState = computed(() =>
126+
!ready.value ? DialogStates.Closed : open.value ? DialogStates.Open : DialogStates.Closed
127+
)
121128
let enabled = computed(() => dialogState.value === DialogStates.Open)
122129

123130
let hasNestedDialogs = computed(() => nestedDialogCount.value > 1) // 1 is the current dialog

0 commit comments

Comments
 (0)