From be5cf4ff6b3c317e60a2b9b39a3517c2296d30dc Mon Sep 17 00:00:00 2001 From: Marek Mihok Date: Wed, 16 Aug 2023 13:03:01 +0200 Subject: [PATCH] chore: apply top-right position based on anchor component ref #2025 --- ui/src/copyable_text.tsx | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/ui/src/copyable_text.tsx b/ui/src/copyable_text.tsx index 805deee753..591b35fb6a 100644 --- a/ui/src/copyable_text.tsx +++ b/ui/src/copyable_text.tsx @@ -4,6 +4,11 @@ import React from 'react' import { stylesheet } from 'typestyle' import { clas, cssVar, pc } from './theme' +const + BUTTON_HEIGHT = 24, + BUTTON_WIDTH = 34, + CORNER_OFFSET = 6 + const css = stylesheet({ animate: { @@ -16,11 +21,8 @@ const btn: { minWidth: 'initial', position: 'fixed', - width: 24, - height: 24, - // TODO: Compute dynamically based on anchor element. - // right: 0, - // transform: 'translate(-4px, 4px)', + width: BUTTON_WIDTH, + height: BUTTON_HEIGHT, zIndex: 1, }, copiedBtn: { @@ -63,6 +65,7 @@ export const ClipboardCopyButton = ({ value, anchorElementRef, showOnHover }: { timeoutRef = React.useRef(), [copied, setCopied] = React.useState(false), [visible, setVisible] = React.useState(!showOnHover), + [position, setPosition] = React.useState({ x: 0, y: 0 }), onClick = async () => { const el = anchorElementRef.current if (!el) return @@ -83,20 +86,25 @@ export const ClipboardCopyButton = ({ value, anchorElementRef, showOnHover }: { React.useEffect(() => { const el = anchorElementRef.current if (el) { - console.log(anchorElementRef.current.getBoundingClientRect()) - // textFieldMultiline: multiline ? { '&:hover button': { opacity: 1 } } : undefined // TODO: Re-implement with pure CSS: '&hover childId' - el.addEventListener('mouseenter', () => { setVisible(true) }) - el.addEventListener('mouseleave', () => { setVisible(false) }) + const rect = anchorElementRef.current.getBoundingClientRect() + setPosition({ x: rect.left + rect.width, y: rect.top }) + el.addEventListener('mouseenter', () => setVisible(true)) + el.addEventListener('mouseleave', (ev: MouseEvent) => { + if ((ev.relatedTarget as HTMLElement)?.id === 'copybutton') return + setVisible(false) + }) } // eslint-disable-next-line react-hooks/exhaustive-deps - }, []) + }, [value]) React.useEffect(() => () => window.clearTimeout(timeoutRef.current), []) return () }