Unverified Commit 2950dae1 authored by Martin Sione's avatar Martin Sione Committed by GitHub

[DevOverlay] fix: shadow host element affects parent's display (#70676)

### Why?

Although the style applied to the shadow host is isolated, the element
that attached the shadow host (i.e. `nextjs-portal`) is still affected
by the parent's style (e.g. `body`). This may occur style conflicts like
`display: flex`, with other children elements therefore give the shadow
host an absolute position.

Fixes #70675
Closes NDX-640

---------
Co-authored-by: 's avatarsamcx <sam@vercel.com>
Co-authored-by: 's avatardevjiwonchoi <devjiwonchoi@gmail.com>
parent 29abd6ad
......@@ -8,6 +8,15 @@ export function Base() {
@import url('https://fonts.googleapis.com/css2?family=Geist:wght@400;900&family=Geist+Mono:wght@400;900&display=swap');
:host {
${
// Although the style applied to the shadow host is isolated,
// the element that attached the shadow host (i.e. `nextjs-portal`)
// is still affected by the parent's style (e.g. `body`). This may
// occur style conflicts like `display: flex`, with other children
// elements therefore give the shadow host an absolute position.
'position: absolute;'
}
--size-gap-half: 4px;
--size-gap: 8px;
--size-gap-double: 16px;
......
......@@ -6,6 +6,15 @@ export function Base() {
<style>
{css`
:host {
${
// Although the style applied to the shadow host is isolated,
// the element that attached the shadow host (i.e. `nextjs-portal`)
// is still affected by the parent's style (e.g. `body`). This may
// occur style conflicts like `display: flex`, with other children
// elements therefore give the shadow host an absolute position.
'position: absolute;'
}
--size-gap-half: 4px;
--size-gap: 8px;
--size-gap-double: 16px;
......
import { ReactNode } from 'react'
export default function Root({ children }: { children: ReactNode }) {
return (
<html>
<body
style={{
margin: 0,
padding: 0,
width: 300,
display: 'flex',
justifyContent: 'space-between',
}}
>
{children}
</body>
</html>
)
}
export default function Page() {
return (
<>
<div id="div1" style={{ background: 'pink', width: 100 }}>
div 1
</div>
<div id="div2" style={{ background: 'cyan', width: 100 }}>
div 2
</div>
</>
)
}
/**
* @type {import('next').NextConfig}
*/
const nextConfig = {}
module.exports = nextConfig
import { nextTestSetup } from 'e2e-utils'
describe('dev-overlay - portal-not-affect-parent', () => {
const { next } = nextTestSetup({
files: __dirname,
})
it('should not affect parent display', async () => {
const browser = await next.browser('/')
const rect = await browser.eval(
// https://developer.mozilla.org/en-US/docs/Web/API/Element/getBoundingClientRect
`document.querySelector('#div2').getBoundingClientRect()`
)
// Since the <body> has `space-between`, if the shadow root takes up
// the space, the x of #div2 will be 100 as is the width of #div1.
// If the shadow root does not take up the space, as the total width of
// the <body> is 300 and as is a space-between, the #div2 will be placed
// at the end of the <body> and the x will be 200.
// Before: <#div1 100> <#div2 100> <nextjs-portal 100>
// After: <#div1 100> [space-between 100] <#div2 100> <nextjs-portal 0>
expect(rect.x).toBe(200)
})
})
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment