Skip to content

Commit

Permalink
chore(docs): make ProgressBar rounded example autoplay (uber#5177)
Browse files Browse the repository at this point in the history
  • Loading branch information
lhbrennan authored Sep 28, 2022
1 parent c148eec commit 1d6bd31
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 64 deletions.
63 changes: 31 additions & 32 deletions documentation-site/examples/progress-bar/rounded.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,37 @@ import {Button} from 'baseui/button';
import {ProgressBarRounded} from 'baseui/progress-bar';
import {useStyletron} from 'baseui';

// https://overreacted.io/making-setinterval-declarative-with-react-hooks/
function useInterval(callback: () => void, delay: number | null) {
const savedCallback = React.useRef(() => {});

// Remember the latest callback.
React.useEffect(() => {
savedCallback.current = callback;
}, [callback]);

// Set up the interval.
React.useEffect(() => {
function tick() {
savedCallback.current();
}
if (delay !== null) {
let id = setInterval(tick, delay);
return () => clearInterval(id);
}
}, [delay]);
}

export default function Example() {
const [css] = useStyletron();
// Mimic some loading
const [progress, setProgress] = React.useState(0);
const tracker = React.useRef(0);
React.useEffect(() => {
tracker.current = progress;
}, [progress]);
return (
<div className={css({display: 'flex'})}>
<Button
kind="secondary"
size="compact"
shape="pill"
onClick={() => {
if (tracker.current >= 1) {
setProgress(0);
return;
}
function tick() {
setProgress(p => p + Math.random() * 0.33);
if (tracker.current < 1) {
setTimeout(tick, Math.random() * 1000);
}
}
tick();
}}
>
{progress >= 1 ? 'Reset' : 'Load'}
</Button>
<div className={css({width: '10px'})} />
<ProgressBarRounded progress={progress} />
</div>
);

useInterval(() => {
if (progress < 1) {
setProgress(progress + 0.1);
} else {
setProgress(0);
}
}, 1000);

return <ProgressBarRounded progress={progress} />;
}
63 changes: 31 additions & 32 deletions documentation-site/examples/progress-bar/rounded.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,37 @@ import {Button} from 'baseui/button';
import {ProgressBarRounded} from 'baseui/progress-bar';
import {useStyletron} from 'baseui';

// https://overreacted.io/making-setinterval-declarative-with-react-hooks/
function useInterval(callback: () => void, delay: number | null) {
const savedCallback = React.useRef(() => {});

// Remember the latest callback.
React.useEffect(() => {
savedCallback.current = callback;
}, [callback]);

// Set up the interval.
React.useEffect(() => {
function tick() {
savedCallback.current();
}
if (delay !== null) {
let id = setInterval(tick, delay);
return () => clearInterval(id);
}
}, [delay]);
}

export default function Example() {
const [css] = useStyletron();
// Mimic some loading
const [progress, setProgress] = React.useState(0);
const tracker = React.useRef(0);
React.useEffect(() => {
tracker.current = progress;
}, [progress]);
return (
<div className={css({display: 'flex'})}>
<Button
kind="secondary"
size="compact"
shape="pill"
onClick={() => {
if (tracker.current >= 1) {
setProgress(0);
return;
}
function tick() {
setProgress(p => p + Math.random() * 0.33);
if (tracker.current < 1) {
setTimeout(tick, Math.random() * 1000);
}
}
tick();
}}
>
{progress >= 1 ? 'Reset' : 'Load'}
</Button>
<div className={css({width: '10px'})} />
<ProgressBarRounded progress={progress} />
</div>
);

useInterval(() => {
if (progress < 1) {
setProgress(progress + 0.1);
} else {
setProgress(0);
}
}, 1000);

return <ProgressBarRounded progress={progress} />;
}

0 comments on commit 1d6bd31

Please sign in to comment.