@base-ui-components/react
Version:
Base UI is a library of headless ('unstyled') React components and low-level hooks. You gain complete control over your app's CSS and accessibility features.
63 lines (61 loc) • 2.02 kB
JavaScript
'use client';
import * as React from 'react';
import { useScrollAreaRootContext } from "../root/ScrollAreaRootContext.js";
import { useScrollAreaScrollbarContext } from "../scrollbar/ScrollAreaScrollbarContext.js";
import { ScrollAreaScrollbarCssVars } from "../scrollbar/ScrollAreaScrollbarCssVars.js";
import { useRenderElement } from "../../utils/useRenderElement.js";
/**
* The draggable part of the the scrollbar that indicates the current scroll position.
* Renders a `<div>` element.
*
* Documentation: [Base UI Scroll Area](https://base-ui.com/react/components/scroll-area)
*/
export const ScrollAreaThumb = /*#__PURE__*/React.forwardRef(function ScrollAreaThumb(componentProps, forwardedRef) {
const {
render,
className,
...elementProps
} = componentProps;
const {
thumbYRef,
thumbXRef,
handlePointerDown,
handlePointerMove,
handlePointerUp,
setScrollingX,
setScrollingY
} = useScrollAreaRootContext();
const {
orientation
} = useScrollAreaScrollbarContext();
const state = React.useMemo(() => ({
orientation
}), [orientation]);
const element = useRenderElement('div', componentProps, {
ref: [forwardedRef, orientation === 'vertical' ? thumbYRef : thumbXRef],
state,
props: [{
onPointerDown: handlePointerDown,
onPointerMove: handlePointerMove,
onPointerUp(event) {
if (orientation === 'vertical') {
setScrollingY(false);
}
if (orientation === 'horizontal') {
setScrollingX(false);
}
handlePointerUp(event);
},
style: {
...(orientation === 'vertical' && {
height: `var(${ScrollAreaScrollbarCssVars.scrollAreaThumbHeight})`
}),
...(orientation === 'horizontal' && {
width: `var(${ScrollAreaScrollbarCssVars.scrollAreaThumbWidth})`
})
}
}, elementProps]
});
return element;
});
if (process.env.NODE_ENV !== "production") ScrollAreaThumb.displayName = "ScrollAreaThumb";