1 | 'use client';
|
2 |
|
3 | import * as React from 'react';
|
4 | import PropTypes from 'prop-types';
|
5 | import debounce from "../utils/debounce.js";
|
6 | import { ownerWindow, unstable_useEnhancedEffect as useEnhancedEffect } from "../utils/index.js";
|
7 | import { jsx as _jsx } from "react/jsx-runtime";
|
8 | const styles = {
|
9 | width: 99,
|
10 | height: 99,
|
11 | position: 'absolute',
|
12 | top: -9999,
|
13 | overflow: 'scroll'
|
14 | };
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 | export 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 _jsx("div", {
|
51 | style: styles,
|
52 | ref: nodeRef,
|
53 | ...other
|
54 | });
|
55 | }
|
56 | process.env.NODE_ENV !== "production" ? ScrollbarSize.propTypes = {
|
57 | onChange: PropTypes.func.isRequired
|
58 | } : void 0; |
\ | No newline at end of file |