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

test: add offsetParent shadow dom polyfill snapshots #2166

Merged
merged 2 commits into from
Feb 5, 2023
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
2 changes: 1 addition & 1 deletion packages/dom/src/platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {getRectRelativeToOffsetParent} from './utils/getRectRelativeToOffsetPare
import {getScale} from './utils/getScale';
import {isElement} from './utils/is';

export const platform: Platform = {
export const platform: Required<Platform> = {
getClippingRect,
convertOffsetParentRelativeRectToViewportRelativeRect,
isElement,
Expand Down
19 changes: 19 additions & 0 deletions packages/dom/test/functional/shadow-dom.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,22 @@ useCases.forEach((useCase) => {
});
});
});

test('relative position host polyfill', async ({page}) => {
await page.goto('http://localhost:1234/shadow-DOM');

await click(
page,
'[data-testid="use-case-relative-host-with-shadowed-floating-child"]'
);

expect(await page.locator('.container').screenshot()).toMatchSnapshot(
`without-polyfill.png`
);

await click(page, '[data-testid="polyfill-true"]');

expect(await page.locator('.container').screenshot()).toMatchSnapshot(
`with-polyfill.png`
);
});
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
19 changes: 19 additions & 0 deletions packages/dom/test/visual/spec/ShadowDOM.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,13 @@ export function ShadowDOM() {
const [cssPosition, setCssPosition] = useState<CSSPosition>('static');
const [strategy, setStrategy] = useState<Strategy>('absolute');
const [withTransform, setWithTransform] = useState<boolean>(false);
const [polyfill, setPolyfill] = useState('false');

const UseCaseTag = useCase;
const hostOptions = {
placement,
strategy,
polyfill,
style: {
position: cssPosition,
transform: withTransform ? 'translate(0)' : 'none',
Expand Down Expand Up @@ -138,6 +141,22 @@ export function ShadowDOM() {
</button>
))}
</Controls>

<h3>Polyfill</h3>
<Controls>
{['true', 'false'].map((strBool) => (
<button
key={strBool}
data-testid={`polyfill-${strBool}`}
onClick={() => setPolyfill(strBool)}
style={{
backgroundColor: strBool === polyfill ? 'black' : '',
}}
>
{strBool}
</button>
))}
</Controls>
</>
);
}
81 changes: 75 additions & 6 deletions packages/dom/test/visual/utils/shadowDOM.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
autoUpdate,
computePosition,
Placement,
platform,
Strategy,
} from '@floating-ui/dom';
import {HTMLAttributes} from 'react';
Expand All @@ -11,6 +12,7 @@ interface FloatingUICustomElement {
floating: HTMLElement;
placement: Placement;
strategy: Strategy;
polyfill: string;
cleanup: () => void;
}

