fix: quick search tips follow when resize (#1580)

Co-authored-by: himself65 <himself65@outlook.com>
This commit is contained in:
Whitewater
2023-03-15 09:27:38 -07:00
committed by GitHub
parent 5ac6632276
commit bc32b07bf0
6 changed files with 190 additions and 122 deletions
+23 -1
View File
@@ -1,6 +1,6 @@
import { nanoid } from '@blocksuite/store';
import { useAtomValue, useSetAtom } from 'jotai';
import { useCallback } from 'react';
import { useCallback, useEffect } from 'react';
import { jotaiWorkspacesAtom, workspacesAtom } from '../atoms';
import { WorkspacePlugins } from '../plugins';
@@ -71,3 +71,25 @@ export function useWorkspacesHelper() {
),
};
}
export const useElementResizeEffect = (
element: Element | null,
fn: () => void | (() => () => void),
// TODO: add throttle
throttle = 0
) => {
useEffect(() => {
if (!element) {
return;
}
let dispose: void | (() => void);
const resizeObserver = new ResizeObserver(entries => {
dispose = fn();
});
resizeObserver.observe(element);
return () => {
dispose?.();
resizeObserver.disconnect();
};
}, [element, fn]);
};