Files
2025-03-07 19:22:02 +01:00

88 lines
2.9 KiB
JavaScript

"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
Object.defineProperty(exports, "useDrawerBody_unstable", {
enumerable: true,
get: function() {
return useDrawerBody_unstable;
}
});
const _interop_require_wildcard = require("@swc/helpers/_/_interop_require_wildcard");
const _react = /*#__PURE__*/ _interop_require_wildcard._(require("react"));
const _reactutilities = require("@fluentui/react-utilities");
const _drawerContext = require("../../contexts/drawerContext");
/**
* @internal
*
* Get the current scroll state of the DrawerBody.
*
* @param element - HTMLElement to check scroll state of
*/ const getScrollState = ({ scrollTop, scrollHeight, clientHeight })=>{
if (scrollHeight <= clientHeight) {
return 'none';
}
if (scrollTop === 0) {
return 'top';
}
if (scrollTop + clientHeight === scrollHeight) {
return 'bottom';
}
return 'middle';
};
const useDrawerBody_unstable = (props, ref)=>{
const { setScrollState } = (0, _drawerContext.useDrawerContext_unstable)();
const scrollRef = _react.useRef(null);
const [setAnimationFrame, cancelAnimationFrame] = (0, _reactutilities.useAnimationFrame)();
const updateScrollState = _react.useCallback(()=>{
if (!scrollRef.current) {
return;
}
setScrollState(getScrollState(scrollRef.current));
}, [
setScrollState
]);
const onScroll = _react.useCallback(()=>{
cancelAnimationFrame();
setAnimationFrame(()=>updateScrollState());
}, [
cancelAnimationFrame,
setAnimationFrame,
updateScrollState
]);
(0, _reactutilities.useIsomorphicLayoutEffect)(()=>{
cancelAnimationFrame();
setAnimationFrame(()=>updateScrollState());
/* update scroll state when children changes */ return ()=>cancelAnimationFrame();
}, [
props.children,
cancelAnimationFrame,
updateScrollState,
setAnimationFrame
]);
(0, _reactutilities.useIsomorphicLayoutEffect)(()=>{
cancelAnimationFrame();
setAnimationFrame(()=>updateScrollState());
return ()=>cancelAnimationFrame();
}, [
cancelAnimationFrame,
updateScrollState,
setAnimationFrame
]);
return {
components: {
root: 'div'
},
root: _reactutilities.slot.always((0, _reactutilities.getIntrinsicElementProps)('div', {
// FIXME:
// `ref` is wrongly assigned to be `HTMLElement` instead of `HTMLDivElement`
// but since it would be a breaking change to fix it, we are casting ref to it's proper type
ref: (0, _reactutilities.useMergedRefs)(ref, scrollRef),
...props,
onScroll: (0, _reactutilities.mergeCallbacks)(props.onScroll, onScroll)
}), {
elementType: 'div'
})
};
};