Repository navigation
Open every menu as a popover placed by Floating UI #4968
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
25 commits
Select commit
Hold shift + click to select a range
00c8186
Delete the unused _NavSelect partial and its menu styles
reakaleek e84629b
Delete the unreachable top-bar section dropdown
reakaleek 1670471
Open data-hover menus on mouse hover and close menus after a link
reakaleek 3f7090b
Open the sidebar section menu as a popover beside its box
reakaleek 734b4a1
Show the page jumper's results in a popover under its field
reakaleek 9656f0d
Open every design-system menu as a popover placed by floating.ts
reakaleek 9e8b526
Describe the one menu pattern in the design-system docs
reakaleek b47d600
Keep a hovered menu open when its button is clicked
reakaleek d6787a3
Merge remote-tracking branch 'origin/main' into floating-menus-everyw…
reakaleek 0d24df4
Target the version menu button in the API browse journey
reakaleek 2d94649
Rebind applies-to badges in the fullscreen table copy
reakaleek ac929a2
Hold hover timers and hover state weakly in floating.ts
reakaleek d96b1f9
Reopen a hover menu only when the pointer arrives from outside
reakaleek 5f0544e
Close a menu when one of its links is middle-clicked
reakaleek 309650b
Drop aria-haspopup="menu" from the link-list disclosures
reakaleek dc7ec74
Let a click focus the unbound applies-to badge in Safari
reakaleek e100889
Reset aria-expanded on an unbound applies-to clone
reakaleek f74cc23
Release popover tracking when the fullscreen table closes
reakaleek ddccc5a
Merge remote-tracking branch 'origin/main' into resolve-pr-comments-4…
reakaleek da45d67
Release removed popovers in the swap-time sweep again
reakaleek 63a5219
Cancel the hover-close timer when a popover closes
reakaleek 7401748
Drop aria-haspopup="menu" from deployment info and the split button
reakaleek 489f874
Drop aria-haspopup="dialog" from the theme switch cog
reakaleek 73ae94f
Leave aria-expanded on popover buttons to the browser
reakaleek 37f4a36
Stop announcing the applies-to panel as a dialog
reakaleek 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
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
93 changes: 93 additions & 0 deletions
93
src/Elastic.Documentation.Site/Assets/ds/applies-popover.test.ts
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,93 @@ | ||
| import { initAppliesPopovers, unbindAppliesPopovers } from './applies-popover' | ||
|
|
||
| const badge = ` | ||
| <span class="applies-popover" data-applies-popover> | ||
| <button type="button" data-applies-trigger>Stack 9.1+</button> | ||
| <span data-applies-panel>Details</span> | ||
| </span>` | ||
|
|
||
| describe('applies-to popovers', () => { | ||
| const showPopover = HTMLElement.prototype.showPopover | ||
|
|
||
| beforeEach(() => { | ||
| // jsdom has no popover API; the module binds only where it exists. | ||
| HTMLElement.prototype.showPopover = vi.fn() | ||
| document.body.innerHTML = badge + badge | ||
| }) | ||
|
|
||
| afterEach(() => { | ||
| HTMLElement.prototype.showPopover = showPopover | ||
| }) | ||
|
|
||
| const triggers = () => | ||
| Array.from(document.querySelectorAll('[data-applies-trigger]')) | ||
| const panels = () => | ||
| Array.from(document.querySelectorAll('[data-applies-panel]')) | ||
|
|
||
| it('makes each button open its own panel as a popover', () => { | ||
| initAppliesPopovers() | ||
|
|
||
| const [first, second] = panels() | ||
| expect(first.getAttribute('popover')).toBe('') | ||
| expect(first.id).not.toBe(second.id) | ||
| triggers().forEach((trigger, i) => | ||
| expect(trigger.getAttribute('popovertarget')).toBe(panels()[i].id) | ||
| ) | ||
| }) | ||
|
|
||
| it('gives badges brought in by a later swap ids of their own', () => { | ||
| initAppliesPopovers() | ||
| const before = panels().map((panel) => panel.id) | ||
|
|
||
| document.body.insertAdjacentHTML('beforeend', badge) | ||
| initAppliesPopovers() | ||
|
|
||
| const added = panels()[2] | ||
| expect(before).not.toContain(added.id) | ||
| expect( | ||
| panels() | ||
| .slice(0, 2) | ||
| .map((panel) => panel.id) | ||
| ).toEqual(before) | ||
| }) | ||
|
|
||
| it('leaves the panel to show on focus where the browser has no popovers', () => { | ||
| HTMLElement.prototype.showPopover = | ||
| undefined as unknown as typeof showPopover | ||
|
|
||
| initAppliesPopovers() | ||
|
|
||
| expect(panels()[0].hasAttribute('popover')).toBe(false) | ||
| expect( | ||
| document | ||
| .querySelector('[data-applies-popover]')! | ||
| .hasAttribute('data-applies-bound') | ||
| ).toBe(false) | ||
| }) | ||
|
|
||
| it('gives a cloned badge a panel of its own once it is unbound', () => { | ||
| document.body.innerHTML = badge | ||
| initAppliesPopovers() | ||
| const original = panels()[0] | ||
| triggers()[0].setAttribute('aria-expanded', 'true') | ||
| const copy = document.createElement('div') | ||
| copy.innerHTML = document.body.innerHTML | ||
|
|
||
| unbindAppliesPopovers(copy) | ||
| document.body.append(copy) | ||
| initAppliesPopovers(copy) | ||
|
|
||
| const copied = copy.querySelector('[data-applies-panel]')! | ||
| expect(copied.id).not.toBe(original.id) | ||
| expect( | ||
| copy | ||
| .querySelector('[data-applies-trigger]')! | ||
| .getAttribute('popovertarget') | ||
| ).toBe(copied.id) | ||
| expect( | ||
| copy | ||
| .querySelector('[data-applies-trigger]')! | ||
| .hasAttribute('aria-expanded') | ||
| ).toBe(false) | ||
| }) | ||
| }) |
38 changes: 38 additions & 0 deletions
38
src/Elastic.Documentation.Site/Assets/ds/applies-popover.ts
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,38 @@ | ||
| // The applies-to badge opens its panel as a popover: the browser opens and closes it, and floating.ts places it beside the button. | ||
| // A page renders the badge many times, from Markdown, tables and API pages that share no counter, so the server cannot give each | ||
| // panel a unique id. Binding gives it one and points the button at it. Unbound, the panel shows while the button has focus. | ||
| // Hooks: [data-applies-popover] on the wrapper, [data-applies-trigger] on the button and [data-applies-panel] on the panel. | ||
| let count = 0 | ||
|
|
||
| export function initAppliesPopovers(scope: ParentNode = document) { | ||
| if (typeof HTMLElement.prototype.showPopover !== 'function') return | ||
| scope | ||
| .querySelectorAll<HTMLElement>( | ||
| '[data-applies-popover]:not([data-applies-bound])' | ||
| ) | ||
| .forEach((root) => { | ||
| const trigger = root.querySelector('[data-applies-trigger]') | ||
| const panel = root.querySelector('[data-applies-panel]') | ||
| if (!trigger || !panel) return | ||
| panel.id = `applies-popover-${++count}` | ||
| panel.setAttribute('popover', '') | ||
| trigger.setAttribute('popovertarget', panel.id) | ||
|
infra-vault-gh-plugin-prod[bot] marked this conversation as resolved.
Resolved
|
||
| root.dataset.appliesBound = '' | ||
| }) | ||
| } | ||
|
|
||
| /** A cloned badge carries the original's panel id and `popovertarget`, so it would open the original panel. Unbind it, then `initAppliesPopovers` binds the copy. */ | ||
| export function unbindAppliesPopovers(scope: ParentNode) { | ||
| scope | ||
| .querySelectorAll<HTMLElement>('[data-applies-bound]') | ||
| .forEach((root) => { | ||
| const trigger = root.querySelector('[data-applies-trigger]') | ||
| const panel = root.querySelector('[data-applies-panel]') | ||
| panel?.removeAttribute('id') | ||
| panel?.removeAttribute('popover') | ||
| trigger?.removeAttribute('popovertarget') | ||
| // The copy's panel starts closed, whatever state the original was in. | ||
| trigger?.removeAttribute('aria-expanded') | ||
| delete root.dataset.appliesBound | ||
|
infra-vault-gh-plugin-prod[bot] marked this conversation as resolved.
Resolved
|
||
| }) | ||
| } | ||
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
This file was deleted.
Oops, something went wrong.
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.