Repository navigation
Open the image and diagram zoom views in native dialogs #4966
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
10 commits
Select commit
Hold shift + click to select a range
54301a4
Pan the mermaid fullscreen view with the shared drag listeners
reakaleek 7334bde
Open the mermaid fullscreen view in a native dialog
reakaleek 2b72476
Open the legacy image modal in a native dialog
reakaleek d199ca7
Encode the alt text in the image dialog and its images
reakaleek 58231e3
Clear the pan state when the mermaid dialog closes
reakaleek 507f323
Close open zoom dialogs before htmx swaps the page
reakaleek 7ecef02
Close an open zoom dialog when Ctrl+K opens the search
reakaleek e982c8a
Add a browser journey for the image and diagram zoom dialogs
reakaleek 4279b8f
Close only the zoom dialogs an htmx swap replaces
reakaleek ed1ea2f
Close the diagram dialog when a swap removes its diagram
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
141 changes: 141 additions & 0 deletions
141
src/Elastic.Documentation.Site/Assets/image-modal.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,141 @@ | ||
| import { bindImageModals } from './image-modal' | ||
|
|
||
| // The markup ImageView.cshtml renders for one image. | ||
| const image = ` | ||
| <a class="reference internal image-reference" href="javascript:void(0)" data-modal-id="modal-a"> | ||
| <img src="/a.png" alt="A"> | ||
| </a> | ||
| <dialog id="modal-a" class="modal" aria-label="A"> | ||
| <div class="modal-content"> | ||
| <form method="dialog" class="modal-close"> | ||
| <button type="submit" class="close-button" aria-label="Close">×</button> | ||
| </form> | ||
| <a class="reference internal image-reference" href="/a.png" target="_blank"><img src="/a.png" alt="A"></a> | ||
| </div> | ||
| </dialog>` | ||
|
|
||
| const dialog = () => document.getElementById('modal-a') as HTMLDialogElement | ||
| const scroll = () => document.documentElement.style.overflow | ||
|
|
||
| function click(target: Element): MouseEvent { | ||
| const event = new MouseEvent('click', { bubbles: true, cancelable: true }) | ||
| target.dispatchEvent(event) | ||
| return event | ||
| } | ||
|
|
||
| describe('legacy image modal', () => { | ||
| beforeAll(() => { | ||
| bindImageModals() | ||
| // jsdom has no modal dialogs; these do what the browser does to the attribute and events. | ||
| HTMLDialogElement.prototype.showModal = function ( | ||
| this: HTMLDialogElement | ||
| ) { | ||
| this.setAttribute('open', '') | ||
| } | ||
| HTMLDialogElement.prototype.close = function (this: HTMLDialogElement) { | ||
| if (!this.open) return | ||
| this.removeAttribute('open') | ||
| this.dispatchEvent(new Event('close')) | ||
| } | ||
| }) | ||
|
|
||
| beforeEach(() => { | ||
| document.body.innerHTML = image | ||
| }) | ||
|
|
||
| afterEach(() => { | ||
| // A test that leaves the dialog open leaves the page scroll locked. | ||
| document.documentElement.removeAttribute('style') | ||
| }) | ||
|
|
||
| it('opens the image in a modal dialog that locks the page scroll', () => { | ||
| const showModal = vi.spyOn(HTMLDialogElement.prototype, 'showModal') | ||
|
|
||
| const event = click(document.querySelector('img')!) | ||
|
|
||
| expect(event.defaultPrevented).toBe(true) | ||
| expect(showModal).toHaveBeenCalledTimes(1) | ||
| expect(dialog().open).toBe(true) | ||
| expect(scroll()).toBe('hidden') | ||
| }) | ||
|
|
||
| it('closes on a click on the backdrop, and stays open for a click inside', () => { | ||
| click(document.querySelector('[data-modal-id]')!) | ||
|
|
||
| click(dialog().querySelector('img')!) | ||
| expect(dialog().open).toBe(true) | ||
|
|
||
| click(dialog()) | ||
| expect(dialog().open).toBe(false) | ||
| expect(scroll()).toBe('') | ||
| }) | ||
|
|
||
| it('unlocks the page scroll when the browser closes it, as it does on Escape', () => { | ||
| click(document.querySelector('[data-modal-id]')!) | ||
|
|
||
| dialog().close() | ||
|
|
||
| expect(scroll()).toBe('') | ||
| }) | ||
|
|
||
| const beforeSwap = (detail: object) => | ||
| document.dispatchEvent(new CustomEvent('htmx:beforeSwap', { detail })) | ||
|
|
||
| it('closes an open dialog and unlocks the page scroll before htmx swaps its page', () => { | ||
| click(document.querySelector('[data-modal-id]')!) | ||
| expect(scroll()).toBe('hidden') | ||
|
|
||
| beforeSwap({ shouldSwap: true, target: document.body }) | ||
|
|
||
| expect(dialog().open).toBe(false) | ||
| expect(scroll()).toBe('') | ||
| }) | ||
|
|
||
| it('keeps the dialog open for a response htmx does not swap', () => { | ||
| click(document.querySelector('[data-modal-id]')!) | ||
|
|
||
| beforeSwap({ shouldSwap: false, target: document.body }) | ||
|
|
||
| expect(dialog().open).toBe(true) | ||
| expect(scroll()).toBe('hidden') | ||
| }) | ||
|
|
||
| it('keeps the dialog open when the swap replaces another part of the page', () => { | ||
| document.body.insertAdjacentHTML( | ||
| 'beforeend', | ||
| '<div id="elsewhere"></div>' | ||
| ) | ||
| click(document.querySelector('[data-modal-id]')!) | ||
|
|
||
| beforeSwap({ | ||
| shouldSwap: true, | ||
| target: document.getElementById('elsewhere'), | ||
| }) | ||
|
|
||
| expect(dialog().open).toBe(true) | ||
| }) | ||
|
|
||
| it('opens and closes again after a close', () => { | ||
| const opener = document.querySelector('[data-modal-id]')! | ||
| click(opener) | ||
| dialog().close() | ||
|
|
||
| click(opener) | ||
| expect(dialog().open).toBe(true) | ||
| expect(scroll()).toBe('hidden') | ||
| click(dialog()) | ||
| expect(dialog().open).toBe(false) | ||
| expect(scroll()).toBe('') | ||
| }) | ||
|
|
||
| it('opens the dialog of a carousel slide', () => { | ||
| document.body.innerHTML = ` | ||
| <div class="carousel-container"><div class="carousel-track"><div class="carousel-slide"> | ||
| <a class="carousel-image-reference" href="javascript:void(0)" data-modal-id="modal-a"><img src="/a.png"></a> | ||
| </div></div></div>${image}` | ||
|
|
||
| click(document.querySelector('.carousel-image-reference img')!) | ||
|
|
||
| expect(dialog().open).toBe(true) | ||
| }) | ||
| }) | ||
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,73 @@ | ||
| import { lockPageScroll } from './scroll-lock' | ||
|
|
||
| function open(dialog: HTMLDialogElement): void { | ||
| dialog.showModal() | ||
|
infra-vault-gh-plugin-prod[bot] marked this conversation as resolved.
Resolved
|
||
| // The dialog handles Escape and returns focus to the image; the page behind it would still scroll. | ||
| const unlockScroll = lockPageScroll() | ||
| const closing = new AbortController() | ||
| // A click on the backdrop lands on the dialog itself, not on its content. | ||
| dialog.addEventListener( | ||
| 'click', | ||
| (event) => { | ||
| if (event.target === dialog) dialog.close() | ||
| }, | ||
| { signal: closing.signal } | ||
| ) | ||
| dialog.addEventListener( | ||
|
infra-vault-gh-plugin-prod[bot] marked this conversation as resolved.
Resolved
|
||
| 'close', | ||
| () => { | ||
| unlockScroll() | ||
| closing.abort() | ||
| }, | ||
| { signal: closing.signal } | ||
| ) | ||
| } | ||
|
|
||
| // A diagram dialog lives in <body>, outside the content a swap replaces, so it records the container that opened it. | ||
| const openers = new WeakMap<HTMLDialogElement, Element>() | ||
|
|
||
| export function anchorZoomDialog( | ||
| dialog: HTMLDialogElement, | ||
| opener: Element | ||
| ): void { | ||
| openers.set(dialog, opener) | ||
| } | ||
|
|
||
| // Closes the open image or diagram dialogs that sit in scope or were opened from it; their close handlers release the scroll lock. | ||
| export function closeOpenZoomDialogs(scope: Node = document): void { | ||
| document | ||
| .querySelectorAll<HTMLDialogElement>( | ||
| 'dialog.modal[open], dialog.mermaid-modal[open]' | ||
| ) | ||
| .forEach((dialog) => { | ||
| const opener = openers.get(dialog) | ||
| if (scope.contains(dialog) || (opener && scope.contains(opener))) | ||
| dialog.close() | ||
| }) | ||
| } | ||
|
|
||
| type BeforeSwapDetail = { shouldSwap?: boolean; target?: Element } | ||
|
|
||
| // A swap removes the dialogs inside its target without firing their close event, which would leave the page scroll | ||
| // locked. A response htmx will not swap, such as an error, leaves the page and its dialogs alone. | ||
| function closeDialogsTheSwapRemoves(event: Event): void { | ||
| const { shouldSwap, target } = | ||
| (event as CustomEvent<BeforeSwapDetail>).detail ?? {} | ||
| if (shouldSwap && target) closeOpenZoomDialogs(target) | ||
|
infra-vault-gh-plugin-prod[bot] marked this conversation as resolved.
Resolved
|
||
| } | ||
|
|
||
| // Opens the dialog named by [data-modal-id] on an image link. One document listener serves every page htmx swaps in. | ||
| export function bindImageModals(): void { | ||
| document.addEventListener('click', (event) => { | ||
| const link = | ||
| event.target instanceof Element | ||
| ? event.target.closest<HTMLElement>('[data-modal-id]') | ||
| : null | ||
| if (!link) return | ||
| const dialog = document.getElementById(link.dataset.modalId ?? '') | ||
| if (!(dialog instanceof HTMLDialogElement)) return | ||
| event.preventDefault() | ||
| open(dialog) | ||
| }) | ||
| document.addEventListener('htmx:beforeSwap', closeDialogsTheSwapRemoves) | ||
| } | ||
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
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.