next-flipbook 1.0.0 → 1.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.
Files changed (123) hide show
  1. package/README.md +30 -4
  2. package/dist/core/animation/curl.d.ts +139 -0
  3. package/dist/core/animation/curl.d.ts.map +1 -0
  4. package/dist/core/animation/curl.js +204 -0
  5. package/dist/core/animation/curl.js.map +1 -0
  6. package/dist/core/animation/turn.d.ts +9 -2
  7. package/dist/core/animation/turn.d.ts.map +1 -1
  8. package/dist/core/animation/turn.js +1 -0
  9. package/dist/core/animation/turn.js.map +1 -1
  10. package/dist/core/engine/FlipBookEngine.d.ts +20 -4
  11. package/dist/core/engine/FlipBookEngine.d.ts.map +1 -1
  12. package/dist/core/engine/FlipBookEngine.js +37 -5
  13. package/dist/core/engine/FlipBookEngine.js.map +1 -1
  14. package/dist/core/index.d.ts +7 -4
  15. package/dist/core/index.d.ts.map +1 -1
  16. package/dist/core/index.js +3 -2
  17. package/dist/core/index.js.map +1 -1
  18. package/dist/core/interaction/GestureRecognizer.d.ts +20 -4
  19. package/dist/core/interaction/GestureRecognizer.d.ts.map +1 -1
  20. package/dist/core/interaction/GestureRecognizer.js +33 -11
  21. package/dist/core/interaction/GestureRecognizer.js.map +1 -1
  22. package/dist/core/interaction/drag.d.ts +13 -0
  23. package/dist/core/interaction/drag.d.ts.map +1 -1
  24. package/dist/core/interaction/drag.js +20 -0
  25. package/dist/core/interaction/drag.js.map +1 -1
  26. package/dist/core/layout/sizing.d.ts +21 -7
  27. package/dist/core/layout/sizing.d.ts.map +1 -1
  28. package/dist/core/layout/sizing.js +31 -9
  29. package/dist/core/layout/sizing.js.map +1 -1
  30. package/dist/core/types.d.ts +7 -0
  31. package/dist/core/types.d.ts.map +1 -1
  32. package/dist/core/zoom/ZoomController.d.ts +2 -0
  33. package/dist/core/zoom/ZoomController.d.ts.map +1 -1
  34. package/dist/core/zoom/ZoomController.js +9 -3
  35. package/dist/core/zoom/ZoomController.js.map +1 -1
  36. package/dist/image/index.d.ts +3 -0
  37. package/dist/image/index.d.ts.map +1 -1
  38. package/dist/image/index.js +2 -0
  39. package/dist/image/index.js.map +1 -1
  40. package/dist/image/loader.d.ts +27 -0
  41. package/dist/image/loader.d.ts.map +1 -1
  42. package/dist/image/loader.js +63 -0
  43. package/dist/image/loader.js.map +1 -1
  44. package/dist/image/nextLoader.d.ts +27 -0
  45. package/dist/image/nextLoader.d.ts.map +1 -0
  46. package/dist/image/nextLoader.js +32 -0
  47. package/dist/image/nextLoader.js.map +1 -0
  48. package/dist/image/select.d.ts +80 -0
  49. package/dist/image/select.d.ts.map +1 -0
  50. package/dist/image/select.js +84 -0
  51. package/dist/image/select.js.map +1 -0
  52. package/dist/index.d.ts +3 -1
  53. package/dist/index.d.ts.map +1 -1
  54. package/dist/index.js +2 -0
  55. package/dist/index.js.map +1 -1
  56. package/dist/react/components/FlipBook.d.ts.map +1 -1
  57. package/dist/react/components/FlipBook.js +27 -13
  58. package/dist/react/components/FlipBook.js.map +1 -1
  59. package/dist/react/components/FlipBookBackgroundControl.js +1 -1
  60. package/dist/react/components/FlipBookBackgroundControl.js.map +1 -1
  61. package/dist/react/components/FlipBookBookmarks.js +2 -2
  62. package/dist/react/components/FlipBookBookmarks.js.map +1 -1
  63. package/dist/react/components/FlipBookControls.d.ts.map +1 -1
  64. package/dist/react/components/FlipBookControls.js +4 -2
  65. package/dist/react/components/FlipBookControls.js.map +1 -1
  66. package/dist/react/components/FlipBookPageView.d.ts +6 -1
  67. package/dist/react/components/FlipBookPageView.d.ts.map +1 -1
  68. package/dist/react/components/FlipBookPageView.js +45 -16
  69. package/dist/react/components/FlipBookPageView.js.map +1 -1
  70. package/dist/react/components/FlipBookProvider.d.ts +0 -5
  71. package/dist/react/components/FlipBookProvider.d.ts.map +1 -1
  72. package/dist/react/components/FlipBookProvider.js +21 -3
  73. package/dist/react/components/FlipBookProvider.js.map +1 -1
  74. package/dist/react/components/FlipBookSearch.js +3 -3
  75. package/dist/react/components/FlipBookSearch.js.map +1 -1
  76. package/dist/react/components/FlipBookSidePanel.d.ts.map +1 -1
  77. package/dist/react/components/FlipBookSidePanel.js +5 -2
  78. package/dist/react/components/FlipBookSidePanel.js.map +1 -1
  79. package/dist/react/components/FlipBookStage.d.ts.map +1 -1
  80. package/dist/react/components/FlipBookStage.js +304 -70
  81. package/dist/react/components/FlipBookStage.js.map +1 -1
  82. package/dist/react/components/FlipBookTOC.js +2 -2
  83. package/dist/react/components/FlipBookTOC.js.map +1 -1
  84. package/dist/react/components/FlipBookThumbnails.js +1 -1
  85. package/dist/react/components/FlipBookThumbnails.js.map +1 -1
  86. package/dist/react/components/FlipBookToolbar.d.ts.map +1 -1
  87. package/dist/react/components/FlipBookToolbar.js +5 -2
  88. package/dist/react/components/FlipBookToolbar.js.map +1 -1
  89. package/dist/react/components/HotspotPopover.js +1 -1
  90. package/dist/react/components/HotspotPopover.js.map +1 -1
  91. package/dist/react/components/PasswordDialog.js +2 -2
  92. package/dist/react/components/PasswordDialog.js.map +1 -1
  93. package/dist/react/components/PdfPageView.js +1 -1
  94. package/dist/react/components/PdfPageView.js.map +1 -1
  95. package/dist/react/components/Thumbnail.d.ts.map +1 -1
  96. package/dist/react/components/Thumbnail.js +15 -4
  97. package/dist/react/components/Thumbnail.js.map +1 -1
  98. package/dist/react/context.d.ts +10 -2
  99. package/dist/react/context.d.ts.map +1 -1
  100. package/dist/react/context.js.map +1 -1
  101. package/dist/react/hooks/useGestures.d.ts +7 -2
  102. package/dist/react/hooks/useGestures.d.ts.map +1 -1
  103. package/dist/react/hooks/useGestures.js +19 -7
  104. package/dist/react/hooks/useGestures.js.map +1 -1
  105. package/dist/react/hooks/usePageAspect.d.ts.map +1 -1
  106. package/dist/react/hooks/usePageAspect.js +9 -3
  107. package/dist/react/hooks/usePageAspect.js.map +1 -1
  108. package/dist/react/index.d.ts +1 -0
  109. package/dist/react/index.d.ts.map +1 -1
  110. package/dist/react/index.js +1 -0
  111. package/dist/react/index.js.map +1 -1
  112. package/dist/react/styles.d.ts +21 -5
  113. package/dist/react/styles.d.ts.map +1 -1
  114. package/dist/react/styles.js +46 -10
  115. package/dist/react/styles.js.map +1 -1
  116. package/dist/react/theme.d.ts +27 -3
  117. package/dist/react/theme.d.ts.map +1 -1
  118. package/dist/react/theme.js +115 -15
  119. package/dist/react/theme.js.map +1 -1
  120. package/dist/styles.css +13 -0
  121. package/dist/types/index.d.ts +107 -6
  122. package/dist/types/index.d.ts.map +1 -1
  123. package/package.json +1 -1
