Skip to content

Commit cacde95

Browse files
Merge pull request #2208 from openedx/master
sync: master to alpha
2 parents 7b40355 + 82693e1 commit cacde95

File tree

2 files changed

+64
-31
lines changed

2 files changed

+64
-31
lines changed

www/src/components/CodeBlock.tsx

+37-12
Original file line numberDiff line numberDiff line change
@@ -20,33 +20,45 @@ import {
2020
import { FormattedMessage, useIntl } from 'react-intl';
2121
import * as ParagonReact from '~paragon-react';
2222
import * as ParagonIcons from '~paragon-icons';
23+
import { ContentCopy } from '~paragon-icons';
2324
import MiyazakiCard from './exampleComponents/MiyazakiCard';
2425
import HipsterIpsum from './exampleComponents/HipsterIpsum';
2526
import ExamplePropsForm from './exampleComponents/ExamplePropsForm';
2627

27-
const { Button, Collapsible } = ParagonReact;
28+
const {
29+
Collapsible, Toast, IconButton, Icon,
30+
} = ParagonReact;
2831

2932
export type CollapsibleLiveEditorTypes = {
30-
children: React.ReactNode,
33+
children: React.ReactNode;
34+
clickToCopy: () => void;
3135
};
3236

33-
function CollapsibleLiveEditor({ children }: CollapsibleLiveEditorTypes) {
37+
function CollapsibleLiveEditor({ children, clickToCopy }: CollapsibleLiveEditorTypes) {
3438
const [collapseIsOpen, setCollapseIsOpen] = useState(false);
3539
return (
3640
<div className="pgn-doc__collapsible-live-editor">
37-
<Collapsible.Advanced
41+
<Collapsible
3842
unmountOnExit={false}
43+
styling="card-lg"
3944
open={collapseIsOpen}
4045
onToggle={(isOpen: boolean) => setCollapseIsOpen(isOpen)}
46+
withIcon
47+
title={<strong>{collapseIsOpen ? 'Hide' : 'Show'} editable code example</strong>}
4148
>
42-
<Collapsible.Trigger tag={Button} variant="link">
43-
<Collapsible.Visible whenClosed>Show code example</Collapsible.Visible>
44-
<Collapsible.Visible whenOpen>Hide code example</Collapsible.Visible>
45-
</Collapsible.Trigger>
46-
<Collapsible.Body className="mt-2">
49+
<p className="small text-gray mb-2">Any Paragon component or export may be added to the code example.</p>
50+
<div className="pgn-doc__collapsible-code-wrapper">
4751
{children}
48-
</Collapsible.Body>
49-
</Collapsible.Advanced>
52+
<IconButton
53+
className="pgn-doc__collapsible-live-editor-copy-btn"
54+
src={ContentCopy}
55+
iconAs={Icon}
56+
alt="Copy code example"
57+
onClick={clickToCopy}
58+
invertColors
59+
/>
60+
</div>
61+
</Collapsible>
5062
</div>
5163
);
5264
}
@@ -68,6 +80,12 @@ function CodeBlock({
6880
}: ICodeBlock) {
6981
const intl = useIntl();
7082
const language: any = className ? className.replace(/language-/, '') : 'jsx';
83+
const [showToast, setShowToast] = useState(false);
84+
85+
const isCodeExampleCopied = () => {
86+
navigator.clipboard.writeText(children);
87+
setShowToast(true);
88+
};
7189

7290
if (live) {
7391
return (
@@ -98,11 +116,18 @@ function CodeBlock({
98116
theme={theme}
99117
>
100118
<LivePreview className="pgn-doc__code-block-preview" />
101-
<CollapsibleLiveEditor>
119+
<CollapsibleLiveEditor clickToCopy={isCodeExampleCopied}>
102120
<LiveEditor className="pgn-doc__code-block-editor" />
103121
</CollapsibleLiveEditor>
104122
<LiveError className="pgn-doc__code-block-error" />
105123
</LiveProvider>
124+
<Toast
125+
onClose={() => setShowToast(false)}
126+
show={showToast}
127+
delay={2000}
128+
>
129+
Code example copied to clipboard!
130+
</Toast>
106131
</div>
107132
);
108133
}

www/src/components/_CodeBlock.scss

+27-19
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,34 @@
11
.pgn-doc__code-block {
2-
margin: 1rem 0 2rem;
3-
4-
// border: solid 2px $light-300;
2+
margin: $spacer 0 2rem;
3+
background-color: $light-200;
4+
border: solid 2px $light-300;
55
border-radius: $border-radius;
6-
}
76

8-
.pgn-doc__code-block-preview {
9-
padding-bottom: 1rem;
10-
}
7+
.pgn-doc__code-block-preview {
8+
padding: $spacer;
9+
}
1110

12-
.pgn-doc__code-block-editor,
13-
.prism-code {
14-
font-size: $font-size-base;
15-
font-family: monospace;
11+
.pgn-doc__code-block-editor,
12+
.prism-code {
13+
font-size: $font-size-base;
14+
font-family: monospace;
15+
border-radius: $input-border-radius;
16+
}
1617

17-
// box-shadow: 1px 0 0 0 #282632, -1px 0 0 0 #282632;
18-
border-radius: $input-border-radius;
19-
}
18+
.pgn-doc__code-block-error {
19+
background-color: $warning-200;
20+
padding: $spacer;
21+
font-size: $font-size-sm;
22+
margin: 0;
23+
}
24+
25+
.pgn-doc__collapsible-code-wrapper {
26+
position: relative;
2027

21-
.pgn-doc__code-block-error {
22-
background: $warning-200;
23-
padding: 1rem;
24-
font-size: $font-size-sm;
25-
margin: 0;
28+
.pgn-doc__collapsible-live-editor-copy-btn {
29+
position: absolute;
30+
top: map-get($spacers, 2);
31+
right: map-get($spacers, 2);
32+
}
33+
}
2634
}

0 commit comments

Comments
 (0)