-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'next-release/main' into next-release-main-into-dev
- Loading branch information
Showing
58 changed files
with
2,639 additions
and
421 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
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
46 changes: 46 additions & 0 deletions
46
src/components/MDXComponents/MDXHighlightedCopyCodeButton.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,46 @@ | ||
import { useState } from 'react'; | ||
import { CopyToClipboard } from 'react-copy-to-clipboard'; | ||
import { VisuallyHidden } from '@aws-amplify/ui-react'; | ||
import { trackCopyClicks } from '@/utils/track'; | ||
import { prepareCopyText } from './utils/copy-code'; | ||
|
||
interface MDXCopyCodeButtonProps { | ||
codeId: string; | ||
codeString: string; | ||
testId?: string; | ||
title?: string; | ||
children?: React.ReactNode; | ||
} | ||
|
||
export const MDXHighlightedCopyCodeButton = ({ | ||
codeString, | ||
title, | ||
codeId, | ||
children | ||
}: MDXCopyCodeButtonProps) => { | ||
const [copied, setCopied] = useState(false); | ||
|
||
const copyText = prepareCopyText(codeString); | ||
|
||
const copy = () => { | ||
trackCopyClicks(copyText); | ||
setCopied(true); | ||
setTimeout(() => { | ||
setCopied(false); | ||
}, 2000); | ||
}; | ||
return ( | ||
<CopyToClipboard text={copyText} onCopy={copy}> | ||
<button className="highlight-copy-block" key={codeId}> | ||
{children} | ||
<span className="highlight-copy-block-hint"> | ||
{copied ? 'Copied' : 'Copy'} | ||
</span> | ||
<VisuallyHidden> | ||
{` `} | ||
{title} highlighted code example | ||
</VisuallyHidden> | ||
</button> | ||
</CopyToClipboard> | ||
); | ||
}; |
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
3 changes: 2 additions & 1 deletion
3
src/components/MDXComponents/__tests__/MDXCopyCodeButton.test.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
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,12 @@ | ||
export const prepareCopyText = (codeString: string): string => { | ||
// We need to strip out markdown comments from the code string | ||
// so they don't show up in our copied text | ||
const highlightStartText = /\/\/\s?highlight-start/g; | ||
const highlightEndText = /\/\/\s?highlight-end/g; | ||
const highlightNextLine = /\/\/\s?highlight-next-line/g; | ||
|
||
return codeString | ||
.replace(highlightStartText, '') | ||
.replace(highlightEndText, '') | ||
.replace(highlightNextLine, ''); | ||
}; |
Oops, something went wrong.