Skip to content

VistaBox Class

The VistaBox class is an abstract base class for all content types in VistaView. Extend this class to create custom content types like videos, maps, or interactive elements.

VistaBox handles:

  • Image sizing and scaling
  • Zoom and pan transformations
  • Lifecycle management (init, load, destroy)
  • State management for width, height, and transformations

Use Case: Create custom content extensions by extending VistaBox. See Extensions for examples.

abstract element: HTMLImageElement | HTMLDivElement

The DOM element containing the content. Must be implemented by subclasses.

state: VistaImageState;

Current state of the image including dimensions and transformations. Contains internal properties prefixed with _ (e.g., _width, _height, _transform).

pos: number;

Position relative to the current image (0 = center, -1 = left, 1 = right).

index: number;

Zero-based index in the gallery.

config: VistaImgConfig;

Image configuration containing src, alt, and srcSet.

origin: VistaImgOrigin | undefined;

Information about the origin element (thumbnail) in the DOM.

isReady: boolean;

Whether the content has finished loading.

thumb: HTMLDivElement | null;

Thumbnail element used during transitions.

constructor(par: VistaImageParams)

Creates a new VistaBox instance. Called by VistaView during initialization.

async init(): Promise<void>

Initializes the content. Override this to implement custom loading logic.

setSizes(par?: { stableSize?: boolean; initDimension?: boolean }): void

Calculates and sets dimensions based on viewport and content size.

Parameters:

  • par.stableSize - Use cached dimensions without recalculation
  • par.initDimension - Calculate initial dimensions from origin element
prepareClose(): void

Prepares the element for closing transition. Called before the lightbox closes.

cancelPendingLoad(): void

Cancels any in-progress image loading operations.

destroy(): void

Cleans up resources and removes event listeners. Called when the element is no longer needed.

cloneStyleFrom(img: VistaBox, state?: VistaHiresTransitionOpt): void

Copies style and state from another VistaBox instance. Used during transitions to maintain visual continuity.

toObject(): VistaImageClone

Returns a serializable representation of the current state. Used for the onContentChange event.

setInitialCenter(center: { x: number; y: number }): void

Sets the center point for zoom and pan operations.

onImageReady(): void

Override this method to perform actions when the content is ready.

animateZoom(scaleFactor: number, center?: { x: number; y: number }): void

Override to implement custom zoom animation logic.

scaleMove(scaleFactor: number, center?: { x: number; y: number }, animate?: boolean): void

Override to implement custom scale and movement logic.

momentumThrow(par: { x: number; y: number }): () => void

Override to implement momentum scrolling after a swipe gesture.

The following protected properties are available when extending VistaBox:

protected initH: number = 0

Initial height of the content based on thumbnail dimensions.

protected initW: number = 0

Initial width of the content based on thumbnail dimensions.

protected fullH: number = 0

Full height of the content when displayed in the lightbox.

protected fullW: number = 0

Full width of the content when displayed in the lightbox.

protected maxW: number = 0

Maximum allowed width for the content.

protected minW: number = 0

Minimum allowed width for the content.

protected defaultWH: number = 200

Default width/height fallback value.

protected isZoomedIn: boolean = false

Whether the content is currently zoomed in.

protected isCancelled: boolean = false

Whether the loading operation has been cancelled.

protected isLoadedResolved: ((val: boolean | PromiseLike<boolean>) => void) | null = null

Resolver function for the isLoaded promise.

protected isLoadedRejected: ((reason?: any) => void) | null = null

Rejection function for the isLoaded promise.

protected isLoaded: Promise<boolean>

Promise that resolves when the content has finished loading.

protected replacement: HTMLImageElement | null = null

Placeholder element that replaces the original thumbnail in the DOM.

protected originalParent: HTMLElement | null = null

Reference to the original parent element of the thumbnail.

protected originalNextSibling: ChildNode | null = null

Reference to the next sibling of the original thumbnail for proper reinsertion.

protected originalStyle = ''

Original CSS text of the thumbnail element.

protected thumbImage: HTMLImageElement | null = null

Reference to the original thumbnail image element.

protected originRect: { width: number; height: number; top: number; left: number }

Bounding rectangle of the origin element.

protected fittedSize: { width: number; height: number } | null = null

Calculated fitted size of the thumbnail image.

protected maxZoomLevel: number

Maximum zoom level allowed for this content.

protected vistaView: VistaView

Reference to the parent VistaView instance.

protected transitionState: VistaHiresTransitionOpt | null = null

State object for high-resolution transitions.

protected transitionShouldWait: () => boolean = () => false

Function that determines if transition should wait before starting.

protected initPointerCenter = { x: 0, y: 0 }

Initial center point for pointer interactions.

protected onScale: (par: {
vistaImage: VistaBox;
scale: number;
isMax: boolean;
isMin: boolean;
}) => void

Callback function invoked when content is scaled.

The following protected methods are available when extending VistaBox:

protected createState(): VistaImageState

Creates and returns a new state object with getters/setters that trigger DOM updates. Called in the constructor.

protected onLessThanMinWidthChange(value: boolean): void

Called when content width falls below minimum. Sets opacity to 0.5 when true.

protected onTranslateChange(value: { x: number; y: number }): void

Updates the CSS translate property when state.translate changes.

protected onTransformChange(value: { x: number; y: number; scale: number }): void

Updates the CSS transform property when state.transform changes.

protected onWidthChange(value: number): void

Updates the CSS width property when state.width changes.

protected onHeightChange(value: number): void

Updates the CSS height property when state.height changes.

protected getFullSizeDim(): { width: number; height: number }

Calculates the full size dimensions based on thumbnail aspect ratio and viewport size.

Returns: Object containing calculated width and height.

protected normalize(): void

Resets content to its default state with no zoom or translation. Sets transform and translate to zero, and dimensions to full size.

protected getFromParsedSrcSet(targetWidth: number): string | null

Selects the most appropriate source from the parsed srcSet based on target width and device pixel ratio.

Parameters:

  • targetWidth - The desired width in pixels

Returns: The selected source URL, or null if no srcSet is available.

setFinalTransform(par?: { propagateEvent?: boolean }): { close: boolean; cancel: () => void }

Finalizes the current transform state by converting temporary transforms into permanent translate and size values. Called at the end of pan/zoom operations.

Parameters:

  • par.propagateEvent - Whether to trigger onContentChange events (default: true)

Returns: Object with close flag and cancel function.

GitHubnpmllms.txtContext7

© 2026 • MIT License