@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,55 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { useLayoutEffect } from "react";
|
|
3
|
+
import { useScrollAreaContext } from "../context/hooks.js";
|
|
4
|
+
import { scrollAreaContentAttrs } from "./attrs.js";
|
|
5
|
+
import { ensureNativeScrollbarHiddenStyles, readScrollMetrics } from "./utils.js";
|
|
6
|
+
import { useMergeRefs } from "../../../utils/merge-refs.js";
|
|
7
|
+
// components/scroll-area/content/hooks.ts
|
|
8
|
+
function useScrollAreaContentState(ref, id) {
|
|
9
|
+
const ctx = useScrollAreaContext();
|
|
10
|
+
const { contentRef, setScrollMetrics, setContentId, autoContentId } = ctx;
|
|
11
|
+
const resolvedId = id ?? autoContentId;
|
|
12
|
+
const mergedRef = useMergeRefs([ref, contentRef]);
|
|
13
|
+
useLayoutEffect(() => {
|
|
14
|
+
setContentId(resolvedId);
|
|
15
|
+
}, [resolvedId, setContentId]);
|
|
16
|
+
useLayoutEffect(() => {
|
|
17
|
+
ensureNativeScrollbarHiddenStyles();
|
|
18
|
+
}, []);
|
|
19
|
+
useLayoutEffect(() => {
|
|
20
|
+
const element = contentRef.current;
|
|
21
|
+
if (!element) return void 0;
|
|
22
|
+
function update() {
|
|
23
|
+
if (!element) return;
|
|
24
|
+
setScrollMetrics(readScrollMetrics(element, ctx.dir));
|
|
25
|
+
}
|
|
26
|
+
update();
|
|
27
|
+
const resizeObserver = typeof ResizeObserver === "undefined" ? null : new ResizeObserver(update);
|
|
28
|
+
if (!resizeObserver) return void 0;
|
|
29
|
+
resizeObserver.observe(element);
|
|
30
|
+
for (const child of element.children) resizeObserver.observe(child);
|
|
31
|
+
return () => resizeObserver.disconnect();
|
|
32
|
+
}, [
|
|
33
|
+
contentRef,
|
|
34
|
+
ctx.dir,
|
|
35
|
+
setScrollMetrics
|
|
36
|
+
]);
|
|
37
|
+
return {
|
|
38
|
+
ref: mergedRef,
|
|
39
|
+
id: resolvedId,
|
|
40
|
+
attrs: scrollAreaContentAttrs({
|
|
41
|
+
overflowX: ctx.overflowX,
|
|
42
|
+
overflowY: ctx.overflowY,
|
|
43
|
+
overflowTop: ctx.overflowTop,
|
|
44
|
+
overflowBottom: ctx.overflowBottom,
|
|
45
|
+
overflowLeft: ctx.overflowLeft,
|
|
46
|
+
overflowRight: ctx.overflowRight
|
|
47
|
+
}),
|
|
48
|
+
onScroll(event) {
|
|
49
|
+
const element = event.currentTarget;
|
|
50
|
+
ctx.setScrollMetrics(readScrollMetrics(element, ctx.dir));
|
|
51
|
+
ctx.notifyScroll();
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
export { useScrollAreaContentState };
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
// components/scroll-area/content/utils.ts
|
|
2
|
+
const STYLE_ID = "cerevia-scroll-area-hide-native";
|
|
3
|
+
const EDGE_EPSILON = 1;
|
|
4
|
+
function ensureNativeScrollbarHiddenStyles() {
|
|
5
|
+
if (typeof document === "undefined") return;
|
|
6
|
+
if (document.getElementById(STYLE_ID)) return;
|
|
7
|
+
const style = document.createElement("style");
|
|
8
|
+
style.id = STYLE_ID;
|
|
9
|
+
style.textContent = ["[data-scroll-area-part=\"content\"]{scrollbar-width:none;-ms-overflow-style:none;}", "[data-scroll-area-part=\"content\"]::-webkit-scrollbar{display:none;}"].join("");
|
|
10
|
+
document.head.appendChild(style);
|
|
11
|
+
}
|
|
12
|
+
function readScrollMetrics(element, dir = "ltr") {
|
|
13
|
+
const { scrollTop, scrollLeft, scrollWidth, scrollHeight, clientWidth, clientHeight } = element;
|
|
14
|
+
const maxY = Math.max(0, scrollHeight - clientHeight);
|
|
15
|
+
const maxX = Math.max(0, scrollWidth - clientWidth);
|
|
16
|
+
const overflowY = maxY > 0;
|
|
17
|
+
const overflowX = maxX > 0;
|
|
18
|
+
const fromStartX = Math.abs(scrollLeft);
|
|
19
|
+
const overflowStartX = fromStartX >= EDGE_EPSILON;
|
|
20
|
+
const overflowEndX = maxX > 0 && fromStartX <= maxX - EDGE_EPSILON;
|
|
21
|
+
return {
|
|
22
|
+
overflowX,
|
|
23
|
+
overflowY,
|
|
24
|
+
overflowTop: scrollTop >= EDGE_EPSILON,
|
|
25
|
+
overflowBottom: maxY > 0 && scrollTop <= maxY - EDGE_EPSILON,
|
|
26
|
+
overflowLeft: dir === "rtl" ? overflowEndX : overflowStartX,
|
|
27
|
+
overflowRight: dir === "rtl" ? overflowStartX : overflowEndX,
|
|
28
|
+
scrollRatioY: maxY === 0 ? 0 : scrollTop / maxY,
|
|
29
|
+
scrollRatioX: maxX === 0 ? 0 : fromStartX / maxX,
|
|
30
|
+
thumbSizeY: scrollHeight === 0 ? 1 : Math.min(1, clientHeight / scrollHeight),
|
|
31
|
+
thumbSizeX: scrollWidth === 0 ? 1 : Math.min(1, clientWidth / scrollWidth)
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
export { ensureNativeScrollbarHiddenStyles, readScrollMetrics };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import "./types.js";
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { createContext } from "react";
|
|
3
|
+
import { jsx } from "react/jsx-runtime";
|
|
4
|
+
// components/scroll-area/context/context.tsx
|
|
5
|
+
const ScrollAreaContext = createContext(void 0);
|
|
6
|
+
function ScrollAreaProvider({ contextValue, children }) {
|
|
7
|
+
return /* @__PURE__ */ jsx(ScrollAreaContext.Provider, {
|
|
8
|
+
value: contextValue,
|
|
9
|
+
children
|
|
10
|
+
});
|
|
11
|
+
}
|
|
12
|
+
export { ScrollAreaContext, ScrollAreaProvider };
|
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { use, useId, useLayoutEffect, useRef, useState } from "react";
|
|
3
|
+
import { ScrollAreaContext } from "./context.js";
|
|
4
|
+
import { axisOverflow, cornerShown, pageScrollOn, pointerActive, scrollToRatioOn, scrollbarShown } from "./utils.js";
|
|
5
|
+
// components/scroll-area/context/hooks.ts
|
|
6
|
+
const INITIAL_METRICS = {
|
|
7
|
+
overflowX: false,
|
|
8
|
+
overflowY: false,
|
|
9
|
+
overflowTop: false,
|
|
10
|
+
overflowBottom: false,
|
|
11
|
+
overflowLeft: false,
|
|
12
|
+
overflowRight: false,
|
|
13
|
+
scrollRatioX: 0,
|
|
14
|
+
scrollRatioY: 0,
|
|
15
|
+
thumbSizeX: 1,
|
|
16
|
+
thumbSizeY: 1
|
|
17
|
+
};
|
|
18
|
+
const INITIAL_AXES = {
|
|
19
|
+
vertical: false,
|
|
20
|
+
horizontal: false
|
|
21
|
+
};
|
|
22
|
+
function useScrollLatest(scrollHideDelay, dir) {
|
|
23
|
+
const latestRef = useRef({
|
|
24
|
+
scrollHideDelay,
|
|
25
|
+
dir
|
|
26
|
+
});
|
|
27
|
+
useLayoutEffect(() => {
|
|
28
|
+
latestRef.current = {
|
|
29
|
+
scrollHideDelay,
|
|
30
|
+
dir
|
|
31
|
+
};
|
|
32
|
+
});
|
|
33
|
+
return latestRef;
|
|
34
|
+
}
|
|
35
|
+
function useScrollActivity(latestRef) {
|
|
36
|
+
const hideTimerRef = useRef(null);
|
|
37
|
+
const [scrolling, setScrolling] = useState(false);
|
|
38
|
+
const [scrollbarHovering, setScrollbarHovering] = useState(false);
|
|
39
|
+
const [api] = useState(() => {
|
|
40
|
+
function clearHideTimer() {
|
|
41
|
+
if (hideTimerRef.current !== null) {
|
|
42
|
+
clearTimeout(hideTimerRef.current);
|
|
43
|
+
hideTimerRef.current = null;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
function scheduleHide() {
|
|
47
|
+
clearHideTimer();
|
|
48
|
+
hideTimerRef.current = setTimeout(() => {
|
|
49
|
+
setScrolling(false);
|
|
50
|
+
hideTimerRef.current = null;
|
|
51
|
+
}, latestRef.current.scrollHideDelay);
|
|
52
|
+
}
|
|
53
|
+
function notifyScroll() {
|
|
54
|
+
setScrolling(true);
|
|
55
|
+
scheduleHide();
|
|
56
|
+
}
|
|
57
|
+
function onScrollbarPointerEnter() {
|
|
58
|
+
clearHideTimer();
|
|
59
|
+
setScrollbarHovering(true);
|
|
60
|
+
}
|
|
61
|
+
function onScrollbarPointerLeave() {
|
|
62
|
+
setScrollbarHovering(false);
|
|
63
|
+
notifyScroll();
|
|
64
|
+
}
|
|
65
|
+
return {
|
|
66
|
+
notifyScroll,
|
|
67
|
+
onScrollbarPointerEnter,
|
|
68
|
+
onScrollbarPointerLeave
|
|
69
|
+
};
|
|
70
|
+
});
|
|
71
|
+
return {
|
|
72
|
+
scrolling,
|
|
73
|
+
scrollbarHovering,
|
|
74
|
+
setScrollbarHovering,
|
|
75
|
+
...api
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
function useScrollbarAxes() {
|
|
79
|
+
const [axes, setAxes] = useState(INITIAL_AXES);
|
|
80
|
+
const [api] = useState(() => ({
|
|
81
|
+
registerScrollbar(orientation) {
|
|
82
|
+
setAxes((prev) => prev[orientation] ? prev : {
|
|
83
|
+
...prev,
|
|
84
|
+
[orientation]: true
|
|
85
|
+
});
|
|
86
|
+
},
|
|
87
|
+
unregisterScrollbar(orientation) {
|
|
88
|
+
setAxes((prev) => prev[orientation] ? {
|
|
89
|
+
...prev,
|
|
90
|
+
[orientation]: false
|
|
91
|
+
} : prev);
|
|
92
|
+
}
|
|
93
|
+
}));
|
|
94
|
+
return {
|
|
95
|
+
axes,
|
|
96
|
+
...api
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
function useScrollCommands(contentRef, latestRef, notifyScroll) {
|
|
100
|
+
const [api] = useState(() => ({
|
|
101
|
+
scrollToRatio(orientation, ratio) {
|
|
102
|
+
const element = contentRef.current;
|
|
103
|
+
if (!element) return;
|
|
104
|
+
scrollToRatioOn(element, orientation, ratio, latestRef.current.dir);
|
|
105
|
+
notifyScroll();
|
|
106
|
+
},
|
|
107
|
+
pageScroll(orientation, direction) {
|
|
108
|
+
const element = contentRef.current;
|
|
109
|
+
if (!element) return;
|
|
110
|
+
pageScrollOn(element, orientation, direction, latestRef.current.dir);
|
|
111
|
+
notifyScroll();
|
|
112
|
+
}
|
|
113
|
+
}));
|
|
114
|
+
return api;
|
|
115
|
+
}
|
|
116
|
+
function useScrollAreaProviderValue({ type = "hover", scrollHideDelay = 600 }) {
|
|
117
|
+
const autoContentId = useId();
|
|
118
|
+
const [contentId, setContentId] = useState(autoContentId);
|
|
119
|
+
const contentRef = useRef(null);
|
|
120
|
+
const [dir, setDir] = useState("ltr");
|
|
121
|
+
const [hovering, setHovering] = useState(false);
|
|
122
|
+
const [interacting, setInteracting] = useState(false);
|
|
123
|
+
const [metrics, setScrollMetrics] = useState(INITIAL_METRICS);
|
|
124
|
+
const latestRef = useScrollLatest(scrollHideDelay, dir);
|
|
125
|
+
const activity = useScrollActivity(latestRef);
|
|
126
|
+
const axes = useScrollbarAxes();
|
|
127
|
+
const commands = useScrollCommands(contentRef, latestRef, activity.notifyScroll);
|
|
128
|
+
const active = pointerActive(activity.scrolling, interacting, activity.scrollbarHovering);
|
|
129
|
+
const shown = scrollbarShown(type, hovering, active);
|
|
130
|
+
return {
|
|
131
|
+
type,
|
|
132
|
+
scrollHideDelay,
|
|
133
|
+
autoContentId,
|
|
134
|
+
contentId,
|
|
135
|
+
setContentId,
|
|
136
|
+
contentRef,
|
|
137
|
+
dir,
|
|
138
|
+
setDir,
|
|
139
|
+
hovering,
|
|
140
|
+
setHovering,
|
|
141
|
+
scrollbarHovering: activity.scrollbarHovering,
|
|
142
|
+
setScrollbarHovering: activity.setScrollbarHovering,
|
|
143
|
+
onScrollbarPointerEnter: activity.onScrollbarPointerEnter,
|
|
144
|
+
onScrollbarPointerLeave: activity.onScrollbarPointerLeave,
|
|
145
|
+
scrolling: activity.scrolling,
|
|
146
|
+
interacting,
|
|
147
|
+
setInteracting,
|
|
148
|
+
activityState: activity.scrolling || interacting ? "scrolling" : "idle",
|
|
149
|
+
notifyScroll: activity.notifyScroll,
|
|
150
|
+
...metrics,
|
|
151
|
+
setScrollMetrics,
|
|
152
|
+
scrollbarVisible(orientation) {
|
|
153
|
+
return axisOverflow(metrics, orientation) && shown;
|
|
154
|
+
},
|
|
155
|
+
cornerVisible: cornerShown(axes.axes, metrics, shown),
|
|
156
|
+
registerScrollbar: axes.registerScrollbar,
|
|
157
|
+
unregisterScrollbar: axes.unregisterScrollbar,
|
|
158
|
+
hasScrollbar(orientation) {
|
|
159
|
+
return axes.axes[orientation];
|
|
160
|
+
},
|
|
161
|
+
scrollToRatio: commands.scrollToRatio,
|
|
162
|
+
pageScroll: commands.pageScroll
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
function useScrollAreaContext() {
|
|
166
|
+
const context = use(ScrollAreaContext);
|
|
167
|
+
if (!context) throw new Error("ScrollArea parts must be used within ScrollArea.Root");
|
|
168
|
+
return context;
|
|
169
|
+
}
|
|
170
|
+
export { useScrollAreaContext, useScrollAreaProviderValue };
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import "./context.js";
|
|
2
|
+
import { useScrollAreaContext } from "./hooks.js";
|
|
3
|
+
import { ScrollAreaActivityState, ScrollAreaContextValue, ScrollAreaOrientation, ScrollAreaType } from "./types.js";
|
|
4
|
+
export { type ScrollAreaActivityState, type ScrollAreaContextValue, type ScrollAreaOrientation, type ScrollAreaType, useScrollAreaContext };
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { Dispatch, RefObject, SetStateAction } from "react";
|
|
2
|
+
type ScrollAreaType = "hover" | "scroll" | "always";
|
|
3
|
+
type ScrollAreaOrientation = "vertical" | "horizontal";
|
|
4
|
+
type ScrollAreaActivityState = "idle" | "scrolling";
|
|
5
|
+
type ScrollAreaDir = "ltr" | "rtl";
|
|
6
|
+
interface ScrollMetrics {
|
|
7
|
+
overflowX: boolean;
|
|
8
|
+
overflowY: boolean;
|
|
9
|
+
overflowTop: boolean;
|
|
10
|
+
overflowBottom: boolean;
|
|
11
|
+
overflowLeft: boolean;
|
|
12
|
+
overflowRight: boolean;
|
|
13
|
+
scrollRatioX: number;
|
|
14
|
+
scrollRatioY: number;
|
|
15
|
+
thumbSizeX: number;
|
|
16
|
+
thumbSizeY: number;
|
|
17
|
+
}
|
|
18
|
+
interface ScrollAreaContextValue extends ScrollMetrics {
|
|
19
|
+
type: ScrollAreaType;
|
|
20
|
+
scrollHideDelay: number;
|
|
21
|
+
autoContentId: string;
|
|
22
|
+
contentId: string;
|
|
23
|
+
setContentId: Dispatch<SetStateAction<string>>;
|
|
24
|
+
contentRef: RefObject<HTMLElement | null>;
|
|
25
|
+
dir: ScrollAreaDir;
|
|
26
|
+
setDir: Dispatch<SetStateAction<ScrollAreaDir>>;
|
|
27
|
+
hovering: boolean;
|
|
28
|
+
setHovering: (value: boolean) => void;
|
|
29
|
+
scrollbarHovering: boolean;
|
|
30
|
+
setScrollbarHovering: (value: boolean) => void;
|
|
31
|
+
onScrollbarPointerEnter: () => void;
|
|
32
|
+
onScrollbarPointerLeave: () => void;
|
|
33
|
+
scrolling: boolean;
|
|
34
|
+
interacting: boolean;
|
|
35
|
+
setInteracting: (value: boolean) => void;
|
|
36
|
+
activityState: ScrollAreaActivityState;
|
|
37
|
+
notifyScroll: () => void;
|
|
38
|
+
setScrollMetrics: (metrics: ScrollMetrics) => void;
|
|
39
|
+
scrollbarVisible: (orientation: ScrollAreaOrientation) => boolean;
|
|
40
|
+
cornerVisible: boolean;
|
|
41
|
+
registerScrollbar: (orientation: ScrollAreaOrientation) => void;
|
|
42
|
+
unregisterScrollbar: (orientation: ScrollAreaOrientation) => void;
|
|
43
|
+
hasScrollbar: (orientation: ScrollAreaOrientation) => boolean;
|
|
44
|
+
scrollToRatio: (orientation: ScrollAreaOrientation, ratio: number) => void;
|
|
45
|
+
pageScroll: (orientation: ScrollAreaOrientation, direction: 1 | -1) => void;
|
|
46
|
+
}
|
|
47
|
+
export type { ScrollAreaActivityState, ScrollAreaContextValue, ScrollAreaDir, ScrollAreaOrientation, ScrollAreaType, ScrollMetrics };
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
// components/scroll-area/context/utils.ts
|
|
2
|
+
function addScroll(element, axis, delta) {
|
|
3
|
+
if (axis === "top") {
|
|
4
|
+
element.scrollTop += delta;
|
|
5
|
+
return;
|
|
6
|
+
}
|
|
7
|
+
element.scrollLeft += delta;
|
|
8
|
+
}
|
|
9
|
+
function scrollByAxis(element, axis, delta) {
|
|
10
|
+
if (typeof element.scrollBy !== "function") {
|
|
11
|
+
addScroll(element, axis, delta);
|
|
12
|
+
return;
|
|
13
|
+
}
|
|
14
|
+
if (axis === "top") {
|
|
15
|
+
element.scrollBy({ top: delta });
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
element.scrollBy({ left: delta });
|
|
19
|
+
}
|
|
20
|
+
function scrollToRatioOn(element, orientation, ratio, dir) {
|
|
21
|
+
const clamped = Math.min(1, Math.max(0, ratio));
|
|
22
|
+
if (orientation === "vertical") {
|
|
23
|
+
element.scrollTop = clamped * (element.scrollHeight - element.clientHeight);
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
const next = clamped * (element.scrollWidth - element.clientWidth);
|
|
27
|
+
element.scrollLeft = dir === "rtl" ? -next : next;
|
|
28
|
+
}
|
|
29
|
+
function pageScrollOn(element, orientation, direction, dir) {
|
|
30
|
+
if (orientation === "vertical") {
|
|
31
|
+
scrollByAxis(element, "top", direction * element.clientHeight);
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
const delta = direction * element.clientWidth;
|
|
35
|
+
scrollByAxis(element, "left", dir === "rtl" ? -delta : delta);
|
|
36
|
+
}
|
|
37
|
+
function horizontalRtl(orientation, dir) {
|
|
38
|
+
return orientation === "horizontal" && dir === "rtl";
|
|
39
|
+
}
|
|
40
|
+
function thumbTranslatePercent(orientation, scrollRatio, thumbSize, dir) {
|
|
41
|
+
if (thumbSize <= 0 || thumbSize >= 1) return 0;
|
|
42
|
+
const travel = (1 / thumbSize - 1) * 100;
|
|
43
|
+
return (horizontalRtl(orientation, dir) ? 1 - scrollRatio : scrollRatio) * travel;
|
|
44
|
+
}
|
|
45
|
+
function dragScrollRatio(startRatio, deltaPx, movable, orientation, dir) {
|
|
46
|
+
const delta = deltaPx / Math.max(1, movable);
|
|
47
|
+
return startRatio + (horizontalRtl(orientation, dir) ? -delta : delta);
|
|
48
|
+
}
|
|
49
|
+
function pageDirectionAt(orientation, dir, offset, trackSize, scrollRatio, thumbSize) {
|
|
50
|
+
const thumbPx = thumbSize * trackSize;
|
|
51
|
+
const movable = trackSize - thumbPx;
|
|
52
|
+
const fromTrackStart = horizontalRtl(orientation, dir) ? (1 - scrollRatio) * movable : scrollRatio * movable;
|
|
53
|
+
if (offset < fromTrackStart) return horizontalRtl(orientation, dir) ? 1 : -1;
|
|
54
|
+
if (offset > fromTrackStart + thumbPx) return horizontalRtl(orientation, dir) ? -1 : 1;
|
|
55
|
+
return null;
|
|
56
|
+
}
|
|
57
|
+
function pointerActive(scrolling, interacting, scrollbarHovering) {
|
|
58
|
+
return scrolling || interacting || scrollbarHovering;
|
|
59
|
+
}
|
|
60
|
+
function scrollbarShown(type, hovering, active) {
|
|
61
|
+
if (type === "always") return true;
|
|
62
|
+
if (type === "hover") return hovering || active;
|
|
63
|
+
return active;
|
|
64
|
+
}
|
|
65
|
+
function axisOverflow(metrics, orientation) {
|
|
66
|
+
if (orientation === "vertical") return metrics.overflowY;
|
|
67
|
+
return metrics.overflowX;
|
|
68
|
+
}
|
|
69
|
+
function cornerShown(axes, metrics, shown) {
|
|
70
|
+
return axes.vertical && axes.horizontal && metrics.overflowX && metrics.overflowY && shown;
|
|
71
|
+
}
|
|
72
|
+
export { axisOverflow, cornerShown, dragScrollRatio, pageDirectionAt, pageScrollOn, pointerActive, scrollToRatioOn, scrollbarShown, thumbTranslatePercent };
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { applyAttrs } from "../../../utils/apply-attrs.js";
|
|
2
|
+
// components/scroll-area/corner/attrs.ts
|
|
3
|
+
const scrollAreaCornerAttrStates = [];
|
|
4
|
+
function scrollAreaCornerAttrs({ visible }) {
|
|
5
|
+
return {
|
|
6
|
+
...applyAttrs("scroll-area", scrollAreaCornerAttrStates, {}, { part: "corner" }),
|
|
7
|
+
"data-state": visible ? "visible" : "hidden"
|
|
8
|
+
};
|
|
9
|
+
}
|
|
10
|
+
export { scrollAreaCornerAttrs };
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { useScrollAreaCornerState } from "./hooks.js";
|
|
3
|
+
import { mergeProps } from "../../../utils/merge-props.js";
|
|
4
|
+
import { useRender } from "../../../utils/use-render.js";
|
|
5
|
+
// components/scroll-area/corner/corner.tsx
|
|
6
|
+
function ScrollAreaCorner({ ref, render, className, children, style, ...props }) {
|
|
7
|
+
const state = useScrollAreaCornerState();
|
|
8
|
+
return useRender({
|
|
9
|
+
defaultTagName: "div",
|
|
10
|
+
ref,
|
|
11
|
+
render,
|
|
12
|
+
props: mergeProps({
|
|
13
|
+
className,
|
|
14
|
+
children,
|
|
15
|
+
style: {
|
|
16
|
+
position: "absolute",
|
|
17
|
+
...style,
|
|
18
|
+
...state.visible ? void 0 : {
|
|
19
|
+
pointerEvents: "none",
|
|
20
|
+
opacity: 0
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
}, state.attrs, props)
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
export { ScrollAreaCorner };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { useScrollAreaContext } from "../context/hooks.js";
|
|
3
|
+
import { scrollAreaCornerAttrs } from "./attrs.js";
|
|
4
|
+
// components/scroll-area/corner/hooks.ts
|
|
5
|
+
function useScrollAreaCornerState() {
|
|
6
|
+
const ctx = useScrollAreaContext();
|
|
7
|
+
return {
|
|
8
|
+
visible: ctx.cornerVisible,
|
|
9
|
+
attrs: scrollAreaCornerAttrs({ visible: ctx.cornerVisible })
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
export { useScrollAreaCornerState };
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { ScrollAreaActivityState, ScrollAreaContextValue, ScrollAreaOrientation, ScrollAreaType } from "./context/types.js";
|
|
2
|
+
import { useScrollAreaContext } from "./context/hooks.js";
|
|
3
|
+
import "./context/index.js";
|
|
4
|
+
import { ScrollAreaContentProps } from "./content/types.js";
|
|
5
|
+
import { ScrollAreaContent } from "./content/content.js";
|
|
6
|
+
import "./content/index.js";
|
|
7
|
+
import { ScrollAreaCornerProps } from "./corner/types.js";
|
|
8
|
+
import { ScrollAreaCorner } from "./corner/corner.js";
|
|
9
|
+
import "./corner/index.js";
|
|
10
|
+
import { ScrollAreaRootProps } from "./root/types.js";
|
|
11
|
+
import { ScrollAreaRoot } from "./root/root.js";
|
|
12
|
+
import "./root/index.js";
|
|
13
|
+
import { ScrollAreaScrollbarProps } from "./scrollbar/types.js";
|
|
14
|
+
import { ScrollAreaScrollbar } from "./scrollbar/scrollbar.js";
|
|
15
|
+
import "./scrollbar/index.js";
|
|
16
|
+
import { ScrollAreaThumbProps } from "./thumb/types.js";
|
|
17
|
+
import { ScrollAreaThumb } from "./thumb/thumb.js";
|
|
18
|
+
import "./thumb/index.js";
|
|
19
|
+
export declare const ScrollArea: {
|
|
20
|
+
Root: typeof ScrollAreaRoot;
|
|
21
|
+
Content: typeof ScrollAreaContent;
|
|
22
|
+
Scrollbar: typeof ScrollAreaScrollbar;
|
|
23
|
+
Thumb: typeof ScrollAreaThumb;
|
|
24
|
+
Corner: typeof ScrollAreaCorner;
|
|
25
|
+
};
|
|
26
|
+
export { type ScrollAreaActivityState, type ScrollAreaContentProps, type ScrollAreaContextValue, type ScrollAreaCornerProps, type ScrollAreaOrientation, type ScrollAreaRootProps, type ScrollAreaScrollbarProps, type ScrollAreaThumbProps, type ScrollAreaType, useScrollAreaContext };
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { useScrollAreaContext } from "./context/hooks.js";
|
|
2
|
+
import { ScrollAreaContent } from "./content/content.js";
|
|
3
|
+
import { ScrollAreaCorner } from "./corner/corner.js";
|
|
4
|
+
import { ScrollAreaRoot } from "./root/root.js";
|
|
5
|
+
import { ScrollAreaScrollbar } from "./scrollbar/scrollbar.js";
|
|
6
|
+
import { ScrollAreaThumb } from "./thumb/thumb.js";
|
|
7
|
+
// components/scroll-area/index.ts
|
|
8
|
+
const ScrollArea = {
|
|
9
|
+
Root: ScrollAreaRoot,
|
|
10
|
+
Content: ScrollAreaContent,
|
|
11
|
+
Scrollbar: ScrollAreaScrollbar,
|
|
12
|
+
Thumb: ScrollAreaThumb,
|
|
13
|
+
Corner: ScrollAreaCorner
|
|
14
|
+
};
|
|
15
|
+
export { ScrollArea, useScrollAreaContext };
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { applyAttrs } from "../../../utils/apply-attrs.js";
|
|
2
|
+
// components/scroll-area/root/attrs.ts
|
|
3
|
+
const scrollAreaRootAttrStates = [
|
|
4
|
+
"idle",
|
|
5
|
+
"scrolling",
|
|
6
|
+
"hovering",
|
|
7
|
+
"interacting"
|
|
8
|
+
];
|
|
9
|
+
function scrollAreaRootAttrs({ type, idle, scrolling, hovering, interacting, activityState }) {
|
|
10
|
+
return {
|
|
11
|
+
...applyAttrs("scroll-area", scrollAreaRootAttrStates, {
|
|
12
|
+
idle,
|
|
13
|
+
scrolling,
|
|
14
|
+
hovering,
|
|
15
|
+
interacting
|
|
16
|
+
}, { part: "root" }),
|
|
17
|
+
"data-type": type,
|
|
18
|
+
"data-state": activityState
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
export { scrollAreaRootAttrs };
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { useLayoutEffect, useRef } from "react";
|
|
3
|
+
import { useScrollAreaContext, useScrollAreaProviderValue } from "../context/hooks.js";
|
|
4
|
+
import { scrollAreaRootAttrs } from "./attrs.js";
|
|
5
|
+
import { useMergeRefs } from "../../../utils/merge-refs.js";
|
|
6
|
+
// components/scroll-area/root/hooks.ts
|
|
7
|
+
function useScrollAreaRoot({ type = "hover", scrollHideDelay = 600, ...hostProps }) {
|
|
8
|
+
return {
|
|
9
|
+
contextValue: useScrollAreaProviderValue({
|
|
10
|
+
type,
|
|
11
|
+
scrollHideDelay
|
|
12
|
+
}),
|
|
13
|
+
hostProps
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
function useScrollAreaRootHost(ref) {
|
|
17
|
+
const ctx = useScrollAreaContext();
|
|
18
|
+
const { setDir } = ctx;
|
|
19
|
+
const localRef = useRef(null);
|
|
20
|
+
const mergedRef = useMergeRefs([ref, localRef]);
|
|
21
|
+
useLayoutEffect(() => {
|
|
22
|
+
const element = localRef.current;
|
|
23
|
+
if (!element) return;
|
|
24
|
+
const computed = getComputedStyle(element).direction;
|
|
25
|
+
if (computed === "rtl" || computed === "ltr") setDir(computed);
|
|
26
|
+
}, [setDir]);
|
|
27
|
+
return {
|
|
28
|
+
ref: mergedRef,
|
|
29
|
+
attrs: scrollAreaRootAttrs({
|
|
30
|
+
type: ctx.type,
|
|
31
|
+
idle: ctx.activityState === "idle",
|
|
32
|
+
scrolling: ctx.scrolling || ctx.interacting,
|
|
33
|
+
hovering: ctx.hovering,
|
|
34
|
+
interacting: ctx.interacting,
|
|
35
|
+
activityState: ctx.activityState
|
|
36
|
+
}),
|
|
37
|
+
onPointerEnter(event) {
|
|
38
|
+
if (event.pointerType === "mouse" || event.pointerType === "pen") ctx.setHovering(true);
|
|
39
|
+
},
|
|
40
|
+
onPointerLeave() {
|
|
41
|
+
ctx.setHovering(false);
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
export { useScrollAreaRoot, useScrollAreaRootHost };
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { ScrollAreaProvider } from "../context/context.js";
|
|
3
|
+
import { useScrollAreaRoot, useScrollAreaRootHost } 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/root/root.tsx
|
|
8
|
+
function ScrollAreaRootHost({ ref, render, className, children, style, ...props }) {
|
|
9
|
+
const { ref: hostRef, attrs, onPointerEnter, onPointerLeave } = useScrollAreaRootHost(ref);
|
|
10
|
+
return useRender({
|
|
11
|
+
defaultTagName: "div",
|
|
12
|
+
ref: hostRef,
|
|
13
|
+
render,
|
|
14
|
+
props: mergeProps({
|
|
15
|
+
className,
|
|
16
|
+
children,
|
|
17
|
+
style: {
|
|
18
|
+
position: "relative",
|
|
19
|
+
...style
|
|
20
|
+
},
|
|
21
|
+
onPointerEnter,
|
|
22
|
+
onPointerLeave
|
|
23
|
+
}, attrs, props)
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
function ScrollAreaRoot(props) {
|
|
27
|
+
const { contextValue, hostProps } = useScrollAreaRoot(props);
|
|
28
|
+
return /* @__PURE__ */ jsx(ScrollAreaProvider, {
|
|
29
|
+
contextValue,
|
|
30
|
+
children: /* @__PURE__ */ jsx(ScrollAreaRootHost, { ...hostProps })
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
export { ScrollAreaRoot };
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { UseRenderComponentProps } from "../../../utils/use-render.js";
|
|
2
|
+
import "../../../utils/index.js";
|
|
3
|
+
import { ScrollAreaType } from "../context/types.js";
|
|
4
|
+
import "../context/index.js";
|
|
5
|
+
export interface ScrollAreaRootProps extends UseRenderComponentProps<"div"> {
|
|
6
|
+
type?: ScrollAreaType;
|
|
7
|
+
scrollHideDelay?: number;
|
|
8
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
// components/scroll-area/scrollbar/a11y.ts
|
|
2
|
+
function scrollAreaScrollbarA11y({ orientation, contentId, valueNow }) {
|
|
3
|
+
return {
|
|
4
|
+
role: "scrollbar",
|
|
5
|
+
"aria-controls": contentId,
|
|
6
|
+
"aria-orientation": orientation,
|
|
7
|
+
"aria-valuemin": 0,
|
|
8
|
+
"aria-valuemax": 100,
|
|
9
|
+
"aria-valuenow": Math.round(valueNow * 100)
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
export { scrollAreaScrollbarA11y };
|