Skip to content
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 docs/development/journeys.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ Each folder under `journeys/` is an area, and each area has one test class under
|---|---|
| `landing` | The home page, the 404 page, a hub page and a group of page cards |
| `navigation` | The sidebar and the table of contents |
| `content` | Tabs, dropdowns, admonitions and the code copy button |
| `content` | Tabs, dropdowns, admonitions, the code copy button and the image and diagram zoom dialogs |
| `chrome` | The isolated header (logo, search button, GitHub link, deployment info) and the footer |
| `page-actions` | The "View as Markdown" and "Edit this page" links |
| `feedback` | The "Was this page helpful?" buttons render with their accessible names (needs the `page-feedback` flag, which the isolated site only turns on in Debug builds) |
Expand Down
17 changes: 0 additions & 17 deletions src/Elastic.Documentation.Site/Assets/image-carousel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,23 +70,6 @@ class ImageCarousel {
indicator.addEventListener('click', () => this.goToSlide(index))
})

// Handle image clicks for modal
this.slides.forEach((slide) => {
const imageLink = slide.querySelector('.carousel-image-reference')
if (imageLink) {
imageLink.addEventListener('click', (e) => {
e.preventDefault()
const modalId = imageLink.getAttribute('data-modal-id')
if (modalId) {
const modal = document.getElementById(modalId)
if (modal) {
modal.style.display = 'flex'
}
}
})
}
})

// Keyboard navigation
document.addEventListener('keydown', (e) => {
if (!this.isInViewport()) return
Expand Down
141 changes: 141 additions & 0 deletions src/Elastic.Documentation.Site/Assets/image-modal.test.ts
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">&times;</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', () => {
Comment thread
infra-vault-gh-plugin-prod[bot] marked this conversation as resolved.
Resolved
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)
})
})
73 changes: 73 additions & 0 deletions src/Elastic.Documentation.Site/Assets/image-modal.ts
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()
Comment thread
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(
Comment thread
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)
Comment thread
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)
}
2 changes: 2 additions & 0 deletions src/Elastic.Documentation.Site/Assets/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { config } from './config'
import { initCopyButton } from './copybutton'
import { initHighlight, preloadHighlight } from './hljs'
import { initImageCarousel } from './image-carousel'
import { bindImageModals } from './image-modal'
import { initListing } from './listing'
import { initMath } from './math'
import { initMermaid } from './mermaid'
Expand Down Expand Up @@ -91,6 +92,7 @@ initApiMenus()
initApiChromeSync()
bindApiBetaBanner()
bindModalEscape()
bindImageModals()
bindHtmxHooks({
forcesFullPageLoad: (targetPath) =>
// A move between a docs page and an API page, or to another API product: the swap would
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,42 +116,6 @@
padding-bottom: 40px; /* Space for indicators */
}

/* Override modal styles for image carousels */
.modal .modal-content {
max-width: 95vw !important;
max-height: 95vh !important;
width: auto !important;
height: auto !important;
padding: 0 !important;
background: transparent !important;
box-shadow: none !important;
}

.modal .modal-content img {
max-width: 95vw;
max-height: 95vh;
width: auto;
height: auto;
object-fit: contain;
display: block;
}

/* Ensure the close button is visible */
.modal .modal-close {
background: rgba(0, 0, 0, 0.7);
border-radius: 50%;
width: 40px;
height: 40px;
display: flex;
align-items: center;
justify-content: center;
top: -20px;
right: -20px;
}

.modal .modal-close a {
color: white;
}
@media (max-width: 768px) {
.carousel-control {
width: 30px;
Expand Down
36 changes: 19 additions & 17 deletions src/Elastic.Documentation.Site/Assets/markdown/mermaid.css
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,26 @@

/* Fullscreen modal */
.mermaid-modal {
position: fixed;
inset: 0;
z-index: 9999;
width: 95vw;
height: 95vh;
max-width: none;
max-height: none;
margin: auto;
padding: 0;
border: 0;
background-color: #ffffff;
border-radius: 0.75rem;
box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.25);
overflow: hidden;
animation: mermaid-fade-in 0.2s ease;
}

.mermaid-modal[open] {
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
}

.mermaid-modal::backdrop {
background-color: rgba(0, 0, 0, 0.5);
animation: mermaid-fade-in 0.2s ease;
}
Expand All @@ -105,18 +119,6 @@
}
}

.mermaid-modal-content {
position: relative;
width: 95vw;
height: 95vh;
background-color: #ffffff;
border-radius: 0.75rem;
box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.25);
overflow: hidden;
display: flex;
flex-direction: column;
}

.mermaid-modal-close {
position: absolute;
top: 0.75rem;
Expand Down
Loading
Loading