forked from SigNoz/signoz
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Onboarding Docs - Copy to clipboard (SigNoz#3634)
* feat: enable copy-to-clipboard to onboarding docs snippets * feat: remove commented code & <br></br> from md docs * feat: remove react-copy-to-clipboard lib and fix type issues * feat: markdown renderer - pre - remove any with reactnode
- Loading branch information
Showing
34 changed files
with
563 additions
and
282 deletions.
There are no files selected for viewing
This file contains 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
34 changes: 34 additions & 0 deletions
34
frontend/src/components/MarkdownRenderer/CodeCopyBtn/CodeCopyBtn.scss
This file contains 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 |
---|---|---|
@@ -0,0 +1,34 @@ | ||
.code-snippet-container { | ||
position: relative; | ||
background-color: rgb(43, 43, 43); | ||
} | ||
|
||
.code-copy-btn { | ||
position: absolute; | ||
top: 8px; | ||
right: 8px; | ||
display: flex; | ||
justify-content: flex-end; | ||
align-items: center; | ||
|
||
button { | ||
cursor: pointer; | ||
|
||
background-color: rgba($color: #1d1d1d, $alpha: 0.7); | ||
color: white; | ||
border: none; | ||
padding: 8px; | ||
border-radius: 3px; | ||
transition: all 0.1s; | ||
|
||
&:hover { | ||
background-color: rgba($color: #1d1d1d, $alpha: 1); | ||
} | ||
} | ||
|
||
&.copied { | ||
button { | ||
background-color: rgba($color: #52c41a, $alpha: 1); | ||
} | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
frontend/src/components/MarkdownRenderer/CodeCopyBtn/CodeCopyBtn.tsx
This file contains 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 |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import './CodeCopyBtn.scss'; | ||
|
||
import { CheckOutlined, CopyOutlined } from '@ant-design/icons'; | ||
import cx from 'classnames'; | ||
import { useState } from 'react'; | ||
|
||
export default function CodeCopyBtn({ | ||
children, | ||
}: { | ||
children: React.ReactNode; | ||
}): JSX.Element { | ||
const [isSnippetCopied, setIsSnippetCopied] = useState(false); | ||
|
||
const handleClick = (): void => { | ||
if (children && Array.isArray(children)) { | ||
setIsSnippetCopied(true); | ||
navigator.clipboard.writeText(children[0].props.children[0]).finally(() => { | ||
setTimeout(() => { | ||
setIsSnippetCopied(false); | ||
}, 1000); | ||
}); | ||
} | ||
}; | ||
|
||
return ( | ||
<div className={cx('code-copy-btn', isSnippetCopied ? 'copied' : '')}> | ||
<button type="button" onClick={handleClick}> | ||
{!isSnippetCopied ? <CopyOutlined /> : <CheckOutlined />} | ||
</button> | ||
</div> | ||
); | ||
} |
43 changes: 43 additions & 0 deletions
43
frontend/src/components/MarkdownRenderer/MarkdownRenderer.tsx
This file contains 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 |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* eslint-disable react/jsx-props-no-spreading */ | ||
import { CodeProps } from 'react-markdown/lib/ast-to-react'; | ||
import { Prism as SyntaxHighlighter } from 'react-syntax-highlighter'; | ||
import { a11yDark } from 'react-syntax-highlighter/dist/cjs/styles/prism'; | ||
|
||
import CodeCopyBtn from './CodeCopyBtn/CodeCopyBtn'; | ||
|
||
function Pre({ children }: { children: React.ReactNode }): JSX.Element { | ||
return ( | ||
<pre className="code-snippet-container"> | ||
<CodeCopyBtn>{children}</CodeCopyBtn> | ||
{children} | ||
</pre> | ||
); | ||
} | ||
|
||
function Code({ | ||
node, | ||
inline, | ||
className = 'blog-code', | ||
children, | ||
...props | ||
}: CodeProps): JSX.Element { | ||
const match = /language-(\w+)/.exec(className || ''); | ||
return !inline && match ? ( | ||
<SyntaxHighlighter | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore | ||
style={a11yDark} | ||
language={match[1]} | ||
PreTag="div" | ||
{...props} | ||
> | ||
{String(children).replace(/\n$/, '')} | ||
</SyntaxHighlighter> | ||
) : ( | ||
<code className={className} {...props}> | ||
{children} | ||
</code> | ||
); | ||
} | ||
|
||
export { Code, Pre }; |
This file contains 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
This file contains 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
Oops, something went wrong.