@destyler/carousel 0.1.0 → 0.2.0-beta.5

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.
@@ -249,4 +249,4 @@ declare const splitIndicatorProps: <Props extends IndicatorProps>(props: Props)
249
249
  declare const itemProps: (keyof ItemProps)[];
250
250
  declare const splitItemProps: <Props extends ItemProps>(props: Props) => [ItemProps, Omit<Props, keyof ItemProps>];
251
251
  //#endregion
252
- export { type MachineApi as Api, type AutoplayStatusDetails, type UserDefinedContext as Context, type DragStatusDetails, type ElementIds, type IndicatorProps, type IntlTranslations, type ItemProps, type Orientation, type PageChangeDetails, type Service, anatomy, connect, indicatorProps, itemProps, machine, props, splitIndicatorProps, splitItemProps, splitProps };
252
+ export { type MachineApi as Api, type AutoplayStatusDetails, type UserDefinedContext as Context, type DragStatusDetails, type ElementIds, type IndicatorProps, type IntlTranslations, type ItemProps, type MachineState, type Orientation, type PageChangeDetails, type Service, type State, anatomy, connect, indicatorProps, itemProps, machine, props, splitIndicatorProps, splitItemProps, splitProps };
@@ -27,8 +27,7 @@ const dom = createScope({
27
27
  syncTabIndex(ctx) {
28
28
  const el = dom.getItemGroupEl(ctx);
29
29
  if (!el) return;
30
- const tabbables = getTabbables(el);
31
- if (tabbables.length > 0) el.removeAttribute("tabindex");
30
+ if (getTabbables(el).length > 0) el.removeAttribute("tabindex");
32
31
  else el.setAttribute("tabindex", "0");
33
32
  }
34
33
  });
@@ -148,7 +147,7 @@ function connect(state, send, normalize) {
148
147
  "dir": state.context.dir,
149
148
  "role": "group",
150
149
  "data-index": props$1.index,
151
- "data-inview": dataAttr(isInView),
150
+ "data-inview": String(isInView),
152
151
  "aria-roledescription": "slide",
153
152
  "data-orientation": state.context.orientation,
154
153
  "aria-label": state.context.slideCount ? translations.item(props$1.index, state.context.slideCount) : void 0,
@@ -209,7 +208,7 @@ function connect(state, send, normalize) {
209
208
  onKeyDown(event) {
210
209
  if (event.defaultPrevented) return;
211
210
  const src = "indicator";
212
- const keyMap = {
211
+ const exec = {
213
212
  ArrowDown(event$1) {
214
213
  if (horizontal) return;
215
214
  send({
@@ -258,12 +257,10 @@ function connect(state, send, normalize) {
258
257
  });
259
258
  event$1.preventDefault();
260
259
  }
261
- };
262
- const key = getEventKey(event, {
260
+ }[getEventKey(event, {
263
261
  dir: state.context.dir,
264
262
  orientation: state.context.orientation
265
- });
266
- const exec = keyMap[key];
263
+ })];
267
264
  exec?.(event);
268
265
  }
269
266
  });
@@ -308,8 +305,7 @@ function connect(state, send, normalize) {
308
305
  function getSnapAlign(ctx, props$1) {
309
306
  const { snapAlign = "start", index } = props$1;
310
307
  const perMove = ctx.slidesPerMove === "auto" ? Math.floor(ctx.slidesPerPage) : ctx.slidesPerMove;
311
- const shouldSnap = (index + perMove) % perMove === 0;
312
- return shouldSnap ? snapAlign : void 0;
308
+ return (index + perMove) % perMove === 0 ? snapAlign : void 0;
313
309
  }
314
310
 
315
311
  //#endregion
@@ -338,6 +334,24 @@ function getPageSnapPoints(totalSlides, slidesPerMove, slidesPerPage) {
338
334
  for (let i = 0; i < totalSlides - 1; i += perMove) snapPoints.push(i);
339
335
  return snapPoints;
340
336
  }
337
+ function ensureItemGroupEl(ctx, fn) {
338
+ const win = dom.getWin(ctx);
339
+ let rafId = null;
340
+ let cleanup;
341
+ const run = () => {
342
+ const el = dom.getItemGroupEl(ctx);
343
+ if (!el) {
344
+ rafId = win.requestAnimationFrame(run);
345
+ return;
346
+ }
347
+ cleanup = fn(el);
348
+ };
349
+ run();
350
+ return () => {
351
+ if (rafId != null) win.cancelAnimationFrame(rafId);
352
+ cleanup?.();
353
+ };
354
+ }
341
355
  function machine(userContext) {
342
356
  const ctx = compact(userContext);
343
357
  return createMachine({
@@ -423,6 +437,11 @@ function machine(userContext) {
423
437
  "AUTOPLAY.START": {
424
438
  target: "autoplay",
425
439
  actions: ["invokeAutoplayStart"]
440
+ },
441
+ "INVIEW.SET": {
442
+ target: "idle",
443
+ internal: false,
444
+ actions: ["setSlidesInView"]
426
445
  }
427
446
  }
428
447
  },
@@ -434,6 +453,11 @@ function machine(userContext) {
434
453
  "DRAGGING.END": {
435
454
  target: "idle",
436
455
  actions: ["endDragging", "invokeDraggingEnd"]
456
+ },
457
+ "INVIEW.SET": {
458
+ target: "dragging",
459
+ internal: false,
460
+ actions: ["setSlidesInView"]
437
461
  }
438
462
  }
439
463
  },
@@ -446,78 +470,85 @@ function machine(userContext) {
446
470
  target: "dragging",
447
471
  actions: ["invokeDragStart"]
448
472
  },
449
- "AUTOPLAY.PAUSE": "idle"
473
+ "AUTOPLAY.PAUSE": "idle",
474
+ "INVIEW.SET": {
475
+ target: "autoplay",
476
+ internal: false,
477
+ actions: ["setSlidesInView"]
478
+ }
450
479
  }
451
480
  }
452
481
  }
