Skip to content

Commit f01daac

Browse files
author
Valentin Hervieu
committed
Introduce ProgressButton to have more possibilities for the loader
1 parent 5e5920f commit f01daac

File tree

6 files changed

+74
-38
lines changed

6 files changed

+74
-38
lines changed

packages/app/src/app/pages/Sandbox/Editor/Header/index.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import SettingsIcon from 'react-icons/lib/md/settings';
88
import ShareIcon from 'react-icons/lib/md/share';
99
import SaveIcon from 'react-icons/lib/md/save';
1010
import { Button } from '@codesandbox/common/lib/components/Button';
11+
import ProgressButton from '@codesandbox/common/lib/components/ProgressButton';
1112
import SignInButton from 'app/pages/common/SignInButton';
1213

1314
import { saveAllModules } from 'app/store/modules/editor/utils';
@@ -75,21 +76,20 @@ const ForkButton = ({
7576
isForking,
7677
style,
7778
}: ForkButtonProps) => (
78-
<Button
79+
<ProgressButton
7980
onClick={() => {
8081
signals.editor.forkSandboxClicked();
8182
}}
8283
style={style}
8384
secondary={secondary}
84-
disabled={isForking}
8585
loading={isForking}
8686
small
8787
>
8888
<>
8989
<Fork style={{ marginRight: '.5rem' }} />
9090
{isForking ? 'Forking...' : 'Fork'}
9191
</>
92-
</Button>
92+
</ProgressButton>
9393
);
9494

9595
const PickButton = ({ store, signals, secondary, style }: ButtonProps) => {

packages/app/src/app/pages/Sandbox/Editor/Workspace/items/More/index.tsx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React from 'react';
22
import { inject, observer } from 'mobx-react';
33

4-
import { Button } from '@codesandbox/common/lib/components/Button';
4+
import ProgressButton from '@codesandbox/common/lib/components/ProgressButton';
55
import SignInButton from 'app/pages/common/SignInButton';
66
import Margin from '@codesandbox/common/lib/components/spacing/Margin';
77
import track from '@codesandbox/common/lib/utils/analytics';
@@ -35,15 +35,14 @@ class More extends React.Component<Props> {
3535
<Description>{message}</Description>
3636
<Margin margin={1}>
3737
{!owned ? (
38-
<Button
38+
<ProgressButton
3939
small
4040
block
41-
disabled={isForkingSandbox}
4241
loading={isForkingSandbox}
4342
onClick={this.forkSandbox}
4443
>
4544
{isForkingSandbox ? 'Forking Sandbox...' : 'Fork Sandbox'}
46-
</Button>
45+
</ProgressButton>
4746
) : (
4847
<SignInButton block />
4948
)}

packages/common/src/components/Button/elements.ts

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
import styled, { css, keyframes } from 'styled-components';
1+
import styled, { css } from 'styled-components';
22
import Link from 'react-router-dom/Link';
33
import theme from '../../theme';
44

55
export type OptionProps = {
66
theme: any;
77
disabled?: boolean;
8-
loading?: boolean;
98
red?: boolean;
109
secondary?: boolean;
1110
danger?: boolean;
@@ -98,39 +97,14 @@ const getBorder = ({
9897
return '2px solid #66B9F4';
9998
};
10099

101-
const progressAnimation = keyframes`
102-
0% { transform: scale(0); }
103-
100% { transform: scale(1); }
104-
`;
105-
106-
const getLoader = ({ disabled }: OptionProps) => css`
107-
&:before {
108-
content: ' ';
109-
position: absolute;
110-
width: 20px;
111-
height: 20px;
112-
left: 50%;
113-
top: 50%;
114-
bottom: 0;
115-
margin-left: -10px;
116-
margin-top: -10px;
117-
border-radius: 50%;
118-
opacity: 0.5;
119-
background: white;
120-
animation: ${progressAnimation} 0.7s ease-in-out infinite alternate;
121-
}
122-
`;
123-
124100
const styles = css<{
125101
disabled?: boolean;
126-
loading?: boolean;
127102
secondary?: boolean;
128103
danger?: boolean;
129104
red?: boolean;
130105
block?: boolean;
131106
small?: boolean;
132107
}>`
133-
position: relative;
134108
transition: 0.3s ease all;
135109
font-family: Poppins, Roboto, sans-serif;
136110
@@ -176,8 +150,6 @@ const styles = css<{
176150
${props => getBackgroundHoverColor(props)};
177151
${props => getHoverColor(props)};
178152
}
179-
180-
${props => props.loading && getLoader(props)};
181153
`;
182154

183155
export const LinkButton = styled(Link)`

packages/common/src/components/Button/index.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React from 'react';
22
import { LinkButton, AButton, Button } from './elements';
33

4-
type Props = {
4+
export type Props = {
55
to?: string;
66
href?: string;
77
small?: boolean;
@@ -11,7 +11,6 @@ type Props = {
1111
children?: React.ReactElement | string;
1212
disabled?: boolean;
1313
type?: 'button' | 'reset' | 'submit';
14-
loading?: boolean;
1514
secondary?: boolean;
1615
};
1716

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import styled, { css, keyframes } from 'styled-components';
2+
import theme from '../../theme';
3+
4+
const loaderAnimation = keyframes`
5+
0% { background-color: ${theme.secondary()}; }
6+
50%, 100% { background-color: ${theme.secondary.lighten(0.5)()}; }
7+
`;
8+
9+
export const Wrapper = styled.div`
10+
position: relative;
11+
`;
12+
13+
const circle = css`
14+
width: 10px;
15+
height: 10px;
16+
border-radius: 50%;
17+
background: ${theme.secondary()};
18+
opacity: 0.5;
19+
animation: ${loaderAnimation} 1s infinite linear alternate;
20+
`;
21+
22+
export const Loader = styled.div`
23+
position: absolute;
24+
top: 50%;
25+
left: 50%;
26+
transform: translate(-50%, -50%);
27+
${circle} animation-delay: 0.5s;
28+
29+
&:before {
30+
content: ' ';
31+
position: absolute;
32+
left: -15px;
33+
${circle};
34+
animation-delay: 0s;
35+
}
36+
37+
&:after {
38+
content: ' ';
39+
position: absolute;
40+
left: 15px;
41+
${circle};
42+
animation-delay: 1s;
43+
}
44+
`;
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import React from 'react';
2+
import { Button, Props as ButtonProps } from '../Button';
3+
import { Wrapper, Loader } from './elements';
4+
5+
type Props = ButtonProps & {
6+
loading?: boolean;
7+
};
8+
9+
function ProgressButton({
10+
loading = false,
11+
disabled = false,
12+
...props
13+
}: Props) {
14+
return (
15+
<Wrapper>
16+
<Button disabled={disabled || loading} {...props} />
17+
{loading && <Loader />}
18+
</Wrapper>
19+
);
20+
}
21+
22+
export default ProgressButton;

0 commit comments

Comments
 (0)