Skip to content

Commit

Permalink
chore: use appendChild instead of insertAdjacentElement, refactor #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 8f35825 commit 83d5b5c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
20 changes: 13 additions & 7 deletions ui/src/copyable_text.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@ import { clas, cssVar, pc } from './theme'

const
css = stylesheet({
animate: {
opacity: 0,
transition: 'opacity .5s'
},
btn: {
position: 'absolute',
minWidth: 'initial',
Expand All @@ -31,6 +27,14 @@ const
}
}
},
animate: {
$nest: {
'button': {
opacity: 0,
transition: 'opacity .5s',
}
}
},
labelContainer: {
position: 'relative'
}
Expand Down Expand Up @@ -58,7 +62,7 @@ export interface CopyableText {
height?: S
}

export const ClipboardCopyButton = ({ value, defaultVisible = true }: { value: S, defaultVisible?: B }) => {
export const ClipboardCopyButton = ({ value }: { value: S }) => {
const
timeoutRef = React.useRef<U>(),
[copied, setCopied] = React.useState(false),
Expand Down Expand Up @@ -86,7 +90,7 @@ export const ClipboardCopyButton = ({ value, defaultVisible = true }: { value: S
title='Copy to clipboard'
onClick={onClick}
iconProps={{ iconName: copied ? 'CheckMark' : 'Copy' }}
className={clas(css.btn, copied ? css.copiedBtn : '', defaultVisible ? '' : css.animate)}
className={clas(css.btn, copied ? css.copiedBtn : '')}
/>
}

Expand All @@ -103,7 +107,9 @@ export const XCopyableText = ({ model }: { model: CopyableText }) => {
onRenderLabel={() =>
<div className={css.labelContainer}>
<Fluent.Label>{label}</Fluent.Label>
<ClipboardCopyButton value={value} defaultVisible={!multiline} />
<span className={multiline ? clas(css.animate) : ''}>
<ClipboardCopyButton value={value} />
</span>
</div>
}
styles={{
Expand Down
8 changes: 6 additions & 2 deletions ui/src/markdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ const
copyButton: {
position: 'relative',
$nest: {
'button': {
opacity: 0,
top: 0
},
'&:hover button': {
opacity: 1
}
Expand Down Expand Up @@ -106,9 +110,9 @@ const highlightSyntax = async (str: S, language: S, codeBlockId: S) => {
const codeBlockContainer = codeBlock.parentElement
if (codeBlockContainer) {
const buttonContainer = document.createElement('span')
ReactDOM.render(<ClipboardCopyButton value={str} defaultVisible={false} />, buttonContainer)
ReactDOM.render(<ClipboardCopyButton value={str} />, buttonContainer)
codeBlockContainer.classList.add(css.copyButton)
codeBlockContainer.insertAdjacentElement('afterbegin', buttonContainer)
codeBlockContainer.appendChild(buttonContainer)
}

return highlightedCode
Expand Down

0 comments on commit 83d5b5c

Please sign in to comment.