Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: update design/functionality of copying highlighted code snippets #7570

Merged
merged 11 commits into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/components/Icons/IconClipboard.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Icon } from '@aws-amplify/ui-react';

export const IconClipboard = ({ ...rest }) => {
return (
<Icon
aria-hidden="true"
{...rest}
viewBox={{
minX: 0,
minY: 0,
width: 24,
height: 24
}}
>
<path
d="M18.75 3H15C15 1.34531 13.6547 0 12 0C10.3453 0 9 1.34531 9 3H5.25C4.00781 3 3 4.00781 3 5.25V21.75C3 22.9922 4.00781 24 5.25 24H18.75C19.9922 24 21 22.9922 21 21.75V5.25C21 4.00781 19.9922 3 18.75 3ZM12 1.875C12.6234 1.875 13.125 2.37656 13.125 3C13.125 3.62344 12.6234 4.125 12 4.125C11.3766 4.125 10.875 3.62344 10.875 3C10.875 2.37656 11.3766 1.875 12 1.875ZM18.75 21.4688C18.75 21.6234 18.6234 21.75 18.4688 21.75H5.53125C5.37656 21.75 5.25 21.6234 5.25 21.4688V5.53125C5.25 5.37656 5.37656 5.25 5.53125 5.25H7.5V6.9375C7.5 7.24687 7.75313 7.5 8.0625 7.5H15.9375C16.2469 7.5 16.5 7.24687 16.5 6.9375V5.25H18.4688C18.6234 5.25 18.75 5.37656 18.75 5.53125V21.4688Z"
fill="currentColor"
/>
</Icon>
);
};
1 change: 1 addition & 0 deletions src/components/Icons/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export { IconAWS } from './IconAWS';
export { IconCheck } from './IconCheck';
export { IconCheckCircle } from './IconCheckCircle';
export { IconChevron } from './IconChevron';
export { IconClipboard } from './IconClipboard';
export { IconDark } from './IconDark';
export { IconDiscord } from './IconDiscord';
export { IconDoubleChevron } from './IconDoubleChevron';
Expand Down
4 changes: 2 additions & 2 deletions src/components/MDXComponents/MDXCopyCodeButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { CopyToClipboard } from 'react-copy-to-clipboard';
import { Button, VisuallyHidden } from '@aws-amplify/ui-react';
import { trackCopyClicks } from '@/utils/track';
import { prepareCopyText } from './utils/copy-code';

import { IconClipboard } from '@/components/Icons';
interface MDXCopyCodeButtonProps {
codeId: string;
codeString: string;
Expand Down Expand Up @@ -38,7 +38,7 @@ export const MDXCopyCodeButton = ({
testId={testId}
aria-describedby={title ? undefined : codeId}
>
{copied ? 'Copied!' : 'Copy'}
<IconClipboard /> {copied ? 'Copied!' : 'Copy'}
<VisuallyHidden>
{` `}
{title} code example
Expand Down
53 changes: 53 additions & 0 deletions src/components/MDXComponents/MDXHighlightedCode.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { useState, useId } from 'react';
import { CopyToClipboard } from 'react-copy-to-clipboard';
import { Button, VisuallyHidden } from '@aws-amplify/ui-react';
import { trackCopyClicks } from '@/utils/track';
import { prepareCopyText } from './utils/copy-code';
import { IconClipboard } from '@/components/Icons';
interface MDXHighlightedCodeProps {
codeString: string;
testId?: string;
children?: React.ReactNode;
}

export const MDXHighlightedCode = ({
codeString,
children
}: MDXHighlightedCodeProps) => {
const [copied, setCopied] = useState(false);

const copyText = prepareCopyText(codeString);
const highlightCodeId = useId();

const copy = () => {
trackCopyClicks(copyText);
setCopied(true);
setTimeout(() => {
setCopied(false);
}, 2000);
};
return (
<>
<CopyToClipboard text={copyText} onCopy={copy}>
<Button
aria-describedby={highlightCodeId}
size="small"
className="highlight-copy-button"
>
<IconClipboard />
{copied ? 'Copied' : 'Copy'}

<VisuallyHidden>
{` `}
highlighted code example
</VisuallyHidden>
</Button>
</CopyToClipboard>
<CopyToClipboard text={copyText} onCopy={copy}>
<div className="highlight-code" id={highlightCodeId}>
{children}
</div>
</CopyToClipboard>
</>
);
};
46 changes: 0 additions & 46 deletions src/components/MDXComponents/MDXHighlightedCopyCodeButton.tsx

This file was deleted.

7 changes: 3 additions & 4 deletions src/components/MDXComponents/TokenList.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Token } from 'prism-react-renderer';
import type { TokenListProps } from './types';
import { MDXHighlightedCopyCodeButton } from './MDXHighlightedCopyCodeButton';
import { MDXHighlightedCode } from './MDXHighlightedCode';
import classNames from 'classnames';

type ProcessedToken = {
Expand Down Expand Up @@ -159,8 +159,7 @@ export const TokenList = ({
.join('\n');

return (
<MDXHighlightedCopyCodeButton
codeId={`highlighted:${i}`}
<MDXHighlightedCode
key={`highlighted:${i}`}
codeString={highlightedCodeString}
>
Expand All @@ -172,7 +171,7 @@ export const TokenList = ({
showLineNumbers
);
})}
</MDXHighlightedCopyCodeButton>
</MDXHighlightedCode>
);
}
});
Expand Down
142 changes: 105 additions & 37 deletions src/styles/code.scss
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ code:not([class]) {
}

