yet-another-react-lightbox 1.13.3 → 2.0.0-rc.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.
Files changed (121) hide show
  1. package/dist/Lightbox.js +1 -1
  2. package/dist/core/components/IconButton.d.ts +1 -1
  3. package/dist/core/components/IconButton.js +3 -2
  4. package/dist/core/components/ImageSlide.js +20 -28
  5. package/dist/core/consts.d.ts +36 -0
  6. package/dist/core/consts.js +36 -0
  7. package/dist/core/contexts/Events.d.ts +5 -9
  8. package/dist/core/contexts/Events.js +23 -25
  9. package/dist/core/contexts/LightboxState.d.ts +20 -0
  10. package/dist/core/contexts/LightboxState.js +23 -0
  11. package/dist/core/contexts/Timeouts.d.ts +1 -2
  12. package/dist/core/contexts/Timeouts.js +26 -29
  13. package/dist/core/contexts/index.d.ts +1 -0
  14. package/dist/core/contexts/index.js +1 -0
  15. package/dist/core/hooks/index.d.ts +2 -1
  16. package/dist/core/hooks/index.js +2 -1
  17. package/dist/core/hooks/useEventCallback.d.ts +1 -0
  18. package/dist/core/hooks/useEventCallback.js +9 -0
  19. package/dist/core/hooks/useForkRef.d.ts +3 -0
  20. package/dist/core/hooks/useForkRef.js +15 -0
  21. package/dist/core/hooks/useMotionPreference.js +1 -2
  22. package/dist/core/hooks/useSensors.d.ts +4 -5
  23. package/dist/core/hooks/useSensors.js +9 -12
  24. package/dist/core/modules/Carousel.js +29 -31
  25. package/dist/core/modules/Controller.d.ts +3 -7
  26. package/dist/core/modules/Controller.js +144 -263
  27. package/dist/core/modules/Core.js +6 -4
  28. package/dist/core/modules/Navigation.js +16 -13
  29. package/dist/core/modules/NoScroll.js +4 -3
  30. package/dist/core/modules/Portal.js +51 -40
  31. package/dist/core/modules/Toolbar.js +6 -4
  32. package/dist/core/modules/controller/index.d.ts +8 -0
  33. package/dist/core/modules/controller/index.js +9 -0
  34. package/dist/core/modules/controller/useOffset.d.ts +2 -0
  35. package/dist/core/modules/controller/useOffset.js +10 -0
  36. package/dist/core/modules/controller/usePointerSwipe.d.ts +3 -0
  37. package/dist/core/modules/controller/usePointerSwipe.js +61 -0
  38. package/dist/core/modules/controller/usePreventSwipeNavigation.d.ts +3 -0
  39. package/dist/core/modules/controller/usePreventSwipeNavigation.js +20 -0
  40. package/dist/core/modules/controller/useWheelSwipe.d.ts +3 -0
  41. package/dist/core/modules/controller/useWheelSwipe.js +94 -0
  42. package/dist/core/utils.d.ts +12 -1
  43. package/dist/core/utils.js +14 -0
  44. package/dist/plugins/captions/Captions.d.ts +7 -0
  45. package/dist/plugins/captions/Captions.js +23 -0
  46. package/dist/plugins/captions/CaptionsContext.d.ts +8 -0
  47. package/dist/plugins/captions/CaptionsContext.js +15 -0
  48. package/dist/plugins/captions/Description.d.ts +5 -0
  49. package/dist/plugins/captions/Description.js +17 -0
  50. package/dist/plugins/captions/Title.d.ts +5 -0
  51. package/dist/plugins/captions/Title.js +9 -0
  52. package/dist/plugins/{captions.css → captions/captions.css} +0 -2
  53. package/dist/plugins/{Captions.d.ts → captions/index.d.ts} +3 -9
  54. package/dist/plugins/captions/index.js +2 -0
  55. package/dist/plugins/captions/utils.d.ts +1 -0
  56. package/dist/plugins/captions/utils.js +2 -0
  57. package/dist/plugins/fullscreen/Fullscreen.d.ts +3 -0
  58. package/dist/plugins/fullscreen/Fullscreen.js +17 -0
  59. package/dist/plugins/fullscreen/FullscreenButton.d.ts +8 -0
  60. package/dist/plugins/{Fullscreen.js → fullscreen/FullscreenButton.js} +16 -30
  61. package/dist/plugins/fullscreen/FullscreenContext.d.ts +5 -0
  62. package/dist/plugins/fullscreen/FullscreenContext.js +9 -0
  63. package/dist/plugins/{Fullscreen.d.ts → fullscreen/index.d.ts} +2 -11
  64. package/dist/plugins/fullscreen/index.js +2 -0
  65. package/dist/plugins/index.d.ts +7 -7
  66. package/dist/plugins/index.js +7 -7
  67. package/dist/plugins/inline/Inline.d.ts +3 -0
  68. package/dist/plugins/{Inline.js → inline/Inline.js} +5 -7
  69. package/dist/plugins/inline/index.d.ts +9 -0
  70. package/dist/plugins/inline/index.js +2 -0
  71. package/dist/plugins/slideshow/Slideshow.d.ts +7 -0
  72. package/dist/plugins/slideshow/Slideshow.js +20 -0
  73. package/dist/plugins/slideshow/SlideshowButton.d.ts +2 -0
  74. package/dist/plugins/{Slideshow.js → slideshow/SlideshowButton.js} +17 -44
  75. package/dist/plugins/{Slideshow.d.ts → slideshow/index.d.ts} +2 -4
  76. package/dist/plugins/slideshow/index.js +2 -0
  77. package/dist/plugins/thumbnails/Thumbnail.d.ts +21 -0
  78. package/dist/plugins/thumbnails/Thumbnail.js +45 -0
  79. package/dist/plugins/thumbnails/Thumbnails.d.ts +13 -0
  80. package/dist/plugins/thumbnails/Thumbnails.js +28 -0
  81. package/dist/plugins/thumbnails/ThumbnailsContainer.d.ts +3 -0
  82. package/dist/plugins/thumbnails/ThumbnailsContainer.js +14 -0
  83. package/dist/plugins/thumbnails/ThumbnailsTrack.d.ts +12 -0
  84. package/dist/plugins/thumbnails/ThumbnailsTrack.js +148 -0
  85. package/dist/plugins/{Thumbnails.d.ts → thumbnails/index.d.ts} +4 -16
  86. package/dist/plugins/thumbnails/index.js +2 -0
  87. package/dist/plugins/{thumbnails.css → thumbnails/thumbnails.css} +27 -37
  88. package/dist/plugins/thumbnails/utils.d.ts +2 -0
  89. package/dist/plugins/thumbnails/utils.js +4 -0
  90. package/dist/plugins/video/Video.d.ts +7 -0
  91. package/dist/plugins/video/Video.js +24 -0
  92. package/dist/plugins/video/VideoSlide.d.ts +9 -0
  93. package/dist/plugins/{Video.js → video/VideoSlide.js} +14 -39
  94. package/dist/plugins/{Video.d.ts → video/index.d.ts} +5 -13
  95. package/dist/plugins/video/index.js +2 -0
  96. package/dist/plugins/zoom/Zoom.d.ts +14 -0
  97. package/dist/plugins/zoom/Zoom.js +36 -0
  98. package/dist/plugins/zoom/ZoomButton.d.ts +7 -0
  99. package/dist/plugins/zoom/ZoomButton.js +50 -0
  100. package/dist/plugins/zoom/ZoomButtonsGroup.d.ts +5 -0
  101. package/dist/plugins/zoom/ZoomButtonsGroup.js +23 -0
  102. package/dist/plugins/zoom/ZoomContainer.d.ts +9 -0
  103. package/dist/plugins/zoom/ZoomContainer.js +303 -0
  104. package/dist/plugins/zoom/ZoomContext.d.ts +12 -0
  105. package/dist/plugins/zoom/ZoomContext.js +18 -0
  106. package/dist/plugins/zoom/ZoomWrapper.d.ts +3 -0
  107. package/dist/plugins/zoom/ZoomWrapper.js +26 -0
  108. package/dist/plugins/{Zoom.d.ts → zoom/index.d.ts} +5 -5
  109. package/dist/plugins/zoom/index.js +4 -0
  110. package/dist/props.d.ts +2 -0
  111. package/dist/props.js +31 -0
  112. package/dist/styles.css +32 -29
  113. package/dist/types.d.ts +8 -22
  114. package/dist/types.js +1 -30
  115. package/package.json +17 -17
  116. package/dist/core/hooks/useLatest.d.ts +0 -3
  117. package/dist/core/hooks/useLatest.js +0 -6
  118. package/dist/plugins/Captions.js +0 -58
  119. package/dist/plugins/Inline.d.ts +0 -15
  120. package/dist/plugins/Thumbnails.js +0 -243
  121. package/dist/plugins/Zoom.js +0 -441
