@codefast/ui 0.5.0-canary.1 → 0.5.0-canary.3

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.
Files changed (74) hide show
  1. package/CHANGELOG.md +39 -0
  2. package/dist/components/attachment.d.mts +136 -0
  3. package/dist/components/attachment.mjs +112 -0
  4. package/dist/components/avatar.mjs +1 -1
  5. package/dist/components/bubble.d.mts +67 -0
  6. package/dist/components/bubble.mjs +58 -0
  7. package/dist/components/card.mjs +2 -2
  8. package/dist/components/carousel.mjs +12 -3
  9. package/dist/components/chart.mjs +2 -1
  10. package/dist/components/empty.mjs +1 -1
  11. package/dist/components/form.mjs +2 -1
  12. package/dist/components/marker.d.mts +49 -0
  13. package/dist/components/marker.mjs +42 -0
  14. package/dist/components/message-scroller.d.mts +89 -0
  15. package/dist/components/message-scroller.mjs +77 -0
  16. package/dist/components/message.d.mts +86 -0
  17. package/dist/components/message.mjs +66 -0
  18. package/dist/components/sidebar.mjs +6 -3
  19. package/dist/hooks/use-animated-value.mjs +3 -1
  20. package/dist/hooks/use-message-scroller-commands.d.mts +39 -0
  21. package/dist/hooks/use-message-scroller-commands.mjs +165 -0
  22. package/dist/hooks/use-message-scroller-controller.d.mts +19 -0
  23. package/dist/hooks/use-message-scroller-controller.mjs +382 -0
  24. package/dist/hooks/use-message-scroller-refs.d.mts +60 -0
  25. package/dist/hooks/use-message-scroller-refs.mjs +77 -0
  26. package/dist/hooks/use-pagination.mjs +18 -12
  27. package/dist/index.d.mts +11 -1
  28. package/dist/index.mjs +11 -2
  29. package/dist/lib/message-scroller/geometry.d.mts +136 -0
  30. package/dist/lib/message-scroller/geometry.mjs +199 -0
  31. package/dist/lib/message-scroller/stores.d.mts +21 -0
  32. package/dist/lib/message-scroller/stores.mjs +52 -0
  33. package/dist/lib/message-scroller/types.d.mts +171 -0
  34. package/dist/lib/message-scroller/types.mjs +53 -0
  35. package/dist/lib/message-scroller/utils.d.mts +7 -0
  36. package/dist/lib/message-scroller/utils.mjs +12 -0
  37. package/dist/lib/use-render.d.mts +43 -0
  38. package/dist/lib/use-render.mjs +84 -0
  39. package/dist/primitives/checkbox-group.mjs +20 -10
  40. package/dist/primitives/message-scroller.d.mts +88 -0
  41. package/dist/primitives/message-scroller.mjs +240 -0
  42. package/dist/variants/attachment.d.mts +42 -0
  43. package/dist/variants/attachment.mjs +42 -0
  44. package/dist/variants/bubble.d.mts +45 -0
  45. package/dist/variants/bubble.mjs +45 -0
  46. package/dist/variants/marker.d.mts +21 -0
  47. package/dist/variants/marker.mjs +18 -0
  48. package/package.json +57 -11
  49. package/src/components/attachment.tsx +284 -0
  50. package/src/components/avatar.tsx +1 -1
  51. package/src/components/bubble.tsx +116 -0
  52. package/src/components/card.tsx +2 -2
  53. package/src/components/carousel.tsx +12 -3
  54. package/src/components/empty.tsx +1 -1
  55. package/src/components/marker.tsx +94 -0
  56. package/src/components/message-scroller.tsx +213 -0
  57. package/src/components/message.tsx +173 -0
  58. package/src/components/sidebar.tsx +11 -4
  59. package/src/css/foundation/tokens.css +6 -18
  60. package/src/css/foundation/utilities.css +451 -0
  61. package/src/css/preset.css +2 -1
  62. package/src/hooks/use-message-scroller-commands.ts +312 -0
  63. package/src/hooks/use-message-scroller-controller.ts +651 -0
  64. package/src/hooks/use-message-scroller-refs.ts +168 -0
  65. package/src/index.ts +72 -0
  66. package/src/lib/message-scroller/geometry.ts +398 -0
  67. package/src/lib/message-scroller/stores.ts +89 -0
  68. package/src/lib/message-scroller/types.ts +310 -0
  69. package/src/lib/message-scroller/utils.ts +15 -0
  70. package/src/lib/use-render.ts +202 -0
  71. package/src/primitives/message-scroller.tsx +399 -0
  72. package/src/variants/attachment.ts +73 -0
  73. package/src/variants/bubble.ts +81 -0
  74. package/src/variants/marker.ts +38 -0
