@reuse-sh/lightbox 0.0.0-stage → 0.0.2
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 +21 -0
- package/README.md +62 -2
- package/dist/geometry.d.ts +21 -0
- package/dist/geometry.js +38 -0
- package/dist/index.d.ts +75 -0
- package/dist/index.js +1051 -0
- package/dist/react.d.ts +9 -0
- package/dist/react.js +52 -0
- package/dist/styles.css +218 -0
- package/package.json +61 -3
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,63 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @reuse-sh/lightbox
|
|
2
2
|
|
|
3
|
-
|
|
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
|
+
<!-- principles:start -->
|
|
6
|
+
|
|
7
|
+
## Principles
|
|
8
|
+
|
|
9
|
+
These are the rules the code keeps. An adaptation on any framework keeps them too.
|
|
10
|
+
|
|
11
|
+
- **Page with native scroll snap.** Pages are a scroll-snap strip from the carousel, so touch and trackpads keep the system's momentum.
|
|
12
|
+
- **Open from the thumbnail and return to it.** The image grows out of its cropped preview and goes back to the same place on close.
|
|
13
|
+
- **Load only what is near.** Only the current and neighboring images load, and a long jump skips the pages in between.
|
|
14
|
+
- **Zoom around the point you touch.** Pinch, wheel and double-tap zoom around the pointer or fingers, and panning stops at the image's edges.
|
|
15
|
+
- **Keep focus inside and give it back.** The viewer is a native modal dialog, and closing it returns focus to the element that opened it.
|
|
16
|
+
- **Leave the page as it was.** Destroy restores the page scroll, the hidden thumbnail, and every style and listener the viewer touched.
|
|
17
|
+
|
|
18
|
+
<!-- principles:end -->
|
|
19
|
+
|
|
20
|
+
## Install
|
|
21
|
+
|
|
22
|
+
```sh
|
|
23
|
+
npm install @reuse-sh/lightbox
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## Browser
|
|
27
|
+
|
|
28
|
+
```js
|
|
29
|
+
import { createLightbox } from "@reuse-sh/lightbox";
|
|
30
|
+
import "@reuse-sh/lightbox/styles.css";
|
|
31
|
+
|
|
32
|
+
const viewer = createLightbox({ thumbnails: true });
|
|
33
|
+
viewer.open([{
|
|
34
|
+
src: "/photo.jpg",
|
|
35
|
+
alt: "A mountain lake",
|
|
36
|
+
width: 1600,
|
|
37
|
+
height: 1200,
|
|
38
|
+
origin: document.querySelector("img"),
|
|
39
|
+
}]);
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
## React
|
|
43
|
+
|
|
44
|
+
```tsx
|
|
45
|
+
import { useLightbox } from "@reuse-sh/lightbox/react";
|
|
46
|
+
import "@reuse-sh/lightbox/styles.css";
|
|
47
|
+
|
|
48
|
+
function Gallery({ images }) {
|
|
49
|
+
const viewer = useLightbox();
|
|
50
|
+
return <button onClick={() => viewer.open(images)}>
|
|
51
|
+
Open gallery
|
|
52
|
+
</button>;
|
|
53
|
+
}
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
## Documentation
|
|
57
|
+
|
|
58
|
+
- Examples and API: https://reuse.sh/lightbox
|
|
59
|
+
- Guide for coding agents: https://reuse.sh/lightbox/guide.md
|
|
60
|
+
|
|
61
|
+
## License
|
|
62
|
+
|
|
63
|
+
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;
|
package/dist/geometry.js
ADDED
|
@@ -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
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -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
|
+
};
|