Skip to content

Commit 776b662

Browse files
feat: Add loading state to RunButton in CodeBlock component (#237)
* feat: Add loading state to RunButton in CodeBlock component * audited
1 parent 4827e5d commit 776b662

File tree

3 files changed

+33
-19
lines changed

3 files changed

+33
-19
lines changed

package-lock.json

Lines changed: 12 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/components/Common/CodeBlock.jsx

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React, { useEffect, useState } from 'react';
22
import PropTypes from 'prop-types';
33
import { Highlight, Prism, themes } from 'prism-react-renderer';
44
import Editor from 'react-simple-code-editor';
5-
import { alpha, Box, Button } from '@mui/material';
5+
import { alpha, Box, Button, CircularProgress } from '@mui/material';
66
import { styled, useTheme } from '@mui/material/styles';
77
import { PlayArrowOutlined } from '@mui/icons-material';
88
import { CopyButton } from './CopyButton';
@@ -25,25 +25,32 @@ const StyledEditor = styled((props) => <Editor padding={0} textareaClassName={'c
2525
* @return {JSX.Element}
2626
* @constructor
2727
*/
28-
export const RunButton = ({ code, onRun }) => {
28+
export const RunButton = ({ code, onRun, loading }) => {
2929
return (
30-
<Button variant="outlined" endIcon={<PlayArrowOutlined />} onClick={() => onRun(code)} data-testid="code-block-run">
31-
Run
30+
<Button
31+
variant="outlined"
32+
endIcon={loading ? <CircularProgress size={24} color="inherit" /> : <PlayArrowOutlined />}
33+
onClick={() => onRun(code)}
34+
disabled={loading}
35+
data-testid="code-block-run"
36+
>
37+
{loading ? 'Running...' : 'Run'}
3238
</Button>
3339
);
3440
};
3541

3642
RunButton.propTypes = {
3743
code: PropTypes.string.isRequired,
3844
onRun: PropTypes.func.isRequired,
45+
loading: PropTypes.bool,
3946
};
4047

4148
/**
4249
* Code block with syntax highlighting
4350
* @return {JSX.Element}
4451
* @constructor
4552
*/
46-
export const CodeBlock = ({ codeStr, language, withRunButton, onRun, title, editable = true }) => {
53+
export const CodeBlock = ({ codeStr, language, withRunButton, onRun, title, editable = true, loading }) => {
4754
const [code, setCode] = useState(codeStr);
4855
const theme = useTheme();
4956
const prismTheme = theme.palette.mode === 'light' ? themes.nightOwlLight : themes.vsDark;
@@ -102,7 +109,7 @@ export const CodeBlock = ({ codeStr, language, withRunButton, onRun, title, edit
102109
>
103110
{withRunButton && onRun && (
104111
<Box sx={{ flexGrow: '1' }}>
105-
<RunButton code={code} onRun={onRun} />
112+
<RunButton code={code} onRun={onRun} loading={loading} />
106113
</Box>
107114
)}
108115
{title && <Box>{title}</Box>}
@@ -131,4 +138,5 @@ CodeBlock.propTypes = {
131138
onRun: PropTypes.func,
132139
title: PropTypes.string,
133140
editable: PropTypes.bool, // by default code block is editable
141+
loading: PropTypes.bool,
134142
};

src/components/InteractiveTutorial/MdxComponents/MdxCodeBlock.jsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,25 @@ export const MdxCodeBlock = ({ children }) => {
1717
const language = className.replace(/language-/, '');
1818
const withRunButton = children.props.withRunButton && bigIntJSON.parse(children.props.withRunButton);
1919
const { setResult } = useTutorial();
20+
const [loading, setLoading] = React.useState(false);
2021

2122
const handleRun = (code) => {
23+
setLoading(true);
2224
setResult('{}');
2325
requestFromCode(code, false)
2426
.then((res) => {
2527
setResult(() => bigIntJSON.stringify(res));
28+
setLoading(false);
2629
})
2730
.catch((err) => {
2831
setResult(() => bigIntJSON.stringify(err));
32+
setLoading(false);
2933
});
3034
};
3135

32-
return <CodeBlock codeStr={code} language={language} withRunButton={withRunButton} onRun={handleRun} />;
36+
return (
37+
<CodeBlock codeStr={code} language={language} withRunButton={withRunButton} onRun={handleRun} loading={loading} />
38+
);
3339
};
3440

3541
MdxCodeBlock.propTypes = {

0 commit comments

Comments
 (0)