Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 3.8k
improvement(home): anchor @-mention popup at caret and right-size dropdown widths#4393
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
Merged
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
4e51396
improvement(home): anchor @-mention popup at caret and right-size dro…
waleedlatif1 f9f0f75
fix(home): align caret-anchor marker to text-top so mention popup cle…
waleedlatif1 8216b53
fix(home): enable collision avoidance for @-mention popup so it fits …
waleedlatif1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
9 changes: 5 additions & 4 deletions
9 .../app/workspace/[workspaceId]/home/components/user-input/components/plus-menu-dropdown.tsx
This file contains hidden or 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
55 changes: 48 additions & 7 deletions
55 apps/sim/app/workspace/[workspaceId]/home/components/user-input/user-input.tsx
This file contains hidden or 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 |
|---|---|---|
| @@ -58,6 +58,51 @@ import type { ChatContext } from '@/stores/panel' | ||
| export type { FileAttachmentForApi } from '@/app/workspace/[workspaceId]/home/types' | ||
| function getCaretAnchor( | ||
| textarea: HTMLTextAreaElement, | ||
| caretPos: number | ||
| ): { left: number; top: number } { | ||
| const textareaRect = textarea.getBoundingClientRect() | ||
| const style = window.getComputedStyle(textarea) | ||
| const mirror = document.createElement('div') | ||
| mirror.style.position = 'absolute' | ||
| mirror.style.top = '0' | ||
| mirror.style.left = '0' | ||
| mirror.style.visibility = 'hidden' | ||
| mirror.style.whiteSpace = 'pre-wrap' | ||
| mirror.style.overflowWrap = 'break-word' | ||
| mirror.style.font = style.font | ||
| mirror.style.padding = style.padding | ||
| mirror.style.border = style.border | ||
| mirror.style.width = style.width | ||
| mirror.style.lineHeight = style.lineHeight | ||
| mirror.style.boxSizing = style.boxSizing | ||
| mirror.style.letterSpacing = style.letterSpacing | ||
| mirror.style.textTransform = style.textTransform | ||
| mirror.style.textIndent = style.textIndent | ||
| mirror.style.textAlign = style.textAlign | ||
| mirror.textContent = textarea.value.substring(0, caretPos) | ||
| const marker = document.createElement('span') | ||
| marker.style.display = 'inline-block' | ||
| marker.style.width = '0px' | ||
| marker.style.padding = '0' | ||
| marker.style.border = '0' | ||
| marker.style.verticalAlign = 'text-top' | ||
| mirror.appendChild(marker) | ||
| document.body.appendChild(mirror) | ||
| const markerRect = marker.getBoundingClientRect() | ||
| const mirrorRect = mirror.getBoundingClientRect() | ||
| document.body.removeChild(mirror) | ||
| return { | ||
| left: textareaRect.left + (markerRect.left - mirrorRect.left) - textarea.scrollLeft, | ||
| top: textareaRect.top + (markerRect.top - mirrorRect.top) - textarea.scrollTop, | ||
| } | ||
| } | ||
waleedlatif1 marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| interface UserInputProps { | ||
| defaultValue?: string | ||
| draftScopeKey?: string | ||
| @@ -304,7 +349,6 @@ export const UserInput = forwardRef<UserInputHandle, UserInputProps>(function Us | ||
| const pendingCursorRef = useRef<number | null>(null) | ||
| const mentionRangeRef = useRef<{ start: number; end: number } | null>(null) | ||
| const [mentionQuery, setMentionQuery] = useState<string | null>(null) | ||
| const containerRef = useRef<HTMLDivElement>(null) | ||
| useImperativeHandle( | ||
| ref, | ||
| @@ -664,7 +708,7 @@ export const UserInput = forwardRef<UserInputHandle, UserInputProps>(function Us | ||
| getActiveMentionAtRef.current = mentionMenu.getActiveMentionQueryAtPosition | ||
| const syncMentionState = useCallback( | ||
| (_textarea: HTMLTextAreaElement, text: string, caret: number) => { | ||
| (textarea: HTMLTextAreaElement, text: string, caret: number) => { | ||
| const active = getActiveMentionAtRef.current(caret, text) | ||
| // Treat any whitespace inside the query as a closer — typing a space | ||
| // after `@foo` should leave the raw `@foo` text and dismiss the menu. | ||
| @@ -682,10 +726,8 @@ export const UserInput = forwardRef<UserInputHandle, UserInputProps>(function Us | ||
| mentionRangeRef.current = { start: active.start, end: active.end } | ||
| setMentionQuery(active.query) | ||
| if (!wasActive) { | ||
| // Anchor above the whole input box (not at the caret) so the menu can never | ||
| // overlap the user's typing. | ||
| const rect = containerRef.current?.getBoundingClientRect() | ||
| const anchor = rect ? { left: rect.left, top: rect.top } : { left: 0, top: 0 } | ||
| // Anchor at the caret so the menu floats above the user's cursor. | ||
| const anchor = getCaretAnchor(textarea, active.start) | ||
| plusMenuRef.current?.open(anchor, { mention: true }) | ||
| } | ||
| }, | ||
| @@ -834,7 +876,6 @@ export const UserInput = forwardRef<UserInputHandle, UserInputProps>(function Us | ||
| return ( | ||
| <div | ||
| ref={containerRef} | ||
| onClick={handleContainerClick} | ||
| className={cn( | ||
| 'relative z-10 mx-auto w-full max-w-[42rem] cursor-text rounded-[20px] border border-[var(--border-1)] bg-[var(--white)] px-2.5 py-2 dark:bg-[var(--surface-4)]', | ||
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.