UNPKG

json-joy

Version:

Collection of libraries for building collaborative editing apps.

42 lines (41 loc) 1.78 kB
import * as React from 'react'; import { rule } from 'nano-theme'; import { CaretToolbar } from 'nice-ui/lib/4-card/Toolbar/ToolbarMenu/CaretToolbar'; import { useToolbarPlugin } from './context'; import { useSyncStore, useSyncStoreOpt, useTimeout } from '../../web/react/hooks'; import { AfterTimeout } from '../../web/react/util/AfterTimeout'; const height = 1.8; const blockClass = rule({ pos: 'relative', w: '0px', h: '100%', va: 'bottom', }); const overClass = rule({ pos: 'absolute', b: `${height}em`, l: 0, isolation: 'isolate', us: 'none', transform: 'translateX(calc(-50% + 0px))', }); export const RenderCaret = ({ children }) => { const { toolbar } = useToolbarPlugin(); const showInlineToolbar = toolbar.showInlineToolbar; const [showCaretToolbarValue, toolbarVisibilityChangeTime] = useSyncStore(showInlineToolbar); const focus = useSyncStoreOpt(toolbar.surface.dom?.cursor.focus) || false; const doHideForCoolDown = toolbarVisibilityChangeTime + 500 > Date.now(); const enableAfterCoolDown = useTimeout(500, [doHideForCoolDown]); // biome-ignore lint/correctness/useExhaustiveDependencies: showInlineToolbar.next do not need to memoize const handleClose = React.useCallback(() => { setTimeout(() => { if (showInlineToolbar.value) showInlineToolbar.next([false, Date.now()]); }, 5); }, []); let toolbarElement = (React.createElement(CaretToolbar, { disabled: !enableAfterCoolDown, menu: toolbar.getCaretMenu(), onPopupClose: handleClose })); if (doHideForCoolDown) { toolbarElement = React.createElement(AfterTimeout, { ms: 500 }, toolbarElement); } return (React.createElement("span", { className: blockClass }, children)); };