@carbonid1/design-system 5.12.0 → 5.14.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -5,6 +5,7 @@ import { cn } from '../helpers/cn/cn';
5
5
  const DEFAULT_SCROLL_EDGE_THRESHOLD = 8;
6
6
  const DEFAULT_SCROLL_MARGIN = 0;
7
7
  const DEFAULT_SCROLL_PREVIOUS_ITEM_PEEK = 64;
8
+ const SCROLL_ANCHOR_TOLERANCE = 4;
8
9
  const SCROLL_TOLERANCE = 0.5;
9
10
  const USER_SCROLL_INTENT_KEYS = new Set([
10
11
  'ArrowDown',
@@ -183,6 +184,7 @@ function useMessageScrollerEngine({ autoFollow, defaultScrollPosition, scrollEdg
183
184
  const scrollPreviousItemPeekRef = useRef(scrollPreviousItemPeek);
184
185
  const modeRef = useRef(autoFollow ? 'following-bottom' : 'free-scrolling');
185
186
  const autoscrollingRef = useRef(false);
187
+ const userScrollIntentRef = useRef(false);
186
188
  // Smooth bottom scrolls can briefly measure as away from bottom while still settling.
187
189
  const bottomFollowIntentRef = useRef(autoFollow);
188
190
  const ignoreAnchoredBottomResumeFrameRef = useRef(null);
@@ -264,6 +266,9 @@ function useMessageScrollerEngine({ autoFollow, defaultScrollPosition, scrollEdg
264
266
  const commitStatus = useCallback(() => {
265
267
  const status = measureStatus();
266
268
  const isAnchoredMode = modeRef.current === 'anchored-to-message' || modeRef.current === 'settling-jump';
269
+ if (status.isAtBottom) {
270
+ userScrollIntentRef.current = false;
271
+ }
267
272
  if (autoFollowRef.current && bottomFollowIntentRef.current && !isAnchoredMode) {
268
273
  modeRef.current = 'following-bottom';
269
274
  }
@@ -398,6 +403,7 @@ function useMessageScrollerEngine({ autoFollow, defaultScrollPosition, scrollEdg
398
403
  const scrollToTop = useCallback(({ behavior = 'auto' } = {}) => {
399
404
  clearSpacer();
400
405
  setAutoscrolling(false);
406
+ userScrollIntentRef.current = false;
401
407
  anchoredElementRef.current = null;
402
408
  bottomFollowIntentRef.current = false;
403
409
  modeRef.current = 'free-scrolling';
@@ -409,6 +415,7 @@ function useMessageScrollerEngine({ autoFollow, defaultScrollPosition, scrollEdg
409
415
  return false;
410
416
  }
411
417
  clearSpacer();
418
+ userScrollIntentRef.current = false;
412
419
  anchoredElementRef.current = null;
413
420
  modeRef.current = autoFollowRef.current ? 'following-bottom' : 'free-scrolling';
414
421
  bottomFollowIntentRef.current =
@@ -426,6 +433,7 @@ function useMessageScrollerEngine({ autoFollow, defaultScrollPosition, scrollEdg
426
433
  }
427
434
  clearSpacer();
428
435
  setAutoscrolling(false);
436
+ userScrollIntentRef.current = false;
429
437
  bottomFollowIntentRef.current = false;
430
438
  const effectiveScrollMargin = scrollMarginOption ??
431
439
  scrollMarginRef.current + (keepPreviousItemPeek ? scrollPreviousItemPeekRef.current : 0);
@@ -448,6 +456,34 @@ function useMessageScrollerEngine({ autoFollow, defaultScrollPosition, scrollEdg
448
456
  setAutoscrolling,
449
457
  setSpacerHeight,
450
458
  ]);
459
+ const scrollToAnchorElement = useCallback((element, options = {}) => scrollToElement(element, {
460
+ align: 'start',
461
+ behavior: options.behavior,
462
+ keepPreviousItemPeek: true,
463
+ scrollMargin: options.scrollMargin,
464
+ }), [scrollToElement]);
465
+ const scrollToLatestAnchor = useCallback(({ behavior = 'auto', scrollMargin: scrollMarginOption, } = {}) => {
466
+ const contentElement = contentElementRef.current;
467
+ const viewportElement = viewportElementRef.current;
468
+ if (!contentElement || !viewportElement) {
469
+ return false;
470
+ }
471
+ const latestAnchorElement = getLastAnchorElement(getMessageElements(contentElement, spacerElementRef.current));
472
+ if (!latestAnchorElement) {
473
+ return scrollToBottom({ behavior });
474
+ }
475
+ const effectiveScrollMargin = scrollMarginOption ?? scrollMarginRef.current + scrollPreviousItemPeekRef.current;
476
+ const anchorTop = getElementViewportTop(latestAnchorElement, viewportElement);
477
+ handledAnchorElementsRef.current.add(latestAnchorElement);
478
+ defaultScrollPositionAppliedRef.current = true;
479
+ if (anchorTop > effectiveScrollMargin + SCROLL_ANCHOR_TOLERANCE) {
480
+ return scrollToAnchorElement(latestAnchorElement, {
481
+ behavior,
482
+ scrollMargin: scrollMarginOption,
483
+ });
484
+ }
485
+ return scrollToBottom({ behavior });
486
+ }, [scrollToAnchorElement, scrollToBottom]);
451
487
  const flushPendingScrollToMessage = useCallback(() => {
452
488
  const pendingScrollToMessage = pendingScrollToMessageRef.current;
453
489
  if (!pendingScrollToMessage) {
@@ -530,17 +566,13 @@ function useMessageScrollerEngine({ autoFollow, defaultScrollPosition, scrollEdg
530
566
  if (defaultScrollPositionRef.current === 'last-anchor') {
531
567
  const lastAnchorElement = getLastAnchorElement(messageElements);
532
568
  if (lastAnchorElement) {
533
- return scrollToElement(lastAnchorElement, {
534
- align: 'start',
535
- behavior: 'auto',
536
- keepPreviousItemPeek: true,
537
- });
569
+ return scrollToAnchorElement(lastAnchorElement);
538
570
  }
539
571
  }
540
572
  const didScroll = scrollToBottom({ behavior: 'auto' });
541
573
  scheduleDefaultBottomScroll();
542
574
  return didScroll;
543
- }, [scheduleDefaultBottomScroll, scrollToBottom, scrollToElement, scrollToTop]);
575
+ }, [scheduleDefaultBottomScroll, scrollToAnchorElement, scrollToBottom, scrollToTop]);
544
576
  const restorePrependAnchor = useCallback(() => {
545
577
  const restore = prependRestoreRef.current;
546
578
  const viewportElement = viewportElementRef.current;
@@ -565,12 +597,8 @@ function useMessageScrollerEngine({ autoFollow, defaultScrollPosition, scrollEdg
565
597
  modeRef.current !== 'anchored-to-message') {
566
598
  return false;
567
599
  }
568
- return scrollToElement(anchoredElement, {
569
- align: 'start',
570
- behavior: 'auto',
571
- keepPreviousItemPeek: true,
572
- });
573
- }, [scrollToElement]);
600
+ return scrollToAnchorElement(anchoredElement);
601
+ }, [scrollToAnchorElement]);
574
602
  const handleContentChange = useCallback(() => {
575
603
  const contentElement = contentElementRef.current;
576
604
  if (!contentElement) {
@@ -591,12 +619,10 @@ function useMessageScrollerEngine({ autoFollow, defaultScrollPosition, scrollEdg
591
619
  const nextAnchorElement = getFirstUnhandledAnchorElement(messageElements, handledAnchorElementsRef.current);
592
620
  if (nextAnchorElement) {
593
621
  handledAnchorElementsRef.current.add(nextAnchorElement);
594
- scrollToElement(nextAnchorElement, {
595
- align: 'start',
596
- behavior: 'auto',
597
- keepPreviousItemPeek: true,
598
- });
599
- return;
622
+ if (!userScrollIntentRef.current) {
623
+ scrollToAnchorElement(nextAnchorElement);
624
+ return;
625
+ }
600
626
  }
601
627
  if (autoFollowRef.current && modeRef.current === 'following-bottom') {
602
628
  scrollToBottom({ behavior: 'auto' });
@@ -617,7 +643,7 @@ function useMessageScrollerEngine({ autoFollow, defaultScrollPosition, scrollEdg
617
643
  reanchorToAnchoredMessage,
618
644
  restorePrependAnchor,
619
645
  scrollToBottom,
620
- scrollToElement,
646
+ scrollToAnchorElement,
621
647
  syncAfterScroll,
622
648
  ]);
623
649
  const handleResize = useCallback(() => {
@@ -643,11 +669,13 @@ function useMessageScrollerEngine({ autoFollow, defaultScrollPosition, scrollEdg
643
669
  modeRef.current = 'following-bottom';
644
670
  autoscrollingRef.current = false;
645
671
  bottomFollowIntentRef.current = false;
672
+ userScrollIntentRef.current = false;
646
673
  anchoredElementRef.current = null;
647
674
  clearSpacer();
648
675
  syncAfterScroll();
649
676
  }, [clearSpacer, measureStatus, syncAfterScroll]);
650
677
  const markUserScrollIntent = useCallback(() => {
678
+ userScrollIntentRef.current = true;
651
679
  if (modeRef.current === 'following-bottom' ||
652
680
  modeRef.current === 'anchored-to-message' ||
653
681
  modeRef.current === 'settling-jump') {
@@ -688,6 +716,7 @@ function useMessageScrollerEngine({ autoFollow, defaultScrollPosition, scrollEdg
688
716
  handledAnchorElementsRef.current = new WeakSet();
689
717
  anchoredElementRef.current = null;
690
718
  bottomFollowIntentRef.current = false;
719
+ userScrollIntentRef.current = false;
691
720
  defaultScrollPositionAppliedRef.current = false;
692
721
  prependRestoreRef.current = null;
693
722
  ignoreAnchoredBottomResumeRef.current = false;
@@ -718,6 +747,7 @@ function useMessageScrollerEngine({ autoFollow, defaultScrollPosition, scrollEdg
718
747
  preserveScrollOnPrependRef,
719
748
  registerMessage,
720
749
  scrollToBottom,
750
+ scrollToLatestAnchor,
721
751
  scrollToMessage,
722
752
  scrollToTop,
723
753
  setContentElement,
@@ -734,6 +764,7 @@ function useMessageScrollerEngine({ autoFollow, defaultScrollPosition, scrollEdg
734
764
  markUserScrollIntent,
735
765
  registerMessage,
736
766
  scrollToBottom,
767
+ scrollToLatestAnchor,
737
768
  scrollToMessage,
738
769
  scrollToTop,
739
770
  setContentElement,
@@ -890,8 +921,8 @@ function MessageScrollerItem({ children, messageId, ref, scrollAnchor = false, .
890
921
  }
891
922
  /** Headless transcript scroller shared by carbonid1 apps; consumers own row rendering. */
892
923
  function useMessageScroller() {
893
- const { scrollToBottom, scrollToMessage, scrollToTop } = useMessageScrollerContext('useMessageScroller');
894
- return useMemo(() => ({ scrollToBottom, scrollToMessage, scrollToTop }), [scrollToBottom, scrollToMessage, scrollToTop]);
924
+ const { scrollToBottom, scrollToLatestAnchor, scrollToMessage, scrollToTop } = useMessageScrollerContext('useMessageScroller');
925
+ return useMemo(() => ({ scrollToBottom, scrollToLatestAnchor, scrollToMessage, scrollToTop }), [scrollToBottom, scrollToLatestAnchor, scrollToMessage, scrollToTop]);
895
926
  }
896
927
  function useMessageScrollerStatus() {
897
928
  const { statusStore } = useMessageScrollerContext('useMessageScrollerStatus');
@@ -6,8 +6,10 @@ export type MessageScrollerScrollOptions = {
6
6
  behavior?: ScrollBehavior;
7
7
  scrollMargin?: number;
8
8
  };
9
+ export type MessageScrollerLatestAnchorOptions = Pick<MessageScrollerScrollOptions, 'behavior' | 'scrollMargin'>;
9
10
  export type MessageScrollerController = {
10
11
  scrollToBottom: (options?: MessageScrollerScrollOptions) => boolean;
12
+ scrollToLatestAnchor: (options?: MessageScrollerLatestAnchorOptions) => boolean;
11
13
  scrollToMessage: (messageId: string, options?: MessageScrollerScrollOptions) => boolean;
12
14
  scrollToTop: (options?: MessageScrollerScrollOptions) => boolean;
13
15
  };
@@ -0,0 +1,9 @@
1
+ import type { ScrollAreaContentProps, ScrollAreaCornerProps, ScrollAreaRootProps, ScrollAreaScrollbarProps, ScrollAreaThumbProps, ScrollAreaViewportProps } from './ScrollArea.types';
2
+ export declare const ScrollArea: {
3
+ Root: ({ className, ...props }: ScrollAreaRootProps) => import("react/jsx-runtime").JSX.Element;
4
+ Viewport: ({ className, ...props }: ScrollAreaViewportProps) => import("react/jsx-runtime").JSX.Element;
5
+ Content: ({ className, ...props }: ScrollAreaContentProps) => import("react/jsx-runtime").JSX.Element;
6
+ Scrollbar: ({ className, orientation, ...props }: ScrollAreaScrollbarProps) => import("react/jsx-runtime").JSX.Element;
7
+ Thumb: ({ className, ...props }: ScrollAreaThumbProps) => import("react/jsx-runtime").JSX.Element;
8
+ Corner: ({ className, ...props }: ScrollAreaCornerProps) => import("react/jsx-runtime").JSX.Element;
9
+ };
@@ -0,0 +1,19 @@
1
+ 'use client';
2
+ import { jsx as _jsx } from "react/jsx-runtime";
3
+ import { ScrollArea as BaseScrollArea } from '@base-ui/react/scroll-area';
4
+ import { cn } from '../helpers/cn/cn';
5
+ const scrollbarClasses = 'm-1 flex touch-none select-none justify-center rounded-full opacity-0 transition-opacity delay-100 duration-150 data-[hovering]:delay-0 data-[hovering]:opacity-100 data-[scrolling]:opacity-100 motion-reduce:transition-none';
6
+ const Root = ({ className, ...props }) => (_jsx(BaseScrollArea.Root, { className: cn('relative overflow-hidden', className), ...props }));
7
+ const Viewport = ({ className, ...props }) => (_jsx(BaseScrollArea.Viewport, { className: cn('focus-visible:ring-ring/50 h-full w-full overscroll-contain rounded-[inherit] outline-hidden focus-visible:ring-2', className), ...props }));
8
+ const Content = ({ className, ...props }) => (_jsx(BaseScrollArea.Content, { className: cn(className), ...props }));
9
+ const Scrollbar = ({ className, orientation = 'vertical', ...props }) => (_jsx(BaseScrollArea.Scrollbar, { orientation: orientation, className: cn(scrollbarClasses, orientation === 'vertical' && 'w-1.5', orientation === 'horizontal' && 'h-1.5 flex-col', className), ...props }));
10
+ const Thumb = ({ className, ...props }) => (_jsx(BaseScrollArea.Thumb, { className: cn('bg-muted-foreground/30 rounded-full data-[orientation=horizontal]:h-full data-[orientation=vertical]:w-full', className), ...props }));
11
+ const Corner = ({ className, ...props }) => (_jsx(BaseScrollArea.Corner, { className: cn('bg-transparent', className), ...props }));
12
+ export const ScrollArea = {
13
+ Root,
14
+ Viewport,
15
+ Content,
16
+ Scrollbar,
17
+ Thumb,
18
+ Corner,
19
+ };
@@ -0,0 +1,20 @@
1
+ import type { ScrollAreaContentProps as BaseContentProps, ScrollAreaCornerProps as BaseCornerProps, ScrollAreaRootProps as BaseRootProps, ScrollAreaScrollbarProps as BaseScrollbarProps, ScrollAreaThumbProps as BaseThumbProps, ScrollAreaViewportProps as BaseViewportProps } from '@base-ui/react/scroll-area';
2
+ import type { Ref } from 'react';
3
+ export type ScrollAreaRootProps = BaseRootProps & {
4
+ ref?: Ref<HTMLDivElement>;
5
+ };
6
+ export type ScrollAreaViewportProps = BaseViewportProps & {
7
+ ref?: Ref<HTMLDivElement>;
8
+ };
9
+ export type ScrollAreaContentProps = BaseContentProps & {
10
+ ref?: Ref<HTMLDivElement>;
11
+ };
12
+ export type ScrollAreaScrollbarProps = BaseScrollbarProps & {
13
+ ref?: Ref<HTMLDivElement>;
14
+ };
15
+ export type ScrollAreaThumbProps = BaseThumbProps & {
16
+ ref?: Ref<HTMLDivElement>;
17
+ };
18
+ export type ScrollAreaCornerProps = BaseCornerProps & {
19
+ ref?: Ref<HTMLDivElement>;
20
+ };
@@ -0,0 +1 @@
1
+ export {};
package/dist/index.d.ts CHANGED
@@ -11,6 +11,8 @@ export { ProgressRing } from './ProgressRing/ProgressRing';
11
11
  export { Slider } from './Slider/Slider';
12
12
  export { SegmentedControl } from './SegmentedControl/SegmentedControl';
13
13
  export type { SegmentedControlItemProps, SegmentedControlRootProps, SegmentedControlSize, } from './SegmentedControl/SegmentedControl.types';
14
+ export { ScrollArea } from './ScrollArea/ScrollArea';
15
+ export type { ScrollAreaContentProps, ScrollAreaCornerProps, ScrollAreaRootProps, ScrollAreaScrollbarProps, ScrollAreaThumbProps, ScrollAreaViewportProps, } from './ScrollArea/ScrollArea.types';
14
16
  export { ContextMenu } from './ContextMenu/ContextMenu';
15
17
  export { Menu } from './Menu/Menu';
16
18
  export { Dialog } from './Dialog/Dialog';
@@ -21,7 +23,7 @@ export type { Column, TableProps } from './Table/Table.types';
21
23
  export { TableScrollRegion } from './TableScrollRegion/TableScrollRegion';
22
24
  export type { TableScrollRegionProps } from './TableScrollRegion/TableScrollRegion.types';
23
25
  export { MessageScroller, useMessageScroller, useMessageScrollerStatus, useMessageScrollerVisibility, } from './MessageScroller/MessageScroller';
24
- export type { MessageScrollerController, MessageScrollerContentProps, MessageScrollerDefaultScrollPosition, MessageScrollerItemProps, MessageScrollerProviderProps, MessageScrollerRootProps, MessageScrollerScrollAlign, MessageScrollerScrollOptions, MessageScrollerStatus, MessageScrollerViewportProps, MessageScrollerVisibility, } from './MessageScroller/MessageScroller.types';
26
+ export type { MessageScrollerController, MessageScrollerContentProps, MessageScrollerDefaultScrollPosition, MessageScrollerItemProps, MessageScrollerLatestAnchorOptions, MessageScrollerProviderProps, MessageScrollerRootProps, MessageScrollerScrollAlign, MessageScrollerScrollOptions, MessageScrollerStatus, MessageScrollerViewportProps, MessageScrollerVisibility, } from './MessageScroller/MessageScroller.types';
25
27
  export { Tooltip } from './Tooltip/Tooltip';
26
28
  export { ThemeProvider } from './ThemeProvider/ThemeProvider';
27
29
  export { ThemeCycler } from './ThemeCycler/ThemeCycler';
package/dist/index.js CHANGED
@@ -6,6 +6,7 @@ export { Text, textVariants } from './Text/Text';
6
6
  export { ProgressRing } from './ProgressRing/ProgressRing';
7
7
  export { Slider } from './Slider/Slider';
8
8
  export { SegmentedControl } from './SegmentedControl/SegmentedControl';
9
+ export { ScrollArea } from './ScrollArea/ScrollArea';
9
10
  export { ContextMenu } from './ContextMenu/ContextMenu';
10
11
  export { Menu } from './Menu/Menu';
11
12
  export { Dialog } from './Dialog/Dialog';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@carbonid1/design-system",
3
- "version": "5.12.0",
3
+ "version": "5.14.0",
4
4
  "description": "Shared React UI primitives + design tokens (themes, postcss config)",
5
5
  "repository": {
6
6
  "type": "git",