UNPKG

1.72 kBJavaScriptView Raw
1'use client';
2
3import * as React from 'react';
4import PropTypes from 'prop-types';
5import debounce from "../utils/debounce.js";
6import { ownerWindow, unstable_useEnhancedEffect as useEnhancedEffect } from "../utils/index.js";
7import { jsx as _jsx } from "react/jsx-runtime";
8const styles = {
9 width: 99,
10 height: 99,
11 position: 'absolute',
12 top: -9999,
13 overflow: 'scroll'
14};
15
16/**
17 * @ignore - internal component.
18 * The component originates from https://github.com/STORIS/react-scrollbar-size.
19 * It has been moved into the core in order to minimize the bundle size.
20 */
21export default function ScrollbarSize(props) {
22 const {
23 onChange,
24 ...other
25 } = props;
26 const scrollbarHeight = React.useRef();
27 const nodeRef = React.useRef(null);
28 const setMeasurements = () => {
29 scrollbarHeight.current = nodeRef.current.offsetHeight - nodeRef.current.clientHeight;
30 };
31 useEnhancedEffect(() => {
32 const handleResize = debounce(() => {
33 const prevHeight = scrollbarHeight.current;
34 setMeasurements();
35 if (prevHeight !== scrollbarHeight.current) {
36 onChange(scrollbarHeight.current);
37 }
38 });
39 const containerWindow = ownerWindow(nodeRef.current);
40 containerWindow.addEventListener('resize', handleResize);
41 return () => {
42 handleResize.clear();
43 containerWindow.removeEventListener('resize', handleResize);
44 };
45 }, [onChange]);
46 React.useEffect(() => {
47 setMeasurements();
48 onChange(scrollbarHeight.current);
49 }, [onChange]);
50 return /*#__PURE__*/_jsx("div", {
51 style: styles,
52 ref: nodeRef,
53 ...other
54 });
55}
56process.env.NODE_ENV !== "production" ? ScrollbarSize.propTypes = {
57 onChange: PropTypes.func.isRequired
58} : void 0;
\No newline at end of file