@@ -0,0 +1,53 @@
1
+ //#region src/lib/message-scroller/types.ts
2
+ /**
3
+ * @since 0.5.0-canary.3
4
+ */
5
+ const DEFAULT_SCROLL_EDGE_THRESHOLD = 8;
6
+ /**
7
+ * @since 0.5.0-canary.3
8
+ */
9
+ const DEFAULT_SCROLL_PREVIOUS_ITEM_PEEK = 64;
10
+ /**
11
+ * @since 0.5.0-canary.3
12
+ */
13
+ const DEFAULT_SCROLL_MARGIN = 0;
14
+ /**
15
+ * @since 0.5.0-canary.3
16
+ */
17
+ const SCROLL_POSITION_EPSILON = .5;
18
+ /**
19
+ * @since 0.5.0-canary.3
20
+ */
21
+ const AUTOSCROLLING_CLEAR_DELAY = 180;
22
+ /**
23
+ * @since 0.5.0-canary.3
24
+ */
25
+ const USER_SCROLL_KEYS = /* @__PURE__ */ new Set([
26
+ "ArrowDown",
27
+ "ArrowUp",
28
+ "End",
29
+ "Home",
30
+ "PageDown",
31
+ "PageUp",
32
+ " "
33
+ ]);
34
+ /**
35
+ * @since 0.5.0-canary.3
36
+ */
37
+ const EMPTY_MESSAGE_SCROLLER_SCROLLABLE = {
38
+ start: false,
39
+ end: false
40
+ };
41
+ /**
42
+ * @since 0.5.0-canary.3
43
+ */
44
+ const EMPTY_VISIBLE_MESSAGE_IDS = [];
45
+ /**
46
+ * @since 0.5.0-canary.3
47
+ */
48
+ const EMPTY_MESSAGE_SCROLLER_VISIBILITY_STATE = {
49
+ currentAnchorId: null,
50
+ visibleMessageIds: EMPTY_VISIBLE_MESSAGE_IDS
51
+ };
52
+ //#endregion
53
+ export { AUTOSCROLLING_CLEAR_DELAY, DEFAULT_SCROLL_EDGE_THRESHOLD, DEFAULT_SCROLL_MARGIN, DEFAULT_SCROLL_PREVIOUS_ITEM_PEEK, EMPTY_MESSAGE_SCROLLER_SCROLLABLE, EMPTY_MESSAGE_SCROLLER_VISIBILITY_STATE, EMPTY_VISIBLE_MESSAGE_IDS, SCROLL_POSITION_EPSILON, USER_SCROLL_KEYS };
@@ -0,0 +1,7 @@
1
+ //#region src/lib/message-scroller/utils.d.ts
2
+ /**
3
+ * @since 0.5.0-canary.3
4
+ */
5
+ declare function useLatest<T>(value: T): import("react").RefObject<T>;
6
+ //#endregion
7
+ export { useLatest };
@@ -0,0 +1,12 @@
1
+ import { useRef } from "react";
2
+ //#region src/lib/message-scroller/utils.ts
3
+ /**
4
+ * @since 0.5.0-canary.3
5
+ */
6
+ function useLatest(value) {
7
+ const ref = useRef(value);
8
+ ref.current = value;
9
+ return ref;
10
+ }
11
+ //#endregion
12
+ export { useLatest };
@@ -0,0 +1,43 @@
1
+ import { ComponentPropsWithRef, ElementType, ReactElement, Ref, RefCallback } from "react";
2
+
3
+ //#region src/lib/use-render.d.ts
4
+ type RenderState = Record<string, unknown>;
5
+ type RenderFunction<TState extends RenderState> = (props: Record<string, unknown>, state: TState) => ReactElement | null;
6
+ type RenderProp<TState extends RenderState> = ReactElement | RenderFunction<TState>;
7
+ type StateAttributesMapping<TState extends RenderState> = Partial<{ [K in keyof TState]: (value: TState[K]) => Record<string, string | undefined> | null | undefined }>;
8
+ /**
9
+ * @since 0.5.0-canary.3
10
+ */
11
+ type UseRenderComponentProps<TElement extends ElementType, TState extends RenderState = RenderState> = ComponentPropsWithRef<TElement> & {
12
+ render?: RenderProp<TState>;
13
+ };
14
+ interface UseRenderOptions<TElement extends ElementType, TState extends RenderState> {
15
+ defaultTagName: TElement;
16
+ props?: ComponentPropsWithRef<TElement>;
17
+ render?: RenderProp<TState>;
18
+ state?: TState;
19
+ stateAttributesMapping?: StateAttributesMapping<TState>;
20
+ }
21
+ /**
22
+ * @since 0.5.0-canary.3
23
+ */
24
+ declare function composeRefs<T>(...refs: Array<Ref<T> | undefined>): RefCallback<T> | undefined;
25
+ /**
26
+ * @since 0.5.0-canary.3
27
+ */
28
+ declare function mergeProps<TElement extends ElementType>(...sources: Array<ComponentPropsWithRef<TElement> | Record<string, unknown> | undefined>): ComponentPropsWithRef<TElement>;
29
+ /**
30
+ * Resolve a component's `render` prop: a default element, a render function, or
31
+ * a cloned element, merging computed state attributes and props/refs/handlers.
32
+ *
33
+ * @since 0.5.0-canary.3
34
+ */
35
+ declare function useRender<TElement extends ElementType, TState extends RenderState = RenderState>({
36
+ defaultTagName,
37
+ props,
38
+ render,
39
+ state,
40
+ stateAttributesMapping
41
+ }: UseRenderOptions<TElement, TState>): ReactElement | null;
42
+ //#endregion
43
+ export { type UseRenderComponentProps, composeRefs, mergeProps, useRender };
@@ -0,0 +1,84 @@
1
+ import { cloneElement, createElement, isValidElement } from "react";
2
+ //#region src/lib/use-render.ts
3
+ function isEventHandler(key) {
4
+ return /^on[A-Z]/.test(key);
5
+ }
6
+ function composeEventHandlers(theirs, ours) {
7
+ return function handleEvent(event) {
8
+ theirs(event);
9
+ if (!event.defaultPrevented) ours(event);
10
+ };
11
+ }
12
+ /**
13
+ * @since 0.5.0-canary.3
14
+ */
15
+ function composeRefs(...refs) {
16
+ const validRefs = refs.filter(Boolean);
17
+ if (validRefs.length === 0) return;
18
+ return (value) => {
19
+ for (const ref of validRefs) if (typeof ref === "function") ref(value);
20
+ else if (ref) ref.current = value;
21
+ };
22
+ }
23
+ /**
24
+ * @since 0.5.0-canary.3
25
+ */
26
+ function mergeProps(...sources) {
27
+ const result = {};
28
+ for (const source of sources) {
29
+ if (!source) continue;
30
+ const sourceProps = source;
31
+ for (const key of Object.keys(sourceProps)) {
32
+ const value = sourceProps[key];
33
+ if (value === void 0) continue;
34
+ const current = result[key];
35
+ if (key === "className") result[key] = [current, value].filter(Boolean).join(" ");
36
+ else if (key === "style") result[key] = {
37
+ ...current,
38
+ ...value
39
+ };
40
+ else if (key === "ref") result[key] = composeRefs(current, value);
41
+ else if (isEventHandler(key) && typeof current === "function" && typeof value === "function") result[key] = composeEventHandlers(value, current);
42
+ else result[key] = value;
43
+ }
44
+ }
45
+ return result;
46
+ }
47
+ function getStateAttributes(state, mapping) {
48
+ const props = {};
49
+ for (const key of Object.keys(state)) {
50
+ const value = state[key];
51
+ const attributes = mapping?.[key]?.(value);
52
+ if (attributes) {
53
+ Object.assign(props, attributes);
54
+ continue;
55
+ }
56
+ if (key === "slot") {
57
+ props["data-slot"] = value;
58
+ continue;
59
+ }
60
+ const attribute = `data-${String(key).replace(/[A-Z]/g, (letter) => `-${letter.toLowerCase()}`)}`;
61
+ if (typeof value === "boolean") props[attribute] = value ? "" : void 0;
62
+ else if (value !== null && value !== void 0) props[attribute] = String(value);
63
+ }
64
+ return props;
65
+ }
66
+ /**
67
+ * Resolve a component's `render` prop: a default element, a render function, or
68
+ * a cloned element, merging computed state attributes and props/refs/handlers.
69
+ *
70
+ * @since 0.5.0-canary.3
71
+ */
72
+ function useRender({ defaultTagName, props, render, state = {}, stateAttributesMapping }) {
73
+ const elementProps = mergeProps(getStateAttributes(state, stateAttributesMapping), props);
74
+ if (!render) return createElement(defaultTagName, elementProps);
75
+ if (typeof render === "function") return render(elementProps, state);
76
+ if (!isValidElement(render)) return null;
77
+ const renderProps = render.props;
78
+ return cloneElement(render, {
79
+ ...mergeProps(elementProps, renderProps),
80
+ ref: composeRefs(elementProps.ref, renderProps.ref)
81
+ });
82
+ }
83
+ //#endregion
84
+ export { composeRefs, mergeProps, useRender };
@@ -30,22 +30,32 @@ function CheckboxGroup({ __scopeCheckboxGroup, defaultValue, dir, disabled = fal
30
30
  onChange: onValueChange,
31
31
  prop: valueProperty
32
32
  });
