Skip to content

Commit

Permalink
fix: CSS :top-layer elements inside containing blocks (#2747)
Browse files Browse the repository at this point in the history
  • Loading branch information
atomiks committed Jan 25, 2024
1 parent 1925eff commit ef24646
Show file tree
Hide file tree
Showing 29 changed files with 641 additions and 28 deletions.
8 changes: 8 additions & 0 deletions .changeset/sharp-donkeys-enjoy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
'@floating-ui/core': minor
'@floating-ui/dom': minor
---

fix: handle CSS `:top-layer` elements inside containing blocks. It's no longer
necessary to implement the middleware workaround outlined in
https://github.com/floating-ui/floating-ui/issues/1842#issuecomment-1872653245.
1 change: 1 addition & 0 deletions packages/core/src/detectOverflow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ export async function detectOverflow(
const elementClientRect = rectToClientRect(
platform.convertOffsetParentRelativeRectToViewportRelativeRect
? await platform.convertOffsetParentRelativeRectToViewportRelativeRect({
elements,
rect,
offsetParent,
strategy,
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export interface Platform {

// Optional
convertOffsetParentRelativeRectToViewportRelativeRect?: (args: {
elements?: Elements;
rect: Rect;
offsetParent: any;
strategy: Strategy;
Expand Down
2 changes: 1 addition & 1 deletion packages/dom/src/platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {isElement} from './platform/isElement';
import {isRTL} from './platform/isRTL';
import type {Platform} from './types';

export const platform: Required<Platform> = {
export const platform: Platform = {
convertOffsetParentRelativeRectToViewportRelativeRect,
getDocumentElement,
getClippingRect,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type {Rect, Strategy} from '@floating-ui/core';
import type {Elements, Rect, Strategy} from '@floating-ui/core';
import {createCoords} from '@floating-ui/utils';
import {
getDocumentElement,
Expand All @@ -10,26 +10,34 @@ import {

import {getBoundingClientRect} from '../utils/getBoundingClientRect';
import {getScale} from './getScale';
import type {Platform} from '../types';
import {topLayer} from '../utils/topLayer';

export function convertOffsetParentRelativeRectToViewportRelativeRect({
rect,
offsetParent,
strategy,
}: {
rect: Rect;
offsetParent: Element | Window;
strategy: Strategy;
}): Rect {
const isOffsetParentAnElement = isHTMLElement(offsetParent);
export function convertOffsetParentRelativeRectToViewportRelativeRect(
this: Platform,
{
elements,
rect,
offsetParent,
strategy,
}: {
elements?: Elements;
rect: Rect;
offsetParent: Element | Window;
strategy: Strategy;
},
): Rect {
const documentElement = getDocumentElement(offsetParent);
const [isTopLayer] = elements ? topLayer(elements.floating) : [false];

if (offsetParent === documentElement) {
if (offsetParent === documentElement || isTopLayer) {
return rect;
}

let scroll = {scrollLeft: 0, scrollTop: 0};
let scale = createCoords(1);
const offsets = createCoords(0);
const isOffsetParentAnElement = isHTMLElement(offsetParent);

if (
isOffsetParentAnElement ||
Expand Down
11 changes: 6 additions & 5 deletions packages/dom/src/platform/getElementRects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@ import {getOffsetParent} from './getOffsetParent';

export const getElementRects: Platform['getElementRects'] = async function (
this: Platform,
{reference, floating, strategy},
data,
) {
const getOffsetParentFn = this.getOffsetParent || getOffsetParent;
const getDimensionsFn = this.getDimensions;
return {
reference: getRectRelativeToOffsetParent(
reference,
await getOffsetParentFn(floating),
strategy,
data.reference,
await getOffsetParentFn(data.floating),
data.strategy,
data.floating,
),
floating: {x: 0, y: 0, ...(await getDimensionsFn(floating))},
floating: {x: 0, y: 0, ...(await getDimensionsFn(data.floating))},
};
};
15 changes: 8 additions & 7 deletions packages/dom/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,21 @@ export interface Platform {
getDimensions: (element: Element) => Promisable<Dimensions>;

// Optional
convertOffsetParentRelativeRectToViewportRelativeRect?: (args: {
convertOffsetParentRelativeRectToViewportRelativeRect: (args: {
elements?: Elements;
rect: Rect;
offsetParent: Element;
strategy: Strategy;
}) => Promisable<Rect>;
getOffsetParent?: (
getOffsetParent: (
element: Element,
polyfill?: (element: HTMLElement) => Element | null,
) => Promisable<Element | Window>;
isElement?: (value: unknown) => Promisable<boolean>;
getDocumentElement?: (element: Element) => Promisable<HTMLElement>;
getClientRects?: (element: Element) => Promisable<Array<ClientRectObject>>;
isRTL?: (element: Element) => Promisable<boolean>;
getScale?: (element: HTMLElement) => Promisable<{x: number; y: number}>;
isElement: (value: unknown) => Promisable<boolean>;
getDocumentElement: (element: Element) => Promisable<HTMLElement>;
getClientRects: (element: Element) => Promisable<Array<ClientRectObject>>;
isRTL: (element: Element) => Promisable<boolean>;
getScale: (element: HTMLElement) => Promisable<{x: number; y: number}>;
}

export interface NodeScroll {
Expand Down
21 changes: 19 additions & 2 deletions packages/dom/src/utils/getRectRelativeToOffsetParent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ import {
import {getDocumentElement} from '../platform/getDocumentElement';
import {getBoundingClientRect} from './getBoundingClientRect';
import {getWindowScrollBarX} from './getWindowScrollBarX';
import {topLayer} from './topLayer';

export function getRectRelativeToOffsetParent(
element: Element | VirtualElement,
offsetParent: Element | Window,
strategy: Strategy,
floating: HTMLElement,
): Rect {
const isOffsetParentAnElement = isHTMLElement(offsetParent);
const documentElement = getDocumentElement(offsetParent);
Expand Down Expand Up @@ -46,9 +48,24 @@ export function getRectRelativeToOffsetParent(
}
}

let x = rect.left + scroll.scrollLeft - offsets.x;
let y = rect.top + scroll.scrollTop - offsets.y;

const [isTopLayer, topLayerX, topLayerY] = topLayer(floating);

if (isTopLayer) {
x += topLayerX;
y += topLayerY;

if (isOffsetParentAnElement) {
x += offsetParent.clientLeft;
y += offsetParent.clientTop;
}
}

return {
x: rect.left + scroll.scrollLeft - offsets.x,
y: rect.top + scroll.scrollTop - offsets.y,
x,
y,
width: rect.width,
height: rect.height,
};
Expand Down
29 changes: 29 additions & 0 deletions packages/dom/src/utils/topLayer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import {getContainingBlock} from '@floating-ui/utils/dom';

const topLayerSelectors = [':popover-open', ':modal'] as const;

export function topLayer(floating: HTMLElement) {
let isTopLayer = false;
let x = 0;
let y = 0;

function setIsTopLayer(selector: (typeof topLayerSelectors)[number]) {
try {
isTopLayer = isTopLayer || floating.matches(selector);
} catch (e) {}
}

topLayerSelectors.forEach((selector) => {
setIsTopLayer(selector);
});

const containingBlock = getContainingBlock(floating);

if (isTopLayer && containingBlock) {
const rect = containingBlock.getBoundingClientRect();
x = rect.x;
y = rect.y;
}

return [isTopLayer, x, y] as const;
}
2 changes: 1 addition & 1 deletion packages/dom/test/functional/autoUpdate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import {click} from './utils/click';
});
});

test.only(`reactive whileElementsMounted`, async ({page}) => {
test(`reactive whileElementsMounted`, async ({page}) => {
await page.goto('http://localhost:1234/autoUpdate');

// option is `false` on mount by default, so test that changing it to `true`
Expand Down

0 comments on commit ef24646

Please sign in to comment.