@@ -1,15 +1,16 @@
1
1
  "use client";
2
2
  import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
3
- import { useEffect, useMemo, useRef, useSyncExternalStore } from "react";
3
+ import { useEffect, useMemo, useRef, useSyncExternalStore, } from "react";
4
+ import { computeCurlBox, computeCurlShading, invertMatrix, matrixToCss, polygonToCss, transformPoint, } from "../../core/animation/curl.js";
4
5
  import { computeTurnVisuals } from "../../core/animation/turn.js";
5
- import { centeringShift, fitBook, resolveView, slotLeft, spineSide, } from "../../core/layout/sizing.js";
6
+ import { centeringShift, fitBook, resolveView, slotPosition, spineSide, } from "../../core/layout/sizing.js";
6
7
  import { useFlipBookContext } from "../context.js";
7
8
  import { useElementSize } from "../hooks/useElementSize.js";
8
9
  import { useEngineState } from "../hooks/useEngineState.js";
9
10
  import { useIsoLayoutEffect } from "../hooks/useIsoLayoutEffect.js";
10
11
  import { useGestures } from "../hooks/useGestures.js";
11
12
  import { usePageAspect } from "../hooks/usePageAspect.js";
12
- import { PAPER, bookTransform, castStyle, flatStyle, leafTransform, spineStyle, } from "../styles.js";
13
+ import { PAPER, SHADOW, backFaceTransform, bookTransform, castStyle, flatStyle, gradientFrom, leafTransform, spineStyle, } from "../styles.js";
13
14
  import { formatMessage } from "../locale.js";
14
15
  import { BookmarkRibbon } from "./BookmarkRibbon.js";
15
16
  import { FlipBookPageView } from "./FlipBookPageView.js";
@@ -24,14 +25,15 @@ import { PasswordDialog } from "./PasswordDialog.js";
24
25
  */
25
26
  export function FlipBookStage(props) {
26
27
  const { aspectRatio = "auto", padding = 16, renderLoader, renderError, renderEmpty } = props;
27
- const { engine, imageLoader, pdf, zoom, bookmarks, search, interaction, document: documentState, password, pages, mode, breakpoint, locale, license, shadowStrength, reportError, notifyReady, } = useFlipBookContext();
28
+ const { engine, imageLoader, pdf, zoom, bookmarks, search, interaction, document: documentState, password, pages, mode, breakpoint, orientation, flipStyle, parts, locale, license, shadowStrength, reportError, notifyReady, } = useFlipBookContext();
28
29
  const state = useEngineState(engine);
29
30
  const containerRef = useRef(null);
30
31
  const size = useElementSize(containerRef);
31
32
  const { aspect, error: aspectError, imageError } = usePageAspect(pages, aspectRatio, imageLoader, pdf);
32
33
  const error = documentState.error ?? aspectError;
33
34
  const measured = size !== null && size.width > 0 && size.height > 0;
34
- const view = measured ? resolveView(mode, size.width, breakpoint) : null;
35
+ // A notebook stacks its two pages, so `auto` asks whether the container is tall enough, not wide.
36
+ const view = measured ? resolveView(mode, orientation === "vertical" ? size.height : size.width, breakpoint) : null;
35
37
  // Layout effect: the engine switches view before the browser paints.
36
38
  useIsoLayoutEffect(() => {
37
39
  if (view !== null)
@@ -53,11 +55,14 @@ export function FlipBookStage(props) {
53
55
  containerHeight: size.height - padding * 2,
54
56
  pageAspect: aspect,
55
57
  view: state.view,
58
+ orientation,
56
59
  })
57
60
  : null;
61
+ const vertical = orientation === "vertical";
58
62
  const pageWidth = layout?.pageWidth ?? 0;
59
63
  const bookWidth = layout?.bookWidth ?? 0;
60
64
  const pageHeight = layout?.pageHeight ?? 0;
65
+ const bookHeight = layout?.bookHeight ?? 0;
61
66
  const viewWidth = size?.width ?? 0;
62
67
  const viewHeight = size?.height ?? 0;
63
68
  // Zoom needs the size of the view and of the book to keep panning in bounds.
@@ -67,11 +72,11 @@ export function FlipBookStage(props) {
67
72
  viewWidth,
68
73
  viewHeight,
69
74
  contentWidth: bookWidth,
70
- contentHeight: pageHeight,
75
+ contentHeight: bookHeight,
71
76
  padding,
72
77
  });
73
78
  }