@@ -1,441 +0,0 @@
1
- import * as React from "react";
2
- import { cleanup, clsx, createIcon, createModule, cssClass, IconButton, ImageSlide, label, makeUseContext, round, useContainerRect, useController, useEvents, useLayoutEffect, useMotionPreference, } from "../core/index.js";
3
- const defaultZoomProps = {
4
- maxZoomPixelRatio: 1,
5
- zoomInMultiplier: 2,
6
- doubleTapDelay: 300,
7
- doubleClickDelay: 500,
8
- doubleClickMaxStops: 2,
9
- keyboardMoveDistance: 50,
10
- wheelZoomDistanceFactor: 100,
11
- pinchZoomDistanceFactor: 100,
12
- scrollToZoom: false,
13
- };
14
- const ZoomInIcon = createIcon("ZoomIn", React.createElement(React.Fragment, null,
15
- React.createElement("path", { d: "M15.5 14h-.79l-.28-.27C15.41 12.59 16 11.11 16 9.5 16 5.91 13.09 3 9.5 3S3 5.91 3 9.5 5.91 16 9.5 16c1.61 0 3.09-.59 4.23-1.57l.27.28v.79l5 4.99L20.49 19l-4.99-5zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z" }),
16
- React.createElement("path", { d: "M12 10h-2v2H9v-2H7V9h2V7h1v2h2v1z" })));
17
- const ZoomOutIcon = createIcon("ZoomOut", React.createElement("path", { d: "M15.5 14h-.79l-.28-.27C15.41 12.59 16 11.11 16 9.5 16 5.91 13.09 3 9.5 3S3 5.91 3 9.5 5.91 16 9.5 16c1.61 0 3.09-.59 4.23-1.57l.27.28v.79l5 4.99L20.49 19l-4.99-5zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14zM7 9h5v1H7z" }));
18
- const ZoomContext = React.createContext(null);
19
- const useZoom = makeUseContext("useZoom", "ZoomContext", ZoomContext);
20
- const ZoomContextProvider = ({ children }) => {
21
- const [isMinZoom, setIsMinZoom] = React.useState(false);
22
- const [isMaxZoom, setIsMaxZoom] = React.useState(false);
23
- const [isZoomSupported, setIsZoomSupported] = React.useState(false);
24
- const context = React.useMemo(() => ({
25
- isMinZoom,
26
- isMaxZoom,
27
- isZoomSupported,
28
- setIsMinZoom,
29
- setIsMaxZoom,
30
- setIsZoomSupported,
31
- }), [isMinZoom, isMaxZoom, isZoomSupported]);
32
- return React.createElement(ZoomContext.Provider, { value: context }, children);
33
- };
34
- const ZoomButton = React.forwardRef(({ labels, render, zoomIn, onLoseFocus }, ref) => {
35
- const wasEnabled = React.useRef(false);
36
- const wasFocused = React.useRef(false);
37
- const { isMinZoom, isMaxZoom, isZoomSupported } = useZoom();
38
- const { publish } = useEvents();
39
- const disabled = !isZoomSupported || (zoomIn ? isMaxZoom : isMinZoom);
40
- const onClick = () => publish(zoomIn ? "zoom-in" : "zoom-out");
41
- const onFocus = React.useCallback(() => {
42
- wasFocused.current = true;
43
- }, []);
44
- const onBlur = React.useCallback(() => {
45
- wasFocused.current = false;
46
- }, []);
47
- React.useEffect(() => {
48
- if (disabled && wasEnabled.current && wasFocused.current) {
49
- onLoseFocus();
50
- }
51
- if (!disabled) {
52
- wasEnabled.current = true;
53
- }
54
- }, [disabled, onLoseFocus]);
55
- if (zoomIn && render.buttonZoomIn)
56
- return (React.createElement(React.Fragment, null, render.buttonZoomIn({
57
- ref,
58
- labels,
59
- disabled,
60
- onClick,
61
- onFocus,
62
- onBlur,
63
- })));
64
- if (zoomIn && render.buttonZoomOut)
65
- return (React.createElement(React.Fragment, null, render.buttonZoomOut({
66
- ref,
67
- labels,
68
- disabled,
69
- onClick,
70
- onFocus,
71
- onBlur,
72
- })));
73
- return (React.createElement(IconButton, { ref: ref, label: label(labels, zoomIn ? "Zoom in" : "Zoom out"), icon: zoomIn ? ZoomInIcon : ZoomOutIcon, renderIcon: zoomIn ? render.iconZoomIn : render.iconZoomOut, disabled: disabled, onClick: onClick, onFocus: onFocus, onBlur: onBlur }));
74
- });
75
- ZoomButton.displayName = "ZoomButton";
76
- const ZoomButtonsGroup = ({ labels, render }) => {
77
- const zoomInRef = React.useRef(null);
78
- const zoomOutRef = React.useRef(null);
79
- const { transferFocus } = useController();
80
- const focusSibling = React.useCallback((sibling) => {
81
- var _a, _b;
82
- if (!((_a = sibling.current) === null || _a === void 0 ? void 0 : _a.disabled)) {
83
- (_b = sibling.current) === null || _b === void 0 ? void 0 : _b.focus();
84
- }
85
- else {
86
- transferFocus();
87
- }
88
- }, [transferFocus]);
89
- const focusZoomIn = React.useCallback(() => focusSibling(zoomInRef), [focusSibling]);
90
- const focusZoomOut = React.useCallback(() => focusSibling(zoomOutRef), [focusSibling]);
91
- return (React.createElement(React.Fragment, null,
92
- React.createElement(ZoomButton, { ref: zoomInRef, key: "zoomIn", zoomIn: true, labels: labels, render: render, onLoseFocus: focusZoomOut }),
93
- React.createElement(ZoomButton, { ref: zoomOutRef, key: "zoomOut", labels: labels, render: render, onLoseFocus: focusZoomIn })));
94
- };
95
- const getSlideRects = (slide, cover, maxZoomPixelRatio, rect) => {
96
- var _a, _b, _c;
97
- let slideRect = { width: 0, height: 0 };
98
- let maxSlideRect = { width: 0, height: 0 };
99
- if (rect && !("type" in slide) && "src" in slide) {
100
- const width = Math.max(...(((_a = slide.srcSet) === null || _a === void 0 ? void 0 : _a.map((x) => x.width)) || []).concat(slide.width ? [slide.width] : []));
101
- const height = Math.max(...((_c = (_b = slide.srcSet) === null || _b === void 0 ? void 0 : _b.map((x) => x.height).filter((x) => Boolean(x))) !== null && _c !== void 0 ? _c : (slide.aspectRatio ? [width / slide.aspectRatio] : [])).concat(slide.height ? [slide.height] : []));
102
- if (width > 0 && height > 0 && rect.width > 0 && rect.height > 0) {
103
- maxSlideRect = cover
104
- ? {
105
- width: Math.round(Math.min(width, (rect.width / rect.height) * height)),
106
- height: Math.round(Math.min(height, (rect.height / rect.width) * width)),
107
- }
108
- : { width, height };
109
- maxSlideRect = {
110
- width: maxSlideRect.width * maxZoomPixelRatio,
111
- height: maxSlideRect.height * maxZoomPixelRatio,
112
- };
113
- slideRect = cover
114
- ? {
115
- width: Math.min(rect.width, maxSlideRect.width),
116
- height: Math.min(rect.height, maxSlideRect.height),
117
- }
118
- : {
119
- width: Math.round(Math.min(rect.width, (rect.height / height) * width)),
120
- height: Math.round(Math.min(rect.height, (rect.width / width) * height)),
121
- };
122
- }
123
- }
124
- return { slideRect, maxSlideRect };
125
- };
126
- const distance = (pointerA, pointerB) => ((pointerA.clientX - pointerB.clientX) ** 2 + (pointerA.clientY - pointerB.clientY) ** 2) ** 0.5;
127
- const ZoomContainer = ({ slide, offset, rect, render, carousel, animation, zoom: originalZoomProps }) => {
128
- var _a;
129
- const zoomProps = { ...defaultZoomProps, ...originalZoomProps };
130
- const { isMinZoom, isMaxZoom, setIsMinZoom, setIsMaxZoom } = useZoom();
131
- const { setContainerRef, containerRef: currentContainerRef, containerRect: currentContainerRect, } = useContainerRect();
132
- const { subscribeSensors, containerRef: currentControllerRef, containerRect: currentControllerRect, } = useController();
133
- const { subscribe } = useEvents();
134
- const currentReduceMotion = useMotionPreference();
135
- const { slideRect: currentSlideRect, maxSlideRect: currentMaxSlideRect } = getSlideRects(slide, carousel.imageFit === "cover" || ("imageFit" in slide && slide.imageFit === "cover"), zoomProps.maxZoomPixelRatio, currentContainerRect);
136
- const currentMaxZoom = currentSlideRect.width
137
- ? Math.max(round(currentMaxSlideRect.width / currentSlideRect.width, 5), 1)
138
- : 1;
139
- const [state, setState] = React.useState({ zoom: 1, offsetX: 0, offsetY: 0 });
140
- const refs = React.useRef({
141
- state,
142
- slideRect: currentSlideRect,
143
- containerRef: currentContainerRef,
144
- controllerRef: currentControllerRef,
145
- containerRect: currentContainerRect,
146
- controllerRect: currentControllerRect,
147
- maxZoom: currentMaxZoom,
148
- reduceMotion: currentReduceMotion,
149
- activePointers: [],
150
- lastPointerDown: 0,
151
- zoomProps,
152
- });
153
- refs.current.state = state;
154
- refs.current.slideRect = currentSlideRect;
155
- refs.current.containerRef = currentContainerRef;
156
- refs.current.controllerRef = currentControllerRef;
157
- refs.current.containerRect = currentContainerRect;
158
- refs.current.controllerRect = currentControllerRect;
159
- refs.current.maxZoom = currentMaxZoom;
160
- refs.current.reduceMotion = currentReduceMotion;
161
- refs.current.zoomAnimationDuration = animation.zoom;
162
- refs.current.zoomProps = zoomProps;
163
- const changeOffsets = React.useCallback((dx, dy, newZoom) => {
164
- const { state: { zoom, offsetX, offsetY }, containerRect, slideRect, } = refs.current;
165
- const targetZoom = newZoom || zoom;
166
- const newOffsetX = offsetX - (dx || 0);
167
- const newOffsetY = offsetY - (dy || 0);
168
- const maxOffsetX = containerRect ? (slideRect.width * targetZoom - containerRect.width) / 2 / targetZoom : 0;
169
- const maxOffsetY = containerRect ? (slideRect.height * targetZoom - containerRect.height) / 2 / targetZoom : 0;
170
- setState((prev) => ({
171
- ...prev,
172
- offsetX: Math.min(Math.abs(newOffsetX), Math.max(maxOffsetX, 0)) * Math.sign(newOffsetX),
173
- offsetY: Math.min(Math.abs(newOffsetY), Math.max(maxOffsetY, 0)) * Math.sign(newOffsetY),
174
- }));
175
- }, []);
176
- const changeZoom = React.useCallback((value, rapid, dx, dy) => {
177
- const { current } = refs;
178
- const { state: { zoom }, containerRef, containerRect, maxZoom, } = current;
179
- if (!containerRef.current || !containerRect)
180
- return;
181
- const newZoom = round(Math.min(Math.max(value + 0.001 < maxZoom ? value : maxZoom, 1), maxZoom), 5);
182
- if (newZoom === zoom)
183
- return;
184
- if (!rapid) {
185
- current.zoomAnimationStart = window.getComputedStyle(containerRef.current).transform;
186
- }
187
- changeOffsets(dx ? dx * (1 / zoom - 1 / newZoom) : 0, dy ? dy * (1 / zoom - 1 / newZoom) : 0, newZoom);
188
- setState((prev) => ({ ...prev, zoom: newZoom }));
189
- }, [changeOffsets]);
190
- useLayoutEffect(() => {
191
- if (refs.current.state.zoom > 1) {
192
- const { maxZoom, state: { zoom: currentZoom }, } = refs.current;
193
- if (currentZoom > maxZoom) {
194
- changeZoom(maxZoom, true);
195
- }
196
- changeOffsets();
197
- }
198
- }, [currentControllerRect.width, currentControllerRect.height, changeOffsets, changeZoom]);
199
- useLayoutEffect(() => {
200
- var _a, _b;
201
- const { current } = refs;
202
- const { zoomAnimation, zoomAnimationStart, zoomAnimationDuration, reduceMotion, containerRef } = current;
203
- zoomAnimation === null || zoomAnimation === void 0 ? void 0 : zoomAnimation.cancel();
204
- if (zoomAnimationStart && containerRef.current) {
205
- current.zoomAnimation = (_b = (_a = containerRef.current).animate) === null || _b === void 0 ? void 0 : _b.call(_a, [
206
- { transform: zoomAnimationStart },
207
- {
208
- transform: `scale(${state.zoom}) translate3d(${state.offsetX}px, ${state.offsetY}px, 0)`,
209
- },
210
- ], {
211
- duration: reduceMotion ? 0 : zoomAnimationDuration !== null && zoomAnimationDuration !== void 0 ? zoomAnimationDuration : 500,
212
- easing: zoomAnimation ? "ease-out" : "ease-in-out",
213
- });
214
- current.zoomAnimationStart = undefined;
215
- if (current.zoomAnimation) {
216
- current.zoomAnimation.onfinish = () => {
217
- current.zoomAnimation = undefined;
218
- };
219
- }
220
- }
221
- }, [state.zoom, state.offsetX, state.offsetY]);
222
- useLayoutEffect(() => {
223
- if (offset === 0) {
224
- const resetZoom = () => {
225
- setState({ zoom: 1, offsetX: 0, offsetY: 0 });
226
- setIsMinZoom(true);
227
- setIsMaxZoom(false);
228
- };
229
- resetZoom();
230
- return () => {
231
- resetZoom();
232
- };
233
- }
234
- return () => { };
235
- }, [offset, setIsMinZoom, setIsMaxZoom]);
236
- useLayoutEffect(() => {
237
- if (offset === 0) {
238
- const newMinZoom = state.zoom <= 1;
239
- if (newMinZoom !== isMinZoom) {
240
- setIsMinZoom(newMinZoom);
241
- }
242
- const newMaxZoom = state.zoom >= currentMaxZoom;
243
- if (newMaxZoom !== isMaxZoom) {
244
- setIsMaxZoom(newMaxZoom);
245
- }
246
- }
247
- }, [offset, state.zoom, currentMaxZoom, isMinZoom, isMaxZoom, setIsMinZoom, setIsMaxZoom]);
248
- const translateCoordinates = React.useCallback((event) => {
249
- const { controllerRef } = refs.current;
250
- if (controllerRef.current) {
251
- const { pageX, pageY } = event;
252
- const { scrollX, scrollY } = window;
253
- const { left, top, width, height } = controllerRef.current.getBoundingClientRect();
254
- return [pageX - left - scrollX - width / 2, pageY - top - scrollY - height / 2];
255
- }
256
- return [];
257
- }, []);
258
- const onKeyDown = React.useCallback((event) => {
259
- const { state: { zoom }, zoomProps: { keyboardMoveDistance, zoomInMultiplier }, } = refs.current;
260
- const preventDefault = () => {
261
- event.preventDefault();
262
- event.stopPropagation();
263
- };
264
- if (zoom > 1) {
265
- const move = (deltaX, deltaY) => {
266
- preventDefault();
267
- changeOffsets(deltaX, deltaY);
268
- };
269
- if (event.key === "ArrowDown") {
270
- move(0, keyboardMoveDistance);
271
- }
272
- else if (event.key === "ArrowUp") {
273
- move(0, -keyboardMoveDistance);
274
- }
275
- else if (event.key === "ArrowLeft") {
276
- move(-keyboardMoveDistance, 0);
277
- }
278
- else if (event.key === "ArrowRight") {
279
- move(keyboardMoveDistance, 0);
280
- }
281
- }
282
- const handleChangeZoom = (zoomValue) => {
283
- preventDefault();
284
- changeZoom(zoomValue);
285
- };
286
- const hasMeta = () => event.getModifierState("Meta") || event.getModifierState("OS");
287
- if (event.key === "+" || (event.key === "=" && hasMeta())) {
288
- handleChangeZoom(zoom * zoomInMultiplier);
289
- }
290
- else if (event.key === "-" || (event.key === "_" && hasMeta())) {
291
- handleChangeZoom(zoom / zoomInMultiplier);
292
- }
293
- else if (event.key === "0" && hasMeta()) {
294
- handleChangeZoom(1);
295
- }
296
- }, [changeZoom, changeOffsets]);
297
- const onWheel = React.useCallback((event) => {
298
- const { state: { zoom }, zoomProps: { wheelZoomDistanceFactor, scrollToZoom }, } = refs.current;
299
- if (event.ctrlKey || scrollToZoom) {
300
- if (Math.abs(event.deltaY) > Math.abs(event.deltaX)) {
301
- event.stopPropagation();
302
- changeZoom(zoom * (1 - event.deltaY / wheelZoomDistanceFactor), true, ...translateCoordinates(event));
303
- return;
304
- }
305
- }
306
- if (zoom > 1) {
307
- event.stopPropagation();
308
- if (!scrollToZoom) {
309
- changeOffsets(event.deltaX, event.deltaY);
310
- }
311
- }
312
- }, [changeZoom, changeOffsets, translateCoordinates]);
313
- const clearPointer = React.useCallback((event) => {
314
- const { activePointers } = refs.current;
315
- activePointers.splice(0, activePointers.length, ...activePointers.filter((p) => p.pointerId !== event.pointerId));
316
- }, []);
317
- const replacePointer = React.useCallback((event) => {
318
- clearPointer(event);
319
- refs.current.activePointers.push(event);
320
- }, [clearPointer]);
321
- const onPointerDown = React.useCallback((event) => {
322
- var _a;
323
- const { current } = refs;
324
- const { state: { zoom }, containerRef, activePointers, lastPointerDown, maxZoom, zoomProps: { doubleTapDelay, doubleClickDelay, zoomInMultiplier, doubleClickMaxStops }, } = current;
325
- if (!((_a = containerRef.current) === null || _a === void 0 ? void 0 : _a.contains(event.target))) {
326
- return;
327
- }
328
- if (zoom > 1) {
329
- event.stopPropagation();
330
- }
331
- const { timeStamp } = event;
332
- if (activePointers.length === 0 &&
333
- timeStamp - lastPointerDown < (event.pointerType === "touch" ? doubleTapDelay : doubleClickDelay)) {
334
- current.lastPointerDown = 0;
335
- changeZoom(zoom !== maxZoom ? zoom * Math.max(maxZoom ** (1 / doubleClickMaxStops), zoomInMultiplier) : 1, false, ...translateCoordinates(event));
336
- }
337
- else {
338
- current.lastPointerDown = timeStamp;
339
- }
340
- replacePointer(event);
341
- if (activePointers.length === 2) {
342
- current.pinchZoomDistance = distance(activePointers[0], activePointers[1]);
343
- }
344
- }, [changeZoom, replacePointer, translateCoordinates]);
345
- const onPointerMove = React.useCallback((event) => {
346
- const { current } = refs;
347
- const { state: { zoom }, activePointers, pinchZoomDistance, zoomProps: { pinchZoomDistanceFactor }, } = current;
348
- const activePointer = activePointers.find((p) => p.pointerId === event.pointerId);
349
- if (activePointers.length === 2 && pinchZoomDistance) {
350
- event.stopPropagation();
351
- replacePointer(event);
352
- const currentDistance = distance(activePointers[0], activePointers[1]);
353
- const delta = currentDistance - pinchZoomDistance;
354
- if (Math.abs(delta) > 0) {
355
- changeZoom(zoom * (1 + delta / pinchZoomDistanceFactor), true, ...activePointers
356
- .map((x) => translateCoordinates(x))
357
- .reduce((acc, coordinate) => coordinate.map((x, i) => acc[i] + x / 2)));
358
- current.pinchZoomDistance = currentDistance;
359
- }
360
- return;
361
- }
362
- if (zoom > 1) {
363
- event.stopPropagation();
364
- if (activePointer) {
365
- if (activePointers.length === 1) {
366
- changeOffsets((activePointer.clientX - event.clientX) / zoom, (activePointer.clientY - event.clientY) / zoom);
367
- }
368
- replacePointer(event);
369
- }
370
- }
371
- }, [changeOffsets, replacePointer, changeZoom, translateCoordinates]);
372
- const onPointerUp = React.useCallback((event) => {
373
- const { current } = refs;
374
- const { activePointers } = current;
375
- if (activePointers.length === 2 && activePointers.find((p) => p.pointerId === event.pointerId)) {
376
- current.pinchZoomDistance = undefined;
377
- }
378
- clearPointer(event);
379
- }, [clearPointer]);
380
- React.useEffect(() => offset === 0
381
- ? cleanup(subscribe("zoom-in", () => changeZoom(refs.current.state.zoom * refs.current.zoomProps.zoomInMultiplier)), subscribe("zoom-out", () => changeZoom(refs.current.state.zoom / refs.current.zoomProps.zoomInMultiplier)), subscribeSensors("onKeyDown", onKeyDown), subscribeSensors("onWheel", onWheel), subscribeSensors("onPointerDown", onPointerDown), subscribeSensors("onPointerMove", onPointerMove), subscribeSensors("onPointerUp", onPointerUp), subscribeSensors("onPointerLeave", onPointerUp), subscribeSensors("onPointerCancel", onPointerUp))
382
- : () => { }, [offset, subscribe, subscribeSensors, onKeyDown, onPointerDown, onPointerMove, onPointerUp, onWheel, changeZoom]);
383
- const { state: { zoom, offsetX, offsetY }, } = refs.current;
384
- const scaledRect = offset === 0
385
- ? {
386
- width: rect.width * zoom,
387
- height: rect.height * zoom,
388
- }
389
- : rect;
390
- let rendered = (_a = render.slide) === null || _a === void 0 ? void 0 : _a.call(render, slide, offset, scaledRect);
391
- if (!rendered && !("type" in slide) && "src" in slide) {
392
- rendered = (React.createElement(ImageSlide, { slide: slide, offset: offset, rect: scaledRect, render: render, imageFit: carousel.imageFit }));
393
- }
394
- return rendered ? (React.createElement("div", { ref: setContainerRef, className: clsx(cssClass("fullsize"), cssClass("flex_center")), ...(offset === 0
395
- ? { style: { transform: `scale(${zoom}) translate3d(${offsetX}px, ${offsetY}px, 0)` } }
396
- : null) }, rendered)) : null;
397
- };
398
- const ZoomWrapper = ({ slide, offset, rect, render, carousel, animation, zoom }) => {
399
- var _a;
400
- const { setIsZoomSupported, isZoomSupported } = useZoom();
401
- const imageSlide = !("type" in slide);
402
- const zoomSupported = imageSlide && ("srcSet" in slide || ("width" in slide && "height" in slide));
403
- React.useEffect(() => {
404
- if (offset === 0 && zoomSupported !== isZoomSupported) {
405
- setIsZoomSupported(zoomSupported);
406
- }
407
- }, [offset, zoomSupported, isZoomSupported, setIsZoomSupported]);
408
- if (zoomSupported) {
409
- return (React.createElement(ZoomContainer, { slide: slide, offset: offset, rect: rect, render: render, carousel: carousel, animation: animation, zoom: zoom }));
410
- }
411
- const rendered = (_a = render.slide) === null || _a === void 0 ? void 0 : _a.call(render, slide, offset, rect);
412
- if (rendered) {
413
- return React.createElement(React.Fragment, null, rendered);
414
- }
415
- if (imageSlide) {
416
- return React.createElement(ImageSlide, { slide: slide, offset: offset, rect: rect, render: render, imageFit: carousel.imageFit });
417
- }
418
- return null;
419
- };
420
- export const ZoomModule = createModule("zoom", ZoomContextProvider);
421
- export const Zoom = ({ augment, append }) => {
422
- augment(({ toolbar: { buttons, ...restToolbar }, render, carousel, animation, zoom, ...restProps }) => ({
423
- toolbar: {
424
- buttons: [React.createElement(ZoomButtonsGroup, { key: "zoom", labels: restProps.labels, render: render }), ...buttons],
425
- ...restToolbar,
426
- },
427
- render: {
428
- ...render,
429
- slide: (slide, offset, rect) => (React.createElement(ZoomWrapper, { slide: slide, offset: offset, rect: rect, render: render, carousel: carousel, animation: animation, zoom: zoom })),
430
- },
431
- zoom: {
432
- ...defaultZoomProps,
433
- ...zoom,
434
- },
435
- carousel,
436
- animation,
437
- ...restProps,
438
- }));
439
- append("controller", ZoomModule);
440
- };
441
- export default Zoom;