mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-14 21:27:20 +00:00
refactor: block-hub in tool wrapper (#3073)
This commit is contained in:
25
packages/component/src/components/block-hub/index.tsx
Normal file
25
packages/component/src/components/block-hub/index.tsx
Normal file
@@ -0,0 +1,25 @@
|
||||
import type { BlockHub } from '@blocksuite/blocks';
|
||||
import type { Atom } from 'jotai';
|
||||
import { useAtomValue } from 'jotai';
|
||||
import type { HTMLAttributes, ReactElement } from 'react';
|
||||
import { useRef } from 'react';
|
||||
|
||||
export interface BlockHubProps extends HTMLAttributes<HTMLDivElement> {
|
||||
blockHubAtom: Atom<Readonly<BlockHub> | null>;
|
||||
}
|
||||
|
||||
export const BlockHubWrapper = (props: BlockHubProps): ReactElement => {
|
||||
const blockHub = useAtomValue(props.blockHubAtom);
|
||||
const ref = useRef<HTMLDivElement>(null);
|
||||
if (ref.current) {
|
||||
const div = ref.current;
|
||||
if (!blockHub) {
|
||||
if (div.hasChildNodes()) {
|
||||
div.removeChild(div.firstChild as ChildNode);
|
||||
}
|
||||
} else {
|
||||
div.appendChild(blockHub);
|
||||
}
|
||||
}
|
||||
return <div ref={ref} data-testid="block-hub" />;
|
||||
};
|
||||
Reference in New Issue
Block a user