Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed floating toolbar safari #2764

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 59 additions & 2 deletions apps/www/src/registry/default/plate-ui/link-floating-toolbar.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use client';

import React from 'react';
import { useEditorRef } from '@udecode/plate-core';
import {
flip,
offset,
Expand All @@ -15,8 +16,11 @@ import {
useFloatingLinkInsert,
useFloatingLinkInsertState,
} from '@udecode/plate-link';
import { useCallback, useMemo } from 'react';
import { ReactEditor } from 'slate-react';

import { cn } from '@/lib/utils';

import { Icons } from '@/components/icons';

import { buttonVariants } from './button';
Expand All @@ -40,6 +44,8 @@ export interface LinkFloatingToolbarProps {
}

export function LinkFloatingToolbar({ state }: LinkFloatingToolbarProps) {
const editor = useEditorRef();

const insertState = useFloatingLinkInsertState({
...state,
floatingOptions: {
Expand Down Expand Up @@ -68,6 +74,47 @@ export function LinkFloatingToolbar({ state }: LinkFloatingToolbarProps) {
unlinkButtonProps,
} = useFloatingLinkEdit(editState);

const getSelectionAbsolutePosition = useCallback(() => {
if (!editor.selection) return;

try {
const domRange = ReactEditor.toDOMRange(
editor as ReactEditor,
editor.selection
);

const rect = domRange.getBoundingClientRect();

const editorContainer = ReactEditor.toDOMNode(
editor as ReactEditor,
editor.children[0]
).parentElement;

if (!editorContainer) return;

const editorRect = editorContainer.getBoundingClientRect();

// Calculate the maximum x position
const maxWidth = editorRect.width - 288; // 288px for w-72
let x = rect.left - editorRect.left + editorContainer.scrollLeft;

// Ensure x does not exceed the maximum width
x = Math.min(x, maxWidth);

return {
x: x,
y: rect.top - editorRect.top + editorContainer.scrollTop + 40,
};
} catch (error) {
return;
}
}, [editor]);

const currentAbsolutePosition = useMemo(() => {
// This will only be recalculated when `editor.selection` changes
return getSelectionAbsolutePosition();
}, [editor.selection, getSelectionAbsolutePosition]);

if (hidden) return null;

const input = (
Expand Down Expand Up @@ -136,20 +183,30 @@ export function LinkFloatingToolbar({ state }: LinkFloatingToolbarProps) {
</div>
);

if (!currentAbsolutePosition) return null;

return (
<>
<div
ref={insertRef}
className={cn(popoverVariants(), 'w-auto p-1')}
{...insertProps}
style={{
...insertProps.style,
top: currentAbsolutePosition.y,
left: currentAbsolutePosition.x,
}}
>
{input}
</div>

<div
ref={editRef}
className={cn(popoverVariants(), 'w-auto p-1')}
{...editProps}
style={{
...editProps.style,
top: currentAbsolutePosition.y,
left: currentAbsolutePosition.x,
}}
>
{editContent}
</div>
Expand Down