Skip to content

Commit

Permalink
Merge pull request #2208 from openedx/master
Browse files Browse the repository at this point in the history
sync: master to alpha
  • Loading branch information
edx-requirements-bot authored and PKulkoRaccoonGang committed Aug 1, 2024
2 parents 0783c55 + 82693e1 commit e339a7d
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 31 deletions.
49 changes: 37 additions & 12 deletions www/src/components/CodeBlock.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,33 +20,45 @@ import {
import { FormattedMessage, useIntl } from 'react-intl';
import * as ParagonReact from '~paragon-react';
import * as ParagonIcons from '~paragon-icons';
import { ContentCopy } from '~paragon-icons';
import MiyazakiCard from './exampleComponents/MiyazakiCard';
import HipsterIpsum from './exampleComponents/HipsterIpsum';
import ExamplePropsForm from './exampleComponents/ExamplePropsForm';

const { Button, Collapsible } = ParagonReact;
const {
Collapsible, Toast, IconButton, Icon,
} = ParagonReact;

export type CollapsibleLiveEditorTypes = {
children: React.ReactNode,
children: React.ReactNode;
clickToCopy: () => void;
};

function CollapsibleLiveEditor({ children }: CollapsibleLiveEditorTypes) {
function CollapsibleLiveEditor({ children, clickToCopy }: CollapsibleLiveEditorTypes) {
const [collapseIsOpen, setCollapseIsOpen] = useState(false);
return (
<div className="pgn-doc__collapsible-live-editor">
<Collapsible.Advanced
<Collapsible
unmountOnExit={false}
styling="card-lg"
open={collapseIsOpen}
onToggle={(isOpen: boolean) => setCollapseIsOpen(isOpen)}
withIcon
title={<strong>{collapseIsOpen ? 'Hide' : 'Show'} editable code example</strong>}
>
<Collapsible.Trigger tag={Button} variant="link">
<Collapsible.Visible whenClosed>Show code example</Collapsible.Visible>
<Collapsible.Visible whenOpen>Hide code example</Collapsible.Visible>
</Collapsible.Trigger>
<Collapsible.Body className="mt-2">
<p className="small text-gray mb-2">Any Paragon component or export may be added to the code example.</p>
<div className="pgn-doc__collapsible-code-wrapper">
{children}
</Collapsible.Body>
</Collapsible.Advanced>
<IconButton
className="pgn-doc__collapsible-live-editor-copy-btn"
src={ContentCopy}
iconAs={Icon}
alt="Copy code example"
onClick={clickToCopy}
invertColors
/>
</div>
</Collapsible>
</div>
);
}
Expand All @@ -68,6 +80,12 @@ function CodeBlock({
}: ICodeBlock) {
const intl = useIntl();
const language: any = className ? className.replace(/language-/, '') : 'jsx';
const [showToast, setShowToast] = useState(false);

const isCodeExampleCopied = () => {
navigator.clipboard.writeText(children);
setShowToast(true);
};

if (live) {
return (
Expand Down Expand Up @@ -98,11 +116,18 @@ function CodeBlock({
theme={theme}
>
<LivePreview className="pgn-doc__code-block-preview" />
<CollapsibleLiveEditor>
<CollapsibleLiveEditor clickToCopy={isCodeExampleCopied}>
<LiveEditor className="pgn-doc__code-block-editor" />
</CollapsibleLiveEditor>
<LiveError className="pgn-doc__code-block-error" />
</LiveProvider>
<Toast
onClose={() => setShowToast(false)}
show={showToast}
delay={2000}
>
Code example copied to clipboard!
</Toast>
</div>
);
}
Expand Down
46 changes: 27 additions & 19 deletions www/src/components/_CodeBlock.scss
Original file line number Diff line number Diff line change
@@ -1,26 +1,34 @@
.pgn-doc__code-block {
margin: 1rem 0 2rem;

// border: solid 2px $light-300;
margin: $spacer 0 2rem;
background-color: $light-200;
border: solid 2px $light-300;
border-radius: $border-radius;
}

.pgn-doc__code-block-preview {
padding-bottom: 1rem;
}
.pgn-doc__code-block-preview {
padding: $spacer;
}

.pgn-doc__code-block-editor,
.prism-code {
font-size: $font-size-base;
font-family: monospace;
.pgn-doc__code-block-editor,
.prism-code {
font-size: $font-size-base;
font-family: monospace;
border-radius: $input-border-radius;
}

// box-shadow: 1px 0 0 0 #282632, -1px 0 0 0 #282632;
border-radius: $input-border-radius;
}
.pgn-doc__code-block-error {
background-color: $warning-200;
padding: $spacer;
font-size: $font-size-sm;
margin: 0;
}

.pgn-doc__collapsible-code-wrapper {
position: relative;

.pgn-doc__code-block-error {
background: $warning-200;
padding: 1rem;
font-size: $font-size-sm;
margin: 0;
.pgn-doc__collapsible-live-editor-copy-btn {
position: absolute;
top: map-get($spacers, 2);
right: map-get($spacers, 2);
}
}
}

0 comments on commit e339a7d

Please sign in to comment.