@slurp-ui/react 1.0.1 → 1.1.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/CHANGELOG.md CHANGED
@@ -1,5 +1,16 @@
1
1
  # @slurp-ui/react
2
2
 
3
+ ## 1.1.0
4
+
5
+ ### Minor Changes
6
+
7
+ - 1bdb695: Add the extended components: `Suggest` (text field with suggestions, the field grows into the list), `Pages` (pagination, buttons or links), `Steps` (the steps of a flow of choices), `Rating` (stars, as an input or read-only), `Table` (sortable data table) and `Carousel` (media carousel on a scroll-snap track).
8
+
9
+ ### Patch Changes
10
+
11
+ - Updated dependencies [9e6f907]
12
+ - @slurp-ui/icons@1.1.0
13
+
3
14
  ## 1.0.1
4
15
 
5
16
  ### Patch Changes
@@ -0,0 +1,36 @@
1
+
2
+ import { PartsProps, PolymorphicProps } from "../lib/polymorphic.js";
3
+ import { ElementType, ReactNode } from "react";
4
+ export interface CarouselLabels {
5
+ previous: string;
6
+ next: string;
7
+ /** Name of a slide ("2 di 5"). */
8
+ slide: (index: number, count: number) => string;
9
+ /** Name of the button of a slide ("Mostra 2 di 5"). */
10
+ show: (index: number, count: number) => string;
11
+ /** Name of the group of slide buttons. */
12
+ picker: string;
13
+ }
14
+ export declare const CAROUSEL_LABELS: CarouselLabels;
15
+ export interface CarouselOwnProps extends PartsProps<'track' | 'slide' | 'controls' | 'previous' | 'next' | 'dot'> {
16
+ /** What it shows ("Foto del prodotto"): the name of the carousel. */
17
+ label: string;
18
+ /** The slides: one per child (a Stage, an image…). */
19
+ children: ReactNode;
20
+ /** Slide shown, from 0. */
21
+ index?: number;
22
+ /** Default 0. */
23
+ defaultIndex?: number;
24
+ onIndexChange?: (index: number) => void;
25
+ /** Above this many slides the dots (44px targets each) become a "2 / 12" counter. Default 5, which fits a phone. */
26
+ maxDots?: number;
27
+ labels?: Partial<CarouselLabels>;
28
+ }
29
+ export type CarouselProps<E extends ElementType = 'section'> = PolymorphicProps<E, CarouselOwnProps>;
30
+ /**
31
+ * Media carousel: one slide at a time on a scroll-snap track (swipe on touch, arrow keys on the focused track), with
32
+ * previous / next and a dot per slide (the current one is a longer pill). It never moves by itself; hidden slides are
33
+ * inert, the shown one is announced politely.
34
+ */
35
+ export declare function Carousel<E extends ElementType = 'section'>(props: CarouselProps<E>): import("react").JSX.Element;
36
+ //# sourceMappingURL=Carousel.d.ts.map
@@ -0,0 +1,179 @@
1
+ "use client";
2
+ import { cx } from "../utils/src/index.js";
3
+ import { useAnimationRoot } from "../internal/animation.js";
4
+ import { useCss, useParts, useThemeProps } from "../internal/styled.js";
5
+ import { Bubble } from "../Bubble/Bubble.js";
6
+ import { useControlled } from "../utils/useControlled.js";
7
+ import { Children, Fragment, useEffect, useId, useRef } from "react";
8
+ import { Fragment as Fragment$1, jsx, jsxs } from "react/jsx-runtime";
9
+ import { ChevronLeftIcon, ChevronRightIcon } from "@slurp-ui/icons";
10
+ // src/Carousel/Carousel.tsx
11
+ const CAROUSEL_LABELS = {
12
+ previous: "Precedente",
13
+ next: "Successiva",
14
+ slide: (i, n) => `${i} di ${n}`,
15
+ show: (i, n) => `Mostra ${i} di ${n}`,
16
+ picker: "Scegli cosa mostrare"
17
+ };
18
+ const reduced = () => typeof matchMedia === "function" && matchMedia("(prefers-reduced-motion: reduce)").matches;
19
+ /** Slide nearest to the track's scroll position (-1 when the track has no layout). */
20
+ function visible(track) {
21
+ const width = track.clientWidth;
22
+ return width ? Math.round(Math.abs(track.scrollLeft) / width) : -1;
23
+ }
24
+ /**
25
+ * Media carousel: one slide at a time on a scroll-snap track (swipe on touch, arrow keys on the focused track), with
26
+ * previous / next and a dot per slide (the current one is a longer pill). It never moves by itself; hidden slides are
27
+ * inert, the shown one is announced politely.
28
+ */
29
+ function Carousel(props) {
30
+ const { as, css, className, label, children, index: indexProp, defaultIndex = 0, onIndexChange, maxDots = 5, labels: labelsProp, parts, partProps, classNames, ...rest } = useThemeProps("Carousel", props);
31
+ const labels = {
32
+ ...CAROUSEL_LABELS,
33
+ ...labelsProp
34
+ };
35
+ const slides = Children.toArray(children);
36
+ const count = slides.length;
37
+ const [raw, setIndex] = useControlled({
38
+ value: indexProp,
39
+ defaultValue: defaultIndex,
40
+ onChange: onIndexChange
41
+ });
42
+ const index = Math.min(Math.max(0, count - 1), Math.max(0, raw));
43
+ const trackId = `${useId()}-track`;
44
+ /** Slide a programmatic scroll is heading to: the scroll events on the way do not change the index. */
45
+ const heading = useRef(null);
46
+ const Root = as ?? "section";
47
+ const styled = useCss("Carousel", "root", css);
48
+ const { part, style } = useParts("Carousel", {
49
+ parts,
50
+ partProps,
51
+ classNames
52
+ });
53
+ const root = useAnimationRoot(rest);
54
+ useEffect(() => {
55
+ const t = document.getElementById(trackId);
56
+ if (!t || visible(t) === index) return;
57
+ const slide = t.children[index];
58
+ if (!slide) return;
59
+ heading.current = {
60
+ to: index,
61
+ until: performance.now() + 1e3
62
+ };
63
+ t.scrollTo?.({
64
+ left: slide.offsetLeft,
65
+ behavior: reduced() ? "auto" : "smooth"
66
+ });
67
+ }, [index, trackId]);
68
+ const go = (to) => {
69
+ const next = Math.min(count - 1, Math.max(0, to));
70
+ if (next !== index) setIndex(next);
71
+ };
72
+ const latest = useRef({
73
+ index,
74
+ count,
75
+ setIndex
76
+ });
77
+ useEffect(() => {
78
+ latest.current = {
79
+ index,
80
+ count,
81
+ setIndex
82
+ };
83
+ });
84
+ useEffect(() => {
85
+ const t = document.getElementById(trackId);
86
+ if (!t) return;
87
+ const onScroll = (e) => {
88
+ const at = visible(t);
89
+ const h = heading.current;
90
+ if (h) {
91
+ if (at === h.to || e.timeStamp > h.until) heading.current = null;
92
+ else return;
93
+ }
94
+ const { index: now, count: n, setIndex: set } = latest.current;
95
+ if (at >= 0 && at < n && at !== now) set(at);
96
+ };
97
+ t.addEventListener("scroll", onScroll, { passive: true });
98
+ return () => t.removeEventListener("scroll", onScroll);
99
+ }, [trackId]);
100
+ const onKeyDown = (e) => {
101
+ if (e.target !== e.currentTarget) return;
102
+ const rtl = getComputedStyle(e.currentTarget).direction === "rtl";
103
+ const step = e.key === "ArrowRight" ? 1 : e.key === "ArrowLeft" ? -1 : 0;
104
+ if (step) go(index + (rtl ? -step : step));
105
+ else if (e.key === "Home") go(0);
106
+ else if (e.key === "End") go(count - 1);
107
+ else return;
108
+ e.preventDefault();
109
+ };
110
+ return /* @__PURE__ */ jsxs(Fragment$1, { children: [
111
+ styled.style,
112
+ style,
113
+ /* @__PURE__ */ jsxs(Root, {
114
+ className: cx("slurp-carousel", styled.className, className),
115
+ "aria-roledescription": "carousel",
116
+ "aria-label": label,
117
+ ...root,
118
+ children: [part("track", "div", {
119
+ id: trackId,
120
+ className: "slurp-carousel-track",
121
+ tabIndex: 0,
122
+ "aria-live": "polite",
123
+ onKeyDown,
124
+ children: slides.map((s, i) => /* @__PURE__ */ jsx(Fragment, { children: part("slide", "div", {
125
+ role: "group",
126
+ "aria-roledescription": "slide",
127
+ "aria-label": labels.slide(i + 1, count),
128
+ className: "slurp-carousel-slide",
129
+ "data-current": i === index ? "" : void 0,
130
+ inert: i !== index,
131
+ children: s
132
+ }) }, i))
133
+ }), count > 1 && part("controls", "div", {
134
+ className: "slurp-carousel-controls",
135
+ children: /* @__PURE__ */ jsxs(Fragment$1, { children: [
136
+ part("previous", Bubble, {
137
+ className: "slurp-carousel-previous",
138
+ label: labels.previous,
139
+ appearance: "soft",
140
+ disabled: index === 0,
141
+ onClick: () => go(index - 1),
142
+ children: /* @__PURE__ */ jsx(ChevronLeftIcon, { size: 20 })
143
+ }),
144
+ count <= maxDots ? /* @__PURE__ */ jsx("div", {
145
+ className: "slurp-carousel-dots",
146
+ role: "group",
147
+ "aria-label": labels.picker,
148
+ children: slides.map((_, i) => /* @__PURE__ */ jsx(Fragment, { children: part("dot", "button", {
149
+ type: "button",
150
+ className: "slurp-carousel-dot",
151
+ "aria-label": labels.show(i + 1, count),
152
+ "aria-current": i === index ? "true" : void 0,
153
+ onClick: () => go(i)
154
+ }) }, i))
155
+ }) : /* @__PURE__ */ jsxs("span", {
156
+ className: "slurp-carousel-status",
157
+ "aria-hidden": "true",
158
+ children: [
159
+ index + 1,
160
+ " / ",
161
+ count
162
+ ]
163
+ }),
164
+ part("next", Bubble, {
165
+ className: "slurp-carousel-next",
166
+ label: labels.next,
167
+ appearance: "soft",
168
+ disabled: index === count - 1,
169
+ onClick: () => go(index + 1),
170
+ children: /* @__PURE__ */ jsx(ChevronRightIcon, { size: 20 })
171
+ })
172
+ ] })
173
+ })]
174
+ })
175
+ ] });
176
+ }
177
+ export { CAROUSEL_LABELS, Carousel };
178
+
179
+ //# sourceMappingURL=Carousel.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Carousel.js","names":[],"sources":["../../src/Carousel/Carousel.tsx"],"sourcesContent":["import { ChevronLeftIcon, ChevronRightIcon } from '@slurp-ui/icons';\nimport { cx } from '@slurp-ui/utils';\nimport {\n Children,\n Fragment,\n useEffect,\n useId,\n useRef,\n type ElementType,\n type KeyboardEvent,\n type ReactNode,\n} from 'react';\n\nimport { Bubble } from '../Bubble/Bubble';\nimport { useAnimationRoot } from '../internal/animation';\nimport { useCss, useParts, useThemeProps } from '../internal/styled';\nimport type { PartsProps, PolymorphicProps } from '../lib/polymorphic';\nimport { useControlled } from '../utils/useControlled';\n\nexport interface CarouselLabels {\n previous: string;\n next: string;\n /** Name of a slide (\"2 di 5\"). */\n slide: (index: number, count: number) => string;\n /** Name of the button of a slide (\"Mostra 2 di 5\"). */\n show: (index: number, count: number) => string;\n /** Name of the group of slide buttons. */\n picker: string;\n}\n\nexport const CAROUSEL_LABELS: CarouselLabels = {\n previous: 'Precedente',\n next: 'Successiva',\n slide: (i, n) => `${i} di ${n}`,\n show: (i, n) => `Mostra ${i} di ${n}`,\n picker: 'Scegli cosa mostrare',\n};\n\nexport interface CarouselOwnProps extends PartsProps<\n 'track' | 'slide' | 'controls' | 'previous' | 'next' | 'dot'\n> {\n /** What it shows (\"Foto del prodotto\"): the name of the carousel. */\n label: string;\n /** The slides: one per child (a Stage, an image…). */\n children: ReactNode;\n /** Slide shown, from 0. */\n index?: number;\n /** Default 0. */\n defaultIndex?: number;\n onIndexChange?: (index: number) => void;\n /** Above this many slides the dots (44px targets each) become a \"2 / 12\" counter. Default 5, which fits a phone. */\n maxDots?: number;\n labels?: Partial<CarouselLabels>;\n}\n\nexport type CarouselProps<E extends ElementType = 'section'> = PolymorphicProps<\n E,\n CarouselOwnProps\n>;\n\nconst reduced = () =>\n typeof matchMedia === 'function' && matchMedia('(prefers-reduced-motion: reduce)').matches;\n\n/** Slide nearest to the track's scroll position (-1 when the track has no layout). */\nfunction visible(track: HTMLElement): number {\n const width = track.clientWidth;\n return width ? Math.round(Math.abs(track.scrollLeft) / width) : -1;\n}\n\n/**\n * Media carousel: one slide at a time on a scroll-snap track (swipe on touch, arrow keys on the focused track), with\n * previous / next and a dot per slide (the current one is a longer pill). It never moves by itself; hidden slides are\n * inert, the shown one is announced politely.\n */\nexport function Carousel<E extends ElementType = 'section'>(props: CarouselProps<E>) {\n const {\n as,\n css,\n className,\n label,\n children,\n index: indexProp,\n defaultIndex = 0,\n onIndexChange,\n maxDots = 5,\n labels: labelsProp,\n parts,\n partProps,\n classNames,\n ...rest\n } = useThemeProps('Carousel', props as CarouselProps);\n const labels = { ...CAROUSEL_LABELS, ...labelsProp };\n const slides = Children.toArray(children);\n const count = slides.length;\n const [raw, setIndex] = useControlled({\n value: indexProp,\n defaultValue: defaultIndex,\n onChange: onIndexChange,\n });\n const index = Math.min(Math.max(0, count - 1), Math.max(0, raw));\n const trackId = `${useId()}-track`;\n /** Slide a programmatic scroll is heading to: the scroll events on the way do not change the index. */\n const heading = useRef<{ to: number; until: number } | null>(null);\n const Root = (as ?? 'section') as ElementType;\n const styled = useCss('Carousel', 'root', css);\n const { part, style } = useParts('Carousel', { parts, partProps, classNames });\n const root = useAnimationRoot(rest);\n\n // The index changed (buttons, keys, a controlled value): bring that slide into view.\n useEffect(() => {\n const t = document.getElementById(trackId);\n if (!t || visible(t) === index) return;\n const slide = t.children[index] as HTMLElement | undefined;\n if (!slide) return;\n heading.current = { to: index, until: performance.now() + 1000 };\n t.scrollTo?.({ left: slide.offsetLeft, behavior: reduced() ? 'auto' : 'smooth' });\n }, [index, trackId]);\n\n const go = (to: number) => {\n const next = Math.min(count - 1, Math.max(0, to));\n if (next !== index) setIndex(next);\n };\n\n // Swipes and scrolling: the slide that snapped into view becomes the current one.\n const latest = useRef({ index, count, setIndex });\n useEffect(() => {\n latest.current = { index, count, setIndex };\n });\n useEffect(() => {\n const t = document.getElementById(trackId);\n if (!t) return;\n const onScroll = (e: Event) => {\n const at = visible(t);\n const h = heading.current;\n if (h) {\n if (at === h.to || e.timeStamp > h.until) heading.current = null;\n else return;\n }\n const { index: now, count: n, setIndex: set } = latest.current;\n if (at >= 0 && at < n && at !== now) set(at);\n };\n t.addEventListener('scroll', onScroll, { passive: true });\n return () => t.removeEventListener('scroll', onScroll);\n }, [trackId]);\n\n const onKeyDown = (e: KeyboardEvent) => {\n if (e.target !== e.currentTarget) return;\n const rtl = getComputedStyle(e.currentTarget).direction === 'rtl';\n const step = e.key === 'ArrowRight' ? 1 : e.key === 'ArrowLeft' ? -1 : 0;\n if (step) go(index + (rtl ? -step : step));\n else if (e.key === 'Home') go(0);\n else if (e.key === 'End') go(count - 1);\n else return;\n e.preventDefault();\n };\n\n return (\n <>\n {styled.style}\n {style}\n <Root\n className={cx('slurp-carousel', styled.className, className)}\n aria-roledescription=\"carousel\"\n aria-label={label}\n {...root}\n >\n {part('track', 'div', {\n id: trackId,\n className: 'slurp-carousel-track',\n tabIndex: 0,\n 'aria-live': 'polite',\n onKeyDown,\n children: slides.map((s, i) => (\n <Fragment key={i}>\n {part('slide', 'div', {\n role: 'group',\n 'aria-roledescription': 'slide',\n 'aria-label': labels.slide(i + 1, count),\n className: 'slurp-carousel-slide',\n 'data-current': i === index ? '' : undefined,\n inert: i !== index,\n children: s,\n })}\n </Fragment>\n )),\n })}\n {count > 1 &&\n part('controls', 'div', {\n className: 'slurp-carousel-controls',\n children: (\n <>\n {part('previous', Bubble, {\n className: 'slurp-carousel-previous',\n label: labels.previous,\n appearance: 'soft',\n disabled: index === 0,\n onClick: () => go(index - 1),\n children: <ChevronLeftIcon size={20} />,\n })}\n {count <= maxDots ? (\n <div className=\"slurp-carousel-dots\" role=\"group\" aria-label={labels.picker}>\n {slides.map((_, i) => (\n <Fragment key={i}>\n {part('dot', 'button', {\n type: 'button',\n className: 'slurp-carousel-dot',\n 'aria-label': labels.show(i + 1, count),\n 'aria-current': i === index ? 'true' : undefined,\n onClick: () => go(i),\n })}\n </Fragment>\n ))}\n </div>\n ) : (\n <span className=\"slurp-carousel-status\" aria-hidden=\"true\">\n {index + 1} / {count}\n </span>\n )}\n {part('next', Bubble, {\n className: 'slurp-carousel-next',\n label: labels.next,\n appearance: 'soft',\n disabled: index === count - 1,\n onClick: () => go(index + 1),\n children: <ChevronRightIcon size={20} />,\n })}\n </>\n ),\n })}\n </Root>\n </>\n );\n}\n"],"mappings":";;;;;;;;;;AA8BA,MAAa,kBAAkC;CAC7C,UAAU;CACV,MAAM;CACN,QAAQ,GAAG,MAAM,GAAG,EAAE,MAAM;CAC5B,OAAO,GAAG,MAAM,UAAU,EAAE,MAAM;CAClC,QAAQ;AACV;AAwBA,MAAM,gBACJ,OAAO,eAAe,cAAc,WAAW,kCAAkC,CAAC,CAAC;;AAGrF,SAAS,QAAQ,OAA4B;CAC3C,MAAM,QAAQ,MAAM;CACpB,OAAO,QAAQ,KAAK,MAAM,KAAK,IAAI,MAAM,UAAU,IAAI,KAAK,IAAI;AAClE;;;;;;AAOA,SAAgB,SAA4C,OAAyB;CACnF,MAAM,EACJ,IACA,KACA,WACA,OACA,UACA,OAAO,WACP,eAAe,GACf,eACA,UAAU,GACV,QAAQ,YACR,OACA,WACA,YACA,GAAG,SACD,cAAc,YAAY,KAAsB;CACpD,MAAM,SAAS;EAAE,GAAG;EAAiB,GAAG;CAAW;C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AC,GACA,QAAQ,KACP,KAAK,YAAY,OAAO;IACtB,WAAW;IACX,UACE,qBAAA,YAAA,EAAA,UAAA;KACG,KAAK,YAAY,QAAQ;MACxB,WAAW;MACX,OAAO,OAAO;MACd,YAAY;MACZ,UAAU,UAAU;MACpB,eAAe,GAAG,QAAQ,CAAC;MAC3B,UAAU,oBAAC,iBAAD,EAAiB,MAAM,GAAK,CAAA;KACxC,CAAC;KACA,SAAS,UACR,oBAAC,OAAD;MAAK,WAAU;MAAsB,MAAK;MAAQ,cAAY,OAAO;MAClE,UAAA,OAAO,KAAK,GAAG,MACd,oBAAC,UAAD,EAAA,UACG,KAAK,OAAO,UAAU;OACrB,MAAM;OACN,WAAW;OACX,cAAc,OAAO,KAAK,IAAI,GAAG,KAAK;OACtC,gBAAgB,MAAM,QAAQ,SAAS,KAAA;OACvC,eAAe,GAAG,CAAC;MACrB,CAAC,EACO,GARK,CAQL,CACX;KACE,CAAA,IAEL,qBAAC,QAAD;MAAM,WAAU;MAAwB,eAAY;MAApD,UAAA;OACG,QAAQ;OAAE;OAAI;MACX;;KAEP,KAAK,QAAQ,QAAQ;MACpB,WAAW;MACX,OAAO,OAAO;MACd,YAAY;MACZ,UAAU,UAAU,QAAQ;MAC5B,eAAe,GAAG,QAAQ,CAAC;MAC3B,UAAU,oBAAC,kBAAD,EAAkB,MAAM,GAAK,CAAA;KACzC,CAAC;IACD,EAAA,CAAA;GAEN,CAAC,CACC;;CACN,EAAA,CAAA;AAEN"}
@@ -0,0 +1,3 @@
1
+
2
+ import { CAROUSEL_LABELS, Carousel, CarouselLabels, CarouselProps } from "./Carousel.js";
3
+ export { CAROUSEL_LABELS, Carousel, type CarouselLabels, type CarouselProps, Carousel as default };
@@ -0,0 +1,2 @@
1
+
2
+ export { CAROUSEL_LABELS, Carousel, Carousel as default } from "./Carousel.js";
@@ -0,0 +1,45 @@
1
+
2
+ import { PartsProps, PolymorphicProps } from "../lib/polymorphic.js";
3
+ import { ElementType } from "react";
4
+ export interface PagesLabels {
5
+ /** Name of the navigation landmark. */
6
+ label: string;
7
+ previous: string;
8
+ next: string;
9
+ /** Name of a page link ("Pagina 3"). */
10
+ page: (page: number) => string;
11
+ /** Where you are, shown on phones instead of the numbers ("3 di 12"). */
12
+ status: (page: number, count: number) => string;
13
+ }
14
+ export declare const PAGES_LABELS: PagesLabels;
15
+ export interface PagesOwnProps extends PartsProps<'list' | 'item' | 'previous' | 'next' | 'status'> {
16
+ /** Number of pages. */
17
+ count: number;
18
+ /** Current page, from 1. */
19
+ page?: number;
20
+ /** Default 1. */
21
+ defaultPage?: number;
22
+ onPageChange?: (page: number) => void;
23
+ /** Pages shown on each side of the current one. Default 1. */
24
+ siblings?: number;
25
+ /** Pages always shown at each end. Default 1. */
26
+ boundaries?: number;
27
+ /** URL of a page: the items become links (crawlable, open in a new tab). */
28
+ href?: (page: number) => string;
29
+ disabled?: boolean;
30
+ labels?: Partial<PagesLabels>;
31
+ }
32
+ export type PagesProps<E extends ElementType = 'nav'> = PolymorphicProps<E, PagesOwnProps>;
33
+ /** A page number or a gap ("…") standing for the pages left out. */
34
+ export type PageItem = number | 'gap';
35
+ /**
36
+ * The items of a pagination: the ends, a window around the current page and a gap where pages are left out. The length
37
+ * stays the same whatever the page, so the controls never jump; a gap never stands for a single page.
38
+ */
39
+ export declare function pageItems(page: number, count: number, siblings?: number, boundaries?: number): PageItem[];
40
+ /**
41
+ * Pagination: previous, the page numbers (the current one filled, `aria-current="page"`) and next. On phones only
42
+ * previous, "3 di 12" and next. With `href` every item is a real link.
43
+ */
44
+ export declare function Pages<E extends ElementType = 'nav'>(props: PagesProps<E>): import("react").JSX.Element;
45
+ //# sourceMappingURL=Pages.d.ts.map
@@ -0,0 +1,128 @@
1
+ "use client";
2
+ import { cx } from "../utils/src/index.js";
3
+ import { useAnimationRoot } from "../internal/animation.js";
4
+ import { useCss, useParts, useThemeProps } from "../internal/styled.js";
5
+ import { useControlled } from "../utils/useControlled.js";
6
+ import { Fragment } from "react";
7
+ import { Fragment as Fragment$1, jsx, jsxs } from "react/jsx-runtime";
8
+ import { ChevronLeftIcon, ChevronRightIcon } from "@slurp-ui/icons";
9
+ // src/Pages/Pages.tsx
10
+ const PAGES_LABELS = {
11
+ label: "Pagine",
12
+ previous: "Pagina precedente",
13
+ next: "Pagina successiva",
14
+ page: (n) => `Pagina ${n}`,
15
+ status: (n, count) => `${n} di ${count}`
16
+ };
17
+ const span = (from, to) => Array.from({ length: Math.max(0, to - from + 1) }, (_, i) => from + i);
18
+ /**
19
+ * The items of a pagination: the ends, a window around the current page and a gap where pages are left out. The length
20
+ * stays the same whatever the page, so the controls never jump; a gap never stands for a single page.
21
+ */
22
+ function pageItems(page, count, siblings = 1, boundaries = 1) {
23
+ if (count <= 2 * boundaries + 2 * siblings + 3) return span(1, count);
24
+ const lo = Math.min(Math.max(page - siblings, boundaries + 2), count - boundaries - 1 - 2 * siblings);
25
+ const hi = lo + 2 * siblings;
26
+ const left = lo === boundaries + 2 ? span(1, boundaries + 1) : [...span(1, boundaries), "gap"];
27
+ const right = hi === count - boundaries - 1 ? span(count - boundaries, count) : ["gap", ...span(count - boundaries + 1, count)];
28
+ return [
29
+ ...left,
30
+ ...span(lo, hi),
31
+ ...right
32
+ ];
33
+ }
34
+ /**
35
+ * Pagination: previous, the page numbers (the current one filled, `aria-current="page"`) and next. On phones only
36
+ * previous, "3 di 12" and next. With `href` every item is a real link.
37
+ */
38
+ function Pages(props) {
39
+ const { as, css, className, count: countProp, page: pageProp, defaultPage = 1, onPageChange, siblings = 1, boundaries = 1, href, disabled = false, labels: labelsProp, parts, partProps, classNames, ...rest } = useThemeProps("Pages", props);
40
+ const labels = {
41
+ ...PAGES_LABELS,
42
+ ...labelsProp
43
+ };
44
+ const count = Math.max(1, Math.floor(countProp));
45
+ const [raw, setPage] = useControlled({
46
+ value: pageProp,
47
+ defaultValue: defaultPage,
48
+ onChange: onPageChange
49
+ });
50
+ const page = Math.min(count, Math.max(1, raw));
51
+ const Root = as ?? "nav";
52
+ const styled = useCss("Pages", "root", css);
53
+ const { part, style } = useParts("Pages", {
54
+ parts,
55
+ partProps,
56
+ classNames
57
+ });
58
+ const root = useAnimationRoot(rest);
59
+ /** A link or a button that goes to `to`; unavailable (the ends, disabled) it keeps its place but does nothing. */
60
+ const control = (to, off) => {
61
+ const go = (e) => {
62
+ if (off) {
63
+ e.preventDefault();
64
+ return;
65
+ }
66
+ if (to !== page) setPage(to);
67
+ };
68
+ if (href) return off ? {
69
+ "aria-disabled": true,
70
+ role: "link",
71
+ onClick: go
72
+ } : {
73
+ href: href(to),
74
+ onClick: go
75
+ };
76
+ return {
77
+ type: "button",
78
+ disabled: off,
79
+ onClick: go
80
+ };
81
+ };
82
+ const Tag = href ? "a" : "button";
83
+ return /* @__PURE__ */ jsxs(Fragment$1, { children: [
84
+ styled.style,
85
+ style,
86
+ /* @__PURE__ */ jsxs(Root, {
87
+ className: cx("slurp-pages", styled.className, className),
88
+ "aria-label": labels.label,
89
+ "data-disabled": disabled ? "" : void 0,
90
+ ...root,
91
+ children: [
92
+ part("previous", Tag, {
93
+ ...control(page - 1, disabled || page <= 1),
94
+ className: "slurp-pages-step",
95
+ "aria-label": labels.previous,
96
+ children: /* @__PURE__ */ jsx(ChevronLeftIcon, { size: 20 })
97
+ }),
98
+ part("list", "ul", {
99
+ className: "slurp-pages-list",
100
+ children: pageItems(page, count, siblings, boundaries).map((item, i) => /* @__PURE__ */ jsx("li", { children: item === "gap" ? /* @__PURE__ */ jsx("span", {
101
+ className: "slurp-pages-gap",
102
+ "aria-hidden": "true",
103
+ children: "…"
104
+ }) : /* @__PURE__ */ jsx(Fragment, { children: part("item", Tag, {
105
+ ...control(item, disabled),
106
+ className: "slurp-pages-item",
107
+ "aria-label": labels.page(item),
108
+ "aria-current": item === page ? "page" : void 0,
109
+ children: item
110
+ }) }) }, item === "gap" ? `gap-${i}` : item))
111
+ }),
112
+ part("status", "span", {
113
+ className: "slurp-pages-status",
114
+ children: labels.status(page, count)
115
+ }),
116
+ part("next", Tag, {
117
+ ...control(page + 1, disabled || page >= count),
118
+ className: "slurp-pages-step",
119
+ "aria-label": labels.next,
120
+ children: /* @__PURE__ */ jsx(ChevronRightIcon, { size: 20 })
121
+ })
122
+ ]
123
+ })
124
+ ] });
125
+ }
126
+ export { PAGES_LABELS, Pages, pageItems };
127
+
128
+ //# sourceMappingURL=Pages.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Pages.js","names":[],"sources":["../../src/Pages/Pages.tsx"],"sourcesContent":["import { ChevronLeftIcon, ChevronRightIcon } from '@slurp-ui/icons';\nimport { cx } from '@slurp-ui/utils';\nimport { Fragment, type ElementType, type MouseEvent } from 'react';\n\nimport { useAnimationRoot } from '../internal/animation';\nimport { useCss, useParts, useThemeProps } from '../internal/styled';\nimport type { PartsProps, PolymorphicProps } from '../lib/polymorphic';\nimport { useControlled } from '../utils/useControlled';\n\nexport interface PagesLabels {\n /** Name of the navigation landmark. */\n label: string;\n previous: string;\n next: string;\n /** Name of a page link (\"Pagina 3\"). */\n page: (page: number) => string;\n /** Where you are, shown on phones instead of the numbers (\"3 di 12\"). */\n status: (page: number, count: number) => string;\n}\n\nexport const PAGES_LABELS: PagesLabels = {\n label: 'Pagine',\n previous: 'Pagina precedente',\n next: 'Pagina successiva',\n page: (n) => `Pagina ${n}`,\n status: (n, count) => `${n} di ${count}`,\n};\n\nexport interface PagesOwnProps extends PartsProps<\n 'list' | 'item' | 'previous' | 'next' | 'status'\n> {\n /** Number of pages. */\n count: number;\n /** Current page, from 1. */\n page?: number;\n /** Default 1. */\n defaultPage?: number;\n onPageChange?: (page: number) => void;\n /** Pages shown on each side of the current one. Default 1. */\n siblings?: number;\n /** Pages always shown at each end. Default 1. */\n boundaries?: number;\n /** URL of a page: the items become links (crawlable, open in a new tab). */\n href?: (page: number) => string;\n disabled?: boolean;\n labels?: Partial<PagesLabels>;\n}\n\nexport type PagesProps<E extends ElementType = 'nav'> = PolymorphicProps<E, PagesOwnProps>;\n\n/** A page number or a gap (\"…\") standing for the pages left out. */\nexport type PageItem = number | 'gap';\n\nconst span = (from: number, to: number) =>\n Array.from({ length: Math.max(0, to - from + 1) }, (_, i) => from + i);\n\n/**\n * The items of a pagination: the ends, a window around the current page and a gap where pages are left out. The length\n * stays the same whatever the page, so the controls never jump; a gap never stands for a single page.\n */\nexport function pageItems(page: number, count: number, siblings = 1, boundaries = 1): PageItem[] {\n const slots = 2 * boundaries + 2 * siblings + 3;\n if (count <= slots) return span(1, count);\n const lo = Math.min(\n Math.max(page - siblings, boundaries + 2),\n count - boundaries - 1 - 2 * siblings,\n );\n const hi = lo + 2 * siblings;\n const left: PageItem[] =\n lo === boundaries + 2 ? span(1, boundaries + 1) : [...span(1, boundaries), 'gap'];\n const right: PageItem[] =\n hi === count - boundaries - 1\n ? span(count - boundaries, count)\n : ['gap', ...span(count - boundaries + 1, count)];\n return [...left, ...span(lo, hi), ...right];\n}\n\n/**\n * Pagination: previous, the page numbers (the current one filled, `aria-current=\"page\"`) and next. On phones only\n * previous, \"3 di 12\" and next. With `href` every item is a real link.\n */\nexport function Pages<E extends ElementType = 'nav'>(props: PagesProps<E>) {\n const {\n as,\n css,\n className,\n count: countProp,\n page: pageProp,\n defaultPage = 1,\n onPageChange,\n siblings = 1,\n boundaries = 1,\n href,\n disabled = false,\n labels: labelsProp,\n parts,\n partProps,\n classNames,\n ...rest\n } = useThemeProps('Pages', props as PagesProps);\n const labels = { ...PAGES_LABELS, ...labelsProp };\n const count = Math.max(1, Math.floor(countProp));\n const [raw, setPage] = useControlled({\n value: pageProp,\n defaultValue: defaultPage,\n onChange: onPageChange,\n });\n const page = Math.min(count, Math.max(1, raw));\n const Root = (as ?? 'nav') as ElementType;\n const styled = useCss('Pages', 'root', css);\n const { part, style } = useParts('Pages', { parts, partProps, classNames });\n const root = useAnimationRoot(rest);\n\n /** A link or a button that goes to `to`; unavailable (the ends, disabled) it keeps its place but does nothing. */\n const control = (to: number, off: boolean) => {\n const go = (e: MouseEvent) => {\n if (off) {\n e.preventDefault();\n return;\n }\n if (to !== page) setPage(to);\n };\n if (href) {\n return off\n ? { 'aria-disabled': true, role: 'link', onClick: go }\n : { href: href(to), onClick: go };\n }\n return { type: 'button', disabled: off, onClick: go };\n };\n const Tag = href ? 'a' : 'button';\n\n return (\n <>\n {styled.style}\n {style}\n <Root\n className={cx('slurp-pages', styled.className, className)}\n aria-label={labels.label}\n data-disabled={disabled ? '' : undefined}\n {...root}\n >\n {part('previous', Tag, {\n ...control(page - 1, disabled || page <= 1),\n className: 'slurp-pages-step',\n 'aria-label': labels.previous,\n children: <ChevronLeftIcon size={20} />,\n })}\n {part('list', 'ul', {\n className: 'slurp-pages-list',\n children: pageItems(page, count, siblings, boundaries).map((item, i) => (\n <li key={item === 'gap' ? `gap-${i}` : item}>\n {item === 'gap' ? (\n <span className=\"slurp-pages-gap\" aria-hidden=\"true\">\n …\n </span>\n ) : (\n <Fragment>\n {part('item', Tag, {\n ...control(item, disabled),\n className: 'slurp-pages-item',\n 'aria-label': labels.page(item),\n 'aria-current': item === page ? 'page' : undefined,\n children: item,\n })}\n </Fragment>\n )}\n </li>\n )),\n })}\n {part('status', 'span', {\n className: 'slurp-pages-status',\n children: labels.status(page, count),\n })}\n {part('next', Tag, {\n ...control(page + 1, disabled || page >= count),\n className: 'slurp-pages-step',\n 'aria-label': labels.next,\n children: <ChevronRightIcon size={20} />,\n })}\n </Root>\n </>\n );\n}\n"],"mappings":";;;;;;;;;AAoBA,MAAa,eAA4B;CACvC,OAAO;CACP,UAAU;CACV,MAAM;CACN,OAAO,MAAM,UAAU;CACvB,SAAS,GAAG,UAAU,GAAG,EAAE,MAAM;AACnC;AA2BA,MAAM,QAAQ,MAAc,OAC1B,MAAM,KAAK,EAAE,QAAQ,KAAK,IAAI,GAAG,KAAK,OAAO,CAAC,EAAE,IAAI,GAAG,MAAM,OAAO,CAAC;;;;;AAMvE,SAAgB,UAAU,MAAc,OAAe,WAAW,GAAG,aAAa,GAAe;CAE/F,IA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G,YAAY,QAAQ,KAAK;KAC9C,WAAW;KACX,cAAc,OAAO;KACrB,UAAU,oBAAC,kBAAD,EAAkB,MAAM,GAAK,CAAA;IACzC,CAAC;GACG;;CACN,EAAA,CAAA;AAEN"}
@@ -0,0 +1,3 @@
1
+
2
+ import { PAGES_LABELS, PageItem, Pages, PagesLabels, PagesProps, pageItems } from "./Pages.js";
3
+ export { PAGES_LABELS, type PageItem, Pages, type PagesLabels, type PagesProps, Pages as default, pageItems };
@@ -0,0 +1,2 @@
1
+
2
+ export { PAGES_LABELS, Pages, Pages as default, pageItems } from "./Pages.js";
@@ -0,0 +1,36 @@
1
+
2
+ import { PolymorphicProps } from "../lib/polymorphic.js";
3
+ import { ControlSize } from "../Button/Button.js";
4
+ import { ElementType } from "react";
5
+ export interface RatingLabels {
6
+ /** The value as words ("4.5 su 5"): the name of a read-only rating, after its label. */
7
+ value: (value: string, max: number) => string;
8
+ /** Name of one star of the input ("3 stelle"). */
9
+ star: (stars: number) => string;
10
+ }
11
+ export declare const RATING_LABELS: RatingLabels;
12
+ export interface RatingOwnProps {
13
+ /** What is rated ("Il tuo voto", "Valutazione media"): the legend of the input, the name of a read-only rating. */
14
+ label: string;
15
+ /** Stars chosen; null for none yet. A read-only rating may be fractional (4.5). */
16
+ value?: number | null;
17
+ defaultValue?: number | null;
18
+ onValueChange?: (value: number) => void;
19
+ /** Number of stars. Default 5. */
20
+ max?: number;
21
+ /** Shows a value instead of asking for one: one image named "Valutazione media: 4.5 su 5". */
22
+ readOnly?: boolean;
23
+ /** Form field name (radio inputs). */
24
+ name?: string;
25
+ disabled?: boolean;
26
+ /** Star size: `sm` 20, `md` 28 (default), `lg` 36. Each star of the input stays a 44px target. */
27
+ size?: ControlSize;
28
+ labels?: Partial<RatingLabels>;
29
+ }
30
+ export type RatingProps<E extends ElementType = 'fieldset'> = PolymorphicProps<E, RatingOwnProps>;
31
+ /**
32
+ * Stars. As an input it is a group of native radios (arrow keys move and choose, the legend names it): chosen stars
33
+ * are filled, the others outlined (shape, not only colour). Read-only it is one image with the value in words.
34
+ */
35
+ export declare function Rating<E extends ElementType = 'fieldset'>(props: RatingProps<E>): import("react").JSX.Element;
36
+ //# sourceMappingURL=Rating.d.ts.map
@@ -0,0 +1,118 @@
1
+ "use client";
2
+ import { cx } from "../utils/src/index.js";
3
+ import { useAnimationRoot } from "../internal/animation.js";
4
+ import { useCss, useThemeProps } from "../internal/styled.js";
5
+ import { useControlled } from "../utils/useControlled.js";
6
+ import { useId } from "react";
7
+ import { Fragment as Fragment$1, jsx, jsxs } from "react/jsx-runtime";
8
+ import { StarIcon } from "@slurp-ui/icons";
9
+ // src/Rating/Rating.tsx
10
+ const RATING_LABELS = {
11
+ value: (v, max) => `${v} su ${max}`,
12
+ star: (n) => n === 1 ? "1 stella" : `${n} stelle`
13
+ };
14
+ const ICON = {
15
+ sm: 20,
16
+ md: 28,
17
+ lg: 36
18
+ };
19
+ /** One decimal, like scores; whole numbers stay whole ("4", "4.5"). */
20
+ const shown = (v) => Number.isInteger(v) ? String(v) : v.toFixed(1);
21
+ /**
22
+ * Stars. As an input it is a group of native radios (arrow keys move and choose, the legend names it): chosen stars
23
+ * are filled, the others outlined (shape, not only colour). Read-only it is one image with the value in words.
24
+ */
25
+ function Rating(props) {
26
+ const { as, css, className, label, value: valueProp, defaultValue = null, onValueChange, max: maxProp = 5, readOnly = false, name: nameProp, disabled = false, size = "md", labels: labelsProp, style, ...rest } = useThemeProps("Rating", props);
27
+ const labels = {
28
+ ...RATING_LABELS,
29
+ ...labelsProp
30
+ };
31
+ const max = Math.max(1, Math.round(maxProp));
32
+ const [value, setValue] = useControlled({
33
+ value: valueProp,
34
+ defaultValue,
35
+ onChange: onValueChange
36
+ });
37
+ const auto = useId();
38
+ const name = nameProp ?? auto;
39
+ const styled = useCss("Rating", "root", css);
40
+ const root = useAnimationRoot(rest);
41
+ const stars = Array.from({ length: max }, (_, i) => i + 1);
42
+ const icon = ICON[size];
43
+ const clamped = Math.min(max, Math.max(0, value ?? 0));
44
+ if (readOnly) {
45
+ const Root = as ?? "span";
46
+ return /* @__PURE__ */ jsxs(Fragment$1, { children: [styled.style, /* @__PURE__ */ jsx(Root, {
47
+ className: cx("slurp-rating", styled.className, className),
48
+ role: "img",
49
+ "aria-label": `${label}: ${labels.value(shown(clamped), max)}`,
50
+ "data-readonly": "",
51
+ "data-size": size,
52
+ style: {
53
+ "--slurp-rating-ratio": clamped / max,
54
+ ...style
55
+ },
56
+ ...root,
57
+ children: /* @__PURE__ */ jsxs("span", {
58
+ className: "slurp-rating-track",
59
+ "aria-hidden": "true",
60
+ children: [stars.map((n) => /* @__PURE__ */ jsx(StarIcon, {
61
+ size: icon,
62
+ className: "slurp-rating-icon"
63
+ }, n)), /* @__PURE__ */ jsx("span", {
64
+ className: "slurp-rating-fill",
65
+ children: stars.map((n) => /* @__PURE__ */ jsx(StarIcon, {
66
+ size: icon,
67
+ className: "slurp-rating-icon",
68
+ fill: "currentColor"
69
+ }, n))
70
+ })]
71
+ })
72
+ })] });
73
+ }
74
+ const Root = as ?? "fieldset";
75
+ const fieldset = Root === "fieldset";
76
+ const Legend = fieldset ? "legend" : "span";
77
+ return /* @__PURE__ */ jsxs(Fragment$1, { children: [styled.style, /* @__PURE__ */ jsxs(Root, {
78
+ className: cx("slurp-rating", styled.className, className),
79
+ "data-size": size,
80
+ "data-disabled": disabled ? "" : void 0,
81
+ disabled: fieldset ? disabled : void 0,
82
+ role: fieldset ? void 0 : "radiogroup",
83
+ "aria-labelledby": fieldset ? void 0 : `${auto}-label`,
84
+ style,
85
+ ...root,
86
+ children: [/* @__PURE__ */ jsx(Legend, {
87
+ id: `${auto}-label`,
88
+ className: "slurp-rating-label",
89
+ children: label
90
+ }), /* @__PURE__ */ jsx("span", {
91
+ className: "slurp-rating-stars",
92
+ children: stars.map((n) => {
93
+ const on = n <= clamped;
94
+ return /* @__PURE__ */ jsxs("label", {
95
+ className: "slurp-rating-star",
96
+ "data-on": on ? "" : void 0,
97
+ children: [/* @__PURE__ */ jsx("input", {
98
+ type: "radio",
99
+ className: "slurp-rating-input",
100
+ name,
101
+ value: n,
102
+ checked: value === n,
103
+ disabled,
104
+ "aria-label": labels.star(n),
105
+ onChange: () => setValue(n)
106
+ }), /* @__PURE__ */ jsx(StarIcon, {
107
+ size: icon,
108
+ className: "slurp-rating-icon",
109
+ fill: on ? "currentColor" : "none"
110
+ })]
111
+ }, n);
112
+ })
113
+ })]
114
+ })] });
115
+ }
116
+ export { RATING_LABELS, Rating };
117
+
118
+ //# sourceMappingURL=Rating.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Rating.js","names":[],"sources":["../../src/Rating/Rating.tsx"],"sourcesContent":["import { StarIcon } from '@slurp-ui/icons';\nimport { cx } from '@slurp-ui/utils';\nimport { useId, type CSSProperties, type ElementType } from 'react';\n\nimport type { ControlSize } from '../Button/Button';\nimport { useAnimationRoot } from '../internal/animation';\nimport { useCss, useThemeProps } from '../internal/styled';\nimport type { PolymorphicProps } from '../lib/polymorphic';\nimport { useControlled } from '../utils/useControlled';\n\nexport interface RatingLabels {\n /** The value as words (\"4.5 su 5\"): the name of a read-only rating, after its label. */\n value: (value: string, max: number) => string;\n /** Name of one star of the input (\"3 stelle\"). */\n star: (stars: number) => string;\n}\n\nexport const RATING_LABELS: RatingLabels = {\n value: (v, max) => `${v} su ${max}`,\n star: (n) => (n === 1 ? '1 stella' : `${n} stelle`),\n};\n\nexport interface RatingOwnProps {\n /** What is rated (\"Il tuo voto\", \"Valutazione media\"): the legend of the input, the name of a read-only rating. */\n label: string;\n /** Stars chosen; null for none yet. A read-only rating may be fractional (4.5). */\n value?: number | null;\n defaultValue?: number | null;\n onValueChange?: (value: number) => void;\n /** Number of stars. Default 5. */\n max?: number;\n /** Shows a value instead of asking for one: one image named \"Valutazione media: 4.5 su 5\". */\n readOnly?: boolean;\n /** Form field name (radio inputs). */\n name?: string;\n disabled?: boolean;\n /** Star size: `sm` 20, `md` 28 (default), `lg` 36. Each star of the input stays a 44px target. */\n size?: ControlSize;\n labels?: Partial<RatingLabels>;\n}\n\nexport type RatingProps<E extends ElementType = 'fieldset'> = PolymorphicProps<E, RatingOwnProps>;\n\nconst ICON: Record<ControlSize, number> = { sm: 20, md: 28, lg: 36 };\n\n/** One decimal, like scores; whole numbers stay whole (\"4\", \"4.5\"). */\nconst shown = (v: number) => (Number.isInteger(v) ? String(v) : v.toFixed(1));\n\n/**\n * Stars. As an input it is a group of native radios (arrow keys move and choose, the legend names it): chosen stars\n * are filled, the others outlined (shape, not only colour). Read-only it is one image with the value in words.\n */\nexport function Rating<E extends ElementType = 'fieldset'>(props: RatingProps<E>) {\n const {\n as,\n css,\n className,\n label,\n value: valueProp,\n defaultValue = null,\n onValueChange,\n max: maxProp = 5,\n readOnly = false,\n name: nameProp,\n disabled = false,\n size = 'md',\n labels: labelsProp,\n style,\n ...rest\n } = useThemeProps('Rating', props as RatingProps);\n const labels = { ...RATING_LABELS, ...labelsProp };\n const max = Math.max(1, Math.round(maxProp));\n const [value, setValue] = useControlled<number | null>({\n value: valueProp,\n defaultValue,\n onChange: onValueChange as ((v: number | null) => void) | undefined,\n });\n const auto = useId();\n const name = nameProp ?? auto;\n const styled = useCss('Rating', 'root', css);\n const root = useAnimationRoot(rest);\n const stars = Array.from({ length: max }, (_, i) => i + 1);\n const icon = ICON[size];\n const clamped = Math.min(max, Math.max(0, value ?? 0));\n\n if (readOnly) {\n const Root = (as ?? 'span') as ElementType;\n return (\n <>\n {styled.style}\n <Root\n className={cx('slurp-rating', styled.className, className)}\n role=\"img\"\n aria-label={`${label}: ${labels.value(shown(clamped), max)}`}\n data-readonly=\"\"\n data-size={size}\n style={{ '--slurp-rating-ratio': clamped / max, ...style } as CSSProperties}\n {...root}\n >\n <span className=\"slurp-rating-track\" aria-hidden=\"true\">\n {stars.map((n) => (\n <StarIcon key={n} size={icon} className=\"slurp-rating-icon\" />\n ))}\n <span className=\"slurp-rating-fill\">\n {stars.map((n) => (\n <StarIcon key={n} size={icon} className=\"slurp-rating-icon\" fill=\"currentColor\" />\n ))}\n </span>\n </span>\n </Root>\n </>\n );\n }\n\n const Root = (as ?? 'fieldset') as ElementType;\n const fieldset = Root === 'fieldset';\n const Legend = fieldset ? 'legend' : 'span';\n return (\n <>\n {styled.style}\n <Root\n className={cx('slurp-rating', styled.className, className)}\n data-size={size}\n data-disabled={disabled ? '' : undefined}\n disabled={fieldset ? disabled : undefined}\n role={fieldset ? undefined : 'radiogroup'}\n aria-labelledby={fieldset ? undefined : `${auto}-label`}\n style={style}\n {...root}\n >\n <Legend id={`${auto}-label`} className=\"slurp-rating-label\">\n {label}\n </Legend>\n <span className=\"slurp-rating-stars\">\n {stars.map((n) => {\n const on = n <= clamped;\n return (\n <label key={n} className=\"slurp-rating-star\" data-on={on ? '' : undefined}>\n <input\n type=\"radio\"\n className=\"slurp-rating-input\"\n name={name}\n value={n}\n checked={value === n}\n disabled={disabled}\n aria-label={labels.star(n)}\n onChange={() => setValue(n)}\n />\n <StarIcon\n size={icon}\n className=\"slurp-rating-icon\"\n fill={on ? 'currentColor' : 'none'}\n />\n </label>\n );\n })}\n </span>\n </Root>\n </>\n );\n}\n"],"mappings":";;;;;;;;;AAiBA,MAAa,gBAA8B;CACzC,QAAQ,GAAG,QAAQ,GAAG,EAAE,MAAM;CAC9B,OAAO,MAAO,MAAM,IAAI,aAAa,GAAG,EAAE;AAC5C;AAuBA,MAAM,OAAoC;CAAE,IAAI;CAAI,IAAI;CAAI,IAAI;AAAG;;AAGnE,MAAM,SAAS,MAAe,OAAO,UAAU,CAAC,IAAI,OAAO,CAAC,IAAI,EAAE,QAAQ,CAAC;;;;;AAM3E,SAAgB,OAA2C,OAAuB;CAChF,MAAM,EACJ,IACA,KACA,WACA,OACA,OAAO,WACP,eAAe,MACf,eACA,KAAK,UAAU,GACf,WAAW,OACX,MAAM,UACN,WAAW,OACX,OAAO,MACP,QAAQ,YACR,OACA,GAAG,SACD,cAAc,UAAU,KAAoB;CAChD,MAAM,SAAS;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AA,UAAA,CACG,OAAO,OACR,qBAAC,MAAD;EACE,WAAW,GAAG,gBAAgB,OAAO,WAAW,SAAS;EACzD,aAAW;EACX,iBAAe,WAAW,KAAK,KAAA;EAC/B,UAAU,WAAW,WAAW,KAAA;EAChC,MAAM,WAAW,KAAA,IAAY;EAC7B,mBAAiB,WAAW,KAAA,IAAY,GAAG,KAAK;EACzC;EACP,GAAI;EARN,UAAA,CAUE,oBAAC,QAAD;GAAQ,IAAI,GAAG,KAAK;GAAS,WAAU;GACpC,UAAA;EACK,CAAA,GACR,oBAAC,QAAD;GAAM,WAAU;GACb,UAAA,MAAM,KAAK,MAAM;IAChB,MAAM,KAAK,KAAK;IAChB,OACE,qBAAC,SAAD;KAAe,WAAU;KAAoB,WAAS,KAAK,KAAK,KAAA;KAAhE,UAAA,CACE,oBAAC,SAAD;MACE,MAAK;MACL,WAAU;MACJ;MACN,OAAO;MACP,SAAS,UAAU;MACT;MACV,cAAY,OAAO,KAAK,CAAC;MACzB,gBAAgB,SAAS,CAAC;KAC3B,CAAA,GACD,oBAAC,UAAD;MACE,MAAM;MACN,WAAU;MACV,MAAM,KAAK,iBAAiB;KAC7B,CAAA,CACI;IAhBK,GAAA,CAgBL;GAEX,CAAC;EACG,CAAA,CACF;CACN,CAAA,CAAA,EAAA,CAAA;AAEN"}
@@ -0,0 +1,3 @@
1
+
2
+ import { RATING_LABELS, Rating, RatingLabels, RatingProps } from "./Rating.js";
3
+ export { RATING_LABELS, Rating, type RatingLabels, type RatingProps, Rating as default };