@cerevialabs/ui 0.0.0-stage → 0.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/components/accordion/content/attrs.js +7 -0
- package/components/accordion/content/content.d.ts +2 -0
- package/components/accordion/content/content.js +18 -0
- package/components/accordion/content/index.d.ts +3 -0
- package/components/accordion/content/types.d.ts +3 -0
- package/components/accordion/context/context.d.ts +1 -0
- package/components/accordion/context/context.js +12 -0
- package/components/accordion/context/hooks.d.ts +3 -0
- package/components/accordion/context/hooks.js +49 -0
- package/components/accordion/context/index.d.ts +4 -0
- package/components/accordion/context/types.d.ts +14 -0
- package/components/accordion/context/utils.js +28 -0
- package/components/accordion/header/attrs.js +7 -0
- package/components/accordion/header/header.d.ts +2 -0
- package/components/accordion/header/header.js +17 -0
- package/components/accordion/header/index.d.ts +3 -0
- package/components/accordion/header/types.d.ts +3 -0
- package/components/accordion/index.d.ts +27 -0
- package/components/accordion/index.js +16 -0
- package/components/accordion/item/attrs.js +7 -0
- package/components/accordion/item/hooks.d.ts +3 -0
- package/components/accordion/item/hooks.js +22 -0
- package/components/accordion/item/index.d.ts +4 -0
- package/components/accordion/item/item.d.ts +2 -0
- package/components/accordion/item/item.js +23 -0
- package/components/accordion/item/types.d.ts +9 -0
- package/components/accordion/root/attrs.js +10 -0
- package/components/accordion/root/hooks.js +31 -0
- package/components/accordion/root/index.d.ts +3 -0
- package/components/accordion/root/root.d.ts +2 -0
- package/components/accordion/root/root.js +27 -0
- package/components/accordion/root/types.d.ts +19 -0
- package/components/accordion/trigger/attrs.js +7 -0
- package/components/accordion/trigger/index.d.ts +3 -0
- package/components/accordion/trigger/trigger.d.ts +2 -0
- package/components/accordion/trigger/trigger.js +10 -0
- package/components/accordion/trigger/types.d.ts +3 -0
- package/components/button/a11y.js +6 -0
- package/components/button/attrs.js +3 -0
- package/components/button/button.d.ts +2 -0
- package/components/button/button.js +25 -0
- package/components/button/index.d.ts +4 -0
- package/components/button/index.js +4 -0
- package/components/button/types.d.ts +5 -0
- package/components/collapsible/content/attrs.js +7 -0
- package/components/collapsible/content/content.d.ts +2 -0
- package/components/collapsible/content/content.js +18 -0
- package/components/collapsible/content/index.d.ts +3 -0
- package/components/collapsible/content/types.d.ts +3 -0
- package/components/collapsible/context/hooks.d.ts +2 -0
- package/components/collapsible/context/hooks.js +7 -0
- package/components/collapsible/context/index.d.ts +3 -0
- package/components/collapsible/context/types.d.ts +3 -0
- package/components/collapsible/index.d.ts +18 -0
- package/components/collapsible/index.js +11 -0
- package/components/collapsible/root/attrs.js +7 -0
- package/components/collapsible/root/index.d.ts +3 -0
- package/components/collapsible/root/root.d.ts +2 -0
- package/components/collapsible/root/root.js +10 -0
- package/components/collapsible/root/types.d.ts +3 -0
- package/components/collapsible/trigger/attrs.js +7 -0
- package/components/collapsible/trigger/index.d.ts +3 -0
- package/components/collapsible/trigger/trigger.d.ts +2 -0
- package/components/collapsible/trigger/trigger.js +10 -0
- package/components/collapsible/trigger/types.d.ts +3 -0
- package/components/index.d.ts +26 -0
- package/components/index.js +9 -0
- package/components/internal/disclosure/attrs.js +8 -0
- package/components/internal/disclosure/content/a11y.js +5 -0
- package/components/internal/disclosure/content/attrs.js +6 -0
- package/components/internal/disclosure/content/content.d.ts +1 -0
- package/components/internal/disclosure/content/content.js +30 -0
- package/components/internal/disclosure/content/hooks.js +81 -0
- package/components/internal/disclosure/content/index.d.ts +3 -0
- package/components/internal/disclosure/content/types.d.ts +11 -0
- package/components/internal/disclosure/content/utils.js +49 -0
- package/components/internal/disclosure/context/context.d.ts +1 -0
- package/components/internal/disclosure/context/context.js +12 -0
- package/components/internal/disclosure/context/hooks.d.ts +1 -0
- package/components/internal/disclosure/context/hooks.js +24 -0
- package/components/internal/disclosure/context/index.d.ts +4 -0
- package/components/internal/disclosure/context/types.d.ts +8 -0
- package/components/internal/disclosure/index.d.ts +9 -0
- package/components/internal/disclosure/index.js +11 -0
- package/components/internal/disclosure/root/hooks.js +23 -0
- package/components/internal/disclosure/root/index.d.ts +3 -0
- package/components/internal/disclosure/root/root.d.ts +1 -0
- package/components/internal/disclosure/root/root.js +27 -0
- package/components/internal/disclosure/root/types.d.ts +8 -0
- package/components/internal/disclosure/trigger/a11y.js +8 -0
- package/components/internal/disclosure/trigger/events.js +9 -0
- package/components/internal/disclosure/trigger/hooks.js +27 -0
- package/components/internal/disclosure/trigger/index.d.ts +3 -0
- package/components/internal/disclosure/trigger/trigger.d.ts +1 -0
- package/components/internal/disclosure/trigger/trigger.js +24 -0
- package/components/internal/disclosure/trigger/types.d.ts +3 -0
- package/components/scroll-area/content/attrs.js +21 -0
- package/components/scroll-area/content/content.d.ts +2 -0
- package/components/scroll-area/content/content.js +29 -0
- package/components/scroll-area/content/hooks.js +55 -0
- package/components/scroll-area/content/index.d.ts +3 -0
- package/components/scroll-area/content/types.d.ts +3 -0
- package/components/scroll-area/content/utils.js +34 -0
- package/components/scroll-area/context/context.d.ts +1 -0
- package/components/scroll-area/context/context.js +12 -0
- package/components/scroll-area/context/hooks.d.ts +3 -0
- package/components/scroll-area/context/hooks.js +170 -0
- package/components/scroll-area/context/index.d.ts +4 -0
- package/components/scroll-area/context/types.d.ts +47 -0
- package/components/scroll-area/context/utils.js +72 -0
- package/components/scroll-area/corner/attrs.js +10 -0
- package/components/scroll-area/corner/corner.d.ts +2 -0
- package/components/scroll-area/corner/corner.js +26 -0
- package/components/scroll-area/corner/hooks.js +12 -0
- package/components/scroll-area/corner/index.d.ts +3 -0
- package/components/scroll-area/corner/types.d.ts +3 -0
- package/components/scroll-area/index.d.ts +26 -0
- package/components/scroll-area/index.js +15 -0
- package/components/scroll-area/root/attrs.js +21 -0
- package/components/scroll-area/root/hooks.js +45 -0
- package/components/scroll-area/root/index.d.ts +3 -0
- package/components/scroll-area/root/root.d.ts +2 -0
- package/components/scroll-area/root/root.js +33 -0
- package/components/scroll-area/root/types.d.ts +8 -0
- package/components/scroll-area/scrollbar/a11y.js +12 -0
- package/components/scroll-area/scrollbar/attrs.js +12 -0
- package/components/scroll-area/scrollbar/context.d.ts +1 -0
- package/components/scroll-area/scrollbar/context.js +10 -0
- package/components/scroll-area/scrollbar/hooks.js +77 -0
- package/components/scroll-area/scrollbar/index.d.ts +4 -0
- package/components/scroll-area/scrollbar/scrollbar.d.ts +2 -0
- package/components/scroll-area/scrollbar/scrollbar.js +38 -0
- package/components/scroll-area/scrollbar/types.d.ts +8 -0
- package/components/scroll-area/thumb/attrs.js +7 -0
- package/components/scroll-area/thumb/hooks.js +75 -0
- package/components/scroll-area/thumb/index.d.ts +3 -0
- package/components/scroll-area/thumb/thumb.d.ts +2 -0
- package/components/scroll-area/thumb/thumb.js +26 -0
- package/components/scroll-area/thumb/types.d.ts +3 -0
- package/package.json +34 -4
- package/utils/apply-a11y.d.ts +5 -0
- package/utils/apply-a11y.js +11 -0
- package/utils/apply-attrs.d.ts +6 -0
- package/utils/apply-attrs.js +8 -0
- package/utils/data-attrs.d.ts +8 -0
- package/utils/data-attrs.js +21 -0
- package/utils/index.d.ts +11 -0
- package/utils/index.js +11 -0
- package/utils/merge-props.d.ts +4 -0
- package/utils/merge-props.js +68 -0
- package/utils/merge-refs.d.ts +2 -0
- package/utils/merge-refs.js +69 -0
- package/utils/prefers-reduced-motion.d.ts +1 -0
- package/utils/prefers-reduced-motion.js +19 -0
- package/utils/to-kebab-case.d.ts +1 -0
- package/utils/to-kebab-case.js +5 -0
- package/utils/use-controllable-state.d.ts +9 -0
- package/utils/use-controllable-state.js +38 -0
- package/utils/use-presence.d.ts +27 -0
- package/utils/use-presence.js +234 -0
- package/utils/use-render.d.ts +20 -0
- package/utils/use-render.js +74 -0
- package/README.md +0 -3
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { applyAttrs } from "../../../utils/apply-attrs.js";
|
|
2
|
+
import { presenceAttrs } from "../../../utils/use-presence.js";
|
|
3
|
+
// components/scroll-area/scrollbar/attrs.ts
|
|
4
|
+
const scrollAreaScrollbarAttrStates = [];
|
|
5
|
+
function scrollAreaScrollbarAttrs({ orientation, presenceStates }) {
|
|
6
|
+
return {
|
|
7
|
+
...applyAttrs("scroll-area", scrollAreaScrollbarAttrStates, {}, { part: "scrollbar" }),
|
|
8
|
+
"data-orientation": orientation,
|
|
9
|
+
...presenceAttrs("scroll-area", presenceStates, void 0, { statesOnly: true })
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
export { scrollAreaScrollbarAttrs };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import "../context/index.js";
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { createContext, use } from "react";
|
|
3
|
+
// components/scroll-area/scrollbar/context.tsx
|
|
4
|
+
const ScrollAreaScrollbarContext = createContext(void 0);
|
|
5
|
+
function useScrollAreaScrollbarContext() {
|
|
6
|
+
const context = use(ScrollAreaScrollbarContext);
|
|
7
|
+
if (!context) throw new Error("ScrollArea.Thumb must be used within ScrollArea.Scrollbar");
|
|
8
|
+
return context;
|
|
9
|
+
}
|
|
10
|
+
export { ScrollAreaScrollbarContext, useScrollAreaScrollbarContext };
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { useEffect, useRef } from "react";
|
|
3
|
+
import { useScrollAreaContext } from "../context/hooks.js";
|
|
4
|
+
import { pageDirectionAt } from "../context/utils.js";
|
|
5
|
+
import { scrollAreaScrollbarA11y } from "./a11y.js";
|
|
6
|
+
import { scrollAreaScrollbarAttrs } from "./attrs.js";
|
|
7
|
+
import { presenceShouldRender, usePresence } from "../../../utils/use-presence.js";
|
|
8
|
+
import { useMergeRefs } from "../../../utils/merge-refs.js";
|
|
9
|
+
// components/scroll-area/scrollbar/hooks.ts
|
|
10
|
+
function useScrollAreaScrollbarState({ orientation, keepMounted = false, ref }) {
|
|
11
|
+
const ctx = useScrollAreaContext();
|
|
12
|
+
const { registerScrollbar, unregisterScrollbar } = ctx;
|
|
13
|
+
const localRef = useRef(null);
|
|
14
|
+
const visible = ctx.scrollbarVisible(orientation);
|
|
15
|
+
const { ref: presenceRef, mounted, presenceStates } = usePresence(visible);
|
|
16
|
+
const mergedRef = useMergeRefs([
|
|
17
|
+
ref,
|
|
18
|
+
localRef,
|
|
19
|
+
presenceRef
|
|
20
|
+
]);
|
|
21
|
+
const shouldRender = presenceShouldRender(visible, mounted, keepMounted);
|
|
22
|
+
const ratio = orientation === "vertical" ? ctx.scrollRatioY : ctx.scrollRatioX;
|
|
23
|
+
useEffect(() => {
|
|
24
|
+
registerScrollbar(orientation);
|
|
25
|
+
return () => unregisterScrollbar(orientation);
|
|
26
|
+
}, [
|
|
27
|
+
orientation,
|
|
28
|
+
registerScrollbar,
|
|
29
|
+
unregisterScrollbar
|
|
30
|
+
]);
|
|
31
|
+
function directionTowardPointer(clientX, clientY) {
|
|
32
|
+
const track = localRef.current;
|
|
33
|
+
if (!track) return null;
|
|
34
|
+
const rect = track.getBoundingClientRect();
|
|
35
|
+
const vertical = orientation === "vertical";
|
|
36
|
+
return pageDirectionAt(orientation, ctx.dir, vertical ? clientY - rect.top : clientX - rect.left, vertical ? rect.height : rect.width, vertical ? ctx.scrollRatioY : ctx.scrollRatioX, vertical ? ctx.thumbSizeY : ctx.thumbSizeX);
|
|
37
|
+
}
|
|
38
|
+
return {
|
|
39
|
+
ref: mergedRef,
|
|
40
|
+
visible,
|
|
41
|
+
shouldRender,
|
|
42
|
+
inert: visible ? void 0 : true,
|
|
43
|
+
attrs: scrollAreaScrollbarAttrs({
|
|
44
|
+
orientation,
|
|
45
|
+
presenceStates
|
|
46
|
+
}),
|
|
47
|
+
a11y: scrollAreaScrollbarA11y({
|
|
48
|
+
orientation,
|
|
49
|
+
contentId: ctx.contentId,
|
|
50
|
+
valueNow: ratio
|
|
51
|
+
}),
|
|
52
|
+
onPointerEnter(event) {
|
|
53
|
+
if (event.pointerType === "mouse" || event.pointerType === "pen") ctx.onScrollbarPointerEnter();
|
|
54
|
+
},
|
|
55
|
+
onPointerLeave() {
|
|
56
|
+
ctx.onScrollbarPointerLeave();
|
|
57
|
+
},
|
|
58
|
+
onPointerDown(event) {
|
|
59
|
+
if (event.button !== 0) return;
|
|
60
|
+
const track = localRef.current;
|
|
61
|
+
if (!track) return;
|
|
62
|
+
if (event.target.closest("[data-scroll-area-part=\"thumb\"]")) return;
|
|
63
|
+
const direction = directionTowardPointer(event.clientX, event.clientY);
|
|
64
|
+
if (direction === null) return;
|
|
65
|
+
ctx.pageScroll(orientation, direction);
|
|
66
|
+
track.setPointerCapture?.(event.pointerId);
|
|
67
|
+
ctx.setInteracting(true);
|
|
68
|
+
},
|
|
69
|
+
onPointerUp(event) {
|
|
70
|
+
ctx.setInteracting(false);
|
|
71
|
+
try {
|
|
72
|
+
localRef.current?.releasePointerCapture?.(event.pointerId);
|
|
73
|
+
} catch {}
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
export { useScrollAreaScrollbarState };
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { ScrollAreaScrollbarContext } from "./context.js";
|
|
3
|
+
import { useScrollAreaScrollbarState } from "./hooks.js";
|
|
4
|
+
import { mergeProps } from "../../../utils/merge-props.js";
|
|
5
|
+
import { useRender } from "../../../utils/use-render.js";
|
|
6
|
+
import { jsx } from "react/jsx-runtime";
|
|
7
|
+
// components/scroll-area/scrollbar/scrollbar.tsx
|
|
8
|
+
function ScrollAreaScrollbar({ ref, render, orientation, keepMounted = false, className, children, style, ...props }) {
|
|
9
|
+
const state = useScrollAreaScrollbarState({
|
|
10
|
+
orientation,
|
|
11
|
+
keepMounted,
|
|
12
|
+
ref
|
|
13
|
+
});
|
|
14
|
+
return /* @__PURE__ */ jsx(ScrollAreaScrollbarContext.Provider, {
|
|
15
|
+
value: { orientation },
|
|
16
|
+
children: useRender({
|
|
17
|
+
defaultTagName: "div",
|
|
18
|
+
ref: state.ref,
|
|
19
|
+
render,
|
|
20
|
+
enabled: state.shouldRender,
|
|
21
|
+
props: mergeProps({
|
|
22
|
+
className,
|
|
23
|
+
children,
|
|
24
|
+
style: {
|
|
25
|
+
position: "absolute",
|
|
26
|
+
...style
|
|
27
|
+
},
|
|
28
|
+
inert: state.inert,
|
|
29
|
+
onPointerEnter: state.onPointerEnter,
|
|
30
|
+
onPointerLeave: state.onPointerLeave,
|
|
31
|
+
onPointerDown: state.onPointerDown,
|
|
32
|
+
onPointerUp: state.onPointerUp,
|
|
33
|
+
onPointerCancel: state.onPointerUp
|
|
34
|
+
}, state.attrs, state.a11y, props)
|
|
35
|
+
})
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
export { ScrollAreaScrollbar };
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { UseRenderComponentProps } from "../../../utils/use-render.js";
|
|
2
|
+
import "../../../utils/index.js";
|
|
3
|
+
import { ScrollAreaOrientation } from "../context/types.js";
|
|
4
|
+
import "../context/index.js";
|
|
5
|
+
export interface ScrollAreaScrollbarProps extends UseRenderComponentProps<"div"> {
|
|
6
|
+
orientation: ScrollAreaOrientation;
|
|
7
|
+
keepMounted?: boolean;
|
|
8
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { applyAttrs } from "../../../utils/apply-attrs.js";
|
|
2
|
+
// components/scroll-area/thumb/attrs.ts
|
|
3
|
+
const scrollAreaThumbAttrStates = [];
|
|
4
|
+
function scrollAreaThumbAttrs() {
|
|
5
|
+
return applyAttrs("scroll-area", scrollAreaThumbAttrStates, {}, { part: "thumb" });
|
|
6
|
+
}
|
|
7
|
+
export { scrollAreaThumbAttrs };
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { useRef } from "react";
|
|
3
|
+
import { useScrollAreaContext } from "../context/hooks.js";
|
|
4
|
+
import { dragScrollRatio, thumbTranslatePercent } from "../context/utils.js";
|
|
5
|
+
import { useScrollAreaScrollbarContext } from "../scrollbar/context.js";
|
|
6
|
+
import { scrollAreaThumbAttrs } from "./attrs.js";
|
|
7
|
+
import { useMergeRefs } from "../../../utils/merge-refs.js";
|
|
8
|
+
// components/scroll-area/thumb/hooks.ts
|
|
9
|
+
const THUMB_HEIGHT_VAR = "--scroll-area-thumb-height";
|
|
10
|
+
const THUMB_WIDTH_VAR = "--scroll-area-thumb-width";
|
|
11
|
+
function useScrollAreaThumbState(ref) {
|
|
12
|
+
const ctx = useScrollAreaContext();
|
|
13
|
+
const { orientation } = useScrollAreaScrollbarContext();
|
|
14
|
+
const localRef = useRef(null);
|
|
15
|
+
const mergedRef = useMergeRefs([ref, localRef]);
|
|
16
|
+
const dragRef = useRef(null);
|
|
17
|
+
const thumbSize = orientation === "vertical" ? ctx.thumbSizeY : ctx.thumbSizeX;
|
|
18
|
+
const scrollRatio = orientation === "vertical" ? ctx.scrollRatioY : ctx.scrollRatioX;
|
|
19
|
+
const translatePercent = thumbTranslatePercent(orientation, scrollRatio, thumbSize, ctx.dir);
|
|
20
|
+
const sizePercent = `${thumbSize * 100}%`;
|
|
21
|
+
const style = orientation === "vertical" ? {
|
|
22
|
+
width: "100%",
|
|
23
|
+
height: `var(${THUMB_HEIGHT_VAR})`,
|
|
24
|
+
transform: `translate3d(0, ${translatePercent}%, 0)`,
|
|
25
|
+
[THUMB_HEIGHT_VAR]: sizePercent
|
|
26
|
+
} : {
|
|
27
|
+
height: "100%",
|
|
28
|
+
width: `var(${THUMB_WIDTH_VAR})`,
|
|
29
|
+
transform: `translate3d(${translatePercent}%, 0, 0)`,
|
|
30
|
+
[THUMB_WIDTH_VAR]: sizePercent
|
|
31
|
+
};
|
|
32
|
+
function onPointerDown(event) {
|
|
33
|
+
if (event.button !== 0) return;
|
|
34
|
+
event.stopPropagation();
|
|
35
|
+
const thumb = localRef.current;
|
|
36
|
+
if (!thumb) return;
|
|
37
|
+
thumb.setPointerCapture(event.pointerId);
|
|
38
|
+
ctx.setInteracting(true);
|
|
39
|
+
dragRef.current = {
|
|
40
|
+
pointerId: event.pointerId,
|
|
41
|
+
startPos: orientation === "vertical" ? event.clientY : event.clientX,
|
|
42
|
+
startRatio: scrollRatio
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
function onPointerMove(event) {
|
|
46
|
+
const drag = dragRef.current;
|
|
47
|
+
if (!drag || drag.pointerId !== event.pointerId) return;
|
|
48
|
+
const track = localRef.current?.parentElement;
|
|
49
|
+
if (!track) return;
|
|
50
|
+
const rect = track.getBoundingClientRect();
|
|
51
|
+
const trackSize = orientation === "vertical" ? rect.height : rect.width;
|
|
52
|
+
const thumbPx = thumbSize * trackSize;
|
|
53
|
+
const movable = Math.max(1, trackSize - thumbPx);
|
|
54
|
+
const current = orientation === "vertical" ? event.clientY : event.clientX;
|
|
55
|
+
ctx.scrollToRatio(orientation, dragScrollRatio(drag.startRatio, current - drag.startPos, movable, orientation, ctx.dir));
|
|
56
|
+
}
|
|
57
|
+
function onPointerUp(event) {
|
|
58
|
+
const drag = dragRef.current;
|
|
59
|
+
if (!drag || drag.pointerId !== event.pointerId) return;
|
|
60
|
+
dragRef.current = null;
|
|
61
|
+
ctx.setInteracting(false);
|
|
62
|
+
try {
|
|
63
|
+
localRef.current?.releasePointerCapture(event.pointerId);
|
|
64
|
+
} catch {}
|
|
65
|
+
}
|
|
66
|
+
return {
|
|
67
|
+
ref: mergedRef,
|
|
68
|
+
attrs: scrollAreaThumbAttrs(),
|
|
69
|
+
style,
|
|
70
|
+
onPointerDown,
|
|
71
|
+
onPointerMove,
|
|
72
|
+
onPointerUp
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
export { useScrollAreaThumbState };
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { useScrollAreaThumbState } from "./hooks.js";
|
|
3
|
+
import { mergeProps } from "../../../utils/merge-props.js";
|
|
4
|
+
import { useRender } from "../../../utils/use-render.js";
|
|
5
|
+
// components/scroll-area/thumb/thumb.tsx
|
|
6
|
+
function ScrollAreaThumb({ ref, render, className, children, style, ...props }) {
|
|
7
|
+
const state = useScrollAreaThumbState(ref);
|
|
8
|
+
return useRender({
|
|
9
|
+
defaultTagName: "div",
|
|
10
|
+
ref: state.ref,
|
|
11
|
+
render,
|
|
12
|
+
props: mergeProps({
|
|
13
|
+
className,
|
|
14
|
+
children,
|
|
15
|
+
style: {
|
|
16
|
+
...state.style,
|
|
17
|
+
...style
|
|
18
|
+
},
|
|
19
|
+
onPointerDown: state.onPointerDown,
|
|
20
|
+
onPointerMove: state.onPointerMove,
|
|
21
|
+
onPointerUp: state.onPointerUp,
|
|
22
|
+
onPointerCancel: state.onPointerUp
|
|
23
|
+
}, state.attrs, props)
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
export { ScrollAreaThumb };
|
package/package.json
CHANGED
|
@@ -1,6 +1,36 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cerevialabs/ui",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.5.1",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"sideEffects": false,
|
|
6
|
+
"exports": {
|
|
7
|
+
"./components": {
|
|
8
|
+
"types": "./components/index.d.ts",
|
|
9
|
+
"import": "./components/index.js"
|
|
10
|
+
},
|
|
11
|
+
"./components/accordion": {
|
|
12
|
+
"types": "./components/accordion/index.d.ts",
|
|
13
|
+
"import": "./components/accordion/index.js"
|
|
14
|
+
},
|
|
15
|
+
"./components/button": {
|
|
16
|
+
"types": "./components/button/index.d.ts",
|
|
17
|
+
"import": "./components/button/index.js"
|
|
18
|
+
},
|
|
19
|
+
"./components/collapsible": {
|
|
20
|
+
"types": "./components/collapsible/index.d.ts",
|
|
21
|
+
"import": "./components/collapsible/index.js"
|
|
22
|
+
},
|
|
23
|
+
"./components/scroll-area": {
|
|
24
|
+
"types": "./components/scroll-area/index.d.ts",
|
|
25
|
+
"import": "./components/scroll-area/index.js"
|
|
26
|
+
},
|
|
27
|
+
"./utils": {
|
|
28
|
+
"types": "./utils/index.d.ts",
|
|
29
|
+
"import": "./utils/index.js"
|
|
30
|
+
}
|
|
31
|
+
},
|
|
32
|
+
"peerDependencies": {
|
|
33
|
+
"react": "^19.0.0",
|
|
34
|
+
"react-dom": "^19.0.0"
|
|
35
|
+
}
|
|
36
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
// utils/apply-a11y.ts
|
|
2
|
+
function applyA11y(spec, state) {
|
|
3
|
+
const result = {};
|
|
4
|
+
for (const key in spec) {
|
|
5
|
+
if (!state[key]) continue;
|
|
6
|
+
const attrs = spec[key];
|
|
7
|
+
for (const attr in attrs) result[attr] = attrs[attr];
|
|
8
|
+
}
|
|
9
|
+
return result;
|
|
10
|
+
}
|
|
11
|
+
export { applyA11y };
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { DataAttrsOptions } from "./data-attrs.js";
|
|
2
|
+
type AttrStateValues = Record<string, boolean | undefined>;
|
|
3
|
+
export declare function applyAttrs(identity: string, stateKeys: readonly string[], values: AttrStateValues, options?: DataAttrsOptions): {
|
|
4
|
+
[x: string]: string | true;
|
|
5
|
+
};
|
|
6
|
+
export {};
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { dataAttrs } from "./data-attrs.js";
|
|
2
|
+
// utils/apply-attrs.ts
|
|
3
|
+
function applyAttrs(identity, stateKeys, values, options) {
|
|
4
|
+
const states = {};
|
|
5
|
+
for (const key of stateKeys) states[key] = values[key];
|
|
6
|
+
return dataAttrs(identity, states, options);
|
|
7
|
+
}
|
|
8
|
+
export { applyAttrs };
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
type BooleanStates = Record<string, boolean | undefined>;
|
|
2
|
+
type DataAttrsResult = Record<string, true | string>;
|
|
3
|
+
interface DataAttrsOptions {
|
|
4
|
+
part?: string;
|
|
5
|
+
statesOnly?: boolean;
|
|
6
|
+
}
|
|
7
|
+
export declare function dataAttrs(identity: string, states?: BooleanStates, options?: DataAttrsOptions): DataAttrsResult;
|
|
8
|
+
export type { DataAttrsOptions };
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { toKebabCase } from "./to-kebab-case.js";
|
|
2
|
+
// utils/data-attrs.ts
|
|
3
|
+
function applyStates(result, states) {
|
|
4
|
+
if (!states) return;
|
|
5
|
+
for (const key in states) if (states[key]) result[`data-${toKebabCase(key)}`] = true;
|
|
6
|
+
}
|
|
7
|
+
function dataAttrs(identity, states, options) {
|
|
8
|
+
const result = {};
|
|
9
|
+
const kebab = toKebabCase(identity);
|
|
10
|
+
if (options?.statesOnly) {
|
|
11
|
+
applyStates(result, states);
|
|
12
|
+
return result;
|
|
13
|
+
}
|
|
14
|
+
if (options?.part !== void 0) {
|
|
15
|
+
result[`data-${kebab}-part`] = options.part;
|
|
16
|
+
if (options.part === "root") result["data-slot"] = kebab;
|
|
17
|
+
} else result["data-slot"] = kebab;
|
|
18
|
+
applyStates(result, states);
|
|
19
|
+
return result;
|
|
20
|
+
}
|
|
21
|
+
export { dataAttrs };
|
package/utils/index.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { applyA11y } from "./apply-a11y.js";
|
|
2
|
+
import { applyAttrs } from "./apply-attrs.js";
|
|
3
|
+
import { DataAttrsOptions, dataAttrs } from "./data-attrs.js";
|
|
4
|
+
import { mergeProps } from "./merge-props.js";
|
|
5
|
+
import { useMergeRefs } from "./merge-refs.js";
|
|
6
|
+
import { usePrefersReducedMotion } from "./prefers-reduced-motion.js";
|
|
7
|
+
import { toKebabCase } from "./to-kebab-case.js";
|
|
8
|
+
import { useControllableState } from "./use-controllable-state.js";
|
|
9
|
+
import { PresenceDataState, PresenceStates, PresenceStatus, presenceAttrs, presenceShouldRender, usePresence } from "./use-presence.js";
|
|
10
|
+
import { UseRenderComponentProps, useRender } from "./use-render.js";
|
|
11
|
+
export { type DataAttrsOptions, type PresenceDataState, type PresenceStates, type PresenceStatus, type UseRenderComponentProps, applyA11y, applyAttrs, dataAttrs, mergeProps, presenceAttrs, presenceShouldRender, toKebabCase, useControllableState, useMergeRefs, usePrefersReducedMotion, usePresence, useRender };
|
package/utils/index.js
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { applyA11y } from "./apply-a11y.js";
|
|
2
|
+
import { applyAttrs } from "./apply-attrs.js";
|
|
3
|
+
import { dataAttrs } from "./data-attrs.js";
|
|
4
|
+
import { mergeProps } from "./merge-props.js";
|
|
5
|
+
import { useMergeRefs } from "./merge-refs.js";
|
|
6
|
+
import { usePrefersReducedMotion } from "./prefers-reduced-motion.js";
|
|
7
|
+
import { toKebabCase } from "./to-kebab-case.js";
|
|
8
|
+
import { useControllableState } from "./use-controllable-state.js";
|
|
9
|
+
import { presenceAttrs, presenceShouldRender, usePresence } from "./use-presence.js";
|
|
10
|
+
import { useRender } from "./use-render.js";
|
|
11
|
+
export { applyA11y, applyAttrs, dataAttrs, mergeProps, presenceAttrs, presenceShouldRender, toKebabCase, useControllableState, useMergeRefs, usePrefersReducedMotion, usePresence, useRender };
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
// utils/merge-props.ts
|
|
2
|
+
function isEventHandler(key, value) {
|
|
3
|
+
const code0 = key.charCodeAt(0);
|
|
4
|
+
const code1 = key.charCodeAt(1);
|
|
5
|
+
const code2 = key.charCodeAt(2);
|
|
6
|
+
return code0 === 111 && code1 === 110 && code2 >= 65 && code2 <= 90 && typeof value === "function";
|
|
7
|
+
}
|
|
8
|
+
function mergeClassNames(left, right) {
|
|
9
|
+
if (right) {
|
|
10
|
+
if (left) return `${left} ${right}`;
|
|
11
|
+
return right;
|
|
12
|
+
}
|
|
13
|
+
return left;
|
|
14
|
+
}
|
|
15
|
+
function mergeStyles(left, right) {
|
|
16
|
+
if (!left && !right) return void 0;
|
|
17
|
+
if (!left) return right;
|
|
18
|
+
if (!right) return left;
|
|
19
|
+
return {
|
|
20
|
+
...left,
|
|
21
|
+
...right
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
function isDefaultPrevented(args) {
|
|
25
|
+
const event = args[0];
|
|
26
|
+
return typeof event === "object" && event !== null && "defaultPrevented" in event && Boolean(event.defaultPrevented);
|
|
27
|
+
}
|
|
28
|
+
function mergeEventHandlers(left, right) {
|
|
29
|
+
if (!left) return right;
|
|
30
|
+
if (!right) return left;
|
|
31
|
+
return (...args) => {
|
|
32
|
+
right(...args);
|
|
33
|
+
if (isDefaultPrevented(args)) return;
|
|
34
|
+
left(...args);
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
function mergeInto(merged, incoming) {
|
|
38
|
+
const source = incoming;
|
|
39
|
+
for (const key in source) {
|
|
40
|
+
if (!Object.prototype.hasOwnProperty.call(source, key)) continue;
|
|
41
|
+
if (key === "ref") continue;
|
|
42
|
+
const value = source[key];
|
|
43
|
+
if (value === void 0) continue;
|
|
44
|
+
if (key === "className") {
|
|
45
|
+
merged[key] = mergeClassNames(merged.className, value);
|
|
46
|
+
continue;
|
|
47
|
+
}
|
|
48
|
+
if (key === "style") {
|
|
49
|
+
merged[key] = mergeStyles(merged.style, value);
|
|
50
|
+
continue;
|
|
51
|
+
}
|
|
52
|
+
if (isEventHandler(key, value)) {
|
|
53
|
+
merged[key] = mergeEventHandlers(merged[key], value);
|
|
54
|
+
continue;
|
|
55
|
+
}
|
|
56
|
+
merged[key] = value;
|
|
57
|
+
}
|
|
58
|
+
return merged;
|
|
59
|
+
}
|
|
60
|
+
function mergeProps(...inputs) {
|
|
61
|
+
const merged = {};
|
|
62
|
+
for (const input of inputs) {
|
|
63
|
+
if (!input) continue;
|
|
64
|
+
mergeInto(merged, input);
|
|
65
|
+
}
|
|
66
|
+
return merged;
|
|
67
|
+
}
|
|
68
|
+
export { mergeProps };
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { useCallback, useLayoutEffect, useRef } from "react";
|
|
3
|
+
// utils/merge-refs.ts
|
|
4
|
+
function sameRefList(previous, next) {
|
|
5
|
+
if (previous.length !== next.length) return false;
|
|
6
|
+
for (let index = 0; index < previous.length; index += 1) if (!Object.is(previous[index], next[index])) return false;
|
|
7
|
+
return true;
|
|
8
|
+
}
|
|
9
|
+
function bindRef(ref, value) {
|
|
10
|
+
if (typeof ref === "function") {
|
|
11
|
+
const cleanup = ref(value);
|
|
12
|
+
if (typeof cleanup === "function") return cleanup;
|
|
13
|
+
return () => {
|
|
14
|
+
ref(null);
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
ref.current = value;
|
|
18
|
+
return () => {
|
|
19
|
+
ref.current = null;
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
function releaseBindings(bindings) {
|
|
23
|
+
for (const binding of bindings) binding.cleanup?.();
|
|
24
|
+
}
|
|
25
|
+
function commitBindings(bindingsRef, nextRefs, node) {
|
|
26
|
+
const previous = bindingsRef.current;
|
|
27
|
+
const next = [];
|
|
28
|
+
const kept = /* @__PURE__ */ new Set();
|
|
29
|
+
for (const ref of nextRefs) {
|
|
30
|
+
if (ref == null || kept.has(ref)) continue;
|
|
31
|
+
const existing = previous.find((binding) => Object.is(binding.ref, ref));
|
|
32
|
+
if (existing) next.push(existing);
|
|
33
|
+
else next.push({
|
|
34
|
+
ref,
|
|
35
|
+
cleanup: bindRef(ref, node)
|
|
36
|
+
});
|
|
37
|
+
kept.add(ref);
|
|
38
|
+
}
|
|
39
|
+
for (const binding of previous) if (!kept.has(binding.ref)) binding.cleanup?.();
|
|
40
|
+
bindingsRef.current = next;
|
|
41
|
+
}
|
|
42
|
+
function useMergeRefs(refs) {
|
|
43
|
+
const refsRef = useRef(refs);
|
|
44
|
+
const nodeRef = useRef(null);
|
|
45
|
+
const bindingsRef = useRef([]);
|
|
46
|
+
useLayoutEffect(() => {
|
|
47
|
+
const previous = refsRef.current;
|
|
48
|
+
refsRef.current = refs;
|
|
49
|
+
const node = nodeRef.current;
|
|
50
|
+
if (node == null || sameRefList(previous, refs)) return;
|
|
51
|
+
commitBindings(bindingsRef, refs, node);
|
|
52
|
+
});
|
|
53
|
+
return useCallback((node) => {
|
|
54
|
+
if (node == null) {
|
|
55
|
+
nodeRef.current = null;
|
|
56
|
+
releaseBindings(bindingsRef.current);
|
|
57
|
+
bindingsRef.current = [];
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
nodeRef.current = node;
|
|
61
|
+
commitBindings(bindingsRef, refsRef.current, node);
|
|
62
|
+
return () => {
|
|
63
|
+
nodeRef.current = null;
|
|
64
|
+
releaseBindings(bindingsRef.current);
|
|
65
|
+
bindingsRef.current = [];
|
|
66
|
+
};
|
|
67
|
+
}, []);
|
|
68
|
+
}
|
|
69
|
+
export { useMergeRefs };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function usePrefersReducedMotion(): boolean;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { useSyncExternalStore } from "react";
|
|
3
|
+
// utils/prefers-reduced-motion.ts
|
|
4
|
+
const QUERY = "(prefers-reduced-motion: reduce)";
|
|
5
|
+
function subscribe(onStoreChange) {
|
|
6
|
+
const mediaQuery = window.matchMedia(QUERY);
|
|
7
|
+
mediaQuery.addEventListener("change", onStoreChange);
|
|
8
|
+
return () => mediaQuery.removeEventListener("change", onStoreChange);
|
|
9
|
+
}
|
|
10
|
+
function getSnapshot() {
|
|
11
|
+
return window.matchMedia(QUERY).matches;
|
|
12
|
+
}
|
|
13
|
+
function getServerSnapshot() {
|
|
14
|
+
return false;
|
|
15
|
+
}
|
|
16
|
+
function usePrefersReducedMotion() {
|
|
17
|
+
return useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot);
|
|
18
|
+
}
|
|
19
|
+
export { usePrefersReducedMotion };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function toKebabCase(str: string): string;
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
// utils/to-kebab-case.ts
|
|
2
|
+
function toKebabCase(str) {
|
|
3
|
+
return str.normalize("NFC").replace(/([\p{Ll}\p{N}])(\p{Lu})/gu, "$1-$2").replace(/(\p{Lu}+)(\p{Lu}\p{Ll})/gu, "$1-$2").replace(/[^\p{L}\p{M}\p{N}]+/gu, "-").replace(/^-+|-+$/g, "").toLowerCase();
|
|
4
|
+
}
|
|
5
|
+
export { toKebabCase };
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { Dispatch, SetStateAction } from "react";
|
|
2
|
+
interface UseControllableStateParams<T> {
|
|
3
|
+
value?: T;
|
|
4
|
+
defaultValue: T | (() => T);
|
|
5
|
+
onChange?: (value: T) => void;
|
|
6
|
+
}
|
|
7
|
+
type UseControllableStateReturn<T> = [T, Dispatch<SetStateAction<T>>];
|
|
8
|
+
export declare function useControllableState<T>({ value: valueProp, defaultValue, onChange }: UseControllableStateParams<T>): UseControllableStateReturn<T>;
|
|
9
|
+
export {};
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { useCallback, useEffect, useLayoutEffect, useRef, useState } from "react";
|
|
3
|
+
// utils/use-controllable-state.ts
|
|
4
|
+
function useControllableState({ value: valueProp, defaultValue, onChange }) {
|
|
5
|
+
const [uncontrolledValue, setUncontrolledValue] = useState(defaultValue);
|
|
6
|
+
const isControlled = valueProp !== void 0;
|
|
7
|
+
const value = isControlled ? valueProp : uncontrolledValue;
|
|
8
|
+
const valueRef = useRef(value);
|
|
9
|
+
const pendingRef = useRef(value);
|
|
10
|
+
const snapTicketRef = useRef(0);
|
|
11
|
+
const isControlledRef = useRef(isControlled);
|
|
12
|
+
const onChangeRef = useRef(onChange);
|
|
13
|
+
const prevControlledRef = useRef(isControlled);
|
|
14
|
+
useLayoutEffect(() => {
|
|
15
|
+
valueRef.current = value;
|
|
16
|
+
pendingRef.current = value;
|
|
17
|
+
isControlledRef.current = isControlled;
|
|
18
|
+
onChangeRef.current = onChange;
|
|
19
|
+
});
|
|
20
|
+
useEffect(() => {
|
|
21
|
+
if (process.env.NODE_ENV !== "production" && prevControlledRef.current !== isControlled) console.error("useControllableState: Switching between controlled and uncontrolled is not supported. Decide between `value` / `onChange` (controlled) or `defaultValue` (uncontrolled).");
|
|
22
|
+
prevControlledRef.current = isControlled;
|
|
23
|
+
}, [isControlled]);
|
|
24
|
+
return [value, useCallback((next) => {
|
|
25
|
+
const prev = pendingRef.current;
|
|
26
|
+
const nextValue = typeof next === "function" ? next(prev) : next;
|
|
27
|
+
if (Object.is(prev, nextValue)) return;
|
|
28
|
+
pendingRef.current = nextValue;
|
|
29
|
+
if (!isControlledRef.current) setUncontrolledValue(nextValue);
|
|
30
|
+
onChangeRef.current?.(nextValue);
|
|
31
|
+
const ticket = ++snapTicketRef.current;
|
|
32
|
+
queueMicrotask(() => {
|
|
33
|
+
if (snapTicketRef.current !== ticket) return;
|
|
34
|
+
pendingRef.current = valueRef.current;
|
|
35
|
+
});
|
|
36
|
+
}, [])];
|
|
37
|
+
}
|
|
38
|
+
export { useControllableState };
|