rockin 0.0.42 → 0.0.43

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.
@@ -35,11 +35,21 @@ function Carousel({
35
35
  );
36
36
  const [canScrollPrev, setCanScrollPrev] = React.useState(false);
37
37
  const [canScrollNext, setCanScrollNext] = React.useState(false);
38
+ const [selectedIndex, setSelectedIndex] = React.useState(0);
39
+ const [snapCount, setSnapCount] = React.useState(0);
38
40
  const onSelect = React.useCallback((api2) => {
39
41
  if (!api2) return;
40
42
  setCanScrollPrev(api2.canScrollPrev());
41
43
  setCanScrollNext(api2.canScrollNext());
44
+ setSelectedIndex(api2.selectedScrollSnap());
45
+ setSnapCount(api2.scrollSnapList().length);
42
46
  }, []);
47
+ const scrollTo = React.useCallback(
48
+ (index) => {
49
+ api?.scrollTo(index);
50
+ },
51
+ [api]
52
+ );
43
53
  const scrollPrev = React.useCallback(() => {
44
54
  api?.scrollPrev();
45
55
  }, [api]);
@@ -83,7 +93,10 @@ function Carousel({
83
93
  scrollPrev,
84
94
  scrollNext,
85
95
  canScrollPrev,
86
- canScrollNext
96
+ canScrollNext,
97
+ selectedIndex,
98
+ snapCount,
99
+ scrollTo
87
100
  },
88
101
  children: /* @__PURE__ */ jsx(
89
102
  "div",
@@ -195,9 +208,66 @@ function CarouselNext({
195
208
  }
196
209
  );
197
210
  }
211
+ function CarouselDots({
212
+ className,
213
+ getDotLabel = (index, count) => `Slide ${index + 1} of ${count}`,
214
+ "aria-label": ariaLabel = "Choose slide",
215
+ ...props
216
+ }) {
217
+ const { orientation, selectedIndex, snapCount, scrollTo } = useCarousel();
218
+ if (snapCount < 2) return null;
219
+ const vertical = orientation === "vertical";
220
+ return /* @__PURE__ */ jsx(
221
+ "div",
222
+ {
223
+ role: "group",
224
+ "aria-label": ariaLabel,
225
+ "data-slot": "carousel-dots",
226
+ className: cn(
227
+ "flex items-center justify-center",
228
+ vertical && "flex-col",
229
+ className
230
+ ),
231
+ ...props,
232
+ children: Array.from({ length: snapCount }, (_, index) => {
233
+ const active = index === selectedIndex;
234
+ return /* @__PURE__ */ jsx(
235
+ "button",
236
+ {
237
+ type: "button",
238
+ "data-slot": "carousel-dot",
239
+ "data-active": active ? "" : void 0,
240
+ "aria-label": getDotLabel(index, snapCount),
241
+ "aria-current": active ? "true" : void 0,
242
+ onClick: () => scrollTo(index),
243
+ className: cn(
244
+ "group/dot relative flex touch-manipulation items-center justify-center rounded-full outline-none",
245
+ "after:absolute after:content-['']",
246
+ vertical ? "min-h-6 w-6 py-1 after:-inset-x-2.5 after:inset-y-0" : "h-6 min-w-6 px-1 after:inset-x-0 after:-inset-y-2.5"
247
+ ),
248
+ children: /* @__PURE__ */ jsx(
249
+ "span",
250
+ {
251
+ "aria-hidden": "true",
252
+ className: cn(
253
+ "block size-1.5 rounded-full bg-muted-foreground/45 transition-[width,height,background-color] duration-200 ease-out motion-reduce:transition-none",
254
+ "group-hover/dot:bg-muted-foreground/70 group-focus-visible/dot:ring-2 group-focus-visible/dot:ring-ring/50 group-focus-visible/dot:ring-offset-1 group-focus-visible/dot:ring-offset-background",
255
+ "group-data-active/dot:bg-foreground",
256
+ vertical ? "group-data-active/dot:h-5" : "group-data-active/dot:w-5"
257
+ )
258
+ }
259
+ )
260
+ },
261
+ index
262
+ );
263
+ })
264
+ }
265
+ );
266
+ }
198
267
  export {
199
268
  Carousel,
200
269
  CarouselContent,
270
+ CarouselDots,
201
271
  CarouselItem,
202
272
  CarouselNext,
203
273
  CarouselPrevious,
package/dist/ui/image.js CHANGED
@@ -4,13 +4,79 @@ import {
4
4
  } from "../chunk-3CZ2LLUN.js";
5
5
 
6
6
  // src/ui/image.tsx
7
+ import { avatar } from "@ui-sdk/core/avatar";
8
+ import {
9
+ createParts
10
+ } from "@ui-sdk/react/primitive";
7
11
  import { cn } from "cn";
8
- import NextImage from "next/image";
9
12
  import {
13
+ createContext,
10
14
  useCallback,
15
+ useContext,
16
+ useEffect,
17
+ useMemo,
11
18
  useState
12
19
  } from "react";
20
+ import { preload as preloadResource } from "react-dom";
13
21
  import { jsx, jsxs } from "react/jsx-runtime";
22
+ import { createElement } from "react";
23
+ var {
24
+ Root: ImageRoot,
25
+ Image: ImageSource,
26
+ Fallback: ImageFallback
27
+ } = createParts(avatar, { root: "span", image: "img", fallback: "span" });
28
+ var DEFAULT_WIDTHS = [
29
+ 64,
30
+ 128,
31
+ 256,
32
+ 384,
33
+ 640,
34
+ 750,
35
+ 828,
36
+ 1080,
37
+ 1200,
38
+ 1920,
39
+ 2048,
40
+ 3840
41
+ ];
42
+ function readNextImageConfig() {
43
+ let options;
44
+ try {
45
+ options = process.env.__NEXT_IMAGE_OPTS;
46
+ } catch {
47
+ return {};
48
+ }
49
+ if (!options || typeof options !== "object" || options.unoptimized) return {};
50
+ if (options.loader && options.loader !== "default") return {};
51
+ const path = options.path || "/_next/image";
52
+ const widths = [...options.imageSizes ?? [], ...options.deviceSizes ?? []];
53
+ return {
54
+ loader: ({ src, width, quality }) => `${path}?url=${encodeURIComponent(src)}&w=${width}&q=${quality ?? 75}`,
55
+ widths: widths.length ? widths : void 0,
56
+ qualities: options.qualities
57
+ };
58
+ }
59
+ var ImageConfigContext = createContext(readNextImageConfig());
60
+ function ImageProvider({
61
+ loader,
62
+ widths,
63
+ quality,
64
+ qualities,
65
+ children
66
+ }) {
67
+ const value = useMemo(
68
+ () => ({ loader, widths, quality, qualities }),
69
+ [loader, widths, quality, qualities]
70
+ );
71
+ return /* @__PURE__ */ jsx(ImageConfigContext.Provider, { value, children });
72
+ }
73
+ function snapQuality(quality, allowed) {
74
+ if (!allowed?.length) return quality;
75
+ const target = quality ?? 75;
76
+ return allowed.reduce(
77
+ (best, q) => Math.abs(q - target) < Math.abs(best - target) ? q : best
78
+ );
79
+ }
14
80
  function textHue(text) {
15
81
  let hash = 2166136261;
16
82
  for (const char of text) {
@@ -59,88 +125,242 @@ function BrokenThumbnail({ className }) {
59
125
  }
60
126
  );
61
127
  }
62
- function ImagePlaceholder({ label }) {
128
+ function EmptyThumbnail({ className }) {
129
+ return /* @__PURE__ */ jsxs(
130
+ "svg",
131
+ {
132
+ viewBox: "0 0 48 48",
133
+ "aria-hidden": "true",
134
+ fill: "none",
135
+ stroke: "currentColor",
136
+ strokeWidth: "2",
137
+ strokeLinecap: "round",
138
+ strokeLinejoin: "round",
139
+ className,
140
+ children: [
141
+ /* @__PURE__ */ jsx(
142
+ "rect",
143
+ {
144
+ x: "6",
145
+ y: "9",
146
+ width: "36",
147
+ height: "30",
148
+ rx: "4",
149
+ fill: "currentColor",
150
+ fillOpacity: "0.1"
151
+ }
152
+ ),
153
+ /* @__PURE__ */ jsx("circle", { cx: "33", cy: "18", r: "3" }),
154
+ /* @__PURE__ */ jsx("path", { d: "M6 33l10-10 9 9 5-5 12 10" })
155
+ ]
156
+ }
157
+ );
158
+ }
159
+ function ImagePlaceholder({
160
+ label,
161
+ reason = "empty",
162
+ className
163
+ }) {
63
164
  const text = (label || "").trim();
165
+ const status = reason === "error" ? "image failed to load" : "no image";
166
+ const Glyph = reason === "error" ? BrokenThumbnail : EmptyThumbnail;
64
167
  return /* @__PURE__ */ jsx(
65
- "div",
168
+ "span",
66
169
  {
67
170
  role: "img",
68
- "aria-label": label ? `${label} (no image)` : "No image",
171
+ "aria-label": text ? `${text} (${status})` : status[0].toUpperCase() + status.slice(1),
172
+ "data-slot": "image-placeholder",
173
+ "data-reason": reason,
69
174
  style: { "--ph-hue": text ? textHue(text) : 250 },
70
- className: "flex size-full items-center justify-center bg-linear-135 from-(--ph-from) to-(--ph-to) text-(--ph-fg) [--ph-fg:oklch(0.55_0.12_var(--ph-hue))] [--ph-from:oklch(0.92_0.06_var(--ph-hue))] [--ph-to:oklch(0.97_0.025_calc(var(--ph-hue)+40))] dark:[--ph-fg:oklch(0.82_0.1_var(--ph-hue))] dark:[--ph-from:oklch(0.36_0.07_var(--ph-hue))] dark:[--ph-to:oklch(0.26_0.04_calc(var(--ph-hue)+40))]",
71
- children: /* @__PURE__ */ jsx(BrokenThumbnail, { className: "aspect-square h-auto w-[34%] max-w-20 min-w-4" })
175
+ className: cn(
176
+ "flex size-full items-center justify-center bg-linear-135 from-(--ph-from) to-(--ph-to) text-(--ph-fg) [--ph-fg:oklch(0.55_0.12_var(--ph-hue))] [--ph-from:oklch(0.92_0.06_var(--ph-hue))] [--ph-to:oklch(0.97_0.025_calc(var(--ph-hue)+40))] dark:[--ph-fg:oklch(0.82_0.1_var(--ph-hue))] dark:[--ph-from:oklch(0.36_0.07_var(--ph-hue))] dark:[--ph-to:oklch(0.26_0.04_calc(var(--ph-hue)+40))]",
177
+ className
178
+ ),
179
+ children: /* @__PURE__ */ jsx(Glyph, { className: "aspect-square h-auto w-[34%] max-w-20 min-w-4" })
72
180
  }
73
181
  );
74
182
  }
75
- function srcValue(src) {
183
+ function resolveSrc(src) {
76
184
  if (!src) return null;
77
- if (typeof src === "string") return src;
78
- if ("src" in src) return src.src;
79
- return src.default.src;
185
+ if (typeof src === "string") return { src };
186
+ if ("default" in src) return src.default;
187
+ return src.src ? src : null;
188
+ }
189
+ function toNumber(value) {
190
+ const number = Number(value);
191
+ return Number.isFinite(number) && number > 0 ? number : void 0;
192
+ }
193
+ function buildSources(url, options) {
194
+ const { loader, quality, width, sizes, srcSet, fill } = options;
195
+ if (!loader || srcSet || /^(data|blob):|\.svg($|[?#])/i.test(url)) {
196
+ return { src: url, srcSet, sizes };
197
+ }
198
+ const widths = [...options.widths].sort((a, b) => a - b);
199
+ const largest = widths[widths.length - 1];
200
+ const at = (w) => loader({ src: url, width: w, quality });
201
+ if (sizes || fill || !width) {
202
+ return {
203
+ src: at(largest),
204
+ srcSet: widths.map((w) => `${at(w)} ${w}w`).join(", "),
205
+ sizes: sizes ?? "100vw"
206
+ };
207
+ }
208
+ const pick = (target) => widths.find((w) => w >= target) ?? largest;
209
+ const density = [.../* @__PURE__ */ new Set([pick(width), pick(width * 2)])];
210
+ return {
211
+ src: at(density[density.length - 1]),
212
+ srcSet: density.map((w, i) => `${at(w)} ${i + 1}x`).join(", ")
213
+ };
80
214
  }
81
215
  function Image({
216
+ src,
217
+ alt = "",
218
+ className,
219
+ style,
82
220
  wrapper,
221
+ fill = false,
83
222
  showSkeleton = true,
84
- className,
85
- loading = "lazy",
86
- fetchPriority,
87
- preload,
88
- quality = 90,
89
- src,
90
- alt,
91
- onLoad,
92
- onError,
223
+ preload: preloadProp,
224
+ priority = false,
225
+ quality: qualityProp,
226
+ unoptimized = false,
227
+ loader: loaderProp,
228
+ retry = 1,
229
+ retryDelay = 1500,
230
+ fallback,
93
231
  children,
94
- fallback = /* @__PURE__ */ jsx(ImagePlaceholder, { label: alt }),
232
+ loading,
233
+ fetchPriority,
234
+ decoding = "async",
235
+ width,
236
+ height,
237
+ srcSet,
238
+ sizes,
239
+ onStateChange,
95
240
  ...props
96
241
  }) {
97
- const currentSrc = srcValue(src);
98
- const [loadedSrc, setLoadedSrc] = useState(null);
99
- const [failedSrc, setFailedSrc] = useState(null);
100
- const hasSource = Boolean(currentSrc);
101
- const failed = hasSource && failedSrc === currentSrc;
102
- const loaded = hasSource && loadedSrc === currentSrc && !failed;
103
- const shouldPreload = preload ?? loading === "eager";
104
- const imageLoading = shouldPreload ? "eager" : loading;
105
- const markLoaded = useCallback(() => {
106
- if (!currentSrc) return;
107
- setFailedSrc(null);
108
- setLoadedSrc(currentSrc);
109
- }, [currentSrc]);
110
- const markFailed = useCallback(() => {
111
- if (!currentSrc) return;
112
- setFailedSrc(currentSrc);
113
- }, [currentSrc]);
114
- return /* @__PURE__ */ jsxs("section", { className: cn("relative block overflow-hidden", wrapper), children: [
115
- showSkeleton && !loaded && !failed && hasSource && /* @__PURE__ */ jsx(Skeleton, { className: "absolute inset-0 size-full" }),
116
- hasSource && !failed ? /* @__PURE__ */ jsx(
117
- NextImage,
118
- {
119
- ...props,
120
- src: src ?? "",
121
- alt,
122
- quality,
123
- loading: imageLoading,
124
- preload: shouldPreload,
125
- fetchPriority: fetchPriority ?? (imageLoading === "eager" ? "high" : void 0),
126
- className: cn(
127
- "transition-opacity duration-200",
128
- loaded ? "opacity-100" : "opacity-0",
129
- className
242
+ const config = useContext(ImageConfigContext);
243
+ const image = resolveSrc(src);
244
+ const url = image?.src ?? null;
245
+ const preload = preloadProp ?? priority;
246
+ const w = fill ? void 0 : toNumber(width) ?? image?.width;
247
+ const h = fill ? void 0 : toNumber(height) ?? image?.height;
248
+ const sources = url ? buildSources(url, {
249
+ loader: unoptimized ? void 0 : loaderProp ?? config.loader,
250
+ widths: config.widths?.length ? config.widths : DEFAULT_WIDTHS,
251
+ quality: snapQuality(qualityProp ?? config.quality, config.qualities),
252
+ width: w,
253
+ sizes,
254
+ srcSet,
255
+ fill
256
+ }) : null;
257
+ const [status, setStatus] = useState("idle");
258
+ const [attempt, setAttempt] = useState({
259
+ src: url,
260
+ count: 0,
261
+ retrying: false
262
+ });
263
+ const current = attempt.src === url ? attempt : { src: url, count: 0, retrying: false };
264
+ const state = !url ? "empty" : current.retrying || status === "idle" || status === "loading" ? "loading" : status;
265
+ if (preload && sources) {
266
+ preloadResource(sources.src, {
267
+ as: "image",
268
+ fetchPriority: "high",
269
+ imageSrcSet: sources.srcSet,
270
+ imageSizes: sources.sizes
271
+ });
272
+ }
273
+ useEffect(() => {
274
+ onStateChange?.(state);
275
+ }, [state, onStateChange]);
276
+ const reload = useCallback(() => {
277
+ setAttempt(
278
+ (prev) => prev.src === url ? { src: url, count: prev.count + 1, retrying: false } : prev
279
+ );
280
+ }, [url]);
281
+ const handleStatus = useCallback(
282
+ (next) => {
283
+ setStatus(next);
284
+ if (next !== "error") return;
285
+ setAttempt((prev) => {
286
+ const count = prev.src === url ? prev.count : 0;
287
+ return { src: url, count, retrying: count < retry };
288
+ });
289
+ },
290
+ [url, retry]
291
+ );
292
+ useEffect(() => {
293
+ if (!current.retrying) return;
294
+ const timer = window.setTimeout(reload, retryDelay);
295
+ return () => window.clearTimeout(timer);
296
+ }, [current.retrying, reload, retryDelay]);
297
+ useEffect(() => {
298
+ if (state !== "error") return;
299
+ window.addEventListener("online", reload);
300
+ return () => window.removeEventListener("online", reload);
301
+ }, [state, reload]);
302
+ const eager = preload || loading === "eager";
303
+ const overlay = fill || Boolean(wrapper);
304
+ return /* @__PURE__ */ jsxs(
305
+ ImageRoot,
306
+ {
307
+ "data-state": state,
308
+ className: cn("relative block overflow-hidden", wrapper),
309
+ onLoadingStatusChange: handleStatus,
310
+ children: [
311
+ sources && /* @__PURE__ */ createElement(
312
+ ImageSource,
313
+ {
314
+ ...props,
315
+ key: `${sources.src}#${current.count}`,
316
+ src: sources.src,
317
+ srcSet: sources.srcSet,
318
+ sizes: sources.sizes,
319
+ alt,
320
+ width: w,
321
+ height: h,
322
+ loading: eager ? "eager" : loading ?? "lazy",
323
+ fetchPriority: fetchPriority ?? (eager ? "high" : void 0),
324
+ decoding,
325
+ style,
326
+ className: cn(
327
+ fill && "absolute inset-0 size-full",
328
+ "transition-opacity duration-200 motion-reduce:transition-none",
329
+ state === "loaded" ? "opacity-100" : "opacity-0",
330
+ className
331
+ )
332
+ }
333
+ ),
334
+ state !== "loaded" && /* @__PURE__ */ jsx(
335
+ ImageFallback,
336
+ {
337
+ style: !overlay && w && h ? {
338
+ "--image-w": `${w}px`,
339
+ aspectRatio: `${w} / ${h}`
340
+ } : void 0,
341
+ className: overlay ? "absolute inset-0 block size-full" : cn(
342
+ w && h ? "block w-(--image-w) max-w-full" : "block size-full",
343
+ className
344
+ ),
345
+ children: state === "loading" ? showSkeleton && /* @__PURE__ */ jsx(Skeleton, { rounded: "none", className: "size-full" }) : fallback ?? /* @__PURE__ */ jsx(
346
+ ImagePlaceholder,
347
+ {
348
+ label: alt,
349
+ reason: state === "error" ? "error" : "empty"
350
+ }
351
+ )
352
+ }
130
353
  ),
131
- onLoad: (event) => {
132
- markLoaded();
133
- onLoad?.(event);
134
- },
135
- onError: (event) => {
136
- markFailed();
137
- onError?.(event);
138
- }
139
- }
140
- ) : fallback,
141
- children
142
- ] });
354
+ children
355
+ ]
356
+ }
357
+ );
143
358
  }
144
359
  export {
145
- Image
360
+ Image,
361
+ ImageFallback,
362
+ ImagePlaceholder,
363
+ ImageProvider,
364
+ ImageRoot,
365
+ ImageSource
146
366
  };