33
+ /**
34
+ * Handles checking a checkbox item
35
+ * @param itemValue - Value of the checkbox being checked
36
+ */
37
+ const handleItemCheck = useCallback((itemValue) => {
38
+ setValue((previousValue) => [...previousValue ?? [], itemValue]);
39
+ }, [setValue]);
40
+ /**
41
+ * Handles unchecking a checkbox item
42
+ * @param itemValue - Value of the checkbox being unchecked
43
+ */
44
+ const handleItemUncheck = useCallback((itemValue) => {
45
+ setValue((previousValue) => {
46
+ const currentValue = previousValue ?? [];
47
+ if (required && currentValue.length === 1 && currentValue[0] === itemValue) return currentValue;
48
+ return currentValue.filter((inputValue) => inputValue !== itemValue);
49
+ });
50
+ }, [setValue, required]);
33
51
  return /* @__PURE__ */ jsx(CheckboxGroupContextProvider, {
34
52
  disabled,
35
53
  name,
36
54
  required,
37
55
  scope: __scopeCheckboxGroup,
38
56
  value,
39
- onItemCheck: useCallback((itemValue) => {
40
- setValue((previousValue) => [...previousValue ?? [], itemValue]);
41
- }, [setValue]),
42
- onItemUncheck: useCallback((itemValue) => {
43
- setValue((previousValue) => {
44
- const currentValue = previousValue ?? [];
45
- if (required && currentValue.length === 1 && currentValue[0] === itemValue) return currentValue;
46
- return currentValue.filter((inputValue) => inputValue !== itemValue);
47
- });
48
- }, [setValue, required]),
57
+ onItemCheck: handleItemCheck,
58
+ onItemUncheck: handleItemUncheck,
49
59
  children: /* @__PURE__ */ jsx(RovingFocus.Root, {
50
60
  asChild: true,
51
61
  ...rovingFocusGroupScope,
@@ -0,0 +1,88 @@
1
+ import { MessageScrollerButtonProps, MessageScrollerContentProps, MessageScrollerItemProps, MessageScrollerProps, MessageScrollerProviderProps, MessageScrollerScrollOptions, MessageScrollerScrollable, MessageScrollerViewportProps, MessageScrollerVisibilityState } from "../lib/message-scroller/types.mjs";
2
+
3
+ //#region src/primitives/message-scroller.d.ts
4
+ /**
5
+ * @since 0.5.0-canary.3
6
+ */
7
+ declare function useMessageScroller(): {
8
+ scrollToEnd: (options?: MessageScrollerScrollOptions) => boolean;
9
+ scrollToMessage: (messageId: string, options?: MessageScrollerScrollOptions) => boolean;
10
+ scrollToStart: (options?: MessageScrollerScrollOptions) => boolean;
11
+ };
12
+ /**
13
+ * @since 0.5.0-canary.3
14
+ */
15
+ declare function useMessageScrollerScrollable(): MessageScrollerScrollable;
16
+ /**
17
+ * @since 0.5.0-canary.3
18
+ */
19
+ declare function useMessageScrollerVisibility(): MessageScrollerVisibilityState;
20
+ /**
21
+ * @since 0.5.0-canary.3
22
+ */
23
+ declare function MessageScrollerProvider({
24
+ autoScroll,
25
+ children,
26
+ defaultScrollPosition,
27
+ scrollEdgeThreshold,
28
+ scrollPreviousItemPeek,
29
+ scrollMargin
30
+ }: MessageScrollerProviderProps): import("react").JSX.Element;
31
+ /**
32
+ * @since 0.5.0-canary.3
33
+ */
34
+ declare function MessageScroller({
35
+ children,
36
+ ...props
37
+ }: MessageScrollerProps): import("react").JSX.Element;
38
+ /**
39
+ * @since 0.5.0-canary.3
40
+ */
41
+ declare function MessageScrollerViewport({
42
+ "aria-label": ariaLabel,
43
+ children,
44
+ onKeyDown,
45
+ onScroll,
46
+ onTouchMove,
47
+ onWheel,
48
+ preserveScrollOnPrepend,
49
+ ref,
50
+ role,
51
+ tabIndex,
52
+ ...props
53
+ }: MessageScrollerViewportProps): import("react").JSX.Element;
54
+ /**
55
+ * @since 0.5.0-canary.3
56
+ */
57
+ declare function MessageScrollerContent({
58
+ "aria-relevant": ariaRelevant,
59
+ children,
60
+ ref,
61
+ role,
62
+ spacerClassName,
63
+ ...props
64
+ }: MessageScrollerContentProps): import("react").JSX.Element;
65
+ /**
66
+ * @since 0.5.0-canary.3
67
+ */
68
+ declare function MessageScrollerItem({
69
+ messageId,
70
+ ref,
71
+ scrollAnchor,
72
+ ...props
73
+ }: MessageScrollerItemProps): import("react").JSX.Element;
74
+ /**
75
+ * @since 0.5.0-canary.3
76
+ */
77
+ declare function MessageScrollerButton({
78
+ behavior,
79
+ children,
80
+ direction,
81
+ onClick,
82
+ render,
83
+ tabIndex,
84
+ type,
85
+ ...props
86
+ }: MessageScrollerButtonProps): import("react").ReactElement<unknown, string | import("react").JSXElementConstructor<any>> | null;
87
+ //#endregion
88
+ export { MessageScroller, MessageScrollerButton, MessageScrollerContent, MessageScrollerItem, MessageScrollerProvider, MessageScrollerViewport, useMessageScroller, useMessageScrollerScrollable, useMessageScrollerVisibility };
@@ -0,0 +1,240 @@
1
+ import { USER_SCROLL_KEYS } from "../lib/message-scroller/types.mjs";
2
+ import { useMessageScrollerController } from "../hooks/use-message-scroller-controller.mjs";
3
+ import { useLatest } from "../lib/message-scroller/utils.mjs";
4
+ import { composeRefs, mergeProps, useRender } from "../lib/use-render.mjs";
5
+ import { jsx, jsxs } from "react/jsx-runtime";
6
+ import { createContext, useCallback, useContext, useEffect, useLayoutEffect, useMemo, useRef, useSyncExternalStore } from "react";
7
+ //#region src/primitives/message-scroller.tsx
8
+ const MessageScrollerContext = createContext(null);
9
+ const MessageScrollerItemContext = createContext(null);
10
+ function useMessageScrollerContext() {
11
+ const context = useContext(MessageScrollerContext);
12
+ if (!context) throw new Error("useMessageScroller must be used within a MessageScroller.");
13
+ return context;
14
+ }
15
+ function useMessageScrollerItemContext() {
16
+ const context = useContext(MessageScrollerItemContext);
17
+ if (!context) throw new Error("MessageScrollerItem must be used within a MessageScroller.");
18
+ return context;
19
+ }
20
+ /**
21
+ * @since 0.5.0-canary.3
22
+ */
23
+ function useMessageScroller() {
24
+ const { scrollToEnd, scrollToMessage, scrollToStart } = useMessageScrollerContext();
25
+ return useMemo(() => ({
26
+ scrollToEnd,
27
+ scrollToMessage,
28
+ scrollToStart
29
+ }), [
30
+ scrollToEnd,
31
+ scrollToMessage,
32
+ scrollToStart
33
+ ]);
34
+ }
35
+ /**
36
+ * @since 0.5.0-canary.3
37
+ */
38
+ function useMessageScrollerScrollable() {
39
+ const { stateStore } = useMessageScrollerContext();
40
+ return useSyncExternalStore(stateStore.subscribe, stateStore.getSnapshot, stateStore.getSnapshot);
41
+ }
42
+ /**
43
+ * @since 0.5.0-canary.3
44
+ */
45
+ function useMessageScrollerVisibility() {
46
+ const { observeVisibility, unobserveVisibility, visibilityStore } = useMessageScrollerContext();
47
+ return useSyncExternalStore(useCallback((listener) => visibilityStore.subscribe(listener, observeVisibility, unobserveVisibility), [
48
+ observeVisibility,
49
+ unobserveVisibility,
50
+ visibilityStore
51
+ ]), visibilityStore.getSnapshot, visibilityStore.getSnapshot);
52
+ }
53
+ /**
54
+ * @since 0.5.0-canary.3
55
+ */
56
+ function MessageScrollerProvider({ autoScroll = false, children, defaultScrollPosition = "end", scrollEdgeThreshold, scrollPreviousItemPeek, scrollMargin }) {
57
+ const { context, registerMessage } = useMessageScrollerController({
58
+ autoScroll,
59
+ defaultScrollPosition,
60
+ scrollEdgeThreshold,
61
+ scrollPreviousItemPeek,
62
+ scrollMargin
63
+ });
64
+ return /* @__PURE__ */ jsx(MessageScrollerContext.Provider, {
65
+ value: context,
66
+ children: /* @__PURE__ */ jsx(MessageScrollerItemContext.Provider, {
67
+ value: registerMessage,
68
+ children
69
+ })
70
+ });
71
+ }
72
+ /**
73
+ * @since 0.5.0-canary.3
74
+ */
75
+ function MessageScroller({ children, ...props }) {
76
+ const { setRootElement } = useMessageScrollerContext();
77
+ return /* @__PURE__ */ jsx("div", {
78
+ ref: setRootElement,
79
+ ...props,
80
+ children
81
+ });
82
+ }
83
+ /**
84
+ * @since 0.5.0-canary.3
85
+ */
86
+ function MessageScrollerViewport({ "aria-label": ariaLabel, children, onKeyDown, onScroll, onTouchMove, onWheel, preserveScrollOnPrepend = true, ref, role, tabIndex, ...props }) {
87
+ const { handleResize, preserveScrollOnPrependRef, setViewportElement, syncAfterScroll, userScrollIntent, viewportRef } = useMessageScrollerContext();
88
+ preserveScrollOnPrependRef.current = preserveScrollOnPrepend;
89
+ const setViewportRef = useCallback((element) => {
90
+ setViewportElement(element);
91
+ composeRefs(ref)?.(element);
92
+ }, [ref, setViewportElement]);
93
+ function handleScroll(event) {
94
+ syncAfterScroll();
95
+ onScroll?.(event);
96
+ }
97
+ function handleWheel(event) {
98
+ userScrollIntent();
99
+ onWheel?.(event);
100
+ }
101
+ function handleTouchMove(event) {
102
+ userScrollIntent();
103
+ onTouchMove?.(event);
104
+ }
105
+ function handleKeyDown(event) {
106
+ if (USER_SCROLL_KEYS.has(event.key)) userScrollIntent();
107
+ onKeyDown?.(event);
108
+ }
109
+ useEffect(() => {
110
+ const viewport = viewportRef.current;
111
+ if (!viewport || typeof ResizeObserver === "undefined") return;
112
+ const observer = new ResizeObserver(handleResize);
113
+ observer.observe(viewport);
114
+ return () => observer.disconnect();
115
+ }, [handleResize, viewportRef]);
116
+ return /* @__PURE__ */ jsx("div", {
117
+ ref: setViewportRef,
118
+ role: role ?? "region",
119
+ "aria-label": ariaLabel ?? "Messages",
120
+ tabIndex: tabIndex ?? 0,
121
+ onKeyDown: handleKeyDown,
122
+ onScroll: handleScroll,
123
+ onTouchMove: handleTouchMove,
124
+ onWheel: handleWheel,
125
+ ...props,
126
+ children
127
+ });
128
+ }
129
+ /**
130
+ * @since 0.5.0-canary.3
131
+ */
132
+ function MessageScrollerContent({ "aria-relevant": ariaRelevant, children, ref, role, spacerClassName, ...props }) {
133
+ const { handleContentChange, handleResize, setContentElement, setSpacerElement } = useMessageScrollerContext();
134
+ const contentRef = useRef(null);
135
+ const setContentRef = useCallback((element) => {
136
+ contentRef.current = element;
137
+ setContentElement(element);
138
+ composeRefs(ref)?.(element);
139
+ }, [ref, setContentElement]);
140
+ useLayoutEffect(() => {
141
+ const content = contentRef.current;
142
+ if (!content) return;
143
+ handleContentChange();
144
+ if (typeof MutationObserver === "undefined") return;
145
+ const observer = new MutationObserver(() => {
146
+ handleContentChange();
147
+ });
148
+ observer.observe(content, { childList: true });
149
+ return () => observer.disconnect();
150
+ }, [handleContentChange]);
151
+ useEffect(() => {
152
+ const content = contentRef.current;
153
+ if (!content || typeof ResizeObserver === "undefined") return;
154
+ const observer = new ResizeObserver(handleResize);
155
+ observer.observe(content);
156
+ return () => observer.disconnect();
157
+ }, [handleResize]);
158
+ return /* @__PURE__ */ jsxs("div", {
159
+ ref: setContentRef,
160
+ role: role ?? "log",
161
+ "aria-relevant": ariaRelevant ?? "additions",
162
+ ...props,
163
+ children: [children, /* @__PURE__ */ jsx("div", {
164
+ ref: setSpacerElement,
165
+ "aria-hidden": "true",
166
+ "data-message-scroller-spacer": "",
167
+ hidden: true,
168
+ className: spacerClassName
169
+ })]
170
+ });
171
+ }
172
+ /**
173
+ * @since 0.5.0-canary.3
174
+ */
175
+ function MessageScrollerItem({ messageId, ref, scrollAnchor = false, ...props }) {
176
+ const registerMessage = useMessageScrollerItemContext();
177
+ const elementRef = useRef(null);
178
+ return /* @__PURE__ */ jsx("div", {
179
+ ref: useCallback((element) => {
180
+ const previousElement = elementRef.current;
181
+ elementRef.current = element;
182
+ if (messageId) registerMessage(messageId, element, previousElement);
183
+ composeRefs(ref)?.(element);
184
+ }, [
185
+ messageId,
186
+ ref,
187
+ registerMessage
188
+ ]),
189
+ "data-message-id": messageId,
190
+ "data-scroll-anchor": scrollAnchor ? "true" : "false",
191
+ ...props
192
+ });
193
+ }
194
+ /**
195
+ * @since 0.5.0-canary.3
196
+ */
197
+ function MessageScrollerButton({ behavior = "smooth", children, direction = "end", onClick, render, tabIndex, type = "button", ...props }) {
198
+ const { scrollToEnd, scrollToStart, stateStore } = useMessageScrollerContext();
199
+ const onClickRef = useLatest(onClick);
200
+ const subscribe = useCallback((listener) => stateStore.subscribe(listener), [stateStore]);
201
+ const getSnapshot = useCallback(() => {
202
+ const state = stateStore.getSnapshot();
203
+ return direction === "start" ? state.start : state.end;
204
+ }, [direction, stateStore]);
205
+ const isActive = useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
206
+ const handleClick = useCallback((event) => {
207
+ if (!isActive) return;
208
+ onClickRef.current?.(event);
209
+ if (!event.defaultPrevented) {
210
+ event.currentTarget.blur();
211
+ if (direction === "start") scrollToStart({ behavior });
212
+ else scrollToEnd({ behavior });
213
+ }
214
+ }, [
215
+ behavior,
216
+ direction,
217
+ isActive,
218
+ onClickRef,
219
+ scrollToEnd,
220
+ scrollToStart
221
+ ]);
222
+ return useRender({
223
+ defaultTagName: "button",
224
+ props: mergeProps({
225
+ type,
226
+ inert: !isActive,
227
+ tabIndex: isActive ? tabIndex : -1,
228
+ children: children ?? /* @__PURE__ */ jsxs("span", { children: ["Scroll to ", direction] }),
229
+ onClick: handleClick
230
+ }, props),
231
+ render,
232
+ state: {
233
+ active: isActive,
234
+ direction
235
+ },
236
+ stateAttributesMapping: { active: (value) => ({ "data-active": value ? "true" : "false" }) }
237
+ });
238
+ }
239
+ //#endregion
240
+ export { MessageScroller, MessageScrollerButton, MessageScrollerContent, MessageScrollerItem, MessageScrollerProvider, MessageScrollerViewport, useMessageScroller, useMessageScrollerScrollable, useMessageScrollerVisibility };
@@ -0,0 +1,42 @@
1
+ import { VariantProps } from "../lib/utils.mjs";
2
+
3
+ //#region src/variants/attachment.d.ts
4
+ /**
5
+ * File/media attachment card. Size drives padding for the media and content
6
+ * slots; orientation switches between a horizontal row and a vertical tile.
7
+ *
8
+ * @since 0.5.0-canary.3
9
+ */
10
+ declare const attachmentVariants: import("@codefast/tailwind-variants").VariantResolver<{
11
+ size: {
12
+ default: string;
13
+ sm: string;
14
+ xs: string;
15
+ };
16
+ orientation: {
17
+ horizontal: string;
18
+ vertical: string;
19
+ };
20
+ }, Record<string, never>>;
21
+ /**
22
+ * @since 0.5.0-canary.3
23
+ */
24
+ type AttachmentVariants = VariantProps<typeof attachmentVariants>;
25
+ /**
26
+ * Media thumbnail — an icon tile or a cover image that reacts to attachment
27
+ * size, orientation, and upload state via the parent `group/attachment`.
28
+ *
29
+ * @since 0.5.0-canary.3
30
+ */
31
+ declare const attachmentMediaVariants: import("@codefast/tailwind-variants").VariantResolver<{
32
+ variant: {
33
+ icon: string;
34
+ image: string;
35
+ };
36
+ }, Record<string, never>>;
37
+ /**
38
+ * @since 0.5.0-canary.3
39
+ */
40
+ type AttachmentMediaVariants = VariantProps<typeof attachmentMediaVariants>;
41
+ //#endregion
42
+ export { type AttachmentMediaVariants, type AttachmentVariants, attachmentMediaVariants, attachmentVariants };
@@ -0,0 +1,42 @@
1
+ import { tv } from "../lib/utils.mjs";
2
+ //#region src/variants/attachment.ts
3
+ /**
4
+ * File/media attachment card. Size drives padding for the media and content
5
+ * slots; orientation switches between a horizontal row and a vertical tile.
6
+ *
7
+ * @since 0.5.0-canary.3
8
+ */
9
+ const attachmentVariants = tv({
10
+ base: "group/attachment relative flex w-fit max-w-full min-w-0 shrink-0 flex-wrap rounded-xl border bg-card text-card-foreground transition-colors focus-within:ring-1 focus-within:ring-ring/50 has-[>a,>button]:hover:bg-muted/50 data-[state=error]:border-destructive/30 data-[state=idle]:border-dashed",
11
+ defaultVariants: {
12
+ orientation: "horizontal",
13
+ size: "default"
14
+ },
15
+ variants: {
16
+ size: {
17
+ default: "gap-2 text-sm has-data-[slot=attachment-content]:px-2.5 has-data-[slot=attachment-content]:py-2 has-data-[slot=attachment-media]:p-2",
18
+ sm: "gap-2.5 text-xs has-data-[slot=attachment-content]:px-2 has-data-[slot=attachment-content]:py-1.5 has-data-[slot=attachment-media]:p-1.5",
19
+ xs: "gap-1.5 rounded-lg text-xs has-data-[slot=attachment-content]:px-1.5 has-data-[slot=attachment-content]:py-1 has-data-[slot=attachment-media]:p-1"
20
+ },
21
+ orientation: {
22
+ horizontal: "min-w-40 items-center",
23
+ vertical: "w-24 flex-col has-data-[slot=attachment-content]:w-30"
24
+ }
25
+ }
26
+ });
27
+ /**
28
+ * Media thumbnail — an icon tile or a cover image that reacts to attachment
29
+ * size, orientation, and upload state via the parent `group/attachment`.
30
+ *
31
+ * @since 0.5.0-canary.3
32
+ */
33
+ const attachmentMediaVariants = tv({
34
+ base: "relative flex aspect-square w-10 shrink-0 items-center justify-center overflow-hidden rounded-lg bg-muted text-foreground group-data-[orientation=vertical]/attachment:w-full group-data-[size=sm]/attachment:w-8 group-data-[size=xs]/attachment:w-7 group-data-[size=xs]/attachment:rounded-md group-data-[state=error]/attachment:bg-destructive/10 group-data-[state=error]/attachment:text-destructive group-data-[orientation=vertical]/attachment:*:data-[slot=spinner]:size-6! [&_svg]:pointer-events-none [&_svg:not([class*='size-'])]:size-4 group-data-[orientation=vertical]/attachment:[&_svg:not([class*='size-'])]:size-6 group-data-[size=xs]/attachment:[&_svg:not([class*='size-'])]:size-3.5",
35
+ defaultVariants: { variant: "icon" },
36
+ variants: { variant: {
37
+ icon: "",
38
+ image: "opacity-60 group-data-[state=done]/attachment:opacity-100 group-data-[state=idle]/attachment:opacity-100 *:[img]:aspect-square *:[img]:w-full *:[img]:object-cover"
39
+ } }
40
+ });
41
+ //#endregion
42
+ export { attachmentMediaVariants, attachmentVariants };