453
482
  }, {
454
483
  activities: {
455
484
  trackSlideMutation(ctx$1, _evt, { send }) {
456
- const el = dom.getItemGroupEl(ctx$1);
457
- if (!el) return;
458
- const win = dom.getWin(ctx$1);
459
- const observer = new win.MutationObserver(() => {
460
- send({
461
- type: "SNAP.REFRESH",
462
- src: "slide.mutation"
485
+ return ensureItemGroupEl(ctx$1, (el) => {
486
+ const observer = new (dom.getWin(ctx$1)).MutationObserver(() => {
487
+ send({
488
+ type: "SNAP.REFRESH",
489
+ src: "slide.mutation"
490
+ });
491
+ dom.syncTabIndex(ctx$1);
463
492
  });
464
493
  dom.syncTabIndex(ctx$1);
494
+ observer.observe(el, {
495
+ childList: true,
496
+ subtree: true
497
+ });
498
+ return () => observer.disconnect();
465
499
  });
466
- dom.syncTabIndex(ctx$1);
467
- observer.observe(el, {
468
- childList: true,
469
- subtree: true
470
- });
471
- return () => observer.disconnect();
472
500
  },
473
501
  trackSlideResize(ctx$1, _evt, { send }) {
474
- const el = dom.getItemGroupEl(ctx$1);
475
- if (!el) return;
476
- const win = dom.getWin(ctx$1);
477
- const observer = new win.ResizeObserver(() => {
478
- send({
479
- type: "SNAP.REFRESH",
480
- src: "slide.resize"
502
+ return ensureItemGroupEl(ctx$1, () => {
503
+ const observer = new (dom.getWin(ctx$1)).ResizeObserver(() => {
504
+ send({
505
+ type: "SNAP.REFRESH",
506
+ src: "slide.resize"
507
+ });
481
508
  });
509
+ dom.getItemEls(ctx$1).forEach((slide) => observer.observe(slide));
510
+ return () => observer.disconnect();
482
511
  });
483
- dom.getItemEls(ctx$1).forEach((slide) => observer.observe(slide));
484
- return () => observer.disconnect();
485
512
  },
486
- trackSlideIntersections(ctx$1) {
487
- const el = dom.getItemGroupEl(ctx$1);
488
- const win = dom.getWin(ctx$1);
489
- const observer = new win.IntersectionObserver((entries) => {
490
- const slidesInView = entries.reduce((acc, entry) => {
491
- const target = entry.target;
492
- const index = Number(target.dataset.index ?? "-1");
493
- if (index == null || Number.isNaN(index) || index === -1) return acc;
494
- return entry.isIntersecting ? add(acc, index) : remove(acc, index);
495
- }, ctx$1.slidesInView);
496
- ctx$1.slidesInView = uniq(slidesInView);
497
- }, {
498
- root: el,
499
- threshold: ctx$1.inViewThreshold
513
+ trackSlideIntersections(ctx$1, _evt, { send }) {
514
+ return ensureItemGroupEl(ctx$1, (el) => {
515
+ const observer = new (dom.getWin(ctx$1)).IntersectionObserver((entries) => {
516
+ const nextSlides = uniq(entries.reduce((acc, entry) => {
517
+ const target = entry.target;
518
+ const index = Number(target.dataset.index ?? "-1");
519
+ if (index == null || Number.isNaN(index) || index === -1) return acc;
520
+ return entry.isIntersecting ? add(acc, index) : remove(acc, index);
521
+ }, ctx$1.slidesInView));
522
+ if (isEqual(ctx$1.slidesInView, nextSlides)) return;
523
+ send({
524
+ type: "INVIEW.SET",
525
+ slidesInView: nextSlides
526
+ });
527
+ }, {
528
+ root: el,
529
+ threshold: ctx$1.inViewThreshold
530
+ });
531
+ dom.getItemEls(ctx$1).forEach((slide) => observer.observe(slide));
532
+ return () => observer.disconnect();
500
533
  });
501
- dom.getItemEls(ctx$1).forEach((slide) => observer.observe(slide));
502
- return () => observer.disconnect();
503
534
  },
504
535
  trackScroll(ctx$1) {
505
- const el = dom.getItemGroupEl(ctx$1);
506
- if (!el) return;
507
- const onScrollEnd = () => {
508
- if (ctx$1.slidesInView.length === 0) return;
509
- const scrollPosition = ctx$1.isHorizontal ? el.scrollLeft : el.scrollTop;
510
- const page = ctx$1.pageSnapPoints.findIndex((point) => Math.abs(point - scrollPosition) < 1);
511
- if (page === -1) return;
512
- set.page(ctx$1, page);
513
- };
514
- const onScroll = () => {
515
- clearTimeout(ctx$1.timeoutRef.current);
516
- ctx$1.timeoutRef.current = setTimeout(() => {
517
- onScrollEnd?.();
518
- }, 150);
519
- };
520
- return addDomEvent(el, "scroll", onScroll, { passive: true });
536
+ return ensureItemGroupEl(ctx$1, (el) => {
537
+ const onScrollEnd = () => {
538
+ if (ctx$1.slidesInView.length === 0) return;
539
+ const scrollPosition = ctx$1.isHorizontal ? el.scrollLeft : el.scrollTop;
540
+ const page = ctx$1.pageSnapPoints.findIndex((point) => Math.abs(point - scrollPosition) < 1);
541
+ if (page === -1) return;
542
+ set.page(ctx$1, page);
543
+ };
544
+ const onScroll = () => {
545
+ clearTimeout(ctx$1.timeoutRef.current);
546
+ ctx$1.timeoutRef.current = setTimeout(() => {
547
+ onScrollEnd?.();
548
+ }, 150);
549
+ };
550
+ return addDomEvent(el, "scroll", onScroll, { passive: true });
551
+ });
521
552
  },
522
553
  trackDocumentVisibility(ctx$1, _evt, { send }) {
523
554
  const doc = dom.getDoc(ctx$1);
@@ -531,8 +562,7 @@ function machine(userContext) {
531
562
  return addDomEvent(doc, "visibilitychange", onVisibilityChange);
532
563
  },
533
564
  trackPointerMove(ctx$1, _evt, { send }) {
534
- const doc = dom.getDoc(ctx$1);
535
- return trackPointerMove(doc, {
565
+ return trackPointerMove(dom.getDoc(ctx$1), {
536
566
  onPointerMove({ event }) {
537
567
  send({
538
568
  type: "DRAGGING",
@@ -549,6 +579,7 @@ function machine(userContext) {
549
579
  actions: {
550
580
  resetScrollPosition(ctx$1) {
551
581
  const el = dom.getItemGroupEl(ctx$1);
582
+ if (!el) return;
552
583
  el.scrollTo(0, 0);
553
584
  },
554
585
  clearScrollEndTimer(ctx$1) {
@@ -589,8 +620,7 @@ function machine(userContext) {
589
620
  },
590
621
  setSnapPoints(ctx$1) {
591
622
  queueMicrotask(() => {
592
- const el = dom.getItemGroupEl(ctx$1);
593
- const scrollSnapPoints = getScrollSnapPositions(el);
623
+ const scrollSnapPoints = getScrollSnapPositions(dom.getItemGroupEl(ctx$1));
594
624
  ctx$1.pageSnapPoints = ctx$1.isHorizontal ? scrollSnapPoints.x : scrollSnapPoints.y;
595
625
  });
596
626
  },
@@ -601,8 +631,7 @@ function machine(userContext) {
601
631
  el.style.setProperty("scroll-snap-type", "none");
602
632
  },
603
633
  scrollSlides(ctx$1, evt) {
604
- const el = dom.getItemGroupEl(ctx$1);
605
- el.scrollBy({
634
+ dom.getItemGroupEl(ctx$1).scrollBy({
606
635
  left: evt.left,
607
636
  top: evt.top,
608
637
  behavior: "instant"
@@ -625,8 +654,7 @@ function machine(userContext) {
625
654
  top: closestY,
626
655
  behavior: "smooth"
627
656
  });
628
- const scrollSnapType = el.dataset.scrollSnapType;
629
- if (scrollSnapType) {
657
+ if (el.dataset.scrollSnapType) {
630
658
  el.style.removeProperty("scroll-snap-type");
631
659
  delete el.dataset.scrollSnapType;
632
660
  }
@@ -678,6 +706,10 @@ function machine(userContext) {
678
706
  isPlaying: false,
679
707
  page: ctx$1.page
680
708
  });
709
+ },
710
+ setSlidesInView(ctx$1, evt) {
711
+ if (evt.type !== "INVIEW.SET") return;
712
+ ctx$1.slidesInView = evt.slidesInView;
681
713
  }
682
714
  },
683
715
  delays: { AUTOPLAY_INTERVAL: (ctx$1) => ctx$1.autoplayInterval }
package/package.json CHANGED
@@ -1,27 +1,36 @@
1
1
  {
2
2
  "name": "@destyler/carousel",
3
3
  "type": "module",
4
- "version": "0.1.0",
4
+ "version": "0.2.0-beta.5",
5
5
  "description": "an interactive slideshow component for cycling through elements.",
6
6
  "author": "Elone Hoo <elonehoo@gmail.com>",
7
7
  "license": "MIT",
8
+ "homepage": "https://github.com/destyler/destyler",
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "git+https://github.com/destyler/destyler.git",
12
+ "directory": "packages/components/carousel"
13
+ },
8
14
  "exports": {
9
- ".": "./dist/index.js"
15
+ ".": "./dist/index.mjs"
10
16
  },
11
- "main": "dist/index.js",
12
- "module": "dist/index.js",
13
- "types": "dist/index.d.ts",
17
+ "main": "dist/index.mjs",
18
+ "module": "dist/index.mjs",
19
+ "types": "dist/index.d.mts",
14
20
  "files": [
15
21
  "*.d.ts",
16
22
  "dist"
17
23
  ],
18
24
  "dependencies": {
19
- "@destyler/anatomy": "0.1.0",
20
- "@destyler/dom": "0.1.0",
21
- "@destyler/utils": "0.1.0",
22
- "@destyler/xstate": "0.1.0",
23
- "@destyler/scroll-snap": "0.1.0",
24
- "@destyler/types": "0.1.0"
25
+ "@destyler/anatomy": "0.2.0-beta.5",
26
+ "@destyler/dom": "0.2.0-beta.5",
27
+ "@destyler/scroll-snap": "0.2.0-beta.5",
28
+ "@destyler/types": "0.2.0-beta.5",
29
+ "@destyler/xstate": "0.2.0-beta.5",
30
+ "@destyler/utils": "0.2.0-beta.5"
31
+ },
32
+ "publishConfig": {
33
+ "access": "public"
25
34
  },
26
35
  "scripts": {
27
36
  "clear": "rimraf ./dist",