Skip to content

Commit

Permalink
fix(react-drawer): update scroll state when children changes (#32818)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcosmoura authored Sep 11, 2024
1 parent 6cae9b1 commit db57a7b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "patch",
"comment": "fix: update scroll state when children changes",
"packageName": "@fluentui/react-drawer",
"email": "marcosvmmoura@gmail.com",
"dependentChangeType": "patch"
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import type { DrawerBodyProps, DrawerBodyState } from './DrawerBody.types';
*
* Get the current scroll state of the DrawerBody.
*
* @param param0 - HTMLElement to check scroll state of
* @param element - HTMLElement to check scroll state of
*/
const getScrollState = ({ scrollTop, scrollHeight, clientHeight }: HTMLElement): DrawerScrollState => {
if (scrollHeight <= clientHeight) {
Expand Down Expand Up @@ -51,26 +51,29 @@ export const useDrawerBody_unstable = (props: DrawerBodyProps, ref: React.Ref<HT
const scrollRef = React.useRef<HTMLDivElement | null>(null);
const [setAnimationFrame, cancelAnimationFrame] = useAnimationFrame();

const onScroll = React.useCallback(() => {
cancelAnimationFrame();
setAnimationFrame(() => {
if (!scrollRef.current) {
return;
}

setScrollState(getScrollState(scrollRef.current));
});
}, [cancelAnimationFrame, setAnimationFrame, setScrollState]);

useIsomorphicLayoutEffect(() => {
const updateScrollState = React.useCallback(() => {
if (!scrollRef.current) {
return;
}

setScrollState(getScrollState(scrollRef.current));
}, [setScrollState]);

const onScroll = React.useCallback(() => {
cancelAnimationFrame();
setAnimationFrame(() => updateScrollState());
}, [cancelAnimationFrame, setAnimationFrame, updateScrollState]);

useIsomorphicLayoutEffect(() => {
updateScrollState();
/* update scroll state when children changes */
}, [props.children, updateScrollState]);

useIsomorphicLayoutEffect(() => {
updateScrollState();

return () => cancelAnimationFrame();
}, [cancelAnimationFrame, setScrollState]);
}, [cancelAnimationFrame, updateScrollState]);

return {
components: {
Expand Down

0 comments on commit db57a7b

Please sign in to comment.