Expand Down Expand Up @@ -42,13 +44,14 @@ export function defineElements(): void {
implements FloatingUICustomElement
{
static get observedAttributes() {
return ['placement', 'strategy', 'style'];
return ['placement', 'strategy', 'style', 'polyfill'];
}

reference: HTMLElement;
floating: HTMLElement;
placement: Placement = defaultOptions.placement;
strategy: Strategy = defaultOptions.strategy;
polyfill = 'false';
cleanup!: () => void;

constructor() {
Expand All @@ -62,14 +65,16 @@ export function defineElements(): void {
}

attributeChangedCallback<
N extends Extract<keyof this, 'placement' | 'strategy'>,
N extends Extract<keyof this, 'placement' | 'strategy' | 'polyfill'>,
V extends Placement | Strategy
>(name: N, _oldValue: V, value: V): void {
if (name === 'placement') {
this.placement = value as Placement;
} else if (name === 'strategy') {
this.strategy = value as Strategy;
this.floating.style.position = value;
} else if (name === 'polyfill') {
this.polyfill = value as string;
}

position(this);
Expand All @@ -89,14 +94,15 @@ export function defineElements(): void {
deepHostChildTag,
class DeepHostChild extends HTMLElement implements FloatingUICustomElement {
static get observedAttributes() {
return ['placement', 'strategy', 'style'];
return ['placement', 'strategy', 'style', 'polyfill'];
}

container: HTMLElement;
reference: HTMLElement;
floating: HTMLElement;
placement: Placement = defaultOptions.placement;
strategy: Strategy = defaultOptions.strategy;
polyfill = 'false';
cleanup!: () => void;

constructor() {
Expand All @@ -112,14 +118,16 @@ export function defineElements(): void {
}

attributeChangedCallback<
N extends Extract<keyof this, 'placement' | 'strategy'>,
N extends Extract<keyof this, 'placement' | 'strategy' | 'polyfill'>,
V extends Placement | Strategy
>(name: N, _oldValue: V, value: V): void {
if (name === 'placement') {
this.placement = value as Placement;
} else if (name === 'strategy') {
this.strategy = value as Strategy;
this.floating.style.position = value;
} else if (name === 'polyfill') {
this.polyfill = value as string;
}

position(this);
Expand Down Expand Up @@ -159,13 +167,14 @@ export function defineElements(): void {
implements FloatingUICustomElement
{
static get observedAttributes() {
return ['placement', 'strategy', 'style'];
return ['placement', 'strategy', 'style', 'polyfill'];
}

reference!: HTMLElement;
floating: HTMLElement;
placement: Placement = defaultOptions.placement;
strategy: Strategy = defaultOptions.strategy;
polyfill = 'false';
cleanup!: () => void;

constructor() {
Expand All @@ -178,14 +187,16 @@ export function defineElements(): void {
}

attributeChangedCallback<
N extends Extract<keyof this, 'placement' | 'strategy'>,
N extends Extract<keyof this, 'placement' | 'strategy' | 'polyfill'>,
V extends Placement | Strategy
>(name: N, _oldValue: V, value: V): void {
if (name === 'placement') {
this.placement = value as Placement;
} else if (name === 'strategy') {
this.strategy = value as Strategy;
this.floating.style.position = value;
} else if (name === 'polyfill') {
this.polyfill = value as string;
}

position(this);
Expand Down Expand Up @@ -245,6 +256,7 @@ async function position({
placement,
reference,
strategy,
polyfill,
}: FloatingUICustomElement): Promise<void> {
if (!floating || !reference) {
return;
Expand All @@ -253,6 +265,13 @@ async function position({
return computePosition(reference, floating, {
placement,
strategy,
platform: {
...platform,
getOffsetParent:
polyfill === 'true'
? (element) => platform.getOffsetParent(element, composedOffsetParent)
: platform.getOffsetParent,
},
}).then(({x, y}) => {
Object.assign(floating.style, {
position: strategy,
Expand All @@ -274,3 +293,53 @@ function setUpAutoUpdate(element: FloatingUICustomElement): () => void {
animationFrame: true,
});
}

function getWindow(node: Node) {
return node.ownerDocument?.defaultView || window;
}

function isShadowRoot(node: Node): node is ShadowRoot {
return node instanceof getWindow(node).ShadowRoot;
}

/**
* Polyfills the old offsetParent behavior from before the spec was changed:
* https://github.com/w3c/csswg-drafts/issues/159
*/
export function composedOffsetParent(element: HTMLElement) {
let {offsetParent} = element;
let ancestor: any = element;
let foundInsideSlot = false;

while (ancestor && ancestor !== offsetParent) {
const {assignedSlot} = ancestor;

if (assignedSlot) {
let newOffsetParent = assignedSlot.offsetParent;

if (getComputedStyle(assignedSlot).display === 'contents') {
const hadStyleAttribute = assignedSlot.hasAttribute('style');
const oldDisplay = assignedSlot.style.display;
assignedSlot.style.display = getComputedStyle(ancestor).display;

newOffsetParent = assignedSlot.offsetParent;

assignedSlot.style.display = oldDisplay;
if (!hadStyleAttribute) {
assignedSlot.removeAttribute('style');
}
}

ancestor = assignedSlot;
if (offsetParent !== newOffsetParent) {
offsetParent = newOffsetParent;
foundInsideSlot = true;
}
} else if (isShadowRoot(ancestor) && ancestor.host && foundInsideSlot) {
break;
}
ancestor = (isShadowRoot(ancestor) && ancestor.host) || ancestor.parentNode;
}

return offsetParent;
}