Skip to content

Commit

Permalink
chore(docs): update example and demo copy buttons (#2078)
Browse files Browse the repository at this point in the history
  • Loading branch information
calebpollman authored Jun 13, 2022
1 parent 28bf92d commit b1d983c
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 37 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
import * as React from 'react';
import { CopyToClipboard } from 'react-copy-to-clipboard';

import { Button } from '@aws-amplify/ui-react';
import { Button, ButtonProps } from '@aws-amplify/ui-react';

export const Copy = ({ text, size, variation, className }) => {
interface CopyButtonProps
extends Pick<ButtonProps, 'className' | 'size' | 'variation'> {
copyText: string;
}

export const CopyButton = ({
className,
copyText,
size,
variation,
}: CopyButtonProps) => {
const [copied, setCopied] = React.useState(false);

const copy = () => {
Expand All @@ -14,12 +24,12 @@ export const Copy = ({ text, size, variation, className }) => {
};

return (
<CopyToClipboard text={text} onCopy={copy}>
<CopyToClipboard text={copyText} onCopy={copy}>
<Button
className={className}
isLoading={copied}
size={size}
variation={variation}
isLoading={copied}
className={className}
>
{copied ? 'Copied!' : 'Copy'}
</Button>
Expand Down
15 changes: 6 additions & 9 deletions docs/src/components/Demo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
Button,
useTheme,
} from '@aws-amplify/ui-react';
import { CopyButton } from './CopyButton';

interface DemoProps {
children: React.ReactNode;
Expand Down Expand Up @@ -64,15 +65,11 @@ export const Demo = ({
position="relative"
backgroundColor={tokens.colors.background.secondary}
>
<CopyToClipboard text={code} onCopy={copy}>
<Button
size="small"
className="example-copy-button"
disabled={copied}
>
{copied ? 'Copied!' : 'Copy'}
</Button>
</CopyToClipboard>
<CopyButton
className="example-copy-button"
copyText={code}
size="small"
/>
<Highlight Prism={defaultProps.Prism} code={code} language="jsx">
{({ className, style, tokens, getLineProps, getTokenProps }) => (
<pre
Expand Down
23 changes: 7 additions & 16 deletions docs/src/components/Example.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as React from 'react';
import { CopyToClipboard } from 'react-copy-to-clipboard';

import { Card, Button, Flex, useTheme } from '@aws-amplify/ui-react';
import { Card, Flex, useTheme } from '@aws-amplify/ui-react';
import { CopyButton } from './CopyButton';

interface ExampleProps {
children: React.ReactNode;
Expand All @@ -22,29 +22,20 @@ export function Example({ children, className = '' }: ExampleProps) {
}

export function ExampleCode({ children }) {
const [copied, setCopied] = React.useState(false);
const [text, setText] = React.useState('');
const ref = React.useRef(null);

React.useLayoutEffect(() => {
setText(ref.current.innerText);
return () => {};
}, [children]);

const copy = () => {
setCopied(true);
setTimeout(() => {
setCopied(false);
}, 2000);
};

return (
<div className="example-code">
<CopyToClipboard text={text} onCopy={copy}>
<Button size="small" isLoading={copied} className="example-copy-button">
{copied ? 'Copied!' : 'Copy'}
</Button>
</CopyToClipboard>
<CopyButton
className="example-copy-button"
copyText={text}
size="small"
/>
<div ref={ref}>{children}</div>
</div>
);
Expand Down
8 changes: 3 additions & 5 deletions docs/src/pages/[platform]/index.page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,14 @@ import {
Image,
Link,
Text,
TextField,
ToggleButton,
ToggleButtonGroup,
View,
useTheme,
useBreakpointValue,
} from '@aws-amplify/ui-react';

import { Copy } from '@/components/Copy';
import { CopyButton } from '@/components/CopyButton';
import { Footer } from '@/components/Layout/Footer';
import { HomeLogo } from '../HomeLogo';
import { HomePrimitivePreview } from '../HomePrimitivePreview';
Expand Down Expand Up @@ -103,11 +102,10 @@ const HomePage = ({ colorMode }) => {
<p className="install-code__content">
{frameworkInstallScript}
</p>
<Copy
<CopyButton
className="install-code__button"
size=""
copyText={frameworkInstallScript}
variation="link"
text={frameworkInstallScript}
/>
</code>
</Flex>
Expand Down
7 changes: 5 additions & 2 deletions docs/src/styles/docs/example.scss
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@
}

.example-copy-button {
background: var(--amplify-colors-background-secondary);
position: absolute;
right: var(--amplify-space-medium);
top: var(--amplify-space-small);
right: var(--amplify-space-small);
}

.docs-component-demo {
Expand All @@ -37,11 +38,13 @@

pre {
white-space: pre-wrap;
margin: 0;
}

& .example-copy-button {
background: var(--amplify-colors-background-secondary);
bottom: var(--amplify-space-small);
top: auto;
right: var(--amplify-space-small);
right: var(--amplify-space-medium);
}
}

0 comments on commit b1d983c

Please sign in to comment.