-
Notifications
You must be signed in to change notification settings - Fork 4.8k
[ui] Add internal wp compat overlay slot helper #77851
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
eb24c34
UI: Add dormant wp compat overlay slot helper
ciampo 87cf4ea
[ui] wp compat overlay slot: tighten hook JSDoc on render-phase visib…
ciampo cdb683b
[ui] wp compat overlay slot: add JSDoc to createSlot
ciampo b3edc90
[ui] wp compat overlay slot: cover ownerDocument-mismatch cache branch
ciampo 137688e
[ui] wp compat overlay slot: remove Base UI mentions from comments
ciampo df14155
[ui] wp compat overlay slot: trim verbose comments
ciampo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
packages/ui/src/utils/css/wp-compat-overlay-slot.module.css
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| /* | ||
| * WP compat overlay slot — body-level positioned container that hosts | ||
| * `@wordpress/ui` overlays so they reliably stack in mixed-library | ||
| * compositions. The `data-wp-compat-overlay-slot` attribute is the | ||
| * architectural identifier (see `getWpCompatOverlaySlot()`); `.slot` is | ||
| * the styling vehicle. | ||
| * | ||
| * Authored unlayered — outside `@layer wp-ui-*` — so the slot's z-index | ||
| * and positioning win against any `@layer`-scoped rule. CSS cascade | ||
| * layers always lose to unlayered styles, so unlayering is the ceiling. | ||
| * | ||
| * Why each declaration is what it is: | ||
| * | ||
| * - `z-index: 1000000003` — sits in a reserved billion-scale band above | ||
| * the legacy z-index map in `packages/base-styles/_z-index.scss`. | ||
| * | ||
| * - `position: fixed` — load-bearing for two reasons. (1) `z-index` is | ||
| * ignored on `position: static`. (2) A non-static position makes the | ||
| * slot a containing block for absolute-positioned descendants; | ||
| * floating-ui writes physical viewport-relative `top` / `left` onto | ||
| * the floating element, so the containing block must sit at viewport | ||
| * `(0, 0)`. `position: fixed` (with `top: 0; left: 0`) pins it there | ||
| * regardless of scroll; `position: relative` would anchor at body's | ||
| * flow position and silently break popover positioning. | ||
| * | ||
| * - `top: 0; left: 0` — physical, not logical. `inset-inline-start: 0` | ||
| * would resolve to `right: 0` in RTL, anchoring the (zero-width) slot | ||
| * at the viewport's top-right corner; an absolute child with | ||
| * `left: 200px` would then resolve off the right edge of the screen. | ||
| * Same hazard for `inset-block-start` under vertical writing modes. | ||
| * | ||
| * - `isolation: isolate` — redundant once `position: fixed` and | ||
| * `z-index` are set (the slot is already a stacking context). Kept as | ||
| * an explicit declaration of intent. | ||
| * | ||
| * The slot has zero content size, so it never intercepts pointer events. | ||
| */ | ||
|
|
||
| .slot { | ||
| position: fixed; | ||
| top: 0; | ||
| /* stylelint-disable-next-line plugin/use-logical-properties-and-values -- Physical anchoring required so floating-ui's viewport-relative coordinates resolve correctly; see file header. */ | ||
| left: 0; | ||
| z-index: 1000000003; | ||
| isolation: isolate; | ||
| } |
79 changes: 79 additions & 0 deletions
79
packages/ui/src/utils/test/use-enable-wp-compat-overlay-slot.test.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| import { render } from '@testing-library/react'; | ||
| import { | ||
| WP_COMPAT_OVERLAY_SLOT_ATTRIBUTE, | ||
| getWpCompatOverlaySlot, | ||
| __resetWpCompatOverlaySlotCacheForTests, | ||
| } from '../wp-compat-overlay-slot'; | ||
| import { useEnableWpCompatOverlaySlot } from '../use-enable-wp-compat-overlay-slot'; | ||
|
|
||
| const internalWindow = window as unknown as { | ||
| __wpUiCompatOverlaySlotEnabled?: boolean; | ||
| }; | ||
|
|
||
| // The slot is identified by a data attribute (cross-tooling marker, not a | ||
| // user-facing role/text), so direct DOM queries are appropriate here — | ||
| // Testing Library's role/text accessors don't apply. | ||
| /* eslint-disable testing-library/no-node-access */ | ||
|
|
||
| function findSlots(): HTMLElement[] { | ||
| return Array.from( | ||
| document.querySelectorAll< HTMLElement >( | ||
| `[${ WP_COMPAT_OVERLAY_SLOT_ATTRIBUTE }]` | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| function HookHost() { | ||
| useEnableWpCompatOverlaySlot(); | ||
| return null; | ||
| } | ||
|
|
||
| describe( 'useEnableWpCompatOverlaySlot', () => { | ||
| afterEach( () => { | ||
| __resetWpCompatOverlaySlotCacheForTests(); | ||
| findSlots().forEach( ( el ) => el.remove() ); | ||
| delete internalWindow.__wpUiCompatOverlaySlotEnabled; | ||
| } ); | ||
|
|
||
| it( 'enables the slot once mounted, so getWpCompatOverlaySlot() returns the slot', () => { | ||
| expect( getWpCompatOverlaySlot() ).toBeNull(); | ||
|
|
||
| render( <HookHost /> ); | ||
|
|
||
| const slot = getWpCompatOverlaySlot(); | ||
| expect( slot ).not.toBeNull(); | ||
| expect( slot?.parentElement ).toBe( document.body ); | ||
| expect( findSlots() ).toHaveLength( 1 ); | ||
| } ); | ||
|
|
||
| it( 'is idempotent across multiple components calling the hook', () => { | ||
| render( | ||
| <> | ||
| <HookHost /> | ||
| <HookHost /> | ||
| <HookHost /> | ||
| </> | ||
| ); | ||
|
|
||
| expect( getWpCompatOverlaySlot() ).not.toBeNull(); | ||
| expect( findSlots() ).toHaveLength( 1 ); | ||
| } ); | ||
|
|
||
| it( 'leaves the slot enabled after the hook caller unmounts (one-way opt-in)', () => { | ||
| // The slot is shared infrastructure across all `@wordpress/ui` | ||
| // consumers in the document; a single component shouldn't be | ||
| // able to disable it for everyone else once enabled. This test | ||
| // pins that one-way behavior — unmounting the hook caller does | ||
| // not flip the gate back off. | ||
| const { unmount } = render( <HookHost /> ); | ||
|
|
||
| expect( getWpCompatOverlaySlot() ).not.toBeNull(); | ||
|
|
||
| unmount(); | ||
|
|
||
| expect( internalWindow.__wpUiCompatOverlaySlotEnabled ).toBe( true ); | ||
| expect( getWpCompatOverlaySlot() ).not.toBeNull(); | ||
| } ); | ||
| } ); | ||
|
|
||
| /* eslint-enable testing-library/no-node-access */ |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.