// // Copyright 2026 DXOS.org // // @import-as-namespace import React, { type PropsWithChildren, type RefObject, forwardRef, useCallback, useEffect, useImperativeHandle, useMemo, useRef, useState, } from 'react'; import { addEventListener, combine } from '@dxos/async'; import { useComposedRefs } from '@dxos/react-hooks'; import { mx } from '@dxos/ui-theme'; import { type ThemedClassName } from '@dxos/ui-types'; import { recipes } from '../../recipes.ts'; import { composableProps, slottable } from '../../util/slots.ts'; import { Button } from '../Button/Button.tsx'; import * as ScrollArea from '../ScrollArea/ScrollArea.tsx'; import { ScrollContainerProvider, type ScrollController, useScrollContainerContext } from './ScrollContainerContext.ts'; // Within a pixel: at fractional zoom the three measures round differently, so exact equality never holds at the end. const isBottom = (element: HTMLElement) => Math.abs(element.scrollHeight - element.scrollTop - element.clientHeight) <= 1; // // Root // type ScrollContainerRootProps = PropsWithChildren<{ /** Start pinned to the end, following content as it grows (a log, a chat). */ pin?: boolean; /** How a pinned container follows new content. */ behavior?: ScrollBehavior; }>; /** * Headless: holds the pinned and overflow state its parts share, and exposes a {@link ScrollController} as its ref. * Scrolling up (by wheel) unpins; scrolling back to the end, or `scrollToBottom`, pins again. */ const ScrollContainerRoot = forwardRef( ({ children, pin = false, behavior: defaultBehavior = 'smooth' }, forwardedRef) => { const viewportRef = useRef(null); const [pinned, setPinned] = useState(pin); const [overflow, setOverflow] = useState(false); const [overflowEnd, setOverflowEnd] = useState(false); const controller = useMemo( () => ({ get viewport() { return viewportRef.current; }, scrollToTop: (behavior = defaultBehavior) => { viewportRef.current?.scrollTo({ top: 0, behavior }); setPinned(false); }, scrollToBottom: (behavior = defaultBehavior) => { const viewport = viewportRef.current; viewport?.scrollTo({ top: viewport.scrollHeight, behavior }); setPinned(true); }, }), [defaultBehavior], ); useImperativeHandle(forwardedRef, () => controller, [controller]); const setViewport = useCallback((viewport: HTMLElement | null) => { viewportRef.current = viewport; }, []); return ( {children} ); }, ); ScrollContainerRoot.displayName = 'ScrollContainer.Root'; // // Content // type ScrollContainerContentProps = Pick; /** The frame: a `ScrollArea.Root` that also positions the Fade and the ScrollDownButton over the viewport. */ const ScrollContainerContent = slottable( ({ children, ...props }, forwardedRef) => ( {children} ), ); ScrollContainerContent.displayName = 'ScrollContainer.Content'; // // Viewport // type ScrollContainerViewportProps = {}; /** The scrolling element (`ScrollArea.Viewport`); while pinned it follows its children as they are added or grow. */ const ScrollContainerViewport = slottable( ({ children, asChild, ...props }, forwardedRef) => { const viewportRef = useRef(null); const ref = useComposedRefs(forwardedRef, viewportRef); const { setViewport, setPinned, setOverflow, setOverflowEnd } = useScrollContainerContext('ScrollContainer.Viewport'); useEffect(() => { const viewport = viewportRef.current; if (!viewport) { return; } setViewport(viewport); const measure = () => { setOverflow(viewport.scrollTop > 0); setOverflowEnd(!isBottom(viewport)); }; measure(); // Content added below changes what is hidden at the end without any scroll event. const mutationObserver = new MutationObserver(measure); mutationObserver.observe(viewport, { childList: true }); return combine( // Only a user's wheel decides pinning: a programmatic scroll to the end must not unpin midway. addEventListener(viewport, 'wheel', () => setPinned(isBottom(viewport))), addEventListener(viewport, 'scroll', measure), () => mutationObserver.disconnect(), () => setViewport(null), ); }, [setViewport, setPinned, setOverflow, setOverflowEnd]); return ( <> {children} ); }, ); ScrollContainerViewport.displayName = 'ScrollContainer.Viewport'; /** Kept apart from Viewport so that pinning does not re-render the viewport's children. */ const ScrollContainerPinEffect = ({ viewportRef }: { viewportRef: RefObject }) => { const { pinned, controller } = useScrollContainerContext('ScrollContainer.PinEffect'); useEffect(() => { const viewport = viewportRef.current; if (!pinned || !viewport) { return; } controller.scrollToBottom('instant'); // The viewport keeps its size as content grows, so its children are what is observed (e.g. a streaming row). const resizeObserver = new ResizeObserver(() => controller.scrollToBottom()); Array.from(viewport.children).forEach((child) => resizeObserver.observe(child)); const mutationObserver = new MutationObserver((mutations) => { mutations.forEach((mutation) => mutation.addedNodes.forEach((node) => node instanceof Element && resizeObserver.observe(node)), ); controller.scrollToBottom(); }); mutationObserver.observe(viewport, { childList: true }); return () => { resizeObserver.disconnect(); mutationObserver.disconnect(); }; }, [pinned, controller, viewportRef]); return null; }; // // Fade // type ScrollContainerFadeProps = ThemedClassName<{ /** The edge the gradient covers. */ edge?: 'top' | 'bottom'; }>; /** A gradient from the surface over an edge, shown while content is hidden past it. */ const ScrollContainerFade = ({ classNames, edge = 'top' }: ScrollContainerFadeProps) => { const { overflow, overflowEnd } = useScrollContainerContext('ScrollContainer.Fade'); const visible = edge === 'top' ? overflow : overflowEnd; return (
); }; ScrollContainerFade.displayName = 'ScrollContainer.Fade'; // // ScrollDownButton // type ScrollContainerScrollDownButtonProps = ThemedClassName<{ /** Names the icon-only button. */ label?: string; }>; /** * A floating icon button in the end corner, shown while unpinned, that scrolls to the end and pins again. It is a * Button, so it keeps Button's `data-scope`/`data-part`; `.dx-scroll-container-scroll-down` identifies it. */ const ScrollContainerScrollDownButton = ({ classNames, label = 'Scroll down', }: ScrollContainerScrollDownButtonProps) => { const { pinned, controller } = useScrollContainerContext('ScrollContainer.ScrollDownButton'); return (