74
- }, [zoom, viewWidth, viewHeight, bookWidth, pageHeight, padding]);
79
+ }, [zoom, viewWidth, viewHeight, bookWidth, bookHeight, padding]);
75
80
  // PDF pages are drawn for the zoomed size once the zoom settles, so they stay
76
81
  // sharp. Pages prepared in advance use the fitted size.
77
82
  const zoomState = useSyncExternalStore(zoom.subscribe, zoom.getSnapshot, zoom.getSnapshot);
@@ -80,6 +85,12 @@ export function FlipBookStage(props) {
80
85
  if (pageWidth > 0)
81
86
  pdf.setViewportWidth(renderWidth, pageWidth);
82
87
  }, [pdf, renderWidth, pageWidth]);
88
+ // Images are fetched for the same size, unless `imageOptions.sharpenOnZoom` is off. Set before
89
+ // paint, so that the first page drawn already knows the size it is drawn at.
90
+ const imageWidth = imageLoader.getOptions().sharpenOnZoom ? renderWidth : pageWidth;
91
+ useIsoLayoutEffect(() => {
92
+ imageLoader.setTargetWidth(imageWidth);
93
+ }, [imageLoader, imageWidth]);
83
94
  useGestures(containerRef, engine, zoom, {
84
95
  active: layout !== null,
85
96
  dragToTurn: interaction.dragToTurn,
@@ -87,7 +98,9 @@ export function FlipBookStage(props) {
87
98
  doubleTapZoom: interaction.doubleTapZoom,
88
99
  wheelZoom: interaction.wheelZoom,
89
100
  reducedMotion: interaction.reducedMotion,
90
- pageWidth,
101
+ orientation,
102
+ pageLength: vertical ? pageHeight : pageWidth,
103
+ pageBreadth: vertical ? pageWidth : pageHeight,
91
104
  });
92
105
  const savedBookmarks = useSyncExternalStore(bookmarks.subscribe, bookmarks.getSnapshot, bookmarks.getSnapshot);
93
106
  const bookmarked = useMemo(() => new Set(savedBookmarks), [savedBookmarks]);
@@ -125,13 +138,23 @@ export function FlipBookStage(props) {
125
138
  else {
126
139
  content = (
127
140
  // Not `will-change`: that would rasterise once and blur the page when zoomed in.
128
- _jsxs("div", { ref: zoomLayerRef, "data-flipbook-zoom": "", style: { flex: "none", position: "relative", transform: zoomTransformValue(zoom.getTransform()) }, children: [_jsx(Book, { engine: engine, state: state, pages: pages, layout: layout, renderWidth: renderWidth, bookmarked: bookmarked, highlights: highlights, shadowStrength: shadowStrength }), _jsx(HotspotPopoverHost, { state: state, layout: layout })] }));
141
+ _jsxs("div", { ref: zoomLayerRef, "data-flipbook-zoom": "", style: {
142
+ flex: "none",
143
+ position: "relative",
144
+ transform: zoomTransformValue(zoom.getTransform()),
145
+ // The `bookShadow` token: a drop shadow of whatever the book looks like at the moment.
146
+ filter: "var(--flipbook-book-filter, none)",
147
+ }, children: [_jsx(Book, { engine: engine, state: state, pages: pages, layout: layout, renderWidth: renderWidth, imageWidth: imageWidth, bookmarked: bookmarked, highlights: highlights, shadowStrength: shadowStrength, orientation: orientation, flipStyle: flipStyle }), _jsx(HotspotPopoverHost, { state: state, layout: layout, orientation: orientation })] }));
129
148
  }
130
- return (_jsxs("div", { ref: containerRef, "data-flipbook-stage": "", style: {
149
+ return (_jsxs("div", { ref: containerRef, "data-flipbook-stage": "", className: parts.classNames.stage, style: {
150
+ // The browser keeps the scroll that the turn does not use: up and down for a book turned
151
+ // sideways, left and right for a notebook turned upwards.
131
152
  touchAction: zoomState.zoomed
132
153
  ? "none"
133
154
  : interaction.dragToTurn || interaction.zoomEnabled
134
- ? "pan-y"
155
+ ? vertical
156
+ ? "pan-x"
157
+ : "pan-y"
135
158
  : "auto",
136
159
  userSelect: interaction.dragToTurn || interaction.zoomEnabled ? "none" : undefined,
137
160
  WebkitUserSelect: interaction.dragToTurn || interaction.zoomEnabled ? "none" : undefined,
@@ -144,6 +167,7 @@ export function FlipBookStage(props) {
144
167
  alignItems: "center",
145
168
  justifyContent: "center",
146
169
  overflow: "hidden",
170
+ ...parts.styles.stage,
147
171
  }, children: [content, license.state === "unlicensed" ? _jsx(FlipBookLicenseMark, {}) : null] }));
148
172
  }
149
173
  const zoomTransformValue = (transform) => `translate3d(${transform.panX}px, ${transform.panY}px, 0) scale(${transform.zoom})`;
@@ -151,7 +175,7 @@ function Message({ role, children }) {
151
175
  return (_jsx("div", { role: role, style: {
152
176
  padding: 24,
153
177
  textAlign: "center",
154
- font: "14px/1.5 system-ui, sans-serif",
178
+ font: "14px/1.5 var(--flipbook-font, system-ui, sans-serif)",
155
179
  color: "var(--flipbook-toolbar-color, #1f2328)",
156
180
  }, children: children }));
157
181
  }
@@ -163,13 +187,107 @@ function DefaultLoader({ progress, label, template, }) {
163
187
  alignItems: "center",
164
188
  gap: 10,
165
189
  padding: 24,
166
- font: "14px/1.5 system-ui, sans-serif",
190
+ font: "14px/1.5 var(--flipbook-font, system-ui, sans-serif)",
167
191
  color: "var(--flipbook-toolbar-color, #1f2328)",
168
192
  }, children: [_jsx("span", { children: text }), progress === null ? null : (_jsx("progress", { value: progress, max: 1, "aria-label": text, style: { width: 200, accentColor: "var(--flipbook-accent, #2563eb)" } }))] }));
169
193
  }
170
194
  function ErrorMessage({ error }) {
171
195
  return _jsx(Message, { role: "alert", children: error.message });
172
196
  }
197
+ /** How far a scripted turn draws the held corner up, as a share of the page breadth. */
198
+ const CURL_LIFT = 0.28;
199
+ const fill = { position: "absolute", inset: 0 };
200
+ function setClip(element, value) {
201
+ if (!element)
202
+ return;
203
+ element.style.clipPath = value;
204
+ // Safari before 13.1 and some embedded browsers only know the prefixed property.
205
+ element.style.setProperty("-webkit-clip-path", value);
206
+ }
207
+ /** Lights and shadows of a curling leaf for one frame. */
208
+ function paintCurlShadows(els, box, shading, width, height) {
209
+ const crease = box.crease;
210
+ const hidden = crease === null;
211
+ if (els.reveal)
212
+ els.reveal.style.opacity = hidden ? "0" : String(shading.revealOpacity);
213
+ if (els.flapShade)
214
+ els.flapShade.style.opacity = hidden ? "0" : String(shading.flapDark);
215
+ if (els.flapLight)
216
+ els.flapLight.style.opacity = hidden ? "0" : String(shading.flapLight);
217
+ if (els.castBlur)
218
+ els.castBlur.style.opacity = hidden ? "0" : String(shading.castOpacity);
219
+ if (crease === null)
220
+ return;
221
+ const [nx, ny] = crease.normal;
222
+ // On the page that has been uncovered: darkest at the crease, fading out across the page.
223
+ if (els.reveal) {
224
+ setClip(els.reveal, polygonToCss(box.revealed));
225
+ els.reveal.style.background = gradientFrom(width, height, crease.point, crease.normal, [
226
+ [SHADOW, 0],
227
+ ["transparent", shading.revealLength],
228
+ ]);
229
+ }
230
+ // On the lifted part, in its own frame: the paper turns away from the light at the crease, with a
231
+ // faint highlight just past it where it bends back. Walking away from the crease is walking
232
+ // against the normal, taken back into the frame of the page that was printed on the back.
233
+ if (els.flapShade || els.flapLight) {
234
+ const inverse = invertMatrix(box.flapMatrix);
235
+ const origin = transformPoint(inverse, crease.point);
236
+ const away = [
237
+ -(inverse[0] * nx + inverse[2] * ny),
238
+ -(inverse[1] * nx + inverse[3] * ny),
239
+ ];
240
+ const depth = Math.max(8, box.flapDepth);
241
+ if (els.flapShade) {
242
+ els.flapShade.style.background = gradientFrom(width, height, origin, away, [
243
+ [SHADOW, 0],
244
+ ["transparent", depth * 0.7],
245
+ ]);
246
+ }
247
+ if (els.flapLight) {
248
+ els.flapLight.style.background = gradientFrom(width, height, origin, away, [
249
+ ["rgba(255, 255, 255, 0)", 0],
250
+ ["rgba(255, 255, 255, 1)", depth * 0.12],
251
+ ["rgba(255, 255, 255, 0)", depth * 0.4],
252
+ ]);
253
+ }
254
+ }
255
+ // On the page the lifted part lies on: its outline, pushed away from the crease and blurred.
256
+ if (els.castClip && els.castBlur && els.castShape && box.flap.length >= 3) {
257
+ const margin = shading.castBlur * 3 + shading.castOffset + 2;
258
+ let minX = Infinity;
259
+ let minY = Infinity;
260
+ let maxX = -Infinity;
261
+ let maxY = -Infinity;
262
+ for (const [x, y] of box.flap) {
263
+ minX = Math.min(minX, x);
264
+ minY = Math.min(minY, y);
265
+ maxX = Math.max(maxX, x);
266
+ maxY = Math.max(maxY, y);
267
+ }
268
+ const left = Math.floor(minX - margin);
269
+ const top = Math.floor(minY - margin);
270
+ const dx = -nx * shading.castOffset;
271
+ const dy = -ny * shading.castOffset;
272
+ setClip(els.castClip, polygonToCss(box.receiver));
273
+ const blur = els.castBlur.style;
274
+ blur.left = `${left}px`;
275
+ blur.top = `${top}px`;
276
+ blur.width = `${Math.ceil(maxX - minX + margin * 2)}px`;
277
+ blur.height = `${Math.ceil(maxY - minY + margin * 2)}px`;
278
+ blur.filter = `blur(${Math.round(shading.castBlur * 10) / 10}px)`;
279
+ setClip(els.castShape, polygonToCss(box.flap.map(([x, y]) => [x - left + dx, y - top + dy])));
280
+ }
281
+ }
282
+ const R = "var(--flipbook-page-radius, 0px)";
283
+ /** `border-radius` for a page by the edge it is bound on: round the three others, keep the spine's square. */
284
+ const PAGE_RADIUS = {
285
+ left: `0 ${R} ${R} 0`,
286
+ right: `${R} 0 0 ${R}`,
287
+ top: `0 0 ${R} ${R}`,
288
+ bottom: `${R} ${R} 0 0`,
289
+ };
290
+ const ribbonSide = (spine) => (spine === "left" ? "right" : spine === "right" ? "left" : "right");
173
291
  /**
174
292
  * The physical model. While a turn runs, four page surfaces exist:
175
293
  *
@@ -180,21 +298,42 @@ function ErrorMessage({ error }) {
180
298
  *
181
299
  * This holds for forward and backward turns and for multi-spread jumps, so
182
300
  * at most four pages are ever in the DOM no matter how long the document is.
301
+ *
302
+ * The leaf is drawn one of two ways. `flat` swings it on its spine in 3D. `curl` keeps it in the
303
+ * plane and folds it along a moving crease: the part still flat shows the front, the part folded
304
+ * over shows the back, and the page underneath shows where the fold has left it. A book that turns
305
+ * upwards is the same model with the spine along the top edge instead of a side edge.
183
306
  */
184
- function Book({ engine, state, pages, layout, renderWidth, bookmarked, highlights, shadowStrength }) {
185
- const { pageWidth, pageHeight, bookWidth } = layout;
307
+ function Book({ engine, state, pages, layout, renderWidth, imageWidth, bookmarked, highlights, shadowStrength, orientation, flipStyle, }) {
308
+ const { pageWidth, pageHeight, bookWidth, bookHeight } = layout;
186
309
  const { view, direction, turn } = state;
310
+ const { parts } = useFlipBookContext();
311
+ const vertical = orientation === "vertical";
312
+ const curl = flipStyle === "curl";
313
+ const shadowed = shadowStrength > 0;
314
+ /** The size of a page along the turn: its width when it turns sideways, its height when it turns up. */
315
+ const pageLength = vertical ? pageHeight : pageWidth;
316
+ const pageBreadth = vertical ? pageWidth : pageHeight;
187
317
  const bookRef = useRef(null);
188
318
  const leafRef = useRef(null);
189
319
  const frontShadeRef = useRef(null);
190
320
  const backShadeRef = useRef(null);
191
321
  const revealRef = useRef(null);
192
322
  const castRef = useRef(null);
323
+ const curlFlatRef = useRef(null);
324
+ const curlFlapClipRef = useRef(null);
325
+ const curlFlapRef = useRef(null);
326
+ const curlRevealRef = useRef(null);
327
+ const curlCastClipRef = useRef(null);
328
+ const curlCastBlurRef = useRef(null);
329
+ const curlCastShapeRef = useRef(null);
330
+ const curlFlapShadeRef = useRef(null);
331
+ const curlFlapLightRef = useRef(null);
193
332
  let versoPage = state.spread.verso;
194
333
  let rectoPage = state.spread.recto;
195
334
  let frontPage = null;
196
335
  let backPage = null;
197
- let shift = centeringShift(state.spread, view, direction);
336
+ let shift = centeringShift(state.spread, view, direction, orientation);
198
337
  let initial = null;
199
338
  if (turn) {
200
339
  const earlier = engine.getSpreadAt(Math.min(turn.from, turn.to));
@@ -203,13 +342,14 @@ function Book({ engine, state, pages, layout, renderWidth, bookmarked, highlight
203
342
  rectoPage = later.recto;
204
343
  frontPage = earlier.recto;
205
344
  backPage = later.verso;
206
- const fromShift = centeringShift(engine.getSpreadAt(turn.from), view, direction);
207
- const toShift = centeringShift(engine.getSpreadAt(turn.to), view, direction);
345
+ const fromShift = centeringShift(engine.getSpreadAt(turn.from), view, direction, orientation);
346
+ const toShift = centeringShift(engine.getSpreadAt(turn.to), view, direction, orientation);
208
347
  shift = fromShift;
209
348
  initial = computeTurnVisuals({
210
349
  forward: turn.forward,
211
350
  progress: 0,
212
351
  direction,
352
+ orientation,
213
353
  shadowIntensity: shadowStrength,
214
354
  fromShift,
215
355
  toShift,
@@ -220,73 +360,164 @@ function Book({ engine, state, pages, layout, renderWidth, bookmarked, highlight
220
360
  useIsoLayoutEffect(() => {
221
361
  if (!turn)
222
362
  return;
223
- const fromShift = centeringShift(engine.getSpreadAt(turn.from), view, direction);
224
- const toShift = centeringShift(engine.getSpreadAt(turn.to), view, direction);
225
- const apply = (progress) => {
226
- const v = computeTurnVisuals({
227
- forward: turn.forward,
228
- progress,
363
+ const fromShift = centeringShift(engine.getSpreadAt(turn.from), view, direction, orientation);
364
+ const toShift = centeringShift(engine.getSpreadAt(turn.to), view, direction, orientation);
365
+ const visuals = (progress) => computeTurnVisuals({
366
+ forward: turn.forward,
367
+ progress,
368
+ direction,
369
+ orientation,
370
+ shadowIntensity: shadowStrength,
371
+ fromShift,
372
+ toShift,
373
+ });
374
+ if (!curl) {
375
+ const apply = (progress) => {
376
+ const v = visuals(progress);
377
+ if (leafRef.current)
378
+ leafRef.current.style.transform = leafTransform(v.rotate, orientation);
379
+ if (frontShadeRef.current)
380
+ frontShadeRef.current.style.opacity = String(v.frontShade);
381
+ if (backShadeRef.current)
382
+ backShadeRef.current.style.opacity = String(v.backShade);
383
+ if (revealRef.current)
384
+ revealRef.current.style.opacity = String(v.revealShade);
385
+ if (castRef.current)
386
+ castRef.current.style.opacity = String(v.castShade);
387
+ if (bookRef.current)
388
+ bookRef.current.style.transform = bookTransform(v.shift, pageLength, orientation);
389
+ };
390
+ apply(engine.getTurnProgress());
391
+ return engine.subscribeFrame((progress) => apply(progress));
392
+ }
393
+ const corner = engine.getTurnCorner();
394
+ const els = {
395
+ flat: curlFlatRef.current,
396
+ flapClip: curlFlapClipRef.current,
397
+ flap: curlFlapRef.current,
398
+ reveal: curlRevealRef.current,
399
+ castClip: curlCastClipRef.current,
400
+ castBlur: curlCastBlurRef.current,
401
+ castShape: curlCastShapeRef.current,
402
+ flapShade: curlFlapShadeRef.current,
403
+ flapLight: curlFlapLightRef.current,
404
+ };
405
+ const apply = (progress, lift) => {
406
+ const v = visuals(progress);
407
+ if (bookRef.current)
408
+ bookRef.current.style.transform = bookTransform(v.shift, pageLength, orientation);
409
+ // `angle` is how far the leaf has gone from lying on its own side: 0 flat, 180 turned over.
410
+ const fold = v.angle / 180;
411
+ const box = computeCurlBox({
412
+ width: pageWidth,
413
+ height: pageHeight,
414
+ orientation,
229
415
  direction,
230
- shadowIntensity: shadowStrength,
231
- fromShift,
232
- toShift,
416
+ fold,
417
+ corner,
418
+ lift: lift * CURL_LIFT * pageBreadth,
233
419
  });
234
- if (leafRef.current)
235
- leafRef.current.style.transform = leafTransform(v.rotateY);
236
- if (frontShadeRef.current)
237
- frontShadeRef.current.style.opacity = String(v.frontShade);
238
- if (backShadeRef.current)
239
- backShadeRef.current.style.opacity = String(v.backShade);
240
- if (revealRef.current)
241
- revealRef.current.style.opacity = String(v.revealShade);
242
- if (castRef.current)
243
- castRef.current.style.opacity = String(v.castShade);
244
- if (bookRef.current)
245
- bookRef.current.style.transform = bookTransform(v.shift, pageWidth);
420
+ setClip(els.flat, polygonToCss(box.flat));
421
+ setClip(els.flapClip, polygonToCss(box.flap));
422
+ if (els.flap)
423
+ els.flap.style.transform = matrixToCss(box.flapMatrix);
424
+ if (shadowed) {
425
+ paintCurlShadows(els, box, computeCurlShading({ fold, length: pageLength, strength: shadowStrength }), pageWidth, pageHeight);
426
+ }
246
427
  };
247
- apply(engine.getTurnProgress());
428
+ apply(engine.getTurnProgress(), engine.getTurnLift());
248
429
  return engine.subscribeFrame(apply);
249
- }, [engine, turn, view, direction, shadowStrength, pageWidth]);
430
+ }, [
431
+ engine,
432
+ turn,
433
+ view,
434
+ direction,
435
+ orientation,
436
+ curl,
437
+ shadowed,
438
+ shadowStrength,
439
+ pageWidth,
440
+ pageHeight,
441
+ pageLength,
442
+ pageBreadth,
443
+ ]);
250
444
  const slot = (side, number, shade, shadeRef) => number === null ? null : (_jsx("div", { "data-flipbook-page": number, style: {
251
445
  position: "absolute",
252
- top: 0,
253
- left: slotLeft(side, view, direction, pageWidth),
446
+ ...slotPosition(side, view, direction, orientation, pageWidth, pageHeight),
254
447
  width: pageWidth,
255
448
  height: pageHeight,
256
449
  zIndex: 1,
257
- }, children: _jsx(PageFace, { page: pages[number - 1], pageNumber: number, bookmarked: bookmarked.has(number), highlights: highlights.get(number), pageWidth: renderWidth, spine: spineSide(side, direction), shade: shade, shadeRef: shadeRef }) }, side));
450
+ }, children: _jsx(PageFace, { page: pages[number - 1], pageNumber: number, bookmarked: bookmarked.has(number), highlights: highlights.get(number), pageWidth: renderWidth, imageWidth: imageWidth, spine: spineSide(side, direction, orientation), shade: shade, shadeRef: shadeRef }) }, side));
258
451
  const faceStyle = {
259
452
  position: "absolute",
260
453
  inset: 0,
261
454
  backfaceVisibility: "hidden",
262
455
  WebkitBackfaceVisibility: "hidden",
263
456
  };
264
- return (_jsxs("div", { ref: bookRef, "data-flipbook-book": "", "data-view": view, style: {
457
+ const leafPlace = slotPosition("recto", view, direction, orientation, pageWidth, pageHeight);
458
+ const frontFace = (shade, shadeRef) => (_jsx(PageFace, { page: frontPage === null ? undefined : pages[frontPage - 1], pageNumber: frontPage, bookmarked: frontPage !== null && bookmarked.has(frontPage), highlights: frontPage === null ? undefined : highlights.get(frontPage), pageWidth: renderWidth, imageWidth: imageWidth, spine: spineSide("recto", direction, orientation), shade: shade, shadeRef: shadeRef }));
459
+ const backFace = (shade, shadeRef) => (_jsx(PageFace, { page: backPage === null ? undefined : pages[backPage - 1], pageNumber: backPage, bookmarked: backPage !== null && bookmarked.has(backPage), highlights: backPage === null ? undefined : highlights.get(backPage), pageWidth: renderWidth, imageWidth: imageWidth, spine: spineSide("verso", direction, orientation), shade: shade, shadeRef: shadeRef }));
460
+ let leaf = null;
461
+ if (turn && initial && !curl) {
462
+ leaf = (_jsxs("div", { ref: leafRef, "aria-hidden": "true", "data-flipbook-leaf": "flat", style: {
463
+ position: "absolute",
464
+ left: leafPlace.left,
465
+ top: leafPlace.top,
466
+ width: pageWidth,
467
+ height: pageHeight,
468
+ zIndex: 2,
469
+ // The leaf is a moving picture of a page: nothing on it can be clicked.
470
+ pointerEvents: "none",
471
+ transformStyle: "preserve-3d",
472
+ transformOrigin: vertical ? "50% 0%" : direction === "ltr" ? "0% 50%" : "100% 50%",
473
+ transform: leafTransform(initial.rotate, orientation),
474
+ willChange: "transform",
475
+ }, children: [_jsx("div", { style: faceStyle, children: frontFace({ opacity: initial.frontShade, kind: "flat" }, frontShadeRef) }), _jsx("div", { style: { ...faceStyle, transform: backFaceTransform(orientation) }, children: backFace({ opacity: initial.backShade, kind: "flat" }, backShadeRef) })] }, `${turn.from}-${turn.to}`));
476
+ }
477
+ else if (turn && initial) {
478
+ leaf = (_jsxs("div", { "aria-hidden": "true", "data-flipbook-leaf": "curl", style: {
479
+ position: "absolute",
480
+ left: leafPlace.left,
481
+ top: leafPlace.top,
482
+ width: pageWidth,
483
+ height: pageHeight,
484
+ zIndex: 2,
485
+ pointerEvents: "none",
486
+ }, children: [shadowed ? _jsx("div", { ref: curlRevealRef, style: { ...fill, opacity: 0 } }) : null, _jsx("div", { ref: curlFlatRef, style: fill, children: frontFace(undefined, undefined) }), shadowed ? (_jsx("div", { ref: curlCastClipRef, style: fill, children: _jsx("div", { ref: curlCastBlurRef, style: { position: "absolute", opacity: 0 }, children: _jsx("div", { ref: curlCastShapeRef, style: { ...fill, background: SHADOW } }) }) })) : null, _jsx("div", { ref: curlFlapClipRef, style: fill, children: _jsxs("div", { ref: curlFlapRef, style: {
487
+ position: "absolute",
488
+ left: 0,
489
+ top: 0,
490
+ width: pageWidth,
491
+ height: pageHeight,
492
+ transformOrigin: "0 0",
493
+ willChange: "transform",
494
+ }, children: [backFace(undefined, undefined), shadowed ? _jsx("div", { ref: curlFlapShadeRef, "aria-hidden": "true", style: { ...fill, opacity: 0 } }) : null, shadowed ? _jsx("div", { ref: curlFlapLightRef, "aria-hidden": "true", style: { ...fill, opacity: 0 } }) : null] }) })] }, `${turn.from}-${turn.to}`));
495
+ }
496
+ return (_jsxs("div", { ref: bookRef, "data-flipbook-book": "", "data-view": view, "data-orientation": orientation, "data-flip-style": flipStyle, className: parts.classNames.book, style: {
497
+ ...parts.styles.book,
265
498
  position: "relative",
266
499
  flex: "none",
267
500
  width: bookWidth,
268
- height: pageHeight,
269
- perspective: Math.round(pageWidth * 3),
270
- transform: bookTransform(shift, pageWidth),
501
+ height: bookHeight,
502
+ perspective: Math.round(pageLength * 3),
503
+ transform: bookTransform(shift, pageLength, orientation),
271
504
  // In single view the page swings out past the book edge and is clipped.
272
505
  overflow: view === "single" ? "hidden" : "visible",
273
- }, children: [slot("verso", versoPage, initial ? { opacity: initial.castShade, kind: "cast" } : undefined, castRef), slot("recto", rectoPage, initial ? { opacity: initial.revealShade, kind: "cast" } : undefined, revealRef), turn && initial ? (_jsxs("div", { ref: leafRef, "aria-hidden": "true", style: {
274
- position: "absolute",
275
- top: 0,
276
- left: slotLeft("recto", view, direction, pageWidth),
277
- width: pageWidth,
278
- height: pageHeight,
279
- zIndex: 2,
280
- // The leaf is a moving picture of a page: nothing on it can be clicked.
281
- pointerEvents: "none",
282
- transformStyle: "preserve-3d",
283
- transformOrigin: direction === "ltr" ? "0% 50%" : "100% 50%",
284
- transform: leafTransform(initial.rotateY),
285
- willChange: "transform",
286
- }, children: [_jsx("div", { style: faceStyle, children: _jsx(PageFace, { page: frontPage === null ? undefined : pages[frontPage - 1], pageNumber: frontPage, bookmarked: frontPage !== null && bookmarked.has(frontPage), highlights: frontPage === null ? undefined : highlights.get(frontPage), pageWidth: renderWidth, spine: spineSide("recto", direction), shade: { opacity: initial.frontShade, kind: "flat" }, shadeRef: frontShadeRef }) }), _jsx("div", { style: { ...faceStyle, transform: "rotateY(180deg)" }, children: _jsx(PageFace, { page: backPage === null ? undefined : pages[backPage - 1], pageNumber: backPage, bookmarked: backPage !== null && bookmarked.has(backPage), highlights: backPage === null ? undefined : highlights.get(backPage), pageWidth: renderWidth, spine: spineSide("verso", direction), shade: { opacity: initial.backShade, kind: "flat" }, shadeRef: backShadeRef }) })] }, `${turn.from}-${turn.to}`)) : null] }));
506
+ }, children: [slot("verso", versoPage, !curl && initial ? { opacity: initial.castShade, kind: "cast" } : undefined, castRef), slot("recto", rectoPage, !curl && initial ? { opacity: initial.revealShade, kind: "cast" } : undefined, revealRef), leaf] }));
287
507
  }
288
- function PageFace({ page, pageNumber, bookmarked, highlights, pageWidth, spine, shade, shadeRef, }) {
289
- return (_jsxs("div", { style: { position: "absolute", inset: 0, overflow: "hidden", background: PAPER }, children: [pageNumber === null ? null : (_jsx(FlipBookPageView, { page: page, pageNumber: pageNumber, pageWidth: pageWidth })), pageNumber === null ? null : _jsx(PageOverlays, { pageNumber: pageNumber, highlights: highlights }), _jsx("div", { "aria-hidden": "true", style: spineStyle(spine) }), bookmarked ? _jsx(BookmarkRibbon, { side: spine === "left" ? "right" : "left" }) : null, shade ? (_jsx("div", { ref: shadeRef, "aria-hidden": "true", style: shade.kind === "flat" ? flatStyle(shade.opacity) : castStyle(spine, shade.opacity) })) : null] }));
508
+ function PageFace({ page, pageNumber, bookmarked, highlights, pageWidth, imageWidth, spine, shade, shadeRef, }) {
509
+ const { parts } = useFlipBookContext();
510
+ return (_jsxs("div", { "data-flipbook-face": "", className: parts.classNames.page, style: {
511
+ background: PAPER,
512
+ // The outer corners only: the ones at the spine stay square.
513
+ borderRadius: PAGE_RADIUS[spine],
514
+ border: "var(--flipbook-page-border, none)",
515
+ ...parts.styles.page,
516
+ position: "absolute",
517
+ inset: 0,
518
+ overflow: "hidden",
519
+ boxSizing: "border-box",
520
+ }, children: [pageNumber === null ? null : (_jsx(FlipBookPageView, { page: page, pageNumber: pageNumber, pageWidth: pageWidth, imageWidth: imageWidth })), pageNumber === null ? null : _jsx(PageOverlays, { pageNumber: pageNumber, highlights: highlights }), _jsx("div", { "aria-hidden": "true", style: spineStyle(spine) }), bookmarked ? _jsx(BookmarkRibbon, { side: ribbonSide(spine) }) : null, shade ? (_jsx("div", { ref: shadeRef, "aria-hidden": "true", style: shade.kind === "flat" ? flatStyle(shade.opacity) : castStyle(spine, shade.opacity) })) : null] }));
290
521
  }
291
522
  const clampNumber = (value, min, max) => Math.min(max, Math.max(min, value));
292
523
  /**
@@ -294,7 +525,7 @@ const clampNumber = (value, min, max) => Math.min(max, Math.max(min, value));
294
525
  * closes when its page leaves the screen, when the reader presses anywhere
295
526
  * else, and from its own button; Escape is handled by the viewer.
296
527
  */
297
- function HotspotPopoverHost({ state, layout }) {
528
+ function HotspotPopoverHost({ state, layout, orientation, }) {
298
529
  const { content, ui, locale } = useFlipBookContext();
299
530
  const activeId = useSyncExternalStore(ui.subscribe, () => ui.getSnapshot().hotspot, () => null);
300
531
  const hotspot = useMemo(() => {
@@ -338,15 +569,18 @@ function HotspotPopoverHost({ state, layout }) {
338
569
  if (!hotspot || hotspot.content === undefined || side === null)
339
570
  return null;
340
571
  const { pageWidth, pageHeight, bookWidth } = layout;
341
- const shift = centeringShift(state.spread, state.view, state.direction);
572
+ const vertical = orientation === "vertical";
573
+ const shift = centeringShift(state.spread, state.view, state.direction, orientation);
574
+ const place = slotPosition(side, state.view, state.direction, orientation, pageWidth, pageHeight);
342
575
  const maxWidth = Math.max(160, Math.min(320, pageWidth * 0.9));
343
- const centre = slotLeft(side, state.view, state.direction, pageWidth) +
344
- (hotspot.x + hotspot.width / 2) * pageWidth +
345
- shift * pageWidth;
576
+ const centre = place.left + (hotspot.x + hotspot.width / 2) * pageWidth + (vertical ? 0 : shift * pageWidth);
346
577
  const half = maxWidth / 2;
347
578
  const left = bookWidth > maxWidth ? clampNumber(centre, half, bookWidth - half) : centre;
348
579
  const above = hotspot.y + hotspot.height > 0.6;
349
- const top = above ? hotspot.y * pageHeight - 8 : (hotspot.y + hotspot.height) * pageHeight + 8;
580
+ const pageTop = place.top + (vertical ? shift * pageHeight : 0);
581
+ const top = above
582
+ ? pageTop + hotspot.y * pageHeight - 8
583
+ : pageTop + (hotspot.y + hotspot.height) * pageHeight + 8;
350
584
  const close = () => {
351
585
  ui.setHotspot(null);
352
586
  for (const element of document.querySelectorAll("[data-flipbook-hotspot]")) {