yet-another-react-lightbox 1.8.0 → 1.8.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.
@@ -3,7 +3,7 @@ import { adjustDevicePixelRatio, clsx, cssClass, hasWindow } from "../utils.js";
3
3
  import { useLatest } from "../hooks/index.js";
4
4
  import { useEvents } from "../contexts/index.js";
5
5
  import { ErrorIcon, LoadingIcon } from "./Icons.js";
6
- import { activeSlideStatus, SLIDE_STATUS_COMPLETE, SLIDE_STATUS_ERROR, SLIDE_STATUS_LOADING } from "../consts.js";
6
+ import { activeSlideStatus, SLIDE_STATUS_COMPLETE, SLIDE_STATUS_ERROR, SLIDE_STATUS_LOADING, } from "../consts.js";
7
7
  export const ImageSlide = ({ slide: image, offset, render, rect, imageFit }) => {
8
8
  var _a;
9
9
  const [status, setStatus] = React.useState(SLIDE_STATUS_LOADING);
@@ -1,4 +1,6 @@
1
1
  export * from "./useContainerRect.js";
2
2
  export * from "./useEnhancedEffect.js";
3
3
  export * from "./useLatest.js";
4
+ export * from "./useMotionPreference.js";
5
+ export * from "./useRTL.js";
4
6
  export * from "./useSensors.js";
@@ -1,4 +1,6 @@
1
1
  export * from "./useContainerRect.js";
2
2
  export * from "./useEnhancedEffect.js";
3
3
  export * from "./useLatest.js";
4
+ export * from "./useMotionPreference.js";
5
+ export * from "./useRTL.js";
4
6
  export * from "./useSensors.js";
@@ -0,0 +1 @@
1
+ export declare const useMotionPreference: () => boolean;
@@ -0,0 +1,12 @@
1
+ import * as React from "react";
2
+ import { useEnhancedEffect } from "./useEnhancedEffect.js";
3
+ export const useMotionPreference = () => {
4
+ const [reduceMotion, setReduceMotion] = React.useState(false);
5
+ useEnhancedEffect(() => {
6
+ var _a;
7
+ const mediaQuery = (_a = window.matchMedia) === null || _a === void 0 ? void 0 : _a.call(window, "(prefers-reduced-motion: reduce)");
8
+ mediaQuery === null || mediaQuery === void 0 ? void 0 : mediaQuery.addEventListener("change", () => setReduceMotion(mediaQuery.matches));
9
+ setReduceMotion(mediaQuery === null || mediaQuery === void 0 ? void 0 : mediaQuery.matches);
10
+ }, []);
11
+ return reduceMotion;
12
+ };
@@ -0,0 +1 @@
1
+ export declare const useRTL: () => boolean;
@@ -0,0 +1,9 @@
1
+ import * as React from "react";
2
+ import { useEnhancedEffect } from "./useEnhancedEffect.js";
3
+ export const useRTL = () => {
4
+ const [isRTL, setIsRTL] = React.useState(false);
5
+ useEnhancedEffect(() => {
6
+ setIsRTL(window.getComputedStyle(window.document.documentElement).direction === "rtl");
7
+ }, []);
8
+ return isRTL;
9
+ };
@@ -4,7 +4,6 @@ import { SubscribeSensors } from "../hooks/index.js";
4
4
  declare type ControllerState = {
5
5
  currentIndex: number;
6
6
  globalIndex: number;
7
- isRTL: boolean;
8
7
  };
9
8
  export declare type ControllerContextType = ControllerState & {
10
9
  latestProps: React.MutableRefObject<ComponentProps>;
@@ -1,8 +1,8 @@
1
1
  import * as React from "react";
2
2
  import { LightboxDefaultProps } from "../../types.js";
3
- import { cleanup, clsx, cssClass, cssVar, isRTL, makeUseContext } from "../utils.js";
3
+ import { cleanup, clsx, cssClass, cssVar, makeUseContext } from "../utils.js";
4
4
  import { createModule } from "../config.js";
5
- import { useContainerRect, useEnhancedEffect, useLatest, useSensors } from "../hooks/index.js";
5
+ import { useContainerRect, useEnhancedEffect, useLatest, useRTL, useSensors, } from "../hooks/index.js";
6
6
  import { useEvents, useTimeouts } from "../contexts/index.js";
7
7
  const SWIPE_OFFSET_THRESHOLD = 30;
8
8
  const ControllerContext = React.createContext(null);
@@ -12,10 +12,10 @@ export const Controller = ({ children, ...props }) => {
12
12
  const { registerSensors, subscribeSensors } = useSensors();
13
13
  const { subscribe, publish } = useEvents();
14
14
  const { setTimeout, clearTimeout } = useTimeouts();
15
+ const isRTL = useLatest(useRTL());
15
16
  const [state, setState] = React.useState({
16
17
  currentIndex: props.index,
17
18
  globalIndex: props.index,
18
- isRTL: false,
19
19
  });
20
20
  const latestProps = useLatest(props);
21
21
  const refs = React.useRef({
@@ -45,12 +45,6 @@ export const Controller = ({ children, ...props }) => {
45
45
  }
46
46
  };
47
47
  }, [containerRef]);
48
- useEnhancedEffect(() => {
49
- const node = containerRef.current;
50
- if (node) {
51
- setState((prev) => ({ ...prev, isRTL: isRTL(node) }));
52
- }
53
- }, [containerRef]);
54
48
  React.useEffect(() => {
55
49
  var _a;
56
50
  if (refs.current.props.controller.focus) {
@@ -87,7 +81,7 @@ export const Controller = ({ children, ...props }) => {
87
81
  clearTimeout(current.swipeIntentCleanup);
88
82
  current.swipeIntentCleanup = undefined;
89
83
  }, [clearTimeout]);
90
- const rtl = React.useCallback((value) => (refs.current.state.isRTL ? -1 : 1) * (typeof value === "number" ? value : 1), [refs]);
84
+ const rtl = React.useCallback((value) => (isRTL.current ? -1 : 1) * (typeof value === "number" ? value : 1), [isRTL]);
91
85
  const isSwipeValid = React.useCallback((offset) => {
92
86
  const { state: { currentIndex }, props: { carousel, slides }, } = refs.current;
93
87
  return !(carousel.finite &&
@@ -279,9 +273,8 @@ export const Controller = ({ children, ...props }) => {
279
273
  latestProps,
280
274
  currentIndex: state.currentIndex,
281
275
  globalIndex: state.globalIndex,
282
- isRTL: state.isRTL,
283
276
  subscribeSensors,
284
- }), [latestProps, state.currentIndex, state.globalIndex, state.isRTL, subscribeSensors]);
277
+ }), [latestProps, state.currentIndex, state.globalIndex, subscribeSensors]);
285
278
  return (React.createElement("div", { ref: setContainerRef, className: clsx(cssClass("container"), cssClass("fullsize"), refs.current.swipeState === "swipe" && cssClass("container_swipe")), style: {
286
279
  ...(refs.current.swipeAnimationDuration !== LightboxDefaultProps.animation.swipe
287
280
  ? {
@@ -4,18 +4,20 @@ import { cssClass, label as translateLabel } from "../utils.js";
4
4
  import { IconButton, NextIcon, PreviousIcon } from "../components/index.js";
5
5
  import { useEvents } from "../contexts/index.js";
6
6
  import { useController } from "./Controller.js";
7
+ import { useLatest, useRTL } from "../hooks/index.js";
7
8
  export const NavigationButton = ({ publish, labels, label, icon, renderIcon, action, disabled, }) => (React.createElement(IconButton, { label: translateLabel(labels, label), icon: icon, renderIcon: renderIcon, className: cssClass(`navigation_${action}`), disabled: disabled, "aria-disabled": disabled, onClick: () => {
8
9
  publish(action);
9
10
  } }));
10
11
  export const Navigation = ({ slides, carousel: { finite }, labels, render: { buttonPrev, buttonNext, iconPrev, iconNext }, }) => {
11
- const { currentIndex, subscribeSensors, isRTL } = useController();
12
+ const { currentIndex, subscribeSensors } = useController();
12
13
  const { publish } = useEvents();
14
+ const isRTL = useLatest(useRTL());
13
15
  React.useEffect(() => subscribeSensors("onKeyUp", (event) => {
14
16
  if (event.code === "ArrowLeft") {
15
- publish(isRTL ? "next" : "prev");
17
+ publish(isRTL.current ? "next" : "prev");
16
18
  }
17
19
  else if (event.code === "ArrowRight") {
18
- publish(isRTL ? "prev" : "next");
20
+ publish(isRTL.current ? "prev" : "next");
19
21
  }
20
22
  }), [subscribeSensors, publish, isRTL]);
21
23
  return (React.createElement(React.Fragment, null,
@@ -8,4 +8,3 @@ export declare const cleanup: (...cleaners: (() => void)[]) => () => void;
8
8
  export declare const makeUseContext: <T>(name: string, contextName: string, context: React.Context<T | null>) => () => T;
9
9
  export declare const hasWindow: () => boolean;
10
10
  export declare const adjustDevicePixelRatio: (value: number) => number;
11
- export declare const isRTL: (node: HTMLElement) => boolean;
@@ -18,4 +18,3 @@ export const makeUseContext = (name, contextName, context) => () => {
18
18
  };
19
19
  export const hasWindow = () => typeof window !== "undefined";
20
20
  export const adjustDevicePixelRatio = (value) => hasWindow() ? Math.round(value / (window.devicePixelRatio || 1)) : value;
21
- export const isRTL = (node) => window.getComputedStyle(node).direction === "rtl";
@@ -1,7 +1,7 @@
1
1
  import * as React from "react";
2
2
  import { Component, LightboxProps, Plugin } from "../types.js";
3
3
  import { ContainerRect } from "../core/index.js";
4
- declare type Position = "top" | "bottom" | "start" | "end";
4
+ export declare type Position = "top" | "bottom" | "start" | "end";
5
5
  declare module "../types.js" {
6
6
  interface LightboxProps {
7
7
  /** Thumbnails plugin settings */
@@ -20,12 +20,16 @@ declare module "../types.js" {
20
20
  padding?: number;
21
21
  /** gap between thumbnails */
22
22
  gap?: number;
23
+ /** `object-fit` setting */
24
+ imageFit?: ImageFit;
23
25
  };
24
26
  }
25
27
  interface Render {
26
- thumbnail: ({ slide, rect }: {
28
+ thumbnail?: ({ slide, rect, render, imageFit, }: {
27
29
  slide: Slide;
28
30
  rect: ContainerRect;
31
+ render: Render;
32
+ imageFit: ImageFit;
29
33
  }) => React.ReactNode;
30
34
  }
31
35
  }
@@ -1,5 +1,5 @@
1
1
  import * as React from "react";
2
- import { clsx, createIcon, createModule, cssClass, cssVar, ImageSlide, isRTL, useEnhancedEffect, useEvents, } from "../core/index.js";
2
+ import { clsx, createIcon, createModule, cssClass, cssVar, ImageSlide, useEnhancedEffect, useEvents, useLatest, useMotionPreference, useRTL, } from "../core/index.js";
3
3
  const defaultThumbnailsProps = {
4
4
  position: "bottom",
5
5
  width: 120,
@@ -8,6 +8,7 @@ const defaultThumbnailsProps = {
8
8
  borderRadius: 4,
9
9
  padding: 4,
10
10
  gap: 16,
11
+ imageFit: "contain",
11
12
  };
12
13
  const VideoThumbnailIcon = createIcon("VideoThumbnail", React.createElement("path", { d: "M10 16.5l6-4.5-6-4.5v9zM12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm0 18c-4.41 0-8-3.59-8-8s3.59-8 8-8 8 3.59 8 8-3.59 8-8 8z" }));
13
14
  const UnknownThumbnailIcon = createIcon("UnknownThumbnail", React.createElement("path", { d: "M23 18V6c0-1.1-.9-2-2-2H3c-1.1 0-2 .9-2 2v12c0 1.1.9 2 2 2h18c1.1 0 2-.9 2-2zM8.5 12.5l2.5 3.01L14.5 11l4.5 6H5l3.5-4.5z" }));
@@ -16,9 +17,9 @@ const cssThumbnailPrefix = (value) => cssPrefix(`thumbnail${value ? `_${value}`
16
17
  const getSlide = (slides, index) => slides[((index % slides.length) + slides.length) % slides.length];
17
18
  const isHorizontal = (position) => ["top", "bottom"].includes(position);
18
19
  const boxSize = (thumbnails, dimension, includeGap) => dimension + 2 * (thumbnails.border + thumbnails.padding) + (includeGap ? thumbnails.gap : 0);
19
- const renderThumbnail = ({ slide, render, rect }) => {
20
+ const renderThumbnail = ({ slide, render, rect, imageFit }) => {
20
21
  var _a;
21
- const customThumbnail = (_a = render.thumbnail) === null || _a === void 0 ? void 0 : _a.call(render, { slide, rect });
22
+ const customThumbnail = (_a = render.thumbnail) === null || _a === void 0 ? void 0 : _a.call(render, { slide, render, rect, imageFit });
22
23
  if (customThumbnail) {
23
24
  return customThumbnail;
24
25
  }
@@ -31,11 +32,11 @@ const renderThumbnail = ({ slide, render, rect }) => {
31
32
  }
32
33
  }
33
34
  else if ("src" in slide) {
34
- return React.createElement(ImageSlide, { slide: slide, rect: rect });
35
+ return React.createElement(ImageSlide, { slide: slide, render: render, rect: rect, imageFit: imageFit });
35
36
  }
36
37
  return React.createElement(UnknownThumbnailIcon, { className: thumbnailIconClass });
37
38
  };
38
- const Thumbnail = ({ rect, slide, onClick, active, fadeIn, fadeOut, placeholder, render, }) => (React.createElement("button", { type: "button", className: clsx(cssClass(cssThumbnailPrefix()), active && cssClass(cssThumbnailPrefix("active")), fadeIn && cssClass(cssThumbnailPrefix("fadein")), fadeOut && cssClass(cssThumbnailPrefix("fadeout")), placeholder && cssClass(cssThumbnailPrefix("placeholder"))), style: {
39
+ const Thumbnail = ({ rect, slide, onClick, active, fadeIn, fadeOut, placeholder, render, imageFit, }) => (React.createElement("button", { type: "button", className: clsx(cssClass(cssThumbnailPrefix()), active && cssClass(cssThumbnailPrefix("active")), fadeIn && cssClass(cssThumbnailPrefix("fadein")), fadeOut && cssClass(cssThumbnailPrefix("fadeout")), placeholder && cssClass(cssThumbnailPrefix("placeholder"))), style: {
39
40
  ...(fadeIn
40
41
  ? {
41
42
  [cssVar(cssThumbnailPrefix("fadein_duration"))]: `${fadeIn.duration}ms`,
@@ -48,13 +49,16 @@ const Thumbnail = ({ rect, slide, onClick, active, fadeIn, fadeOut, placeholder,
48
49
  [cssVar(cssThumbnailPrefix("fadeout_delay"))]: `${fadeOut.delay}ms`,
49
50
  }
50
51
  : null),
51
- }, onClick: onClick }, slide && renderThumbnail({ slide, render, rect })));
52
+ }, onClick: onClick }, slide && renderThumbnail({ slide, render, rect, imageFit })));
52
53
  export const ThumbnailsTrack = ({ container, startingIndex, slides, carousel, animation, render, thumbnails, thumbnailRect, }) => {
53
54
  const track = React.useRef(null);
54
55
  const [state, setState] = React.useState({
55
56
  index: startingIndex,
56
57
  offset: 0,
57
58
  });
59
+ const { publish, subscribe } = useEvents();
60
+ const reduceMotion = useLatest(useMotionPreference());
61
+ const isRTL = useLatest(useRTL());
58
62
  const refs = React.useRef({
59
63
  state,
60
64
  thumbnails,
@@ -67,12 +71,6 @@ export const ThumbnailsTrack = ({ container, startingIndex, slides, carousel, an
67
71
  refs.current.carousel = carousel;
68
72
  refs.current.animation = animation;
69
73
  const animationRef = React.useRef();
70
- const { publish, subscribe } = useEvents();
71
- React.useEffect(() => {
72
- if (track.current) {
73
- refs.current.isRTL = isRTL(track.current);
74
- }
75
- }, []);
76
74
  React.useEffect(() => subscribe("controller-swipe", (_, event) => {
77
75
  if (event && typeof event === "object" && "globalIndex" in event) {
78
76
  const { current } = refs;
@@ -106,7 +104,7 @@ export const ThumbnailsTrack = ({ container, startingIndex, slides, carousel, an
106
104
  animationRef.current = (_d = (_c = track.current).animate) === null || _d === void 0 ? void 0 : _d.call(_c, isHorizontal(current.thumbnails.position)
107
105
  ? [
108
106
  {
109
- transform: `translate3d(${(current.isRTL ? -1 : 1) *
107
+ transform: `translate3d(${(isRTL.current ? -1 : 1) *
110
108
  boxSize(current.thumbnails, current.thumbnails.width, true) *
111
109
  state.offset +
112
110
  current.animationOffset}px, 0, 0)`,
@@ -119,7 +117,7 @@ export const ThumbnailsTrack = ({ container, startingIndex, slides, carousel, an
119
117
  current.animationOffset}px, 0)`,
120
118
  },
121
119
  { transform: "translate3d(0, 0, 0)" },
122
- ], animationDuration);
120
+ ], reduceMotion.current ? 0 : animationDuration);
123
121
  if (animationRef.current) {
124
122
  animationRef.current.onfinish = () => {
125
123
  animationRef.current = undefined;
@@ -130,7 +128,7 @@ export const ThumbnailsTrack = ({ container, startingIndex, slides, carousel, an
130
128
  }
131
129
  current.animationOffset = 0;
132
130
  }
133
- }, [state.index, state.offset]);
131
+ }, [state.index, state.offset, isRTL, reduceMotion]);
134
132
  const { index, offset } = state;
135
133
  const { finite, preload } = carousel;
136
134
  const items = [];
@@ -171,25 +169,24 @@ export const ThumbnailsTrack = ({ container, startingIndex, slides, carousel, an
171
169
  publish("prev", index - slideIndex);
172
170
  }
173
171
  };
172
+ const { width, height, border, borderRadius, padding, gap, imageFit } = thumbnails;
174
173
  return (React.createElement("div", { className: clsx(cssClass(cssPrefix("container")), cssClass("flex_center")), style: {
175
- ...(thumbnails.width !== defaultThumbnailsProps.width
176
- ? { [cssVar(cssThumbnailPrefix("width"))]: `${boxSize(thumbnails, thumbnails.width)}px` }
177
- : null),
178
- ...(thumbnails.height !== defaultThumbnailsProps.height
179
- ? { [cssVar(cssThumbnailPrefix("height"))]: `${boxSize(thumbnails, thumbnails.height)}px` }
174
+ ...(width !== defaultThumbnailsProps.width
175
+ ? { [cssVar(cssThumbnailPrefix("width"))]: `${boxSize(thumbnails, width)}px` }
180
176
  : null),
181
- ...(thumbnails.border !== defaultThumbnailsProps.border
182
- ? { [cssVar(cssThumbnailPrefix("border"))]: `${thumbnails.border}px` }
177
+ ...(height !== defaultThumbnailsProps.height
178
+ ? { [cssVar(cssThumbnailPrefix("height"))]: `${boxSize(thumbnails, height)}px` }
183
179
  : null),
184
- ...(thumbnails.borderRadius !== defaultThumbnailsProps.borderRadius
185
- ? { [cssVar(cssThumbnailPrefix("border_radius"))]: `${thumbnails.borderRadius}px` }
180
+ ...(border !== defaultThumbnailsProps.border
181
+ ? { [cssVar(cssThumbnailPrefix("border"))]: `${border}px` }
186
182
  : null),
187
- ...(thumbnails.padding !== defaultThumbnailsProps.padding
188
- ? { [cssVar(cssThumbnailPrefix("padding"))]: `${thumbnails.padding}px` }
183
+ ...(borderRadius !== defaultThumbnailsProps.borderRadius
184
+ ? { [cssVar(cssThumbnailPrefix("border_radius"))]: `${borderRadius}px` }
189
185
  : null),
190
- ...(thumbnails.gap !== defaultThumbnailsProps.gap
191
- ? { [cssVar(cssThumbnailPrefix("gap"))]: `${thumbnails.gap}px` }
186
+ ...(padding !== defaultThumbnailsProps.padding
187
+ ? { [cssVar(cssThumbnailPrefix("padding"))]: `${padding}px` }
192
188
  : null),
189
+ ...(gap !== defaultThumbnailsProps.gap ? { [cssVar(cssThumbnailPrefix("gap"))]: `${gap}px` } : null),
193
190
  } },
194
191
  React.createElement("nav", { ref: track, className: cssClass(cssPrefix("track")) }, items.map(({ slide, index: slideIndex, placeholder }) => {
195
192
  var _a;
@@ -213,7 +210,7 @@ export const ThumbnailsTrack = ({ container, startingIndex, slides, carousel, an
213
210
  : -offset - (slideIndex - (index + preload))) * fadeAnimationDuration,
214
211
  }
215
212
  : undefined;
216
- return (React.createElement(Thumbnail, { key: slideIndex, rect: thumbnailRect, slide: slide, render: render, active: slideIndex === index, fadeIn: fadeIn, fadeOut: fadeOut, placeholder: Boolean(placeholder), onClick: handleClick(slideIndex) }));
213
+ return (React.createElement(Thumbnail, { key: slideIndex, rect: thumbnailRect, slide: slide, imageFit: imageFit, render: render, active: slideIndex === index, fadeIn: fadeIn, fadeOut: fadeOut, placeholder: Boolean(placeholder), onClick: handleClick(slideIndex) }));
217
214
  }))));
218
215
  };
219
216
  export const ThumbnailsComponent = ({ thumbnails: originalThumbnails, slides, index, carousel, animation, render, children, }) => {
package/dist/styles.css CHANGED
@@ -144,7 +144,6 @@
144
144
  display: flex;
145
145
  justify-content: flex-end;
146
146
  padding: 8px;
147
- gap: 8px;
148
147
  }
149
148
  [dir=rtl] .yarl__toolbar {
150
149
  inset: 0 auto auto 0;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "yet-another-react-lightbox",
3
- "version": "1.8.0",
3
+ "version": "1.8.1",
4
4
  "description": "Modern React lightbox component",
5
5
  "author": "Igor Danchenko",
6
6
  "license": "MIT",