/** * Full-screen image viewer built on top of a served gallery. * * @augments IvComponent */ export class Lightbox extends IvComponent { /** @type {Readonly} */ static defaults: Readonly; /** * Returns the instance attached to `el`, if any. * * @param {Element} el Host element. * @returns {Lightbox|undefined} The instance or `undefined`. */ static get(el: Element): Lightbox | undefined; /** * Returns the existing instance or creates one. * * @param {Element} el Host element. * @param {Partial} [options] Options passed in JavaScript. * @returns {Lightbox} The instance. */ static getOrCreate(el: Element, options?: Partial): Lightbox; /** * Instantiates every `[data-iv-component="lightbox"]` inside `root`. * * @param {ParentNode} [root] Subtree to scan. * @returns {Lightbox[]} Newly created instances. */ static initAll(root?: ParentNode): Lightbox[]; /** * @param {Element} el Gallery root. * @param {Partial} [options] Options passed in JavaScript. */ constructor(el: Element, options?: Partial); /** @type {HTMLDialogElement|null} The generated viewer. */ _dialogEl: HTMLDialogElement | null; /** @type {Dialog|null} The dialog composed on it. */ _dialog: Dialog | null; /** @type {boolean} Whether this instance created that dialog instance. */ _ownsDialog: boolean; /** @type {HTMLImageElement|null} The image on stage. */ _img: HTMLImageElement | null; /** @type {HTMLElement|null} The `
` that holds it. */ _stage: HTMLElement | null; /** @type {HTMLElement|null} The `
`. */ _caption: HTMLElement | null; /** @type {HTMLElement|null} The "3 / 12" counter. */ _counter: HTMLElement | null; /** @type {HTMLButtonElement|null} The zoom button. */ _zoomButton: HTMLButtonElement | null; /** @type {number} Index currently on stage. */ _index: number; /** @type {boolean} Whether the stage is zoomed in. */ _zoomed: boolean; /** @type {LightboxDrag|null} Drag in progress. */ _drag: LightboxDrag | null; /** @type {boolean} Whether the click after a swipe must be swallowed. */ _swallowClick: boolean; /** @type {EventTarget|null} Where the press behind the current click landed. */ _pressTarget: EventTarget | null; /** @type {number} Pending frame that ends the crossfade. */ _frame: number; /** * Resolved options. * * @returns {Readonly} The options. */ get options(): Readonly; /** * The gallery items, in document order. * * @returns {HTMLElement[]} The `.iv-gallery__item` elements. */ get items(): HTMLElement[]; /** * Index of the image on stage. * * @returns {number} Zero-based index. */ get index(): number; /** * Whether the viewer is open. * * @returns {boolean} `true` while the dialog is open. */ get isOpen(): boolean; /** * Whether the stage is zoomed in. * * @returns {boolean} `true` when zoomed. */ get zoomed(): boolean; /** * The generated viewer. * * @returns {HTMLDialogElement|null} The `` or `null` before `init`. */ get dialog(): HTMLDialogElement | null; /** * Builds the viewer and inserts it right after the gallery. * * @returns {void} */ _build(): void; /** * Builds one control of the viewer. * * @param {Document} doc Owner document. * @param {"close"|"prev"|"next"|"zoom"} kind Which control. * @param {string} label Accessible name. * @returns {HTMLButtonElement} The button. */ _button(doc: Document, kind: "close" | "prev" | "next" | "zoom", label: string): HTMLButtonElement; /** * Reacts to a control. * * @param {"close"|"prev"|"next"|"zoom"} kind Which control. * @returns {void} */ _onButton(kind: "close" | "prev" | "next" | "zoom"): void; /** * Opens the viewer when a gallery item is activated. * * @param {Event} event Click event. * @returns {void} */ _onGalleryClick(event: Event): void; /** * Keyboard of the viewer: arrows, Home, End and `Z`. * * @param {Event} event Keydown event. * @returns {void} */ _onKeydown(event: Event): void; /** * Mirrors a dialog event on the gallery root. The two cancelable ones carry * the cancellation back to the dialog, so a prevented `iv:open` on the * gallery really does keep the viewer closed. * * @param {Event} event The dialog event. * @param {"open"|"opened"|"close"|"closed"} name Mirrored name. * @returns {void} */ _mirror(event: Event, name: "open" | "opened" | "close" | "closed"): void; /** * Opens the viewer on one item. * * @param {number} [index] Zero-based index of the image to show. * @returns {void} */ open(index?: number): void; /** * Closes the viewer. * * @param {string} [reason] Why the viewer closes. * @returns {void} */ close(reason?: string): void; /** * Shows the next image. * * @returns {void} */ next(): void; /** * Shows the previous image. * * @returns {void} */ prev(): void; /** * Shows one image by index. * * @param {number} index Zero-based index; wrapped when `loop` is on. * @param {LightboxReason} [reason] Why the viewer moves. * @returns {void} */ go(index: number, reason?: LightboxReason): void; /** * Zooms the stage in or out. * * @param {boolean} [on] Target state; omitted toggles. * @returns {void} */ zoom(on?: boolean): void; /** * Moves by one step in either direction, honouring `loop`. * * @param {number} delta `1` forwards, `-1` backwards. * @param {LightboxReason} reason Why the viewer moves. * @returns {void} */ _step(delta: number, reason: LightboxReason): void; /** * Wraps or clamps an index depending on `loop`. * * @param {number} index Requested index. * @returns {number} A valid index. */ _clamp(index: number): number; /** * Puts the current item on stage. * * @param {boolean} fade Whether the swap crossfades. * @returns {void} */ _render(fade: boolean): void; /** * Crossfades the stage: the image is faded out by an attribute, and the next * pair of frames takes it away again so the transition plays forwards. * * @returns {void} */ _fade(): void; /** * Fetches the neighbours of the current image, so a swipe or an arrow lands * on a picture the browser already has. * * @returns {void} */ _preload(): void; /** * Leaves the zoom without emitting a state change nobody asked for. * * @returns {void} */ _resetZoom(): void; /** * Starts tracking a drag over the stage. * * @param {Event} event Pointer event. * @returns {void} */ _onPointerDown(event: Event): void; /** * Pans the zoomed stage, or just remembers where the pointer travelled. * * @param {Event} event Pointer event. * @returns {void} */ _onPointerMove(event: Event): void; /** * Turns a horizontal drag longer than the threshold into a change. * * @param {Event} event Pointer event. * @returns {void} */ _onPointerUp(event: Event): void; /** * A click on the dark surround closes the viewer; the click that ends a * gesture never does. * * @param {Event} event Click event. * @returns {void} */ _onStageClick(event: Event): void; } export type LightboxOptions = { /** * Wrap around at both ends. */ loop: boolean; /** * Offer the zoom button and the `Z` shortcut. */ zoom: boolean; /** * Change image with a horizontal drag. */ swipe: boolean; /** * How many neighbours are fetched ahead. */ preload: number; /** * Show the "3 / 12" counter. */ counter: boolean; /** * Show `data-iv-caption` under the image. */ captions: boolean; /** * Accessible name of the close button. */ closeText: string; /** * Accessible name of the previous button. */ prevText: string; /** * Accessible name of the next button. */ nextText: string; /** * Accessible name of the zoom button. */ zoomText: string; /** * Accessible name of the generated dialog. */ galleryLabel: string; /** * Visible counter; `{index}` and `{total}` are replaced with digits in the language of the gallery. */ counterText: string; }; export type LightboxReason = "next" | "prev" | "keyboard" | "swipe" | "api"; /** * A drag in progress over the stage. */ export type LightboxDrag = { /** * Pointer id. */ id: number; /** * Client x where the press landed. */ x: number; /** * Client y where the press landed. */ y: number; /** * Last client x seen. */ lastX: number; /** * Last client y seen. */ lastY: number; /** * `scrollLeft` of the stage when the press landed. */ scrollX: number; /** * `scrollTop` of the stage when the press landed. */ scrollY: number; /** * Whether the pointer ever moved. */ moved: boolean; }; import { IvComponent } from "../core/component.js"; import { Dialog } from "./dialog.js";