.pre-wrapper {
padding: 0 var(--amplify-space-large) 0 var(--amplify-space-xs);
padding: 0 var(--amplify-space-xs);
}

.pre-header {
Expand Down Expand Up @@ -70,13 +70,16 @@ code:not([class]) {
-webkit-text-size-adjust: 100%;

&:focus-visible {
outline: none;
box-shadow: 0 0 0 2px var(--amplify-colors-white) inset;
outline: 2px solid #fff;
outline-offset: 2px;
}
}

.pre-code {
flex: 1;
display: flex;
flex-direction: column;
--highlight-code-hover-background-color: hsl(211, 22%, 19%);
}

.token-line {
Expand All @@ -92,7 +95,15 @@ code:not([class]) {
}

.line-highlight {
&:first-child {
padding-top: var(--amplify-space-xxs);
}
&:last-child {
padding-bottom: var(--amplify-space-xxs);
}
&:before {
transition: background-color
var(--amplify-components-button-transition-duration) ease;
content: '';
position: absolute;
left: 0;
Expand Down Expand Up @@ -138,45 +149,101 @@ code:not([class]) {
width: 1.8rem;
}

.highlight-copy-block {
all: unset;
width: 100%;
cursor: pointer;
.highlight-copy-button {
background-color: var(--amplify-colors-neutral-90);
margin-top: var(--amplify-space-xxs);
border-color: transparent;
color: #fff;
align-self: flex-start;
border-end-end-radius: 0;
border-end-start-radius: 0;
margin-inline-start: var(--amplify-space-xxl);
position: relative;
}

.highlight-copy-block-hint {
position: absolute;
top: 0;
right: 1.8rem;
color: white;
}

.highlight-copy-block .highlight-c4py-block-hint {
display: none;
}

.highlight-copy-block:focus .highlight-copy-block-hint {
display: block;
}

.highlight-copy-block:hover .highlight-copy-block-hint {
display: block;
}

.highlight-copy-block:focus .line-highlight::before {
background-color: var(--amplify-colors-primary-80);

user-select: none;
gap: var(--amplify-space-xxs);
&:focus {
box-shadow: none;
}
&:focus-visible {
outline: 2px solid #fff;
outline-offset: -2px;
&:after {
content: '';
position: absolute;
height: 2px;
left: 1px;
right: 1px;
bottom: -1px;
background-color: var(--amplify-colors-neutral-90);
z-index: 2;
}
& + .highlight-code {
outline: 2px solid #fff;
.line-highlight:after {
background-color: #fff;
}
}
}
&:hover {
background-color: var(--highlight-code-hover-background-color);
&:after {
background-color: var(--highlight-code-hover-background-color);
}
& + .highlight-code {
.line-highlight:before {
background-color: var(--highlight-code-hover-background-color);
}
}
}
@include darkMode {
background-color: var(--amplify-colors-neutral-40);
background-color: var(--amplify-colors-neutral-20);
&:focus-visible {
&:after {
background-color: var(--amplify-colors-neutral-20);
}
}
&:hover {
background-color: var(--highlight-code-hover-background-color);
&:after {
background-color: var(--highlight-code-hover-background-color);
}
& + .highlight-code {
.line-highlight:before {
background-color: var(--highlight-code-hover-background-color);
}
}
}
}
}

.highlight-copy-block:hover .line-highlight::before {
background-color: var(--amplify-colors-primary-90);
/* This :has selector allows us to style the .highlight-copy-button
that is preceding the .highlight-code block so that interaction between
them looks "connected", i.e. the hover style on one triggers the
hover style on the other */
.highlight-copy-button:has(+ .highlight-code:hover) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice!!

background-color: var(--highlight-code-hover-background-color);
&:focus-visible {
&:after {
height: 4px;
bottom: -3px;
background-color: var(--highlight-code-hover-background-color);
}
}
&:hover {
&:after {
background-color: var(--highlight-code-hover-background-color);
}
}
}

@include darkMode {
background-color: var(--amplify-colors-primary-10);
.highlight-code {
position: relative;
margin-bottom: var(--amplify-space-xxs);
&:hover {
cursor: pointer;
.line-highlight:before {
background-color: var(--highlight-code-hover-background-color);
}
}
}

Expand All @@ -188,6 +255,7 @@ code:not([class]) {
padding-block: var(--amplify-space-xxxs);
color: var(--amplify-colors-white);
border-radius: var(--amplify-radii-small);
gap: var(--amplify-space-xxs);
&:hover {
color: var(--amplify-colors-white);
background-color: var(--code-copy-hover-background-color);
Expand All @@ -202,4 +270,4 @@ code:not([class]) {
--code-copy-focus-background-color: var(--amplify-colors-neutral-20);
--code-copy-focus-box-shadow: var(--amplify-colors-neutral-100);
}
}
}
Loading