import React, { forwardRef, useImperativeHandle, useRef } from 'react'; import { isEnableStyle, scrollTo } from './utils'; import type { ScrollbarProps } from './types'; import { useScrollbar } from './useScrollbar'; import './MacScrollbar.less'; export interface MacScrollbarProps extends ScrollbarProps, React.HtmlHTMLAttributes { /** * Custom element type. * @defaultValue 'div' */ as?: keyof JSX.IntrinsicElements | string; } export const MacScrollbar = forwardRef( ( { className = '', onScroll, children, style, skin, trackGap, trackStyle, thumbStyle, minThumbSize, suppressAutoHide, suppressScrollX, suppressScrollY, as = 'div', ...props }, ref, ) => { const scrollBoxRef = useRef(null); useImperativeHandle(ref, () => scrollBoxRef.current as HTMLElement); const [scrollbarNode, moveTo] = useScrollbar( scrollBoxRef, (scrollOffset, horizontal) => scrollTo(scrollBoxRef.current, scrollOffset, horizontal), { skin, trackGap, trackStyle, thumbStyle, minThumbSize, suppressAutoHide, suppressScrollX, suppressScrollY, }, ); function handleScroll(evt: React.UIEvent) { if (onScroll) { onScroll(evt); } moveTo(evt.target as HTMLElement); } const Wrapper = as as unknown as React.HTMLFactory; return ( {scrollbarNode} {children} ); }, );