Skip to content

Commit

Permalink
chore: use css only to show/hide button #2025
Browse files Browse the repository at this point in the history
  • Loading branch information
marek-mihok authored and mturoci committed Sep 6, 2023
1 parent 71c54e7 commit ab32afb
Showing 1 changed file with 8 additions and 12 deletions.
20 changes: 8 additions & 12 deletions ui/src/copyable_text.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,18 @@ const
visible: {
opacity: 1,
},
hover: {
$nest: {
'&:hover #copybutton': { opacity: 1 }
}
},
btn: {
minWidth: 'initial',
// position: 'fixed',
position: 'absolute',
top: CORNER_OFFSET,
right: CORNER_OFFSET,
width: BUTTON_WIDTH,
height: BUTTON_HEIGHT,
zIndex: 1,
},
copiedBtn: {
background: cssVar('$green'),
Expand Down Expand Up @@ -67,7 +70,6 @@ export const ClipboardCopyButton = ({ value, anchorElement, showOnHoverOnly = fa
const
timeoutRef = React.useRef<U>(),
[copied, setCopied] = React.useState(false),
[visible, setVisible] = React.useState(!showOnHoverOnly),
onClick = async () => {
if (!anchorElement) return
try {
Expand All @@ -85,14 +87,8 @@ export const ClipboardCopyButton = ({ value, anchorElement, showOnHoverOnly = fa
}

React.useEffect(() => {
if (!anchorElement) return

anchorElement.addEventListener('mouseenter', () => setVisible(true))
anchorElement.addEventListener('mouseleave', (ev: MouseEvent) => {
if ((ev.relatedTarget as HTMLElement)?.id === 'copybutton') return
setVisible(false)
})
}, [anchorElement])
if (showOnHoverOnly && anchorElement) anchorElement.classList.add(css.hover)
}, [anchorElement, showOnHoverOnly])

React.useEffect(() => () => window.clearTimeout(timeoutRef.current), [])

Expand All @@ -101,7 +97,7 @@ export const ClipboardCopyButton = ({ value, anchorElement, showOnHoverOnly = fa
title='Copy to clipboard'
onClick={onClick}
iconProps={{ iconName: copied ? 'CheckMark' : 'Copy' }}
className={clas(css.btn, copied ? css.copiedBtn : '', showOnHoverOnly ? css.animate : '', visible ? css.visible : '')}
className={clas(css.btn, copied ? css.copiedBtn : '', showOnHoverOnly ? css.animate : '', showOnHoverOnly ? '' : css.visible)}
/>)
}

Expand Down

0 comments on commit ab32afb

Please sign in to comment.