@open-slide/core 1.12.1 → 1.13.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.
@@ -16,7 +16,7 @@ import {
16
16
  } from '@dnd-kit/sortable';
17
17
  import { CSS } from '@dnd-kit/utilities';
18
18
  import { Copy, Grid2x2, ListOrdered, type LucideIcon, Sparkles, Trash2 } from 'lucide-react';
19
- import { Fragment, useEffect, useRef, useState } from 'react';
19
+ import { Fragment, useCallback, useEffect, useRef, useState } from 'react';
20
20
  import {
21
21
  ContextMenu,
22
22
  ContextMenuContent,
@@ -62,6 +62,9 @@ const DEFAULT_VERTICAL_THUMB_WIDTH = 184;
62
62
  const VERTICAL_RAIL_CHROME = 80;
63
63
  const MIN_VERTICAL_THUMB_WIDTH = 120;
64
64
  const HORIZONTAL_THUMB_HEIGHT = 64;
65
+ const VERTICAL_THUMB_PADDING_Y = 12;
66
+ const VERTICAL_THUMB_GAP = 8;
67
+ const VIRTUAL_OVERSCAN = 4;
65
68
 
66
69
  export function ThumbnailRail({
67
70
  pages,
@@ -89,9 +92,88 @@ export function ThumbnailRail({
89
92
  });
90
93
  }, [current]);
91
94
 
95
+ const thumbWidth =
96
+ width != null
97
+ ? Math.max(MIN_VERTICAL_THUMB_WIDTH, width - VERTICAL_RAIL_CHROME)
98
+ : DEFAULT_VERTICAL_THUMB_WIDTH;
99
+ const scale = thumbWidth / CANVAS_WIDTH;
100
+ const height = CANVAS_HEIGHT * scale;
101
+ const rowHeight = height + VERTICAL_THUMB_PADDING_Y + VERTICAL_THUMB_GAP;
102
+
103
+ const renderThumb = useCallback(
104
+ (PageComp: Page, i: number) => {
105
+ const active = i === current;
106
+ const inner = (
107
+ <ThumbContents
108
+ index={i}
109
+ total={pages.length}
110
+ active={active}
111
+ page={PageComp}
112
+ design={design}
113
+ scale={scale}
114
+ thumbWidth={thumbWidth}
115
+ height={height}
116
+ moduleTransition={moduleTransition}
117
+ />
118
+ );
119
+
120
+ const node = onReorder ? (
121
+ <SortableThumb
122
+ index={i}
123
+ active={active}
124
+ activeRef={active ? activeRef : undefined}
125
+ onSelect={() => onSelect(i)}
126
+ ariaLabel={format(t.thumbnailRail.goToPageAria, { n: i + 1 })}
127
+ >
128
+ {inner}
129
+ </SortableThumb>
130
+ ) : (
131
+ <button
132
+ type="button"
133
+ ref={active ? activeRef : undefined}
134
+ onClick={() => onSelect(i)}
135
+ aria-label={format(t.thumbnailRail.goToPageAria, { n: i + 1 })}
136
+ aria-current={active ? 'true' : undefined}
137
+ className={thumbButtonClass(active)}
138
+ >
139
+ {inner}
140
+ </button>
141
+ );
142
+
143
+ if (!actions) {
144
+ return <Fragment key={i}>{node}</Fragment>;
145
+ }
146
+ return (
147
+ <ThumbContextMenu
148
+ key={i}
149
+ index={i}
150
+ actions={actions}
151
+ pageCount={pages.length}
152
+ ariaLabel={format(t.thumbnailRail.pageActionsAria, { n: i + 1 })}
153
+ >
154
+ {node}
155
+ </ThumbContextMenu>
156
+ );
157
+ },
158
+ [
159
+ actions,
160
+ current,
161
+ design,
162
+ height,
163
+ moduleTransition,
164
+ onReorder,
165
+ onSelect,
166
+ pages.length,
167
+ scale,
168
+ thumbWidth,
169
+ t.thumbnailRail.goToPageAria,
170
+ t.thumbnailRail.pageActionsAria,
171
+ ],
172
+ );
173
+
92
174
  if (orientation === 'horizontal') {
93
175
  const scale = HORIZONTAL_THUMB_HEIGHT / CANVAS_HEIGHT;
94
- const width = CANVAS_WIDTH * scale;
176
+ const horizontalWidth = CANVAS_WIDTH * scale;
95
177
  return (
96
178
  <div className="bg-sidebar">
97
179
  <div className="overflow-x-auto overflow-y-hidden">
@@ -124,7 +206,7 @@ export function ThumbnailRail({
124
206
  ? 'border-brand shadow-[0_0_0_1px_var(--brand)]'
125
207
  : 'border-hairline group-hover/thumb:border-foreground/25',
126
208
  )}
127
- style={{ width, height: HORIZONTAL_THUMB_HEIGHT }}
209
+ style={{ width: horizontalWidth, height: HORIZONTAL_THUMB_HEIGHT }}
128
210
  >
129
211
  <SlideCanvas scale={scale} center={false} flat freezeMotion design={design}>
130
212
  <SlidePageProvider index={i} total={pages.length}>
@@ -154,68 +236,6 @@ export function ThumbnailRail({
154
236
  );
155
237
  }
156
238
 
157
- const thumbWidth =
158
- width != null
159
- ? Math.max(MIN_VERTICAL_THUMB_WIDTH, width - VERTICAL_RAIL_CHROME)
160
- : DEFAULT_VERTICAL_THUMB_WIDTH;
161
- const scale = thumbWidth / CANVAS_WIDTH;
162
- const height = CANVAS_HEIGHT * scale;
163
-
164
- const renderThumb = (PageComp: Page, i: number) => {
165
- const active = i === current;
166
- const inner = (
167
- <ThumbContents
168
- index={i}
169
- total={pages.length}
170
- active={active}
171
- page={PageComp}
172
- design={design}
173
- scale={scale}
174
- thumbWidth={thumbWidth}
175
- height={height}
176
- moduleTransition={moduleTransition}
177
- />
178
- );
179
-
180
- const node = onReorder ? (
181
- <SortableThumb
182
- index={i}
183
- active={active}
184
- activeRef={active ? activeRef : undefined}
185
- onSelect={() => onSelect(i)}
186
- ariaLabel={format(t.thumbnailRail.goToPageAria, { n: i + 1 })}
187
- >
188
- {inner}
189
- </SortableThumb>
190
- ) : (
191
- <button
192
- type="button"
193
- ref={active ? activeRef : undefined}
194
- onClick={() => onSelect(i)}
195
- aria-label={format(t.thumbnailRail.goToPageAria, { n: i + 1 })}
196
- aria-current={active ? 'true' : undefined}
197
- className={thumbButtonClass(active)}
198
- >
199
- {inner}
200
- </button>
201
- );
202
-
203
- if (!actions) {
204
- return <Fragment key={i}>{node}</Fragment>;
205
- }
206
- return (
207
- <ThumbContextMenu
208
- key={i}
209
- index={i}
210
- actions={actions}
211
- pageCount={pages.length}
212
- ariaLabel={format(t.thumbnailRail.pageActionsAria, { n: i + 1 })}
213
- >
214
- {node}
215
- </ThumbContextMenu>
216
- );
217
- };
218
-
219
239
  const list = (
220
240
  <aside className="flex flex-col gap-2 px-3 pb-3">
221
241
  <div className="-mx-3 sticky top-0 z-10 bg-sidebar px-4 pt-3 pb-1">
@@ -247,7 +267,13 @@ export function ThumbnailRail({
247
267
  </div>
248
268
  </div>
249
269
  </div>
250
- {pages.map(renderThumb)}
270
+ <VirtualThumbList
271
+ pages={pages}
272
+ current={current}
273
+ rowHeight={rowHeight}
274
+ activeRef={activeRef}
275
+ renderThumb={renderThumb}
276
+ />
251
277
  </aside>
252
278
  );
253
279
 
@@ -280,6 +306,137 @@ function thumbButtonClass(active: boolean): string {
280
306
  );
281
307
  }
282
308
 
309
+ function VirtualThumbList({
310
+ pages,
311
+ current,
312
+ rowHeight,
313
+ activeRef,
314
+ renderThumb,
315
+ }: {
316
+ pages: Page[];
317
+ current: number;
318
+ rowHeight: number;
319
+ activeRef: React.MutableRefObject<HTMLButtonElement | null>;
320
+ renderThumb: (page: Page, index: number) => React.ReactNode;
321
+ }) {
322
+ const rootRef = useRef<HTMLDivElement | null>(null);
323
+ const viewportRef = useRef<HTMLElement | null>(null);
324
+ const [range, setRange] = useState(() => getInitialVisibleRange(current, pages.length));
325
+
326
+ const updateRange = useCallback(() => {
327
+ const viewport = viewportRef.current;
328
+ const root = rootRef.current;
329
+ if (!viewport || !root) return;
330
+ const scrollTop = Math.max(0, viewport.scrollTop - root.offsetTop);
331
+ setRange(getVisibleRange(scrollTop, pages.length, viewport.clientHeight, rowHeight));
332
+ }, [pages.length, rowHeight]);
333
+
334
+ useEffect(() => {
335
+ const root = rootRef.current;
336
+ if (!root) return;
337
+ const viewport = root.closest('[data-slot="scroll-area-viewport"]') as HTMLElement | null;
338
+ if (!viewport) return;
339
+ viewportRef.current = viewport;
340
+
341
+ let frame = 0;
342
+ const scheduleUpdate = () => {
343
+ cancelAnimationFrame(frame);
344
+ frame = requestAnimationFrame(updateRange);
345
+ };
346
+ const resizeObserver = new ResizeObserver(scheduleUpdate);
347
+
348
+ viewport.addEventListener('scroll', scheduleUpdate, { passive: true });
349
+ resizeObserver.observe(viewport);
350
+ scheduleUpdate();
351
+
352
+ return () => {
353
+ cancelAnimationFrame(frame);
354
+ viewport.removeEventListener('scroll', scheduleUpdate);
355
+ resizeObserver.disconnect();
356
+ if (viewportRef.current === viewport) viewportRef.current = null;
357
+ };
358
+ }, [updateRange]);
359
+
360
+ useEffect(() => {
361
+ const viewport = viewportRef.current;
362
+ const root = rootRef.current;
363
+ if (!viewport || !root) return;
364
+
365
+ const top = root.offsetTop + current * rowHeight;
366
+ const bottom = top + rowHeight;
367
+ const viewportTop = viewport.scrollTop;
368
+ const viewportBottom = viewportTop + viewport.clientHeight;
369
+
370
+ if (top < viewportTop) {
371
+ viewport.scrollTo({ top, behavior: scrollBehavior() });
372
+ } else if (bottom > viewportBottom) {
373
+ viewport.scrollTo({ top: bottom - viewport.clientHeight, behavior: scrollBehavior() });
374
+ } else {
375
+ activeRef.current?.scrollIntoView({
376
+ block: 'nearest',
377
+ inline: 'nearest',
378
+ behavior: scrollBehavior(),
379
+ });
380
+ }
381
+ }, [activeRef, current, rowHeight]);
382
+
383
+ const visibleRange = clampVisibleRange(range, current, pages.length);
384
+ const visible = [];
385
+ for (let i = visibleRange.start; i < visibleRange.end; i++) {
386
+ visible.push(
387
+ <div
388
+ key={i}
389
+ className="absolute right-0 left-0"
390
+ style={{ top: i * rowHeight, height: rowHeight }}
391
+ >
392
+ {renderThumb(pages[i], i)}
393
+ </div>,
394
+ );
395
+ }
396
+
397
+ return (
398
+ <div ref={rootRef} className="relative" style={{ height: pages.length * rowHeight }}>
399
+ {visible}
400
+ </div>
401
+ );
402
+ }
403
+
404
+ type VisibleRange = { start: number; end: number };
405
+
406
+ function clampVisibleRange(range: VisibleRange, current: number, count: number): VisibleRange {
407
+ if (count <= 0) return { start: 0, end: 0 };
408
+ if (range.start >= 0 && range.start < count && range.start < range.end) {
409
+ return { start: range.start, end: Math.min(range.end, count) };
410
+ }
411
+ const clampedCurrent = Math.min(Math.max(current, 0), count - 1);
412
+ return getInitialVisibleRange(clampedCurrent, count);
413
+ }
414
+
415
+ function getVisibleRange(
416
+ scrollTop: number,
417
+ count: number,
418
+ viewportHeight: number,
419
+ rowHeight: number,
420
+ ): VisibleRange {
421
+ if (count <= 0) return { start: 0, end: 0 };
422
+ const visibleRows = Math.max(1, Math.ceil(viewportHeight / rowHeight));
423
+ const firstVisible = Math.min(count - 1, Math.max(0, Math.floor(scrollTop / rowHeight)));
424
+ const start = Math.max(0, firstVisible - VIRTUAL_OVERSCAN);
425
+ const end = Math.min(count, firstVisible + visibleRows + VIRTUAL_OVERSCAN + 1);
426
+ return { start, end };
427
+ }
428
+
429
+ function getInitialVisibleRange(current: number, count: number): VisibleRange {
430
+ if (count <= 0) return { start: 0, end: 0 };
431
+ const start = Math.max(0, current - VIRTUAL_OVERSCAN);
432
+ const end = Math.min(count, current + VIRTUAL_OVERSCAN + 1);
433
+ return { start, end };
434
+ }
435
+
436
+ function scrollBehavior(): ScrollBehavior {
437
+ return window.matchMedia('(prefers-reduced-motion: reduce)').matches ? 'auto' : 'smooth';
438
+ }
439
+
283
440
  function ThumbContents({
284
441
  index,
285
442
  total,
@@ -12,6 +12,13 @@ export type SlideTransition = {
12
12
  easing?: string;
13
13
  enter?: TransitionPhase;
14
14
  exit?: TransitionPhase;
15
+ sharedElements?: boolean | SharedElementTransition;
16
+ };
17
+
18
+ export type SharedElementTransition = {
19
+ duration?: number;
20
+ easing?: string;
21
+ delay?: number;
15
22
  };
16
23
 
17
24
  export function resolveTransition(
@@ -15,6 +15,8 @@ type UseClickPageNavigationOptions<T extends HTMLElement> = {
15
15
  canNext: boolean;
16
16
  onPrev: () => void;
17
17
  onNext: () => void;
18
+ onCenterClick?: () => void;
19
+ onViewportClick?: (point: { x: number; y: number }) => void;
18
20
  };
19
21
 
20
22
  export function useClickPageNavigation<T extends HTMLElement>({
@@ -25,6 +27,8 @@ export function useClickPageNavigation<T extends HTMLElement>({
25
27
  canNext,
26
28
  onPrev,
27
29
  onNext,
30
+ onCenterClick,
31
+ onViewportClick,
28
32
  }: UseClickPageNavigationOptions<T>) {
29
33
  useEffect(() => {
30
34
  const el = ref.current;
@@ -33,20 +37,24 @@ export function useClickPageNavigation<T extends HTMLElement>({
33
37
  const onClick = (event: MouseEvent) => {
34
38
  if (event.button !== 0 || event.defaultPrevented) return;
35
39
  const target = event.target;
36
- if (target instanceof HTMLElement && target.closest(NAV_PASSTHROUGH)) return;
40
+ if (target instanceof Element && target.closest(NAV_PASSTHROUGH)) return;
37
41
  if (window.getSelection()?.toString()) return;
38
42
 
39
43
  const rect = el.getBoundingClientRect();
40
44
  if (rect.width === 0) return;
41
45
  const x = (event.clientX - rect.left) / rect.width;
46
+ const y = rect.height === 0 ? 0 : (event.clientY - rect.top) / rect.height;
47
+ onViewportClick?.({ x, y });
42
48
  if (x < edgeRatio) {
43
49
  if (canPrev) onPrev();
44
50
  } else if (x > 1 - edgeRatio) {
45
51
  if (canNext) onNext();
52
+ } else {
53
+ onCenterClick?.();
46
54
  }
47
55
  };
48
56
 
49
57
  el.addEventListener('click', onClick);
50
58
  return () => el.removeEventListener('click', onClick);
51
- }, [ref, enabled, edgeRatio, canPrev, canNext, onPrev, onNext]);
59
+ }, [ref, enabled, edgeRatio, canPrev, canNext, onPrev, onNext, onCenterClick, onViewportClick]);
52
60
  }
@@ -86,6 +86,7 @@ export function Slide() {
86
86
  const { renameSlide } = useFolders();
87
87
  const slideViewportRef = useRef<HTMLElement>(null);
88
88
  const t = useLocale();
89
+ const isMobile = useIsMobile();
89
90
  const prefersReducedMotion = usePrefersReducedMotion();
90
91
 
91
92
  const modulePages = useMemo(() => slide?.default ?? [], [slide]);
@@ -636,8 +637,8 @@ export function Slide() {
636
637
  <Button
637
638
  size="sm"
638
639
  variant="brand"
639
- onClick={() => setPlayMode('fullscreen')}
640
- className="rounded-r-none px-2.5 md:px-3"
640
+ onClick={() => setPlayMode(isMobile ? 'window' : 'fullscreen')}
641
+ className="px-2.5 md:rounded-r-none md:px-3"
641
642
  >
642
643
  <Play className="size-3.5 fill-current" />
643
644
  <span className="hidden md:inline">{t.slide.present}</span>
@@ -652,7 +653,7 @@ export function Slide() {
652
653
  title={t.slide.presentMenuAria}
653
654
  className={cn(
654
655
  buttonVariants({ variant: 'brand', size: 'sm' }),
655
- 'rounded-l-none px-1.5 shadow-[inset_1px_0_0_oklch(0_0_0/0.12),inset_0_1px_0_oklch(1_0_0/0.18),0_1px_0_oklch(0_0_0/0.16)]',
656
+ 'hidden rounded-l-none px-1.5 shadow-[inset_1px_0_0_oklch(0_0_0/0.12),inset_0_1px_0_oklch(1_0_0/0.18),0_1px_0_oklch(0_0_0/0.16)] md:inline-flex',
656
657
  )}
657
658
  >
658
659
  <ChevronDown className="size-3.5" />