@reuse-sh/lightbox 0.0.0-stage → 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Nikita
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,48 @@
1
- # Temporary Holding Version
1
+ # @reuse-sh/lightbox
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ An image viewer that opens from a thumbnail, including a cropped one, and returns to it on close. Pages scroll and swipe with the browser, and images zoom with pinch, pan, double-tap, and the pointer.
4
+
5
+ ## Install
6
+
7
+ ```sh
8
+ npm install @reuse-sh/lightbox
9
+ ```
10
+
11
+ ## Browser
12
+
13
+ ```js
14
+ import { createLightbox } from "@reuse-sh/lightbox";
15
+ import "@reuse-sh/lightbox/styles.css";
16
+
17
+ const viewer = createLightbox({ thumbnails: true });
18
+ viewer.open([{
19
+ src: "/photo.jpg",
20
+ alt: "A mountain lake",
21
+ width: 1600,
22
+ height: 1200,
23
+ origin: document.querySelector("img"),
24
+ }]);
25
+ ```
26
+
27
+ ## React
28
+
29
+ ```tsx
30
+ import { useLightbox } from "@reuse-sh/lightbox/react";
31
+ import "@reuse-sh/lightbox/styles.css";
32
+
33
+ function Gallery({ images }) {
34
+ const viewer = useLightbox();
35
+ return <button onClick={() => viewer.open(images)}>
36
+ Open gallery
37
+ </button>;
38
+ }
39
+ ```
40
+
41
+ ## Documentation
42
+
43
+ - Examples and API: https://reuse.sh/lightbox
44
+ - Guide for coding agents: https://reuse.sh/lightbox/guide.md
45
+
46
+ ## License
47
+
48
+ MIT
@@ -0,0 +1,21 @@
1
+ export interface Point {
2
+ x: number;
3
+ y: number;
4
+ }
5
+ export interface Zoom extends Point {
6
+ scale: number;
7
+ }
8
+ export interface Size {
9
+ width: number;
10
+ height: number;
11
+ }
12
+ export interface Rect extends Size {
13
+ left: number;
14
+ top: number;
15
+ }
16
+ export declare const identity: Zoom;
17
+ export declare function clamp(value: number, min: number, max: number): number;
18
+ export declare function contain(image: Size, viewport: Size, padding?: number): Size;
19
+ export declare function zoomAround(from: Zoom, scale: number, focus: Point): Zoom;
20
+ export declare function boundZoom(zoom: Zoom, content: Size, viewport: Size): Zoom;
21
+ export declare function cover(image: Size, frame: Size, position?: Point): Rect;
@@ -0,0 +1,38 @@
1
+ export const identity = { scale: 1, x: 0, y: 0 };
2
+ export function clamp(value, min, max) {
3
+ return Math.max(min, Math.min(max, value));
4
+ }
5
+ export function contain(image, viewport, padding = 32) {
6
+ const width = Math.max(1, viewport.width - padding * 2);
7
+ const height = Math.max(1, viewport.height - padding * 2);
8
+ const scale = Math.min(width / image.width, height / image.height, 1);
9
+ return { width: image.width * scale, height: image.height * scale };
10
+ }
11
+ export function zoomAround(from, scale, focus) {
12
+ const ratio = scale / from.scale;
13
+ return {
14
+ scale,
15
+ x: focus.x - ratio * (focus.x - from.x),
16
+ y: focus.y - ratio * (focus.y - from.y),
17
+ };
18
+ }
19
+ export function boundZoom(zoom, content, viewport) {
20
+ const x = Math.max(0, (content.width * zoom.scale - viewport.width) / 2);
21
+ const y = Math.max(0, (content.height * zoom.scale - viewport.height) / 2);
22
+ return {
23
+ scale: zoom.scale,
24
+ x: clamp(zoom.x, -x, x),
25
+ y: clamp(zoom.y, -y, y),
26
+ };
27
+ }
28
+ export function cover(image, frame, position = { x: 0.5, y: 0.5 }) {
29
+ const scale = Math.max(frame.width / image.width, frame.height / image.height);
30
+ const width = image.width * scale;
31
+ const height = image.height * scale;
32
+ return {
33
+ width,
34
+ height,
35
+ left: (frame.width - width) * position.x,
36
+ top: (frame.height - height) * position.y,
37
+ };
38
+ }
@@ -0,0 +1,75 @@
1
+ import { type Point, type Zoom } from "./geometry.js";
2
+ export type { Point, Rect, Size, Zoom } from "./geometry.js";
3
+ export interface LightboxItem {
4
+ src: string;
5
+ previewSrc?: string;
6
+ width?: number;
7
+ height?: number;
8
+ alt?: string;
9
+ caption?: string;
10
+ thumbnailSrc?: string;
11
+ origin?: HTMLElement | null;
12
+ }
13
+ export interface LightboxState {
14
+ isOpen: boolean;
15
+ isClosing: boolean;
16
+ index: number;
17
+ count: number;
18
+ zoom: Zoom;
19
+ }
20
+ export interface LightboxContentContext {
21
+ item: LightboxItem;
22
+ index: number;
23
+ container: HTMLElement;
24
+ image: HTMLImageElement;
25
+ }
26
+ export interface LightboxOptions {
27
+ container?: HTMLElement;
28
+ label?: string;
29
+ imageClick?: "zoom" | "close" | "none";
30
+ doubleTapZoom?: boolean;
31
+ maxZoom?: number;
32
+ tapZoom?: number;
33
+ preload?: number;
34
+ thumbnails?: boolean;
35
+ padding?: number;
36
+ duration?: number;
37
+ className?: string;
38
+ icons?: Partial<Record<"close" | "prev" | "next" | "zoom", () => Node>>;
39
+ renderContent?: (context: LightboxContentContext) => undefined | (() => void);
40
+ /** Reports discrete actions, completed pointer gestures, and wheel input after 150ms idle. */
41
+ onChange?: (state: LightboxState) => void;
42
+ }
43
+ export interface Lightbox {
44
+ open(items: readonly LightboxItem[], index?: number): void;
45
+ close(): Promise<void>;
46
+ next(): void;
47
+ prev(): void;
48
+ goTo(index: number): void;
49
+ zoomTo(scale: number, focus?: Point): void;
50
+ toggleZoom(focus?: Point): void;
51
+ /** Returns live geometry, including pointer and wheel samples before notification. */
52
+ getState(): LightboxState;
53
+ /** Subscribes to the settled changes reported by onChange. */
54
+ subscribe(listener: (state: LightboxState) => void): () => void;
55
+ destroy(): void;
56
+ }
57
+ export declare function createLightbox(options?: LightboxOptions): Lightbox;
58
+ export declare function lightboxItemFromImage(image: HTMLImageElement, source?: string): LightboxItem;
59
+ export declare function bindLightboxGallery(root: HTMLElement, options?: LightboxOptions & {
60
+ selector?: string;
61
+ getItem?: (image: HTMLImageElement) => LightboxItem;
62
+ }): {
63
+ open(items: readonly LightboxItem[], index?: number): void;
64
+ close(): Promise<void>;
65
+ next(): void;
66
+ prev(): void;
67
+ goTo(index: number): void;
68
+ zoomTo(scale: number, focus?: Point): void;
69
+ toggleZoom(focus?: Point): void;
70
+ /** Returns live geometry, including pointer and wheel samples before notification. */
71
+ getState(): LightboxState;
72
+ /** Subscribes to the settled changes reported by onChange. */
73
+ subscribe(listener: (state: LightboxState) => void): () => void;
74
+ destroy: () => void;
75
+ };