// // Copyright 2026 DXOS.org // // @import-as-namespace import { ark } from '@ark-ui/react/factory'; import React, { useCallback, useState } from 'react'; import { createContext, useComposedRefs } from '@dxos/react-hooks'; import { type AllowedAxis } from '@dxos/ui-types'; import { recipes } from '../../recipes.ts'; import { type Size } from '../../sizes.ts'; import { composableProps, slottable } from '../../util/slots.ts'; import { ScrollAreaThumbs } from './ScrollAreaThumbs.tsx'; import { scrollbar } from './scrollbar.ts'; type ScrollAreaContextValue = { native: boolean; setViewport: (viewport: HTMLElement | null) => void; }; const [ScrollAreaProvider, useScrollAreaContext] = createContext('ScrollArea'); // // Root // type ScrollAreaRootProps = { size?: Size; /** `overlay` paints the thumb over the end gutter; `reserve` takes its width out of the end track. */ mode?: 'overlay' | 'reserve'; /** `thin` fits the thumb in a rail Block's margin at every size; `regular` overlaps it. */ width?: 'thin' | 'regular'; /** Platform scrollbar instead of overlay thumbs; implies `reserve`. */ native?: boolean; /** Scrolling axis; `all` scrolls both (the current ScrollArea's values). */ orientation?: AllowedAxis; /** Overlay thumbs show only while the pointer is over the frame (or a thumb is dragged); `false` keeps them visible. */ autoHide?: boolean; /** Mandatory snapping on the scrolling axis; children carry their own `scroll-snap-align`. */ snap?: boolean; /** `false` scrolls without any visible bar, overlay or native. */ scrollbars?: boolean; /** Pixels at the start of the horizontal bar's track left clear, so the bar spans only what scrolls past a sticky column. */ trackStart?: number; }; /** Tailwind group names the overlay thumbs' `autoHide` hover rule targets (`ScrollAreaThumbs`). */ const AUTO_HIDE_GROUP: Record = { vertical: 'group/scroll-v', horizontal: 'group/scroll-h', all: 'group/scroll-all', }; /** Non-scrolling frame: hosts the overlay thumbs and is the query container for its content. */ const ScrollAreaRoot = slottable( ( { children, asChild, size, mode = 'overlay', width = 'thin', native = false, orientation = 'vertical', autoHide = true, snap = false, scrollbars = true, trackStart, ...props }, forwardedRef, ) => { const [viewport, setViewport] = useState(null); // Which axes currently show an overlay thumb, published as `data-overflow-*` so CSS can reserve its strip. const [overflow, setOverflow] = useState({ vertical: false, horizontal: false }); const handleOverflowChange = useCallback( (next: { vertical: boolean; horizontal: boolean }) => setOverflow((current) => current.vertical === next.vertical && current.horizontal === next.horizontal ? current : next, ), [], ); const { className, ...rest } = composableProps(props, { classNames: [recipes.scrollRoot(), autoHide && AUTO_HIDE_GROUP[orientation]], }); return ( {children} {!native && scrollbars && viewport && ( )} ); }, ); ScrollAreaRoot.displayName = 'ScrollArea.Root'; // // Viewport // type ScrollAreaViewportProps = {}; /** The scrolling element; under `asChild` it is the child (e.g. a Container) itself. */ const ScrollAreaViewport = slottable( ({ children, asChild, ...props }, forwardedRef) => { const { native, setViewport } = useScrollAreaContext('ScrollArea.Viewport'); const ref = useComposedRefs(forwardedRef, setViewport); const { className, ...rest } = composableProps(props, { classNames: recipes.scrollViewport() }); return ( {children} ); }, ); ScrollAreaViewport.displayName = 'ScrollArea.Viewport'; export { ScrollAreaRoot as Root, ScrollAreaViewport as Viewport }; export type { ScrollAreaRootProps as RootProps, ScrollAreaViewportProps as ViewportProps };