Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don’t throw when SSR rendering internal portals in Vue #1459

Merged
merged 2 commits into from
May 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Simplify `Popover` Tab logic by using sentinel nodes instead of keydown event interception ([#1440](https://github.com/tailwindlabs/headlessui/pull/1440))
- Ensure the `PopoverPanel` is clickable without closing the `Popover` ([#1443](https://github.com/tailwindlabs/headlessui/pull/1443))
- Improve "Scroll lock" scrollbar width for `Dialog` component ([#1457](https://github.com/tailwindlabs/headlessui/pull/1457))
- Don’t throw when SSR rendering internal portals in Vue ([#1459](https://github.com/tailwindlabs/headlessui/pull/1459))

## [Unreleased - @headlessui/react]

Expand Down
77 changes: 76 additions & 1 deletion packages/@headlessui-vue/src/components/portal/portal.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { defineComponent, ref, nextTick, ComponentOptionsWithoutProps } from 'vue'
import { h, defineComponent, ref, nextTick, ComponentOptionsWithoutProps, createSSRApp } from 'vue'

import { render } from '../../test-utils/vue-testing-library'
import { renderToString } from 'vue/server-renderer'
import { Portal, PortalGroup } from './portal'
import { click } from '../../test-utils/interactions'
import { html } from '../../test-utils/html'
Expand Down Expand Up @@ -38,6 +39,80 @@ function renderTemplate(input: string | ComponentOptionsWithoutProps) {
)
}

async function ssrRenderTemplate(input: string | ComponentOptionsWithoutProps) {
let defaultComponents = { Portal, PortalGroup }

if (typeof input === 'string') {
let app = createSSRApp({
render: () => h(defineComponent({ template: input, components: defaultComponents })),
})

return await renderToString(app)
}

let app = createSSRApp({
render: () =>
h(
defineComponent(
Object.assign({}, input, {
components: { ...defaultComponents, ...input.components },
}) as Parameters<typeof defineComponent>[0]
)
),
})

return await renderToString(app)
}

async function withoutBrowserGlobals<T>(fn: () => Promise<T>) {
let oldWindow = globalThis.window
let oldDocument = globalThis.document

Object.defineProperty(globalThis, '_document', {
value: undefined,
configurable: true,
})

Object.defineProperty(globalThis, '_globalProxy', {
value: undefined,
configurable: true,
})

try {
return await fn()
} finally {
Object.defineProperty(globalThis, '_globalProxy', {
value: oldWindow,
configurable: true,
})

Object.defineProperty(globalThis, '_document', {
value: oldDocument,
configurable: true,
})
}
}

it('SSR-rendering a Portal should not error', async () => {
expect(getPortalRoot()).toBe(null)

let result = await withoutBrowserGlobals(() =>
ssrRenderTemplate(
html`
<main id="parent">
<Portal>
<p id="content">Contents...</p>
</Portal>
</main>
`
)
)

expect(getPortalRoot()).toBe(null)

expect(result).toBe(html`<main id="parent"><!----></main>`)
})

it('should be possible to use a Portal', () => {
expect(getPortalRoot()).toBe(null)

Expand Down
4 changes: 4 additions & 0 deletions packages/@headlessui-vue/src/components/portal/portal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ import { getOwnerDocument } from '../../utils/owner'
function getPortalRoot(contextElement?: Element | null) {
let ownerDocument = getOwnerDocument(contextElement)
if (!ownerDocument) {
if (contextElement === null) {
return null
}

throw new Error(
`[Headless UI]: Cannot find ownerDocument for contextElement: ${contextElement}`
)
Expand Down