-
-
Notifications
You must be signed in to change notification settings - Fork 754
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d207251
commit 0e5bfb9
Showing
20 changed files
with
833 additions
and
241 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
apps/www/src/components/plate-ui/playground-floating-toolbar.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
'use client'; | ||
|
||
import React from 'react'; | ||
|
||
import { cn, withRef } from '@udecode/cn'; | ||
import { AIPlugin } from '@udecode/plate-ai/react'; | ||
import { | ||
PortalBody, | ||
useComposedRef, | ||
useEditorId, | ||
useEditorRef, | ||
useEventEditorSelectors, | ||
} from '@udecode/plate-common/react'; | ||
import { | ||
type FloatingToolbarState, | ||
flip, | ||
offset, | ||
useFloatingToolbar, | ||
useFloatingToolbarState, | ||
} from '@udecode/plate-floating'; | ||
|
||
import { Toolbar } from '@/registry/default/plate-ui/toolbar'; | ||
|
||
export const PlaygroundFloatingToolbar = withRef< | ||
typeof Toolbar, | ||
{ | ||
state?: FloatingToolbarState; | ||
} | ||
>(({ children, state, ...props }, componentRef) => { | ||
const editor = useEditorRef(); | ||
const editorId = useEditorId(); | ||
const focusedEditorId = useEventEditorSelectors.focus(); | ||
|
||
const aiOpen = editor.useOptions(AIPlugin).openEditorId === editor.id; | ||
|
||
const floatingToolbarState = useFloatingToolbarState({ | ||
editorId, | ||
focusedEditorId, | ||
hideToolbar: aiOpen, | ||
...state, | ||
floatingOptions: { | ||
middleware: [ | ||
offset(12), | ||
flip({ | ||
fallbackPlacements: [ | ||
'top-start', | ||
'top-end', | ||
'bottom-start', | ||
'bottom-end', | ||
], | ||
padding: 12, | ||
}), | ||
], | ||
placement: 'top', | ||
...state?.floatingOptions, | ||
}, | ||
}); | ||
|
||
const { | ||
hidden, | ||
props: rootProps, | ||
ref: floatingRef, | ||
} = useFloatingToolbar(floatingToolbarState); | ||
|
||
const ref = useComposedRef<HTMLDivElement>(componentRef, floatingRef); | ||
|
||
if (hidden) return null; | ||
|
||
return ( | ||
<PortalBody> | ||
<Toolbar | ||
ref={ref} | ||
className={cn( | ||
'absolute z-50 whitespace-nowrap rounded-md border bg-popover px-1 opacity-100 shadow-md print:hidden' | ||
)} | ||
{...rootProps} | ||
{...props} | ||
> | ||
{children} | ||
</Toolbar> | ||
</PortalBody> | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.