import type { VNode } from 'vue'; import { Teleport } from 'vue'; import VueEasyLightbox from 'vue-easy-lightbox'; import { Prop, toNative } from 'vue-facing-decorator'; import { globalState } from '../../app/global-state'; import TsxComponent, { Component } from '../../app/vuetsx'; import { SlotNameIdentifier } from '../../common/slot-name-identifier'; import { PortalUtils } from '../../common/utils/utils'; import './css/lightbox.css'; interface LightboxItem { src: string; title: string; } /** The viewer's own name and its buttons' names. */ export interface LightboxLabels { /** The viewer itself, a dialog: "Photo gallery". */ dialog?: string; previous?: string; next?: string; close?: string; zoomIn?: string; zoomOut?: string; /** The toolbar's "original size" button. */ resize?: string; rotateLeft?: string; rotateRight?: string; } interface LightboxArgs { imageArr: string[] | LightboxItem[]; scrollDisabled?: boolean; moveDisabled?: boolean; rotateDisabled?: boolean; customPortalContainer?: string; /** Names of the viewer and its buttons, key by key over `LightboxConfig.labels`. */ labels?: LightboxLabels; } interface LightboxShowArgs { index?: number; /** Runs once the viewer has closed — after the focus went back to what opened it. */ onHidden?: () => void; } /** The viewer's own functions, as the library hands them to its `toolbar` slot. */ interface LightboxToolbarMethods { zoomIn: (e: MouseEvent) => void; zoomOut: (e: MouseEvent) => void; resize: (e: MouseEvent) => void; rotateLeft: (e: MouseEvent) => void; rotateRight: (e: MouseEvent) => void; } const DEFAULT_LABELS: Required = { dialog: 'Image viewer', previous: 'Previous image', next: 'Next image', close: 'Close', zoomIn: 'Zoom in', zoomOut: 'Zoom out', resize: 'Original size', rotateLeft: 'Rotate left', rotateRight: 'Rotate right', }; /** What takes the keyboard's focus inside the viewer (not a bare `[href]`: the icons' SVG `` has one). */ const FOCUSABLE_SELECTOR = 'a[href], button:not([disabled]), [tabindex]:not([tabindex^="-"])'; export class LightboxConfig { static defaultPortalTarget = 'body'; /** * The names every viewer uses where its own `labels` give none — English until the app sets its language's, * once, at startup (a missing key keeps the English one). */ static labels: LightboxLabels = { ...DEFAULT_LABELS }; } /** * The image viewer (`vue-easy-lightbox`), as a dialog: its buttons are real ` ); } private renderToolbarButton( type: string, label: string, run: (e: MouseEvent) => void, ): VNode { return ( ); } private renderToolbar(methods: LightboxToolbarMethods): VNode { return (
{this.renderToolbarButton( 'zoomin', this.label('zoomIn'), methods.zoomIn, )} {this.renderToolbarButton( 'zoomout', this.label('zoomOut'), methods.zoomOut, )} {this.renderToolbarButton( 'resize', this.label('resize'), methods.resize, )} {!this.rotateDisabled && this.renderToolbarButton( 'rotate-left', this.label('rotateLeft'), methods.rotateLeft, )} {!this.rotateDisabled && this.renderToolbarButton( 'rotate-right', this.label('rotateRight'), methods.rotateRight, )}
); } render(h) { return ( {this.everShown && ( { this.shownIndex = newIndex; }} onHide={() => { this.close(); }} > {{ 'prev-btn': ({ prev }: { prev: () => void }) => this.renderArrow('prev', prev), 'next-btn': ({ next }: { next: () => void }) => this.renderArrow('next', next), 'close-btn': ({ close }: { close: () => void }) => ( ), 'toolbar': ({ toolbarMethods }: { toolbarMethods: LightboxToolbarMethods }) => this.renderToolbar(toolbarMethods), }} )} ); } } SlotNameIdentifier(LightboxComponent, 'Lightbox'); const Lightbox = toNative(LightboxComponent); export default Lightbox;