@bonprix-ds/react-carousel-v2 1.0.0
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.
- package/dist/index.d.ts +93 -0
- package/dist/index.js +2 -0
- package/dist/src/CarouselArrow.js +63 -0
- package/dist/src/CarouselArrow.js.map +1 -0
- package/dist/src/CarouselV2.js +13 -0
- package/dist/src/CarouselV2.js.map +1 -0
- package/dist/src/CarouselV2Root.js +118 -0
- package/dist/src/CarouselV2Root.js.map +1 -0
- package/dist/src/PositionIndicator/PositionIndicator.js +19 -0
- package/dist/src/PositionIndicator/PositionIndicator.js.map +1 -0
- package/dist/src/Slide/Slide.js +26 -0
- package/dist/src/Slide/Slide.js.map +1 -0
- package/dist/src/Slides/Slides.js +38 -0
- package/dist/src/Slides/Slides.js.map +1 -0
- package/dist/src/carouselContext.js +20 -0
- package/dist/src/carouselContext.js.map +1 -0
- package/dist/src/useConsumeCarousel.js +8 -0
- package/dist/src/useConsumeCarousel.js.map +1 -0
- package/dist/src/useCreateCarousel.js +161 -0
- package/dist/src/useCreateCarousel.js.map +1 -0
- package/dist/src/useSimulateTouch.js +30 -0
- package/dist/src/useSimulateTouch.js.map +1 -0
- package/dist/src/useStart.js +18 -0
- package/dist/src/useStart.js.map +1 -0
- package/dist/src/utils.js +41 -0
- package/dist/src/utils.js.map +1 -0
- package/package.json +57 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { Breakpoint } from '@bonprix-ds/utils';
|
|
2
|
+
import { default as default_2 } from 'react';
|
|
3
|
+
import { ReactNode } from 'react';
|
|
4
|
+
import { Ref } from 'react';
|
|
5
|
+
|
|
6
|
+
declare type AccessibilityLabel = {
|
|
7
|
+
/** Sets an invisible header for the carousel to create a landmark. Do not use both `title` and `aria-labelledby`. */
|
|
8
|
+
title: string;
|
|
9
|
+
/** Defines the level of the used header element. Only applicable if using `title` instead of `aria-labelledby`. */
|
|
10
|
+
titleLevel?: "h2" | "h3" | "h4" | "h5" | "h6";
|
|
11
|
+
"aria-labelledby"?: never;
|
|
12
|
+
} | {
|
|
13
|
+
/** Use if a title already exists on the page. Do not use both `title`and `aria-labelledby`. */
|
|
14
|
+
"aria-labelledby": string;
|
|
15
|
+
title?: never;
|
|
16
|
+
titleLevel?: never;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
declare const CarouselV2: {
|
|
20
|
+
(props: CarouselV2Props): default_2.JSX.Element;
|
|
21
|
+
displayName: string;
|
|
22
|
+
Slides: {
|
|
23
|
+
({ children }: SlidesProps): default_2.JSX.Element;
|
|
24
|
+
displayName: string;
|
|
25
|
+
};
|
|
26
|
+
Slide: {
|
|
27
|
+
({ children, ...props }: SlideProps): default_2.JSX.Element;
|
|
28
|
+
displayName: string;
|
|
29
|
+
};
|
|
30
|
+
PositionIndicator: {
|
|
31
|
+
({ ref }: PositionIndicatorProps): default_2.JSX.Element;
|
|
32
|
+
displayName: string;
|
|
33
|
+
};
|
|
34
|
+
};
|
|
35
|
+
export default CarouselV2;
|
|
36
|
+
|
|
37
|
+
export declare type CarouselV2Props = AccessibilityLabel & {
|
|
38
|
+
/** The content of the carousel. Here, all of the subcomponents should be placed. */
|
|
39
|
+
children: ReactNode;
|
|
40
|
+
/** Amount of slides that will be visible at the same time, thus building a page. */
|
|
41
|
+
perPage?: number | ResponsiveNumber;
|
|
42
|
+
/** Amount of slides to move per arrow icon button click. */
|
|
43
|
+
perMove?: number | ResponsiveNumber;
|
|
44
|
+
/** If true, the carousel will rewind to the beginning when reaching the end or going to end when going before the first slide. There must be more than two pages for this to work! Doesn't run smoothly with `padding` right now. */
|
|
45
|
+
rewind?: boolean;
|
|
46
|
+
/** If set to `true`, the primary variant of icon buttons will be used to achieve a high contrast. */
|
|
47
|
+
buttonsContrast?: boolean;
|
|
48
|
+
/** The padding at the right side of the page in pixels. Can be used to implement the so-called "scroll hint" */
|
|
49
|
+
padding?: number;
|
|
50
|
+
/** Callback function that is called when the slide changes. */
|
|
51
|
+
onMove?: (destIndex: number, prevIndex: number) => void;
|
|
52
|
+
/** Manages the behavior of the arrow icon buttons. */
|
|
53
|
+
showArrows?: "always" | "hover" | "never";
|
|
54
|
+
/** The gap between slides in pixels. */
|
|
55
|
+
gap?: number;
|
|
56
|
+
/** If set to `true`, the color of the arrow buttons will be inverted. */
|
|
57
|
+
invertButtons?: boolean;
|
|
58
|
+
/** If set to `true`, the horizontal padding for the arrow buttons will be removed. */
|
|
59
|
+
disableHorizontalPadding?: boolean;
|
|
60
|
+
/** If set to `true`, scrolling via mouse drag will be enabled just like you would expect from a mobile touch pointer. */
|
|
61
|
+
simulateTouch?: boolean;
|
|
62
|
+
/** The index of the initial slide to be displayed. For programmatic scrolling after initial mount, use `ref`'s `moveToPage`. */
|
|
63
|
+
start?: number;
|
|
64
|
+
/** A reference to the Carousel instance. You can call `moveToPage(pageNumber: number)` on this one, e.g. `carouselRef.current.moveToPage(2)`. To set an initial slide, use `start` prop instead. */
|
|
65
|
+
ref?: Ref<CarouselV2Ref>;
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
declare interface CarouselV2Ref {
|
|
69
|
+
moveToPage: (pageNumber: number) => void;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
declare interface PositionIndicatorProps {
|
|
73
|
+
/** Ref to the element */
|
|
74
|
+
ref?: Ref<HTMLDivElement>;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
declare type ResponsiveNumber = Partial<Record<Breakpoint, number>>;
|
|
78
|
+
|
|
79
|
+
declare interface SlideProps {
|
|
80
|
+
/** The content of the slide item. */
|
|
81
|
+
children: ReactNode;
|
|
82
|
+
/** An optional label of the slide item. */
|
|
83
|
+
"aria-label"?: string;
|
|
84
|
+
/** An optional id of a DOM element that labels the slide item. */
|
|
85
|
+
"aria-labelledby"?: string;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
declare interface SlidesProps {
|
|
89
|
+
/** The content of the slides section. This is where `<CarouselV2.Slide>` items go. */
|
|
90
|
+
children: ReactNode;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export { }
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import "react";
|
|
3
|
+
import { tv as e } from "tailwind-variants";
|
|
4
|
+
import { BaseButton as t } from "@bonprix-ds/internal-components";
|
|
5
|
+
import n from "@bonprix-ds/react-theme-forcer";
|
|
6
|
+
import { jsx as r } from "react/jsx-runtime";
|
|
7
|
+
//#region src/CarouselArrow.tsx
|
|
8
|
+
var i = e({
|
|
9
|
+
slots: {
|
|
10
|
+
wrapper: "z-[1] absolute top-1/2 transform -translate-y-1/2",
|
|
11
|
+
button: "disabled:hidden"
|
|
12
|
+
},
|
|
13
|
+
variants: {
|
|
14
|
+
position: {
|
|
15
|
+
left: { wrapper: "start-3" },
|
|
16
|
+
right: { wrapper: "end-3" }
|
|
17
|
+
},
|
|
18
|
+
disableHorizontalPadding: {
|
|
19
|
+
true: "",
|
|
20
|
+
false: ""
|
|
21
|
+
},
|
|
22
|
+
show: {
|
|
23
|
+
always: { button: "" },
|
|
24
|
+
hover: { button: "hidden group-hover:block" },
|
|
25
|
+
never: { button: "hidden" }
|
|
26
|
+
}
|
|
27
|
+
},
|
|
28
|
+
compoundVariants: [{
|
|
29
|
+
disableHorizontalPadding: !0,
|
|
30
|
+
position: "left",
|
|
31
|
+
class: { wrapper: "start-0" }
|
|
32
|
+
}, {
|
|
33
|
+
disableHorizontalPadding: !0,
|
|
34
|
+
position: "right",
|
|
35
|
+
class: { wrapper: "end-0" }
|
|
36
|
+
}]
|
|
37
|
+
}), a = ({ position: e, buttonsContrast: a, invertButtons: o, disableHorizontalPadding: s, onClick: c, disabled: l, show: u = "always" }) => {
|
|
38
|
+
let d = e === "left", f = d ? "arrow-left" : "arrow-right", p = d ? "zurück" : "weiter", m = a ? "primary" : "tertiary", h = o ? "dark" : "light", { wrapper: g, button: _ } = i({
|
|
39
|
+
position: e,
|
|
40
|
+
disableHorizontalPadding: s,
|
|
41
|
+
show: u
|
|
42
|
+
});
|
|
43
|
+
return /* @__PURE__ */ r(n, {
|
|
44
|
+
theme: h,
|
|
45
|
+
children: /* @__PURE__ */ r("span", {
|
|
46
|
+
className: g(),
|
|
47
|
+
children: /* @__PURE__ */ r(t, {
|
|
48
|
+
type: "button",
|
|
49
|
+
className: _(),
|
|
50
|
+
variant: m,
|
|
51
|
+
icon: f,
|
|
52
|
+
componentName: "icon-button",
|
|
53
|
+
onClick: c,
|
|
54
|
+
disabled: l,
|
|
55
|
+
children: p
|
|
56
|
+
})
|
|
57
|
+
})
|
|
58
|
+
});
|
|
59
|
+
};
|
|
60
|
+
//#endregion
|
|
61
|
+
export { a as default };
|
|
62
|
+
|
|
63
|
+
//# sourceMappingURL=CarouselArrow.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"CarouselArrow.js","names":[],"sources":["../../src/CarouselArrow.tsx"],"sourcesContent":["\"use client\";\n\nimport { BaseButton } from \"@bonprix-ds/internal-components\";\nimport ThemeForcer from \"@bonprix-ds/react-theme-forcer\";\nimport React from \"react\";\nimport { tv } from \"tailwind-variants\";\n\nconst generateClasses = tv({\n slots: {\n wrapper: \"z-[1] absolute top-1/2 transform -translate-y-1/2\",\n button: \"disabled:hidden\",\n },\n variants: {\n position: {\n left: {\n wrapper: \"start-3\",\n },\n right: {\n wrapper: \"end-3\",\n },\n },\n disableHorizontalPadding: {\n true: \"\",\n false: \"\",\n },\n show: {\n always: { button: \"\" },\n hover: { button: \"hidden group-hover:block\" },\n never: { button: \"hidden\" },\n },\n },\n compoundVariants: [\n {\n disableHorizontalPadding: true,\n position: \"left\",\n class: {\n wrapper: \"start-0\",\n },\n },\n {\n disableHorizontalPadding: true,\n position: \"right\",\n class: {\n wrapper: \"end-0\",\n },\n },\n ],\n});\n\ninterface Props {\n position: \"left\" | \"right\";\n buttonsContrast: boolean;\n invertButtons: boolean;\n disableHorizontalPadding: boolean;\n onClick: () => void;\n disabled: boolean;\n show?: \"always\" | \"hover\" | \"never\";\n}\n\nconst CarouselArrow = ({\n position,\n buttonsContrast,\n invertButtons,\n disableHorizontalPadding,\n onClick,\n disabled,\n show = \"always\",\n}: Props) => {\n const isLeftArrow = position === \"left\" ? true : false;\n const icon = isLeftArrow ? \"arrow-left\" : \"arrow-right\";\n const label = isLeftArrow ? \"zurück\" : \"weiter\";\n const variant = buttonsContrast ? \"primary\" : \"tertiary\";\n const themeMode = invertButtons ? \"dark\" : \"light\";\n const { wrapper, button } = generateClasses({ position, disableHorizontalPadding, show });\n\n return (\n <ThemeForcer theme={themeMode}>\n <span className={wrapper()}>\n <BaseButton\n type=\"button\"\n className={button()}\n variant={variant}\n icon={icon}\n componentName=\"icon-button\"\n onClick={onClick}\n disabled={disabled}\n >\n {label}\n </BaseButton>\n </span>\n </ThemeForcer>\n );\n};\n\nexport default CarouselArrow;\n"],"mappings":";;;;;;;AAOA,IAAM,IAAkB,EAAG;CACzB,OAAO;EACL,SAAS;EACT,QAAQ;CACV;CACA,UAAU;EACR,UAAU;GACR,MAAM,EACJ,SAAS,UACX;GACA,OAAO,EACL,SAAS,QACX;EACF;EACA,0BAA0B;GACxB,MAAM;GACN,OAAO;EACT;EACA,MAAM;GACJ,QAAQ,EAAE,QAAQ,GAAG;GACrB,OAAO,EAAE,QAAQ,2BAA2B;GAC5C,OAAO,EAAE,QAAQ,SAAS;EAC5B;CACF;CACA,kBAAkB,CAChB;EACE,0BAA0B;EAC1B,UAAU;EACV,OAAO,EACL,SAAS,UACX;CACF,GACA;EACE,0BAA0B;EAC1B,UAAU;EACV,OAAO,EACL,SAAS,QACX;CACF,CACF;AACF,CAAC,GAYK,KAAiB,EACrB,aACA,oBACA,kBACA,6BACA,YACA,aACA,UAAO,eACI;CACX,IAAM,IAAc,MAAa,QAC3B,IAAO,IAAc,eAAe,eACpC,IAAQ,IAAc,WAAW,UACjC,IAAU,IAAkB,YAAY,YACxC,IAAY,IAAgB,SAAS,SACrC,EAAE,YAAS,cAAW,EAAgB;EAAE;EAAU;EAA0B;CAAK,CAAC;CAExF,OACE,kBAAC,GAAD;EAAa,OAAO;EAClB,UAAA,kBAAC,QAAD;GAAM,WAAW,EAAQ;GACvB,UAAA,kBAAC,GAAD;IACE,MAAK;IACL,WAAW,EAAO;IACT;IACH;IACN,eAAc;IACL;IACC;IAET,UAAA;GACS,CAAA;EACR,CAAA;CACK,CAAA;AAEjB"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import e from "./CarouselV2Root.js";
|
|
2
|
+
import t from "./PositionIndicator/PositionIndicator.js";
|
|
3
|
+
import n from "./Slide/Slide.js";
|
|
4
|
+
import r from "./Slides/Slides.js";
|
|
5
|
+
import "react";
|
|
6
|
+
import { jsx as i } from "react/jsx-runtime";
|
|
7
|
+
//#region src/CarouselV2.tsx
|
|
8
|
+
var a = (t) => /* @__PURE__ */ i(e, { ...t });
|
|
9
|
+
a.displayName = "CarouselV2", a.Slides = r, a.Slide = n, a.PositionIndicator = t;
|
|
10
|
+
//#endregion
|
|
11
|
+
export { a as default };
|
|
12
|
+
|
|
13
|
+
//# sourceMappingURL=CarouselV2.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"CarouselV2.js","names":[],"sources":["../../src/CarouselV2.tsx"],"sourcesContent":["import React from \"react\";\n\nimport CarouselV2Root, { type CarouselV2Props } from \"./CarouselV2Root\";\nimport PositionIndicator from \"./PositionIndicator/PositionIndicator\";\nimport Slide from \"./Slide/Slide\";\nimport Slides from \"./Slides/Slides\";\n\nexport type { CarouselV2Props, CarouselV2Ref, ResponsiveNumber } from \"./CarouselV2Root\";\n\nconst CarouselV2 = (props: CarouselV2Props) => <CarouselV2Root {...props} />;\n\nCarouselV2.displayName = \"CarouselV2\";\n\nCarouselV2.Slides = Slides;\nCarouselV2.Slide = Slide;\nCarouselV2.PositionIndicator = PositionIndicator;\n\nexport default CarouselV2;\n"],"mappings":";;;;;;;AASA,IAAM,KAAc,MAA2B,kBAAC,GAAD,EAAgB,GAAI,EAAQ,CAAA;AAE3E,EAAW,cAAc,cAEzB,EAAW,SAAS,GACpB,EAAW,QAAQ,GACnB,EAAW,oBAAoB"}
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import e from "./CarouselArrow.js";
|
|
3
|
+
import t from "./carouselContext.js";
|
|
4
|
+
import { getActiveSlide as n, getFirstSlideIndexOnPage as r } from "./utils.js";
|
|
5
|
+
import i from "./useCreateCarousel.js";
|
|
6
|
+
import { useId as a, useImperativeHandle as o, useRef as s, useState as c } from "react";
|
|
7
|
+
import { tv as l } from "tailwind-variants";
|
|
8
|
+
import { jsx as u, jsxs as d } from "react/jsx-runtime";
|
|
9
|
+
//#region src/CarouselV2Root.tsx
|
|
10
|
+
var f = l({
|
|
11
|
+
base: "relative min-h-8 touch-manipulation group",
|
|
12
|
+
slots: {
|
|
13
|
+
header: "sr-only",
|
|
14
|
+
status: "sr-only"
|
|
15
|
+
}
|
|
16
|
+
}), p = ({ children: l, title: p, titleLevel: m = "h3", perPage: h = 1, perMove: g = h, rewind: _ = !1, buttonsContrast: v = !1, padding: y = 0, onMove: b, showArrows: x = "always", gap: S = 0, invertButtons: C = !1, disableHorizontalPadding: w = !1, simulateTouch: T = !1, start: E, ref: D, ...O }) => {
|
|
17
|
+
let k = i({
|
|
18
|
+
rewind: _,
|
|
19
|
+
perPage: h,
|
|
20
|
+
perMove: g,
|
|
21
|
+
padding: y,
|
|
22
|
+
gap: S,
|
|
23
|
+
children: l,
|
|
24
|
+
simulateTouch: T,
|
|
25
|
+
start: E
|
|
26
|
+
}), A = a(), j = s(null), { listRef: M, pages: N, activePage: P, activeSlide: F, canScrollPrev: I, canScrollNext: L, onClickLeft: R, onClickRight: z, slideWidth: B, getIntersectionObserver: V, setIntersectionObserver: H, buttonTriggeredScroll: U, scrollDirection: W, finishButtonTriggeredScroll: G, slidesCount: K, currentPerPage: q } = k, { base: J, header: Y, status: X } = f({ showArrows: x }), [Z, Q] = c(""), [$, ee] = c(0);
|
|
27
|
+
return o(D, () => ({ moveToPage(e) {
|
|
28
|
+
if (!M.current || e < 0 || e >= N || N === 1) return;
|
|
29
|
+
let t = r({
|
|
30
|
+
pageNumber: e,
|
|
31
|
+
perPage: q
|
|
32
|
+
});
|
|
33
|
+
M.current.querySelector(`[data-index='${t}']`)?.scrollIntoView({
|
|
34
|
+
behavior: "instant",
|
|
35
|
+
inline: "start"
|
|
36
|
+
});
|
|
37
|
+
} })), /* @__PURE__ */ d("section", {
|
|
38
|
+
className: J(),
|
|
39
|
+
"data-bpds": "carousel-v2",
|
|
40
|
+
"aria-roledescription": "carousel",
|
|
41
|
+
"aria-labelledby": O["aria-labelledby"] ?? A,
|
|
42
|
+
"data-bpds-per-page": h,
|
|
43
|
+
"data-bpds-per-move": g,
|
|
44
|
+
"data-bpds-rewind": _,
|
|
45
|
+
"data-bpds-buttons-contrast": v,
|
|
46
|
+
"data-bpds-show-arrows": x,
|
|
47
|
+
"data-bpds-invert-buttons": C,
|
|
48
|
+
"data-bpds-disable-horizontal-padding": w,
|
|
49
|
+
"data-bpds-gap": S,
|
|
50
|
+
"data-bpds-padding": y,
|
|
51
|
+
children: [
|
|
52
|
+
!O["aria-labelledby"] && /* @__PURE__ */ u(m, {
|
|
53
|
+
id: A,
|
|
54
|
+
className: Y(),
|
|
55
|
+
children: p
|
|
56
|
+
}),
|
|
57
|
+
/* @__PURE__ */ u("div", {
|
|
58
|
+
className: X(),
|
|
59
|
+
role: "status",
|
|
60
|
+
children: Z
|
|
61
|
+
}),
|
|
62
|
+
/* @__PURE__ */ u("div", {
|
|
63
|
+
tabIndex: -1,
|
|
64
|
+
ref: j
|
|
65
|
+
}),
|
|
66
|
+
/* @__PURE__ */ u(e, {
|
|
67
|
+
invertButtons: C,
|
|
68
|
+
position: "left",
|
|
69
|
+
buttonsContrast: v,
|
|
70
|
+
disableHorizontalPadding: w,
|
|
71
|
+
onClick: R,
|
|
72
|
+
disabled: !I,
|
|
73
|
+
show: x
|
|
74
|
+
}),
|
|
75
|
+
/* @__PURE__ */ u(t.Provider, {
|
|
76
|
+
value: {
|
|
77
|
+
perPage: h,
|
|
78
|
+
activeSlide: F,
|
|
79
|
+
activePage: P,
|
|
80
|
+
pages: N,
|
|
81
|
+
listRef: M,
|
|
82
|
+
handleScrollEnd: () => {
|
|
83
|
+
let e = M.current ? n({
|
|
84
|
+
scrollPosition: M.current.scrollLeft,
|
|
85
|
+
slidesContainer: M.current
|
|
86
|
+
}) : F;
|
|
87
|
+
e !== $ && (b?.(e, $), ee(e));
|
|
88
|
+
let t = [], r = M.current?.querySelectorAll("li[aria-roledescription='slide']") ?? [];
|
|
89
|
+
for (let e = 0; e < r.length; e++) r[e].hasAttribute("inert") || t.push(e);
|
|
90
|
+
U && ((W === "left" && !I || W === "right" && !L) && j.current?.focus(), G());
|
|
91
|
+
let i = "";
|
|
92
|
+
t.length === 1 ? i = `Slide ${t[0] + 1} aus ${K}` : t.length > 1 && (i = `Slides ${t[0] + 1} bis ${t[t.length - 1] + 1} aus ${K}`), Q(i);
|
|
93
|
+
},
|
|
94
|
+
gap: S,
|
|
95
|
+
slideWidth: B,
|
|
96
|
+
getIntersectionObserver: V,
|
|
97
|
+
setIntersectionObserver: H,
|
|
98
|
+
simulateTouch: T
|
|
99
|
+
},
|
|
100
|
+
children: l
|
|
101
|
+
}),
|
|
102
|
+
/* @__PURE__ */ u(e, {
|
|
103
|
+
invertButtons: C,
|
|
104
|
+
position: "right",
|
|
105
|
+
buttonsContrast: v,
|
|
106
|
+
disableHorizontalPadding: w,
|
|
107
|
+
onClick: z,
|
|
108
|
+
disabled: !L,
|
|
109
|
+
show: x
|
|
110
|
+
})
|
|
111
|
+
]
|
|
112
|
+
});
|
|
113
|
+
};
|
|
114
|
+
p.displayName = "CarouselV2";
|
|
115
|
+
//#endregion
|
|
116
|
+
export { p as default };
|
|
117
|
+
|
|
118
|
+
//# sourceMappingURL=CarouselV2Root.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"CarouselV2Root.js","names":[],"sources":["../../src/CarouselV2Root.tsx"],"sourcesContent":["\"use client\";\n\nimport { Breakpoint } from \"@bonprix-ds/utils\";\nimport React, { ReactNode, Ref, useId, useImperativeHandle, useRef, useState } from \"react\";\nimport { tv } from \"tailwind-variants\";\n\nimport CarouselArrow from \"./CarouselArrow\";\nimport CarouselContext from \"./carouselContext\";\nimport useCreateCarousel from \"./useCreateCarousel\";\nimport { getActiveSlide, getFirstSlideIndexOnPage } from \"./utils\";\n\nconst generateClasses = tv({\n base: \"relative min-h-8 touch-manipulation group\", // group is for arrows on hover\n slots: {\n header: \"sr-only\",\n status: \"sr-only\",\n },\n});\n\nexport type ResponsiveNumber = Partial<Record<Breakpoint, number>>;\n\ntype AccessibilityLabel =\n | {\n /** Sets an invisible header for the carousel to create a landmark. Do not use both `title` and `aria-labelledby`. */\n title: string;\n /** Defines the level of the used header element. Only applicable if using `title` instead of `aria-labelledby`. */\n titleLevel?: \"h2\" | \"h3\" | \"h4\" | \"h5\" | \"h6\";\n \"aria-labelledby\"?: never;\n }\n | {\n /** Use if a title already exists on the page. Do not use both `title`and `aria-labelledby`. */\n \"aria-labelledby\": string;\n title?: never;\n titleLevel?: never;\n };\n\nexport interface CarouselV2Ref {\n moveToPage: (pageNumber: number) => void;\n}\n\nexport type CarouselV2Props = AccessibilityLabel & {\n /** The content of the carousel. Here, all of the subcomponents should be placed. */\n children: ReactNode;\n /** Amount of slides that will be visible at the same time, thus building a page. */\n perPage?: number | ResponsiveNumber;\n /** Amount of slides to move per arrow icon button click. */\n perMove?: number | ResponsiveNumber;\n /** If true, the carousel will rewind to the beginning when reaching the end or going to end when going before the first slide. There must be more than two pages for this to work! Doesn't run smoothly with `padding` right now. */\n rewind?: boolean;\n /** If set to `true`, the primary variant of icon buttons will be used to achieve a high contrast. */\n buttonsContrast?: boolean;\n /** The padding at the right side of the page in pixels. Can be used to implement the so-called \"scroll hint\" */\n padding?: number;\n /** Callback function that is called when the slide changes. */\n onMove?: (destIndex: number, prevIndex: number) => void;\n /** Manages the behavior of the arrow icon buttons. */\n showArrows?: \"always\" | \"hover\" | \"never\";\n /** The gap between slides in pixels. */\n gap?: number;\n /** If set to `true`, the color of the arrow buttons will be inverted. */\n invertButtons?: boolean;\n /** If set to `true`, the horizontal padding for the arrow buttons will be removed. */\n disableHorizontalPadding?: boolean;\n /** If set to `true`, scrolling via mouse drag will be enabled just like you would expect from a mobile touch pointer. */\n simulateTouch?: boolean;\n /** The index of the initial slide to be displayed. For programmatic scrolling after initial mount, use `ref`'s `moveToPage`. */\n start?: number;\n /** A reference to the Carousel instance. You can call `moveToPage(pageNumber: number)` on this one, e.g. `carouselRef.current.moveToPage(2)`. To set an initial slide, use `start` prop instead. */\n ref?: Ref<CarouselV2Ref>;\n};\nconst CarouselV2Root = ({\n children,\n title,\n titleLevel: TitleComponent = \"h3\",\n perPage = 1,\n perMove = perPage,\n rewind = false,\n buttonsContrast = false,\n padding = 0,\n onMove,\n showArrows = \"always\",\n gap = 0,\n invertButtons = false,\n disableHorizontalPadding = false,\n simulateTouch = false,\n start,\n ref,\n ...props\n}: CarouselV2Props) => {\n const options = useCreateCarousel({ rewind, perPage, perMove, padding, gap, children, simulateTouch, start });\n const headerId = useId();\n const focusManagerRef = useRef<HTMLDivElement>(null);\n const {\n listRef,\n pages,\n activePage,\n activeSlide,\n canScrollPrev,\n canScrollNext,\n onClickLeft,\n onClickRight,\n slideWidth,\n getIntersectionObserver,\n setIntersectionObserver,\n buttonTriggeredScroll,\n scrollDirection,\n finishButtonTriggeredScroll,\n slidesCount,\n currentPerPage,\n } = options;\n const { base, header, status: statusClass } = generateClasses({ showArrows });\n const [status, setStatus] = useState<string>(\"\");\n const [previousActiveSlide, setPreviousActiveSlide] = useState<number>(0);\n\n // executed on scroll end in Slides component\n const handleScrollEnd = () => {\n // When calling handleScrollEnd, `activeSlide` might be stale due to a race condition with our handleScroll callback and its throttling.\n // Therefore, we get the currentActiveSlide from the browser scroll position fresh here. Otherwise, `moveToPage` would result in wrong `onMove` callbacks.\n const currentActiveSlide = listRef.current\n ? getActiveSlide({ scrollPosition: listRef.current.scrollLeft, slidesContainer: listRef.current })\n : activeSlide;\n\n if (currentActiveSlide !== previousActiveSlide) {\n onMove?.(currentActiveSlide, previousActiveSlide);\n setPreviousActiveSlide(currentActiveSlide);\n }\n\n const activeSlideNumbers = [];\n const slides = listRef.current?.querySelectorAll(\"li[aria-roledescription='slide']\") ?? [];\n for (let i = 0; i < slides.length; i++) {\n const slide = slides[i];\n if (!slide.hasAttribute(\"inert\")) {\n activeSlideNumbers.push(i);\n }\n }\n\n if (buttonTriggeredScroll) {\n if ((scrollDirection === \"left\" && !canScrollPrev) || (scrollDirection === \"right\" && !canScrollNext))\n focusManagerRef.current?.focus();\n\n finishButtonTriggeredScroll();\n }\n\n // TODO: Set this on initial load, too. It only gets set after scrolling right now.\n // This is the only place where we use 1-indexes because it is actually read to users and more intuitive than 0-based indexes.\n let newStatus = \"\";\n if (activeSlideNumbers.length === 1) {\n newStatus = `Slide ${activeSlideNumbers[0] + 1} aus ${slidesCount}`;\n } else if (activeSlideNumbers.length > 1) {\n newStatus = `Slides ${activeSlideNumbers[0] + 1} bis ${activeSlideNumbers[activeSlideNumbers.length - 1] + 1} aus ${slidesCount}`;\n }\n setStatus(newStatus);\n };\n\n useImperativeHandle(ref, () => ({\n moveToPage(pageNumber: number) {\n if (!listRef.current || pageNumber < 0 || pageNumber >= pages || pages === 1) return;\n\n const firstSlideIndexOnPage = getFirstSlideIndexOnPage({ pageNumber, perPage: currentPerPage });\n const firstSlideOfPage = listRef.current.querySelector(`[data-index='${firstSlideIndexOnPage}']`);\n\n firstSlideOfPage?.scrollIntoView({ behavior: \"instant\", inline: \"start\" });\n },\n }));\n\n return (\n <section\n className={base()}\n data-bpds=\"carousel-v2\"\n aria-roledescription=\"carousel\"\n aria-labelledby={props[\"aria-labelledby\"] ?? headerId}\n data-bpds-per-page={perPage}\n data-bpds-per-move={perMove}\n data-bpds-rewind={rewind}\n data-bpds-buttons-contrast={buttonsContrast}\n data-bpds-show-arrows={showArrows}\n data-bpds-invert-buttons={invertButtons}\n data-bpds-disable-horizontal-padding={disableHorizontalPadding}\n data-bpds-gap={gap}\n data-bpds-padding={padding}\n >\n {!props[\"aria-labelledby\"] && (\n <TitleComponent id={headerId} className={header()}>\n {title}\n </TitleComponent>\n )}\n <div className={statusClass()} role=\"status\">\n {status}\n </div>\n <div tabIndex={-1} ref={focusManagerRef}></div>\n <CarouselArrow\n invertButtons={invertButtons}\n position=\"left\"\n buttonsContrast={buttonsContrast}\n disableHorizontalPadding={disableHorizontalPadding}\n onClick={onClickLeft}\n disabled={!canScrollPrev}\n show={showArrows}\n />\n <CarouselContext.Provider\n value={{\n perPage,\n activeSlide,\n activePage,\n pages,\n listRef,\n handleScrollEnd,\n gap,\n slideWidth,\n getIntersectionObserver,\n setIntersectionObserver,\n simulateTouch,\n }}\n >\n {children}\n </CarouselContext.Provider>\n <CarouselArrow\n invertButtons={invertButtons}\n position=\"right\"\n buttonsContrast={buttonsContrast}\n disableHorizontalPadding={disableHorizontalPadding}\n onClick={onClickRight}\n disabled={!canScrollNext}\n show={showArrows}\n />\n </section>\n );\n};\n\nCarouselV2Root.displayName = \"CarouselV2\";\n\nexport default CarouselV2Root;\n"],"mappings":";;;;;;;;;AAWA,IAAM,IAAkB,EAAG;CACzB,MAAM;CACN,OAAO;EACL,QAAQ;EACR,QAAQ;CACV;AACF,CAAC,GAqDK,KAAkB,EACtB,aACA,UACA,YAAY,IAAiB,MAC7B,aAAU,GACV,aAAU,GACV,YAAS,IACT,qBAAkB,IAClB,aAAU,GACV,WACA,gBAAa,UACb,SAAM,GACN,mBAAgB,IAChB,8BAA2B,IAC3B,mBAAgB,IAChB,UACA,QACA,GAAG,QACkB;CACrB,IAAM,IAAU,EAAkB;EAAE;EAAQ;EAAS;EAAS;EAAS;EA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ACA;KACA,uBA3FsB;MAG5B,IAAM,IAAqB,EAAQ,UAC/B,EAAe;OAAE,gBAAgB,EAAQ,QAAQ;OAAY,iBAAiB,EAAQ;MAAQ,CAAC,IAC/F;MAEJ,AAAI,MAAuB,MACzB,IAAS,GAAoB,CAAmB,GAChD,GAAuB,CAAkB;MAG3C,IAAM,IAAqB,CAAC,GACtB,IAAS,EAAQ,SAAS,iBAAiB,kCAAkC,KAAK,CAAC;MACzF,KAAK,IAAI,IAAI,GAAG,IAAI,EAAO,QAAQ,KAEjC,AADc,EAAO,EAChB,CAAM,aAAa,OAAO,KAC7B,EAAmB,KAAK,CAAC;MAI7B,AAAI,OACG,MAAoB,UAAU,CAAC,KAAmB,MAAoB,WAAW,CAAC,MACrF,EAAgB,SAAS,MAAM,GAEjC,EAA4B;MAK9B,IAAI,IAAY;MAMhB,AALI,EAAmB,WAAW,IAChC,IAAY,SAAS,EAAmB,KAAK,EAAE,OAAO,MAC7C,EAAmB,SAAS,MACrC,IAAY,UAAU,EAAmB,KAAK,EAAE,OAAO,EAAmB,EAAmB,SAAS,KAAK,EAAE,OAAO,MAEtH,EAAU,CAAS;KACrB;KAuDQ;KACA;KACA;KACA;KACA;IACF;IAEC;GACuB,CAAA;GAC1B,kBAAC,GAAD;IACiB;IACf,UAAS;IACQ;IACS;IAC1B,SAAS;IACT,UAAU,CAAC;IACX,MAAM;GACP,CAAA;EACM;;AAEb;AAEA,EAAe,cAAc"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import e from "../useConsumeCarousel.js";
|
|
3
|
+
import "react";
|
|
4
|
+
import { jsx as t } from "react/jsx-runtime";
|
|
5
|
+
//#region src/PositionIndicator/PositionIndicator.tsx
|
|
6
|
+
var n = ({ ref: n }) => {
|
|
7
|
+
let { activePage: r, pages: i } = e(), a = (e) => r === e ? "bg-clr-primary w-3 h-1" : "bg-clr-on-bg-low w-3 h-0.25", o = [];
|
|
8
|
+
for (let e = 0; e < i; e++) o.push(/* @__PURE__ */ t("div", { className: a(e) }, e));
|
|
9
|
+
return /* @__PURE__ */ t("div", {
|
|
10
|
+
className: "flex items-center gap-1 absolute bottom-4 start-1/2 -translate-x-1/2 min-h-1",
|
|
11
|
+
ref: n,
|
|
12
|
+
children: o
|
|
13
|
+
});
|
|
14
|
+
};
|
|
15
|
+
n.displayName = "CarouselV2.PositionIndicator";
|
|
16
|
+
//#endregion
|
|
17
|
+
export { n as default };
|
|
18
|
+
|
|
19
|
+
//# sourceMappingURL=PositionIndicator.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"PositionIndicator.js","names":[],"sources":["../../../src/PositionIndicator/PositionIndicator.tsx"],"sourcesContent":["\"use client\";\n\nimport React, { Ref } from \"react\";\n\nimport useConsumeCarousel from \"../useConsumeCarousel\";\n\nexport interface PositionIndicatorProps {\n /** Ref to the element */\n ref?: Ref<HTMLDivElement>;\n}\n\nconst PositionIndicator = ({ ref }: PositionIndicatorProps) => {\n const { activePage, pages } = useConsumeCarousel();\n const getPageClasses = (pageNumber: number): string => {\n return activePage === pageNumber ? \"bg-clr-primary w-3 h-1\" : \"bg-clr-on-bg-low w-3 h-0.25\";\n };\n const indicators = [];\n for (let i = 0; i < pages; i++) {\n indicators.push(<div className={getPageClasses(i)} key={i}></div>);\n }\n return (\n <div className=\"flex items-center gap-1 absolute bottom-4 start-1/2 -translate-x-1/2 min-h-1\" ref={ref}>\n {indicators}\n </div>\n );\n};\n\nPositionIndicator.displayName = \"CarouselV2.PositionIndicator\";\n\nexport default PositionIndicator;\n"],"mappings":";;;;;AAWA,IAAM,KAAqB,EAAE,aAAkC;CAC7D,IAAM,EAAE,eAAY,aAAU,EAAmB,GAC3C,KAAkB,MACf,MAAe,IAAa,2BAA2B,+BAE1D,IAAa,CAAC;CACpB,KAAK,IAAI,IAAI,GAAG,IAAI,GAAO,KACzB,EAAW,KAAK,kBAAC,OAAD,EAAK,WAAW,EAAe,CAAC,EAAgB,GAAR,CAAQ,CAAC;CAEnE,OACE,kBAAC,OAAD;EAAK,WAAU;EAAoF;EAChG,UAAA;CACE,CAAA;AAET;AAEA,EAAkB,cAAc"}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import e from "../useConsumeCarousel.js";
|
|
3
|
+
import { useEffect as t, useRef as n } from "react";
|
|
4
|
+
import { jsx as r } from "react/jsx-runtime";
|
|
5
|
+
//#region src/Slide/Slide.tsx
|
|
6
|
+
var i = ({ children: i, ...a }) => {
|
|
7
|
+
let o = n(null), { slideWidth: s, getIntersectionObserver: c } = e(), l = c();
|
|
8
|
+
return t(() => {
|
|
9
|
+
if (!o.current || !l) return;
|
|
10
|
+
let e = o.current;
|
|
11
|
+
return l?.observe(e), () => l?.unobserve(e);
|
|
12
|
+
}, [o, l]), /* @__PURE__ */ r("li", {
|
|
13
|
+
className: "snap-start shrink-0 relative",
|
|
14
|
+
style: { width: s },
|
|
15
|
+
"aria-roledescription": "slide",
|
|
16
|
+
"aria-label": a["aria-label"],
|
|
17
|
+
"aria-labelledby": a["aria-labelledby"],
|
|
18
|
+
ref: o,
|
|
19
|
+
children: i
|
|
20
|
+
});
|
|
21
|
+
};
|
|
22
|
+
i.displayName = "CarouselV2.Slide";
|
|
23
|
+
//#endregion
|
|
24
|
+
export { i as default };
|
|
25
|
+
|
|
26
|
+
//# sourceMappingURL=Slide.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Slide.js","names":[],"sources":["../../../src/Slide/Slide.tsx"],"sourcesContent":["\"use client\";\n\nimport React, { ReactNode, useEffect, useRef } from \"react\";\n\nimport useConsumeCarousel from \"../useConsumeCarousel\";\n\nexport interface SlideProps {\n /** The content of the slide item. */\n children: ReactNode;\n /** An optional label of the slide item. */\n \"aria-label\"?: string;\n /** An optional id of a DOM element that labels the slide item. */\n \"aria-labelledby\"?: string;\n}\n\nconst Slide = ({ children, ...props }: SlideProps) => {\n const listItemRef = useRef<HTMLLIElement>(null);\n const { slideWidth, getIntersectionObserver } = useConsumeCarousel();\n const observer = getIntersectionObserver();\n useEffect(() => {\n if (!listItemRef.current || !observer) return;\n const slideRef = listItemRef.current;\n observer?.observe(slideRef);\n return () => observer?.unobserve(slideRef);\n }, [listItemRef, observer]);\n return (\n <li\n className=\"snap-start shrink-0 relative\"\n style={{ width: slideWidth }}\n aria-roledescription=\"slide\"\n aria-label={props[\"aria-label\"]}\n aria-labelledby={props[\"aria-labelledby\"]}\n ref={listItemRef}\n >\n {children}\n </li>\n );\n};\n\nSlide.displayName = \"CarouselV2.Slide\";\n\nexport default Slide;\n"],"mappings":";;;;;AAeA,IAAM,KAAS,EAAE,aAAU,GAAG,QAAwB;CACpD,IAAM,IAAc,EAAsB,IAAI,GACxC,EAAE,eAAY,+BAA4B,EAAmB,GAC7D,IAAW,EAAwB;CAOzC,OANA,QAAgB;EACd,IAAI,CAAC,EAAY,WAAW,CAAC,GAAU;EACvC,IAAM,IAAW,EAAY;EAE7B,OADA,GAAU,QAAQ,CAAQ,SACb,GAAU,UAAU,CAAQ;CAC3C,GAAG,CAAC,GAAa,CAAQ,CAAC,GAExB,kBAAC,MAAD;EACE,WAAU;EACV,OAAO,EAAE,OAAO,EAAW;EAC3B,wBAAqB;EACrB,cAAY,EAAM;EAClB,mBAAiB,EAAM;EACvB,KAAK;EAEJ;CACC,CAAA;AAER;AAEA,EAAM,cAAc"}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import e from "../useConsumeCarousel.js";
|
|
3
|
+
import { useEffect as t } from "react";
|
|
4
|
+
import { tv as n } from "tailwind-variants";
|
|
5
|
+
import { jsx as r } from "react/jsx-runtime";
|
|
6
|
+
//#region src/Slides/Slides.tsx
|
|
7
|
+
var i = n({
|
|
8
|
+
slots: { root: "list-none ps-0 m-0 flex overflow-x-scroll overflow-y-hidden overscroll-x-none snap-x snap-mandatory [&::-webkit-scrollbar]:hidden scrollbar-none focus-visible:outline outline-clr-primary outline-2 outline-offset-2" },
|
|
9
|
+
variants: { simulateTouch: { true: { root: "select-none" } } }
|
|
10
|
+
}), a = ({ children: n }) => {
|
|
11
|
+
let { listRef: a, handleScrollEnd: o, gap: s, setIntersectionObserver: c, simulateTouch: l } = e(), { root: u } = i({ simulateTouch: l });
|
|
12
|
+
return t(() => {
|
|
13
|
+
if (!a) return;
|
|
14
|
+
let e = new IntersectionObserver((e) => {
|
|
15
|
+
e.forEach((e) => {
|
|
16
|
+
e.isIntersecting ? e.target.removeAttribute("inert") : e.target.setAttribute("inert", "");
|
|
17
|
+
});
|
|
18
|
+
}, {
|
|
19
|
+
root: a.current,
|
|
20
|
+
threshold: 0,
|
|
21
|
+
rootMargin: "0px -1px 0px -1px"
|
|
22
|
+
});
|
|
23
|
+
return c(e), () => {
|
|
24
|
+
e.disconnect(), c(null);
|
|
25
|
+
};
|
|
26
|
+
}, [a, c]), /* @__PURE__ */ r("ul", {
|
|
27
|
+
style: { gap: s },
|
|
28
|
+
className: u(),
|
|
29
|
+
ref: a,
|
|
30
|
+
onScrollEnd: o,
|
|
31
|
+
children: n
|
|
32
|
+
});
|
|
33
|
+
};
|
|
34
|
+
a.displayName = "CarouselV2.Slides";
|
|
35
|
+
//#endregion
|
|
36
|
+
export { a as default };
|
|
37
|
+
|
|
38
|
+
//# sourceMappingURL=Slides.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Slides.js","names":[],"sources":["../../../src/Slides/Slides.tsx"],"sourcesContent":["\"use client\";\n\nimport React, { ReactNode, useEffect } from \"react\";\nimport { tv } from \"tailwind-variants\";\n\nimport useConsumeCarousel from \"../useConsumeCarousel\";\n\nconst generateClasses = tv({\n slots: {\n root: \"list-none ps-0 m-0 flex overflow-x-scroll overflow-y-hidden overscroll-x-none snap-x snap-mandatory [&::-webkit-scrollbar]:hidden scrollbar-none focus-visible:outline outline-clr-primary outline-2 outline-offset-2\",\n },\n variants: {\n simulateTouch: {\n true: {\n root: \"select-none\",\n },\n },\n },\n});\n\nexport interface SlidesProps {\n /** The content of the slides section. This is where `<CarouselV2.Slide>` items go. */\n children: ReactNode;\n}\n\nconst Slides = ({ children }: SlidesProps) => {\n const { listRef, handleScrollEnd, gap, setIntersectionObserver, simulateTouch } = useConsumeCarousel();\n\n const { root } = generateClasses({ simulateTouch });\n\n useEffect(() => {\n if (!listRef) return;\n const observer = new IntersectionObserver(\n (entries) => {\n entries.forEach((entry) => {\n if (entry.isIntersecting) {\n entry.target.removeAttribute(\"inert\");\n } else {\n // We use inert here to avoid focusing or interaction of all nested elements as well.\n entry.target.setAttribute(\"inert\", \"\");\n }\n });\n },\n // Every visible slide should be interactive, but `isIntersecting` is already `true` when slides touch container!\n // Therefore, we decrease horizontal detection size by `1px`.\n // https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API#threshold\n // > The default is 0 (meaning the callback will be run as soon as the target element intersects or touches the boundary of the root, even if no pixels are yet visible).\n { root: listRef.current, threshold: 0, rootMargin: \"0px -1px 0px -1px\" },\n );\n setIntersectionObserver(observer);\n return () => {\n observer.disconnect();\n setIntersectionObserver(null);\n };\n }, [listRef, setIntersectionObserver]);\n return (\n <ul style={{ gap }} className={root()} ref={listRef} onScrollEnd={handleScrollEnd}>\n {children}\n </ul>\n );\n};\n\nSlides.displayName = \"CarouselV2.Slides\";\n\nexport default Slides;\n"],"mappings":";;;;;;AAOA,IAAM,IAAkB,EAAG;CACzB,OAAO,EACL,MAAM,wNACR;CACA,UAAU,EACR,eAAe,EACb,MAAM,EACJ,MAAM,cACR,EACF,EACF;AACF,CAAC,GAOK,KAAU,EAAE,kBAA4B;CAC5C,IAAM,EAAE,YAAS,oBAAiB,QAAK,4BAAyB,qBAAkB,EAAmB,GAE/F,EAAE,YAAS,EAAgB,EAAE,iBAAc,CAAC;CA2BlD,OAzBA,QAAgB;EACd,IAAI,CAAC,GAAS;EACd,IAAM,IAAW,IAAI,sBAClB,MAAY;GACX,EAAQ,SAAS,MAAU;IACzB,AAAI,EAAM,iBACR,EAAM,OAAO,gBAAgB,OAAO,IAGpC,EAAM,OAAO,aAAa,SAAS,EAAE;GAEzC,CAAC;EACH,GAKA;GAAE,MAAM,EAAQ;GAAS,WAAW;GAAG,YAAY;EAAoB,CACzE;EAEA,OADA,EAAwB,CAAQ,SACnB;GAEX,AADA,EAAS,WAAW,GACpB,EAAwB,IAAI;EAC9B;CACF,GAAG,CAAC,GAAS,CAAuB,CAAC,GAEnC,kBAAC,MAAD;EAAI,OAAO,EAAE,OAAI;EAAG,WAAW,EAAK;EAAG,KAAK;EAAS,aAAa;EAC/D;CACC,CAAA;AAER;AAEA,EAAO,cAAc"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { createContext as e } from "react";
|
|
3
|
+
//#region src/carouselContext.ts
|
|
4
|
+
var t = e({
|
|
5
|
+
activePage: 1,
|
|
6
|
+
activeSlide: 1,
|
|
7
|
+
pages: 1,
|
|
8
|
+
perPage: 1,
|
|
9
|
+
listRef: null,
|
|
10
|
+
handleScrollEnd: () => null,
|
|
11
|
+
gap: 0,
|
|
12
|
+
slideWidth: "",
|
|
13
|
+
setIntersectionObserver: () => null,
|
|
14
|
+
getIntersectionObserver: () => null,
|
|
15
|
+
simulateTouch: !1
|
|
16
|
+
});
|
|
17
|
+
//#endregion
|
|
18
|
+
export { t as default };
|
|
19
|
+
|
|
20
|
+
//# sourceMappingURL=carouselContext.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"carouselContext.js","names":[],"sources":["../../src/carouselContext.ts"],"sourcesContent":["\"use client\";\n\nimport { createContext, RefObject } from \"react\";\n\nimport type { ResponsiveNumber } from \"./CarouselV2\";\n\nexport interface CarouselContextProps {\n activePage: number;\n activeSlide: number;\n pages: number;\n perPage: number | ResponsiveNumber;\n listRef: RefObject<HTMLUListElement | null> | null;\n handleScrollEnd?: () => void;\n gap?: number;\n slideWidth: string;\n setIntersectionObserver: (observer: IntersectionObserver | null) => void;\n getIntersectionObserver: () => IntersectionObserver | null;\n simulateTouch?: boolean;\n}\n\nconst CarouselContext = createContext<CarouselContextProps>({\n activePage: 1,\n activeSlide: 1,\n pages: 1,\n perPage: 1,\n listRef: null,\n handleScrollEnd: () => null,\n gap: 0,\n slideWidth: \"\",\n setIntersectionObserver: () => null,\n getIntersectionObserver: () => null,\n simulateTouch: false,\n});\n\nexport default CarouselContext;\n"],"mappings":";;;AAoBA,IAAM,IAAkB,EAAoC;CAC1D,YAAY;CACZ,aAAa;CACb,OAAO;CACP,SAAS;CACT,SAAS;CACT,uBAAuB;CACvB,KAAK;CACL,YAAY;CACZ,+BAA+B;CAC/B,+BAA+B;CAC/B,eAAe;AACjB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useConsumeCarousel.js","names":[],"sources":["../../src/useConsumeCarousel.ts"],"sourcesContent":["import { useContext } from \"react\";\n\nimport CarouselContext from \"./carouselContext\";\n\nconst useConsumeCarousel = () => {\n return useContext(CarouselContext);\n};\n\nexport default useConsumeCarousel;\n"],"mappings":";;;AAIA,IAAM,UACG,EAAW,CAAe"}
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
import e from "./useSimulateTouch.js";
|
|
2
|
+
import t from "./useStart.js";
|
|
3
|
+
import { getActivePage as n, getActiveSlide as r, getBreakpoint as i, getCanScrollNext as a, getCanScrollPrev as o, getCurrentPerMove as ee, getCurrentPerPage as s, getPages as c, getScrollDistanceNext as te, getScrollDistancePrev as l, getSlideWidthStyle as u, roughlyEqual as d } from "./utils.js";
|
|
4
|
+
import { useCallback as f, useEffect as p, useMemo as m, useRef as h, useState as g } from "react";
|
|
5
|
+
import _ from "lodash.throttle";
|
|
6
|
+
//#region src/useCreateCarousel.ts
|
|
7
|
+
var v = ({ perPage: v = 1, perMove: y = v, rewind: b = !1, padding: x = 0, gap: S = 0, children: ne, simulateTouch: re = !1, start: ie }) => {
|
|
8
|
+
let C = h(null), w = typeof document < "u" ? document.documentElement.clientWidth : 0, [T, E] = g(w), [D, ae] = g(0), [O, oe] = g(0), [k, se] = g(0), [A, j] = g(!1), [M, N] = g(!0), [P, F] = g(!1), [I, L] = g(null), [R, z] = g(0), B = () => {
|
|
9
|
+
F(!1), L(null);
|
|
10
|
+
}, V = h(null), H = f(() => V.current, []), U = f((e) => {
|
|
11
|
+
V.current = e;
|
|
12
|
+
}, []), W = i({ viewportWidth: T }), G = s({
|
|
13
|
+
perPage: v,
|
|
14
|
+
breakpoint: W
|
|
15
|
+
}), K = ee({
|
|
16
|
+
perMove: y,
|
|
17
|
+
breakpoint: W
|
|
18
|
+
}) / G, ce = u({
|
|
19
|
+
perPage: G,
|
|
20
|
+
padding: x,
|
|
21
|
+
gap: S
|
|
22
|
+
}), q = m(() => c({
|
|
23
|
+
slidesCount: R,
|
|
24
|
+
currentPerPage: G
|
|
25
|
+
}), [R, G]), J = m(() => q < 3, [q]), [Y, le] = g(0), ue = n({
|
|
26
|
+
activeSlide: Y,
|
|
27
|
+
currentPerPage: G,
|
|
28
|
+
slidesCount: R,
|
|
29
|
+
pages: q,
|
|
30
|
+
rewind: b
|
|
31
|
+
}), X = l({
|
|
32
|
+
scrollPosition: k,
|
|
33
|
+
scrollPortWidth: O,
|
|
34
|
+
scrollContainerWidth: D,
|
|
35
|
+
rewind: b,
|
|
36
|
+
distanceMultiplier: K
|
|
37
|
+
}), Z = te({
|
|
38
|
+
scrollPosition: k,
|
|
39
|
+
scrollPortWidth: O,
|
|
40
|
+
scrollContainerWidth: D,
|
|
41
|
+
rewind: b,
|
|
42
|
+
distanceMultiplier: K
|
|
43
|
+
});
|
|
44
|
+
p(() => {
|
|
45
|
+
let e = C.current;
|
|
46
|
+
e && Array.from(e.children).forEach((e, t) => e.dataset.index ??= t.toString());
|
|
47
|
+
}, [ne]);
|
|
48
|
+
let Q = f((e) => {
|
|
49
|
+
let t = e.scrollWidth, n = e.scrollLeft, r = e.offsetWidth, i = Array.from(e.children);
|
|
50
|
+
J || !r || !t || (d(n, 0) && requestAnimationFrame(() => {
|
|
51
|
+
i.slice(-G).reverse().forEach((t) => e.prepend(t)), e.scrollLeft = n + r;
|
|
52
|
+
}), d(t - n, r) && requestAnimationFrame(() => {
|
|
53
|
+
i.slice(0, G).forEach((t) => e.append(t)), e.scrollLeft = n - r;
|
|
54
|
+
}));
|
|
55
|
+
}, [G, J]), $ = h(void 0);
|
|
56
|
+
return p(() => {
|
|
57
|
+
if (!b) return;
|
|
58
|
+
let e = C.current;
|
|
59
|
+
if (!e) return;
|
|
60
|
+
let t = () => {
|
|
61
|
+
$ && clearTimeout($.current), $.current = setTimeout(() => Q(e), 50);
|
|
62
|
+
};
|
|
63
|
+
return e.addEventListener("scroll", t), () => {
|
|
64
|
+
$.current && clearTimeout($.current), e.removeEventListener("scroll", t);
|
|
65
|
+
};
|
|
66
|
+
}, [b, Q]), p(() => {
|
|
67
|
+
let e = C.current;
|
|
68
|
+
if (!e) return;
|
|
69
|
+
let t = new ResizeObserver(([{ target: t }]) => {
|
|
70
|
+
let n = t.getBoundingClientRect().width, r = t.scrollWidth;
|
|
71
|
+
oe(n), ae(r), b && Q(e);
|
|
72
|
+
});
|
|
73
|
+
return t.observe(e), () => {
|
|
74
|
+
t.disconnect();
|
|
75
|
+
};
|
|
76
|
+
}, [
|
|
77
|
+
G,
|
|
78
|
+
Q,
|
|
79
|
+
b,
|
|
80
|
+
R
|
|
81
|
+
]), p(() => {
|
|
82
|
+
if (!C.current) return;
|
|
83
|
+
let e = C.current.querySelectorAll("li[aria-roledescription='slide']").length;
|
|
84
|
+
z(e);
|
|
85
|
+
let t = () => {
|
|
86
|
+
let e = document.documentElement.clientWidth;
|
|
87
|
+
E(e);
|
|
88
|
+
}, n = (e) => {
|
|
89
|
+
if (!e.target) return;
|
|
90
|
+
let t = e.target, n = t.scrollLeft, i = r({
|
|
91
|
+
scrollPosition: n,
|
|
92
|
+
slidesContainer: t
|
|
93
|
+
});
|
|
94
|
+
se(n), le(i);
|
|
95
|
+
}, i = _(t, 200, { trailing: !0 }), a = _(n, 200, { trailing: !0 });
|
|
96
|
+
window.addEventListener("resize", i);
|
|
97
|
+
let o = C.current;
|
|
98
|
+
return o.addEventListener("scroll", a), () => {
|
|
99
|
+
window.removeEventListener("resize", i), o.removeEventListener("scroll", a);
|
|
100
|
+
};
|
|
101
|
+
}, []), e({
|
|
102
|
+
simulateTouch: re,
|
|
103
|
+
listRef: C
|
|
104
|
+
}), t({
|
|
105
|
+
start: ie,
|
|
106
|
+
slidesCount: R,
|
|
107
|
+
listRef: C
|
|
108
|
+
}), p(() => {
|
|
109
|
+
if (C.current) {
|
|
110
|
+
let e = o({
|
|
111
|
+
scrollPosition: k,
|
|
112
|
+
rewind: b,
|
|
113
|
+
forceDisableRewind: J
|
|
114
|
+
}), t = a({
|
|
115
|
+
scrollPosition: k,
|
|
116
|
+
scrollPortWidth: O,
|
|
117
|
+
scrollContainerWidth: D,
|
|
118
|
+
rewind: b,
|
|
119
|
+
forceDisableRewind: J
|
|
120
|
+
});
|
|
121
|
+
j(e), N(t);
|
|
122
|
+
}
|
|
123
|
+
}, [
|
|
124
|
+
b,
|
|
125
|
+
k,
|
|
126
|
+
O,
|
|
127
|
+
D,
|
|
128
|
+
J
|
|
129
|
+
]), {
|
|
130
|
+
activePage: ue,
|
|
131
|
+
activeSlide: Y,
|
|
132
|
+
pages: q,
|
|
133
|
+
listRef: C,
|
|
134
|
+
canScrollPrev: A,
|
|
135
|
+
canScrollNext: M,
|
|
136
|
+
onClickLeft: f(() => {
|
|
137
|
+
!C.current || !A || (C.current.scrollBy({
|
|
138
|
+
left: -X,
|
|
139
|
+
behavior: "smooth"
|
|
140
|
+
}), F(!0), L("left"));
|
|
141
|
+
}, [A, X]),
|
|
142
|
+
onClickRight: f(() => {
|
|
143
|
+
!C.current || !M || (C.current.scrollBy({
|
|
144
|
+
left: Z,
|
|
145
|
+
behavior: "smooth"
|
|
146
|
+
}), F(!0), L("right"));
|
|
147
|
+
}, [M, Z]),
|
|
148
|
+
slideWidth: ce,
|
|
149
|
+
getIntersectionObserver: H,
|
|
150
|
+
setIntersectionObserver: U,
|
|
151
|
+
buttonTriggeredScroll: P,
|
|
152
|
+
finishButtonTriggeredScroll: B,
|
|
153
|
+
scrollDirection: I,
|
|
154
|
+
slidesCount: R,
|
|
155
|
+
currentPerPage: G
|
|
156
|
+
};
|
|
157
|
+
};
|
|
158
|
+
//#endregion
|
|
159
|
+
export { v as default };
|
|
160
|
+
|
|
161
|
+
//# sourceMappingURL=useCreateCarousel.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useCreateCarousel.js","names":[],"sources":["../../src/useCreateCarousel.ts"],"sourcesContent":["import throttle from \"lodash.throttle\";\nimport { ReactNode, useCallback, useEffect, useMemo, useRef, useState } from \"react\";\n\nimport type { ResponsiveNumber } from \"./CarouselV2\";\nimport useSimulateTouch from \"./useSimulateTouch\";\nimport useStart from \"./useStart\";\nimport {\n getActivePage,\n getActiveSlide,\n getBreakpoint,\n getCanScrollNext,\n getCanScrollPrev,\n getCurrentPerMove,\n getCurrentPerPage,\n getPages,\n getScrollDistanceNext,\n getScrollDistancePrev,\n getSlideWidthStyle,\n roughlyEqual,\n} from \"./utils\";\n\ninterface UseCarouselProps {\n perPage?: number | ResponsiveNumber;\n perMove?: number | ResponsiveNumber;\n rewind?: boolean;\n gap?: number;\n padding?: number;\n children?: ReactNode;\n simulateTouch?: boolean;\n start?: number;\n}\n\nconst useCreateCarousel = ({\n perPage = 1,\n perMove = perPage,\n rewind = false,\n padding = 0,\n gap = 0,\n children,\n simulateTouch = false,\n start,\n}: UseCarouselProps) => {\n ////// STATE //////\n const listRef = useRef<HTMLUListElement>(null);\n\n const initialViewportWidth = typeof document !== \"undefined\" ? document.documentElement.clientWidth : 0;\n const [viewportWidth, setViewportWidth] = useState(initialViewportWidth);\n\n /** The overall width of the scroll container (also the overflowed part).\n * It's dynamic, because the slides are growing and shrinking according to the viewport width! */\n const [scrollContainerWidth, setScrollContainerWidth] = useState(0);\n // The width that is available for the slides. https://developer.mozilla.org/en-US/docs/Glossary/Scroll_container.\n const [scrollPortWidth, setScrollPortWidth] = useState(0);\n /** Position within the scroll container, measured from the left corner. Equals to \"scrollLeft\". */\n const [scrollPosition, setScrollPosition] = useState(0);\n const [canScrollPrev, setCanScrollPrev] = useState(false);\n const [canScrollNext, setCanScrollNext] = useState(true);\n /** If `true`, the carousel is scrolled with help of one of the arrow icon buttons. */\n const [buttonTriggeredScroll, setButtonTriggeredScroll] = useState(false);\n /** If not null, it indicates the direction of the button-triggered scroll. */\n const [scrollDirection, setScrollDirection] = useState<\"left\" | \"right\" | null>(null);\n\n const [slidesCount, setSlidesCount] = useState(0);\n\n const finishButtonTriggeredScroll = () => {\n setButtonTriggeredScroll(false);\n setScrollDirection(null);\n };\n\n const observerRef = useRef<IntersectionObserver | null>(null);\n const getIntersectionObserver = useCallback(() => observerRef.current, []);\n const setIntersectionObserver = useCallback((observer: IntersectionObserver | null) => {\n observerRef.current = observer;\n }, []);\n\n ////// DERIVED STATE //////\n\n const breakpoint = getBreakpoint({ viewportWidth });\n const currentPerPage = getCurrentPerPage({ perPage, breakpoint });\n const currentPerMove = getCurrentPerMove({ perMove, breakpoint });\n const distanceMultiplier = currentPerMove / currentPerPage;\n const slideWidth = getSlideWidthStyle({ perPage: currentPerPage, padding, gap });\n\n /** Overall amount of pages in the carousel. a page can hold one or multiple slides. */\n const pages = useMemo(() => getPages({ slidesCount, currentPerPage }), [slidesCount, currentPerPage]);\n\n // Disable rewind if there are less than 3 pages to avoid infinite prepending/appending loop\n const forceDisableRewind = useMemo(() => pages < 3, [pages]);\n\n /** Currently active slide */\n const [activeSlide, setActiveSlide] = useState(0);\n\n /** Currently active page, derived from activeSlide */\n const activePage = getActivePage({\n activeSlide,\n currentPerPage,\n slidesCount,\n pages,\n rewind,\n });\n\n /** Scroll distance is the amount in px the scroll port will be moved either to left or right. */\n const scrollDistancePrev = getScrollDistancePrev({ scrollPosition, scrollPortWidth, scrollContainerWidth, rewind, distanceMultiplier });\n const scrollDistanceNext = getScrollDistanceNext({\n scrollPosition,\n scrollPortWidth,\n scrollContainerWidth,\n rewind,\n distanceMultiplier,\n });\n\n ////// EFFECTS //////\n\n useEffect(() => {\n const slidesRef = listRef.current;\n if (!slidesRef) return;\n\n const items = Array.from(slidesRef.children) as HTMLLIElement[];\n\n // Add index to each slide item to figure out the current slide and page later on.\n // This effect runs when re-rendering children, so also when context changes due to resizing.\n // We need to ensure the item ids stay the same even in that case, so we only apply indexes if we don't have them already.\n items.forEach((item, i) => (item.dataset.index ??= i.toString()));\n }, [children]);\n\n // inspired by `updateSort` from here: https://codepen.io/scottjehl/pen/jOEzYWw\n const rearrangeSlides = useCallback(\n (el: HTMLUListElement) => {\n const scrollWidth = el.scrollWidth;\n const scrollLeft = el.scrollLeft;\n const width = el.offsetWidth;\n const items = Array.from(el.children);\n\n if (forceDisableRewind) return;\n\n // When teleported, width and scrollWidth might be 0 temporarily,\n // so we don't rearrange in that case to not mess up item order.\n if (!width || !scrollWidth) return;\n\n if (roughlyEqual(scrollLeft, 0)) {\n requestAnimationFrame(() => {\n const itemsToPrepend = items.slice(-currentPerPage).reverse();\n itemsToPrepend.forEach((item) => el.prepend(item));\n el.scrollLeft = scrollLeft + width;\n });\n }\n // roughly right edge\n if (roughlyEqual(scrollWidth - scrollLeft, width)) {\n requestAnimationFrame(() => {\n const itemsToAppend = items.slice(0, currentPerPage);\n itemsToAppend.forEach((item) => el.append(item));\n el.scrollLeft = scrollLeft - width;\n });\n }\n },\n [currentPerPage, forceDisableRewind],\n );\n\n const rearrangeSlidesTimeout = useRef<NodeJS.Timeout | undefined>(undefined);\n useEffect(() => {\n if (!rewind) return;\n\n const slidesRef = listRef.current;\n if (!slidesRef) return;\n\n const callRearrangeSlides = () => {\n if (rearrangeSlidesTimeout) clearTimeout(rearrangeSlidesTimeout.current);\n rearrangeSlidesTimeout.current = setTimeout(() => rearrangeSlides(slidesRef), 50);\n };\n\n slidesRef.addEventListener(\"scroll\", callRearrangeSlides);\n\n return () => {\n if (rearrangeSlidesTimeout.current) clearTimeout(rearrangeSlidesTimeout.current);\n slidesRef.removeEventListener(\"scroll\", callRearrangeSlides);\n };\n }, [rewind, rearrangeSlides]);\n\n useEffect(() => {\n const slidesRef = listRef.current;\n if (!slidesRef) return;\n\n const resizeObserver = new ResizeObserver(([{ target }]) => {\n // When teleporting, `scrollPortWidth`, `scrollContainerWidth` and `pages` are temporarily 0,\n // so we need to check once dimensions have stabilized.\n const newScrollPortWidth = target.getBoundingClientRect().width;\n const newScrollContainerWidth = target.scrollWidth;\n\n setScrollPortWidth(newScrollPortWidth);\n setScrollContainerWidth(newScrollContainerWidth);\n\n // This gets called on initial mount, but also after teleporting which is what we need in order\n // to fix a bug where you can't rewind from first to last page when clicking the prev button.\n if (rewind) rearrangeSlides(slidesRef);\n });\n\n resizeObserver.observe(slidesRef);\n\n return () => {\n resizeObserver.disconnect();\n };\n }, [currentPerPage, rearrangeSlides, rewind, slidesCount]);\n\n useEffect(() => {\n if (!listRef.current) return;\n\n const _slidesCount = listRef.current.querySelectorAll(\"li[aria-roledescription='slide']\").length;\n setSlidesCount(_slidesCount);\n\n const handleResize = () => {\n const newViewportWidth = document.documentElement.clientWidth;\n setViewportWidth(newViewportWidth);\n };\n\n const handleScroll = (event: Event) => {\n if (!event.target) return;\n\n const slidesContainer = event.target as HTMLUListElement;\n\n const newScrollPosition = slidesContainer.scrollLeft;\n const newActiveSlide = getActiveSlide({ scrollPosition: newScrollPosition, slidesContainer });\n\n setScrollPosition(newScrollPosition);\n setActiveSlide(newActiveSlide);\n };\n\n const throttledHandleResize = throttle(handleResize, 200, { trailing: true });\n const throttledHandleScroll = throttle(handleScroll, 200, { trailing: true });\n\n window.addEventListener(\"resize\", throttledHandleResize);\n const slidesRef = listRef.current;\n slidesRef.addEventListener(\"scroll\", throttledHandleScroll);\n\n return () => {\n window.removeEventListener(\"resize\", throttledHandleResize);\n slidesRef.removeEventListener(\"scroll\", throttledHandleScroll);\n };\n }, []);\n\n useSimulateTouch({ simulateTouch, listRef });\n useStart({ start, slidesCount, listRef });\n\n /////////////////////\n\n useEffect(() => {\n if (listRef.current) {\n const _canScrollLeft = getCanScrollPrev({ scrollPosition, rewind, forceDisableRewind });\n const _canScrollNext = getCanScrollNext({ scrollPosition, scrollPortWidth, scrollContainerWidth, rewind, forceDisableRewind });\n setCanScrollPrev(_canScrollLeft);\n setCanScrollNext(_canScrollNext);\n }\n }, [rewind, scrollPosition, scrollPortWidth, scrollContainerWidth, forceDisableRewind]);\n\n ////// CALLBACKS //////\n\n const onClickLeft = useCallback(() => {\n if (!listRef.current || !canScrollPrev) return;\n listRef.current.scrollBy({ left: -scrollDistancePrev, behavior: \"smooth\" });\n setButtonTriggeredScroll(true);\n setScrollDirection(\"left\");\n }, [canScrollPrev, scrollDistancePrev]);\n\n const onClickRight = useCallback(() => {\n if (!listRef.current || !canScrollNext) return;\n listRef.current.scrollBy({ left: scrollDistanceNext, behavior: \"smooth\" });\n setButtonTriggeredScroll(true);\n setScrollDirection(\"right\");\n }, [canScrollNext, scrollDistanceNext]);\n\n return {\n activePage,\n activeSlide,\n pages,\n listRef,\n canScrollPrev,\n canScrollNext,\n onClickLeft,\n onClickRight,\n slideWidth,\n getIntersectionObserver,\n setIntersectionObserver,\n buttonTriggeredScroll,\n finishButtonTriggeredScroll,\n scrollDirection,\n slidesCount,\n currentPerPage,\n };\n};\n\nexport default useCreateCarousel;\n"],"mappings":";;;;;;AAgCA,IAAM,KAAqB,EACzB,aAAU,GACV,aAAU,GACV,YAAS,IACT,aAAU,GACV,SAAM,GACN,cACA,oBAAgB,IAChB,gBACsB;CAEtB,IAAM,IAAU,EAAyB,IAAI,GAEvC,IAAuB,OAAO,WAAa,MAAc,SAAS,gBAAgB,cAAc,GAChG,CAAC,GAAe,KAAoB,EAAS,CAAoB,GAIjE,CAAC,GAAsB,MAA2B,EAAS,CAAC,GAE5D,CAAC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Q,WAAW,CAAC,MACzB,EAAQ,QAAQ,SAAS;IAAE,MAAM,CAAC;IAAoB,UAAU;GAAS,CAAC,GAC1E,EAAyB,EAAI,GAC7B,EAAmB,MAAM;EAC3B,GAAG,CAAC,GAAe,CAAkB,CAgBnC;EACA,cAfmB,QAAkB;GACjC,CAAC,EAAQ,WAAW,CAAC,MACzB,EAAQ,QAAQ,SAAS;IAAE,MAAM;IAAoB,UAAU;GAAS,CAAC,GACzE,EAAyB,EAAI,GAC7B,EAAmB,OAAO;EAC5B,GAAG,CAAC,GAAe,CAAkB,CAUnC;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;CACF;AACF"}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { useEffect as e } from "react";
|
|
2
|
+
//#region src/useSimulateTouch.ts
|
|
3
|
+
var t = ({ simulateTouch: t, listRef: n }) => {
|
|
4
|
+
e(() => {
|
|
5
|
+
if (!t) return;
|
|
6
|
+
let e = n.current;
|
|
7
|
+
if (!e) return;
|
|
8
|
+
let r = !1, i = !1, a, o, s = (t) => {
|
|
9
|
+
r = !0, i = !1, e.style.setProperty("scroll-snap-type", "none", "important"), a = t.pageX - e.offsetLeft, o = e.scrollLeft;
|
|
10
|
+
}, c = () => {
|
|
11
|
+
r && (r = !1, e.style.setProperty("scroll-behavior", "smooth", "important"), e.style.scrollSnapType = "", setTimeout(() => e.style.removeProperty("scroll-behavior"), 50));
|
|
12
|
+
}, l = (t) => {
|
|
13
|
+
if (!r) return;
|
|
14
|
+
t.preventDefault();
|
|
15
|
+
let n = t.pageX - e.offsetLeft - a;
|
|
16
|
+
Math.abs(n) > 10 && (i = !0), e.scrollLeft = o - n;
|
|
17
|
+
}, u = (e) => {
|
|
18
|
+
e.preventDefault();
|
|
19
|
+
}, d = (e) => {
|
|
20
|
+
i && (e.preventDefault(), e.stopPropagation());
|
|
21
|
+
};
|
|
22
|
+
return e.addEventListener("mousedown", s), e.addEventListener("mouseleave", c), e.addEventListener("mouseup", c), e.addEventListener("mousemove", l), e.addEventListener("dragstart", u), e.addEventListener("click", d, { capture: !0 }), () => {
|
|
23
|
+
e.removeEventListener("mousedown", s), e.removeEventListener("mouseleave", c), e.removeEventListener("mouseup", c), e.removeEventListener("mousemove", l), e.removeEventListener("dragstart", u), e.removeEventListener("click", d, { capture: !0 });
|
|
24
|
+
};
|
|
25
|
+
}, [n, t]);
|
|
26
|
+
};
|
|
27
|
+
//#endregion
|
|
28
|
+
export { t as default };
|
|
29
|
+
|
|
30
|
+
//# sourceMappingURL=useSimulateTouch.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useSimulateTouch.js","names":[],"sources":["../../src/useSimulateTouch.ts"],"sourcesContent":["import { RefObject, useEffect } from \"react\";\n\ninterface UseSimulateTouchProps {\n listRef: RefObject<HTMLUListElement | null>;\n simulateTouch: boolean;\n}\n\nconst useSimulateTouch = ({ simulateTouch, listRef }: UseSimulateTouchProps) => {\n useEffect(() => {\n if (!simulateTouch) return;\n const slidesContainer = listRef.current;\n if (!slidesContainer) return;\n\n let dragging = false;\n let hasDragged = false;\n let startX: number;\n let startScrollLeft: number;\n\n const handleMouseDown = (event: MouseEvent) => {\n dragging = true;\n hasDragged = false;\n slidesContainer.style.setProperty(\"scroll-snap-type\", \"none\", \"important\");\n startX = event.pageX - slidesContainer.offsetLeft;\n startScrollLeft = slidesContainer.scrollLeft;\n };\n\n const endDrag = () => {\n if (!dragging) return;\n dragging = false;\n\n // Make the first snap after releasing the mouse button smooth, restore normal behavior after scrolling has occured\n // TODO: Make it work in Safari.\n slidesContainer.style.setProperty(\"scroll-behavior\", \"smooth\", \"important\");\n slidesContainer.style.scrollSnapType = \"\";\n setTimeout(() => slidesContainer.style.removeProperty(\"scroll-behavior\"), 50);\n };\n\n const handleMouseMove = (event: MouseEvent) => {\n if (!dragging) return;\n event.preventDefault();\n const newX = event.pageX - slidesContainer.offsetLeft;\n const deltaX = newX - startX;\n if (Math.abs(deltaX) > 10) hasDragged = true;\n\n slidesContainer.scrollLeft = startScrollLeft - deltaX;\n };\n\n const handleDragStart = (e: DragEvent) => {\n e.preventDefault();\n };\n\n const handleClick = (event: MouseEvent) => {\n if (hasDragged) {\n event.preventDefault();\n event.stopPropagation();\n }\n };\n\n slidesContainer.addEventListener(\"mousedown\", handleMouseDown);\n slidesContainer.addEventListener(\"mouseleave\", endDrag);\n slidesContainer.addEventListener(\"mouseup\", endDrag);\n slidesContainer.addEventListener(\"mousemove\", handleMouseMove);\n slidesContainer.addEventListener(\"dragstart\", handleDragStart);\n slidesContainer.addEventListener(\"click\", handleClick, { capture: true });\n\n return () => {\n slidesContainer.removeEventListener(\"mousedown\", handleMouseDown);\n slidesContainer.removeEventListener(\"mouseleave\", endDrag);\n slidesContainer.removeEventListener(\"mouseup\", endDrag);\n slidesContainer.removeEventListener(\"mousemove\", handleMouseMove);\n slidesContainer.removeEventListener(\"dragstart\", handleDragStart);\n slidesContainer.removeEventListener(\"click\", handleClick, { capture: true });\n };\n }, [listRef, simulateTouch]);\n};\n\nexport default useSimulateTouch;\n"],"mappings":";;AAOA,IAAM,KAAoB,EAAE,kBAAe,iBAAqC;CAC9E,QAAgB;EACd,IAAI,CAAC,GAAe;EACpB,IAAM,IAAkB,EAAQ;EAChC,IAAI,CAAC,GAAiB;EAEtB,IAAI,IAAW,IACX,IAAa,IACb,GACA,GAEE,KAAmB,MAAsB;GAK7C,AAJA,IAAW,IACX,IAAa,IACb,EAAgB,MAAM,YAAY,oBAAoB,QAAQ,WAAW,GACzE,IAAS,EAAM,QAAQ,EAAgB,YACvC,IAAkB,EAAgB;EACpC,GAEM,UAAgB;GACf,MACL,IAAW,IAIX,EAAgB,MAAM,YAAY,mBAAmB,UAAU,WAAW,GAC1E,EAAgB,MAAM,iBAAiB,IACvC,iBAAiB,EAAgB,MAAM,eAAe,iBAAiB,GAAG,EAAE;EAC9E,GAEM,KAAmB,MAAsB;GAC7C,IAAI,CAAC,GAAU;GACf,EAAM,eAAe;GAErB,IAAM,IADO,EAAM,QAAQ,EAAgB,aACrB;GAGtB,AAFI,KAAK,IAAI,CAAM,IAAI,OAAI,IAAa,KAExC,EAAgB,aAAa,IAAkB;EACjD,GAEM,KAAmB,MAAiB;GACxC,EAAE,eAAe;EACnB,GAEM,KAAe,MAAsB;GACzC,AAAI,MACF,EAAM,eAAe,GACrB,EAAM,gBAAgB;EAE1B;EASA,OAPA,EAAgB,iBAAiB,aAAa,CAAe,GAC7D,EAAgB,iBAAiB,cAAc,CAAO,GACtD,EAAgB,iBAAiB,WAAW,CAAO,GACnD,EAAgB,iBAAiB,aAAa,CAAe,GAC7D,EAAgB,iBAAiB,aAAa,CAAe,GAC7D,EAAgB,iBAAiB,SAAS,GAAa,EAAE,SAAS,GAAK,CAAC,SAE3D;GAMX,AALA,EAAgB,oBAAoB,aAAa,CAAe,GAChE,EAAgB,oBAAoB,cAAc,CAAO,GACzD,EAAgB,oBAAoB,WAAW,CAAO,GACtD,EAAgB,oBAAoB,aAAa,CAAe,GAChE,EAAgB,oBAAoB,aAAa,CAAe,GAChE,EAAgB,oBAAoB,SAAS,GAAa,EAAE,SAAS,GAAK,CAAC;EAC7E;CACF,GAAG,CAAC,GAAS,CAAa,CAAC;AAC7B"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { useEffect as e } from "react";
|
|
2
|
+
//#region src/useStart.ts
|
|
3
|
+
var t = ({ slidesCount: t, start: n, listRef: r }) => {
|
|
4
|
+
e(() => {
|
|
5
|
+
!r.current || !n || n < 1 || n > t || r.current.querySelector(`[data-index='${n}']`)?.scrollIntoView({
|
|
6
|
+
behavior: "instant",
|
|
7
|
+
inline: "start"
|
|
8
|
+
});
|
|
9
|
+
}, [
|
|
10
|
+
n,
|
|
11
|
+
t,
|
|
12
|
+
r
|
|
13
|
+
]);
|
|
14
|
+
};
|
|
15
|
+
//#endregion
|
|
16
|
+
export { t as default };
|
|
17
|
+
|
|
18
|
+
//# sourceMappingURL=useStart.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useStart.js","names":[],"sources":["../../src/useStart.ts"],"sourcesContent":["import { RefObject, useEffect } from \"react\";\n\ninterface UseStartProps {\n listRef: RefObject<HTMLUListElement | null>;\n start?: number;\n slidesCount: number;\n}\n\nconst useStart = ({ slidesCount, start, listRef }: UseStartProps) => {\n useEffect(() => {\n if (!listRef.current || !start || start < 1 || start > slidesCount) return;\n\n const startSlide = listRef.current.querySelector(`[data-index='${start}']`);\n\n startSlide?.scrollIntoView({ behavior: \"instant\", inline: \"start\" });\n }, [start, slidesCount, listRef]);\n};\n\nexport default useStart;\n"],"mappings":";;AAQA,IAAM,KAAY,EAAE,gBAAa,UAAO,iBAA6B;CACnE,QAAgB;EACV,CAAC,EAAQ,WAAW,CAAC,KAAS,IAAQ,KAAK,IAAQ,KAIvD,EAF2B,QAAQ,cAAc,gBAAgB,EAAM,GAEvE,CAAA,EAAY,eAAe;GAAE,UAAU;GAAW,QAAQ;EAAQ,CAAC;CACrE,GAAG;EAAC;EAAO;EAAa;CAAO,CAAC;AAClC"}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { breakpoints as e } from "@bonprix-ds/utils";
|
|
2
|
+
//#region src/utils.ts
|
|
3
|
+
var t = (e, t, n = 10) => Math.abs(e - t) <= n, n = ({ slidesCount: e, currentPerPage: t }) => Math.ceil(e / t), r = ({ perPage: e, padding: t, gap: n }) => {
|
|
4
|
+
let r = i(e), s = a(t, e);
|
|
5
|
+
return `calc(${r} - ${o(n, e, t)} - ${s})`;
|
|
6
|
+
}, i = (e) => `100% / ${e}`, a = (e, t) => e ? `${e / t / 16}rem` : "0rem", o = (e, t, n) => e ? `${e * (n ? t : t - 1) / t / 16}rem` : "0rem", s = ({ scrollPosition: e, rewind: n, forceDisableRewind: r = !1 }) => n && !r ? !0 : !t(e, 0), c = ({ scrollPosition: e, scrollPortWidth: n, scrollContainerWidth: r, rewind: i = !1, forceDisableRewind: a = !1 }) => i && !a ? !0 : !t(e + n, r), l = ({ scrollPosition: e, scrollPortWidth: n, rewind: r = !1, distanceMultiplier: i = 1 }) => {
|
|
7
|
+
let a = t(e, 0), o = n * i;
|
|
8
|
+
return a && !r ? 0 : o;
|
|
9
|
+
}, u = ({ scrollPosition: e, scrollPortWidth: n, scrollContainerWidth: r, rewind: i = !1, distanceMultiplier: a = 1 }) => {
|
|
10
|
+
let o = t(e + n, r), s = n * a;
|
|
11
|
+
return o && !i ? 0 : s;
|
|
12
|
+
}, d = ({ viewportWidth: t }) => {
|
|
13
|
+
let n = "s";
|
|
14
|
+
return Object.entries(e).forEach(([e, r]) => {
|
|
15
|
+
t / 16 >= Number(r.split("rem")[0]) && (n = e);
|
|
16
|
+
}), n;
|
|
17
|
+
}, f = ({ scrollPosition: e, slidesContainer: t }) => g(h(t), e), p = ({ pages: e, slidesCount: t, currentPerPage: n, rewind: r, activeSlide: i }) => !r && m({
|
|
18
|
+
activeSlide: i,
|
|
19
|
+
currentPerPage: n,
|
|
20
|
+
slidesCount: t
|
|
21
|
+
}) ? e - 1 : Math.floor(i / n), m = ({ activeSlide: e, currentPerPage: t, slidesCount: n }) => e + t > n - 1, h = (e) => {
|
|
22
|
+
let t = Array.from(e.children), n = {};
|
|
23
|
+
return t.forEach((e) => n[Number(e.dataset.index)] = Math.abs(e.offsetLeft)), n;
|
|
24
|
+
}, g = (e, t) => {
|
|
25
|
+
let n = -1, r = Infinity;
|
|
26
|
+
for (let [i, a] of Object.entries(e)) {
|
|
27
|
+
let e = Math.abs(a - t);
|
|
28
|
+
e < r && (r = e, n = Number(i));
|
|
29
|
+
}
|
|
30
|
+
return n;
|
|
31
|
+
}, _ = (e) => {
|
|
32
|
+
let { perPage: t, breakpoint: n } = e;
|
|
33
|
+
return typeof t == "number" ? t : t[n] ?? Object.values(t)[Object.values(t).length - 1];
|
|
34
|
+
}, v = (e) => {
|
|
35
|
+
let { perMove: t, breakpoint: n } = e;
|
|
36
|
+
return typeof t == "number" ? t : t[n] ?? 1;
|
|
37
|
+
}, y = ({ pageNumber: e, perPage: t }) => e * t;
|
|
38
|
+
//#endregion
|
|
39
|
+
export { p as getActivePage, f as getActiveSlide, d as getBreakpoint, c as getCanScrollNext, s as getCanScrollPrev, g as getClosestSlideIndex, v as getCurrentPerMove, _ as getCurrentPerPage, y as getFirstSlideIndexOnPage, n as getPages, u as getScrollDistanceNext, l as getScrollDistancePrev, r as getSlideWidthStyle, m as rightEdgeReached, t as roughlyEqual };
|
|
40
|
+
|
|
41
|
+
//# sourceMappingURL=utils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.js","names":[],"sources":["../../src/utils.ts"],"sourcesContent":["import { Breakpoint, breakpoints } from \"@bonprix-ds/utils\";\n\nimport type { ResponsiveNumber } from \"./CarouselV2\";\n\n/** There is a bug at least on Google Phones + Chrome where scroll snaps to weird numbers like 130.13 instead of 130\n * and thus messing up the calculations. So we need to check for \"roughly equal\" and \"roughly divisible\" for now.\n */\nexport const roughlyEqual = (a: number, b: number, tolerance = 10) => {\n return Math.abs(a - b) <= tolerance;\n};\n\nexport const getPages = ({ slidesCount, currentPerPage }: { slidesCount: number; currentPerPage: number }) =>\n Math.ceil(slidesCount / currentPerPage);\n\ninterface GetSlideWidthOptions {\n perPage: number;\n padding: number;\n gap: number;\n}\n\nexport const getSlideWidthStyle = ({ perPage, padding, gap }: GetSlideWidthOptions) => {\n const percentagePart = getPercentagePart(perPage);\n const paddingPart = getPaddingPart(padding, perPage);\n const gapPart = getGapPart(gap, perPage, padding);\n\n return `calc(${percentagePart} - ${gapPart} - ${paddingPart})`;\n};\n\nconst getPercentagePart = (perPage: number) => `100% / ${perPage}`;\n\nconst getPaddingPart = (padding: number, perPage: number) => {\n if (!padding) return \"0rem\";\n return `${padding / perPage / 16}rem`;\n};\n\nconst getGapPart = (gap: number, perPage: number, padding: number) => {\n if (!gap) return \"0rem\";\n\n // When padding is set, there is an additional visible gap, so it should be accounted for\n const gapCount = padding ? perPage : perPage - 1;\n\n return `${(gap * gapCount) / perPage / 16}rem`;\n};\n\ninterface CanScrollPrevOptions {\n scrollPosition: number;\n rewind: boolean;\n forceDisableRewind?: boolean;\n}\n\nexport const getCanScrollPrev = ({ scrollPosition, rewind, forceDisableRewind = false }: CanScrollPrevOptions) =>\n rewind && !forceDisableRewind ? true : !roughlyEqual(scrollPosition, 0);\n\ninterface ScrollFunctionOptions {\n scrollPosition: number;\n scrollPortWidth: number;\n scrollContainerWidth: number;\n rewind?: boolean;\n forceDisableRewind?: boolean;\n}\n\nexport const getCanScrollNext = ({\n scrollPosition,\n scrollPortWidth,\n scrollContainerWidth,\n rewind = false,\n forceDisableRewind = false,\n}: ScrollFunctionOptions) => {\n return rewind && !forceDisableRewind ? true : !roughlyEqual(scrollPosition + scrollPortWidth, scrollContainerWidth);\n};\n\ninterface ScrollDistanceFunctionOptions extends Omit<ScrollFunctionOptions, \"forceDisableRewind\"> {\n distanceMultiplier?: number;\n}\n\nexport const getScrollDistancePrev = ({\n scrollPosition,\n scrollPortWidth,\n rewind = false,\n distanceMultiplier = 1,\n}: ScrollDistanceFunctionOptions) => {\n const firstSlideReached = roughlyEqual(scrollPosition, 0);\n const prevStepDistance = scrollPortWidth * distanceMultiplier;\n\n if (firstSlideReached && !rewind) return 0;\n return prevStepDistance;\n};\n\nexport const getScrollDistanceNext = ({\n scrollPosition,\n scrollPortWidth,\n scrollContainerWidth,\n rewind = false,\n distanceMultiplier = 1,\n}: ScrollDistanceFunctionOptions) => {\n const lastSlideReached = roughlyEqual(scrollPosition + scrollPortWidth, scrollContainerWidth);\n const nextStepDistance = scrollPortWidth * distanceMultiplier;\n\n if (lastSlideReached && !rewind) return 0;\n return nextStepDistance;\n};\n\nexport const getBreakpoint = ({ viewportWidth }: { viewportWidth: number }) => {\n let currentBreakpoint: Breakpoint = \"s\";\n Object.entries(breakpoints).forEach(([breakpoint, value]) => {\n // Convert to rem for comparison\n if (viewportWidth / 16 >= Number(value.split(\"rem\")[0])) currentBreakpoint = breakpoint as Breakpoint;\n });\n return currentBreakpoint;\n};\n\nexport const getActiveSlide = ({ scrollPosition, slidesContainer }: { scrollPosition: number; slidesContainer: HTMLUListElement }) => {\n const slideOffsets = getSlideOffsets(slidesContainer);\n return getClosestSlideIndex(slideOffsets, scrollPosition);\n};\n\nexport const getActivePage = ({\n pages,\n slidesCount,\n currentPerPage,\n rewind,\n activeSlide,\n}: {\n pages: number;\n slidesCount: number;\n currentPerPage: number;\n rewind: boolean;\n activeSlide: number;\n}) => {\n // always return last page\n if (!rewind && rightEdgeReached({ activeSlide, currentPerPage, slidesCount })) return pages - 1;\n\n return Math.floor(activeSlide / currentPerPage);\n};\n\nexport const rightEdgeReached = ({\n activeSlide,\n currentPerPage,\n slidesCount,\n}: {\n activeSlide: number;\n currentPerPage: number;\n slidesCount: number;\n}) => {\n // e.g. |0123|45..| (pages=2, currentPerPage=4, slidesCount=6)\n // activeSlide=1 would return false, because 1234 are visible and you can still scroll to the right to see 5\n // activeSlide=2 would return true, because 2345 are visible, right edge is reached\n return activeSlide + currentPerPage > slidesCount - 1;\n};\n\n// TODO: Add tests after jsdom or happy-dom are enabled in vitest?\nconst getSlideOffsets = (slidesContainer: HTMLUListElement) => {\n const items = Array.from(slidesContainer.children) as HTMLLIElement[];\n const slideOffsets = {} as Record<number, number>;\n items.forEach((item) => (slideOffsets[Number(item.dataset.index)] = Math.abs(item.offsetLeft)));\n\n return slideOffsets;\n};\n\nexport const getClosestSlideIndex = (slideOffsets: Record<number, number>, scrollPosition: number) => {\n let closestSlideIndex = -1;\n let minOffset = Infinity;\n\n for (const [index, offset] of Object.entries(slideOffsets)) {\n const distance = Math.abs(offset - scrollPosition);\n if (distance < minOffset) {\n minOffset = distance;\n closestSlideIndex = Number(index);\n }\n }\n\n return closestSlideIndex;\n};\n\nexport const getCurrentPerPage = (options: { perPage: number | ResponsiveNumber; breakpoint: Breakpoint }) => {\n const { perPage, breakpoint } = options;\n if (typeof perPage === \"number\") return perPage;\n\n return perPage[breakpoint] ?? Object.values(perPage)[Object.values(perPage).length - 1];\n};\n\nexport const getCurrentPerMove = (options: { perMove: number | ResponsiveNumber; breakpoint: Breakpoint }) => {\n const { perMove, breakpoint } = options;\n\n if (typeof perMove === \"number\") return perMove;\n\n return perMove[breakpoint] ?? 1;\n};\n\nexport const getFirstSlideIndexOnPage = ({ pageNumber, perPage }: { pageNumber: number; perPage: number }) => pageNumber * perPage;\n"],"mappings":";;AAOA,IAAa,KAAgB,GAAW,GAAW,IAAY,OACtD,KAAK,IAAI,IAAI,CAAC,KAAK,GAGf,KAAY,EAAE,gBAAa,wBACtC,KAAK,KAAK,IAAc,CAAc,GAQ3B,KAAsB,EAAE,YAAS,YAAS,aAAgC;CACrF,IAAM,IAAiB,EAAkB,CAAO,GAC1C,IAAc,EAAe,GAAS,CAAO;CAGnD,OAAO,QAAQ,EAAe,KAFd,EAAW,GAAK,GAAS,CAEN,EAAQ,KAAK,EAAY;AAC9D,GAEM,KAAqB,MAAoB,UAAU,KAEnD,KAAkB,GAAiB,MAClC,IACE,GAAG,IAAU,IAAU,GAAG,OADZ,QAIjB,KAAc,GAAa,GAAiB,MAC3C,IAKE,GAAI,KAFM,IAAU,IAAU,IAAU,KAElB,IAAU,GAAG,OALzB,QAcN,KAAoB,EAAE,mBAAgB,WAAQ,wBAAqB,SAC9E,KAAU,CAAC,IAAqB,KAAO,CAAC,EAAa,GAAgB,CAAC,GAU3D,KAAoB,EAC/B,mBACA,oBACA,yBACA,YAAS,IACT,wBAAqB,SAEd,KAAU,CAAC,IAAqB,KAAO,CAAC,EAAa,IAAiB,GAAiB,CAAoB,GAOvG,KAAyB,EACpC,mBACA,oBACA,YAAS,IACT,wBAAqB,QACc;CACnC,IAAM,IAAoB,EAAa,GAAgB,CAAC,GAClD,IAAmB,IAAkB;CAG3C,OADI,KAAqB,CAAC,IAAe,IAClC;AACT,GAEa,KAAyB,EACpC,mBACA,oBACA,yBACA,YAAS,IACT,wBAAqB,QACc;CACnC,IAA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5G,IAAM,EAAE,YAAS,kBAAe;CAIhC,OAFI,OAAO,KAAY,WAAiB,IAEjC,EAAQ,MAAe;AAChC,GAEa,KAA4B,EAAE,eAAY,iBAAuD,IAAa"}
|
package/package.json
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@bonprix-ds/react-carousel-v2",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "CarouselV2 component, implemented in React, for the Bonprix Design System.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"publishConfig": {
|
|
7
|
+
"access": "public",
|
|
8
|
+
"provenance": true
|
|
9
|
+
},
|
|
10
|
+
"module": "./dist/index.js",
|
|
11
|
+
"sideEffects": false,
|
|
12
|
+
"exports": {
|
|
13
|
+
".": {
|
|
14
|
+
"import": "./dist/index.js",
|
|
15
|
+
"types": "./dist/index.d.ts",
|
|
16
|
+
"default": "./dist/index.js"
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
"types": "./dist/index.d.ts",
|
|
20
|
+
"files": [
|
|
21
|
+
"dist"
|
|
22
|
+
],
|
|
23
|
+
"keywords": [],
|
|
24
|
+
"author": "",
|
|
25
|
+
"license": "UNLICENSED",
|
|
26
|
+
"repository": {
|
|
27
|
+
"type": "git",
|
|
28
|
+
"url": "git+https://github.com/bonprix/design-system.git",
|
|
29
|
+
"directory": "packages/react/carousel-v2"
|
|
30
|
+
},
|
|
31
|
+
"dependencies": {
|
|
32
|
+
"lodash.throttle": "4.1.1",
|
|
33
|
+
"@bonprix-ds/utils": "0.2.1",
|
|
34
|
+
"@bonprix-ds/internal-components": "0.1.5",
|
|
35
|
+
"@bonprix-ds/react-theme-forcer": "1.0.0"
|
|
36
|
+
},
|
|
37
|
+
"peerDependencies": {
|
|
38
|
+
"react": "^19.0.0",
|
|
39
|
+
"react-dom": "^19.0.0",
|
|
40
|
+
"tailwind-variants": "0.3.1"
|
|
41
|
+
},
|
|
42
|
+
"devDependencies": {
|
|
43
|
+
"vite": "8.2.2",
|
|
44
|
+
"vitest": "4.1.11",
|
|
45
|
+
"@testing-library/dom": "10.4.1",
|
|
46
|
+
"@testing-library/react": "16.3.3",
|
|
47
|
+
"@types/lodash.throttle": "4.1.9",
|
|
48
|
+
"@types/react": "19.2.18",
|
|
49
|
+
"@vitejs/plugin-react": "6.1.1",
|
|
50
|
+
"rollup-preserve-directives": "1.1.3",
|
|
51
|
+
"unplugin-dts": "1.0.3",
|
|
52
|
+
"@microsoft/api-extractor": "7.59.0"
|
|
53
|
+
},
|
|
54
|
+
"scripts": {
|
|
55
|
+
"build": "vite build"
|
|
56
|
+
}
|
|
57
|
+
}
|