UNPKG

@dnb/eufemia

Version:

DNB Eufemia Design System UI Library

77 lines (76 loc) 2.45 kB
"use client"; import { useContext, useRef, useState } from 'react'; import clsx from 'clsx'; import { extendPropsWithContext, validateDOMAttributes } from "../../shared/component-helper.js"; import Context from "../../shared/Context.js"; import { useSpacing } from "../../components/space/SpacingUtils.js"; import { useIsomorphicLayoutEffect as useLayoutEffect } from "../../shared/helpers/useIsomorphicLayoutEffect.js"; import useCombinedRef from "../../shared/helpers/useCombinedRef.js"; import withComponentMarkers from "../../shared/helpers/withComponentMarkers.js"; import { jsx as _jsx } from "react/jsx-runtime"; function ScrollView(localProps) { const context = useContext(Context); const props = extendPropsWithContext(localProps, {}, context.ScrollView); const { interactive, scrollbarGutter, children, className = null, ref: refProp, ...attributes } = props; const mainParams = useSpacing(props, { ...attributes, className: clsx('dnb-scroll-view', className, scrollbarGutter === 'stable' && 'dnb-scroll-view--scrollbar-gutter-stable') }); const localRef = useRef(undefined); const combinedRef = useCombinedRef(refProp, localRef); mainParams.ref = combinedRef; mainParams.tabIndex = useInteractive({ interactive, children, ref: localRef }); validateDOMAttributes(props, mainParams); return _jsx("div", { ...mainParams, children: children }); } function useInteractive({ interactive, children, ref }) { const [isInteractive, setAsInteractive] = useState(Boolean(interactive)); useLayoutEffect(() => { if (interactive === 'auto') { setAsInteractive(hasScrollbar()); } }, [interactive, children]); useLayoutEffect(() => { if (interactive === 'auto' && typeof ResizeObserver !== 'undefined') { const observer = new ResizeObserver(() => { setAsInteractive(hasScrollbar()); }); observer.observe(ref.current); return () => observer?.disconnect(); } return undefined; }, [interactive, ref]); if (isInteractive) { return 0; } return undefined; function hasScrollbar() { if (!ref.current) { return true; } return ref.current.scrollWidth - 1 > ref.current.offsetWidth || ref.current.scrollHeight - 1 > ref.current.offsetHeight; } } withComponentMarkers(ScrollView, { _supportsSpacingProps: true }); export default ScrollView; //# sourceMappingURL=ScrollView.js.map