@dnb/eufemia
Version:
DNB Eufemia Design System UI Library
77 lines (76 loc) • 2.35 kB
JavaScript
"use client";
import React from 'react';
import clsx from 'clsx';
import { extendPropsWithContext, validateDOMAttributes } from "../../shared/component-helper.js";
import Context from "../../shared/Context.js";
import { applySpacing } from "../../components/space/SpacingUtils.js";
import { useIsomorphicLayoutEffect as useLayoutEffect } from "../../shared/helpers/useIsomorphicLayoutEffect.js";
import withComponentMarkers from "../../shared/helpers/withComponentMarkers.js";
import { jsx as _jsx } from "react/jsx-runtime";
const defaultProps = {
children: null
};
function ScrollView(localProps) {
const context = React.useContext(Context);
const props = extendPropsWithContext(localProps, defaultProps, context.ScrollView);
const {
interactive,
children,
className = null,
ref: refProp,
...attributes
} = props;
const mainParams = applySpacing(props, {
...attributes,
className: clsx('dnb-scroll-view', className)
});
const localRef = React.useRef(undefined);
mainParams.ref = refProp ? refProp : localRef;
mainParams.tabIndex = useInteractive({
interactive,
children,
ref: mainParams.ref
});
validateDOMAttributes(props, mainParams);
return _jsx("div", {
...mainParams,
children: children
});
}
function useInteractive({
interactive,
children,
ref
}) {
const [isInteractive, setAsInteractive] = React.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 === null || observer === void 0 ? void 0 : 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