Skip to content

Commit 5946bd9

Browse files
committed
chore(website): Fixes for Concurrent Rendering
1 parent ba96bb6 commit 5946bd9

File tree

15 files changed

+146
-92
lines changed

15 files changed

+146
-92
lines changed

packages/documentation/src/components/DemoSandbox/CodePreview.module.scss

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,5 @@
1616
}
1717

1818
.offset {
19-
@include rmd-sheet-theme(margin-left, static-width);
20-
@include rmd-utils-rtl {
21-
@include rmd-sheet-theme(margin-right, static-width);
22-
23-
margin-left: auto;
24-
}
19+
@include rmd-utils-rtl-auto(margin-left, rmd-sheet-theme-var(static-width));
2520
}

packages/documentation/src/components/DemoSandbox/CodePreview.tsx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
import { ReactElement, useEffect, useRef } from "react";
22
import cn from "classnames";
33
import { IFiles } from "codesandbox-import-utils/lib/api/define";
4-
import { CircularProgress } from "@react-md/progress";
54

65
import CodeBlock from "components/CodeBlock";
76

8-
import FileNotFound from "./FileNotFound";
9-
107
import styles from "./CodePreview.module.scss";
8+
import FileNotFound from "./FileNotFound";
9+
import { LoadingCode } from "./LoadingCode";
1110

1211
export interface CodePreviewProps {
1312
loading: boolean;
@@ -49,7 +48,7 @@ export default function CodePreview({
4948
}, [content]);
5049

5150
if (loading) {
52-
return <CircularProgress id="loading-code" />;
51+
return <LoadingCode offset={offset} />;
5352
}
5453

5554
if (!content) {
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
@use 'react-md' as *;
2+
3+
.container {
4+
@include rmd-app-bar-theme(top, prominent-dense-height);
5+
6+
align-items: center;
7+
bottom: 0;
8+
display: flex;
9+
left: 0;
10+
position: fixed;
11+
right: 0;
12+
z-index: 1;
13+
}
14+
15+
.offset {
16+
@include rmd-utils-rtl-auto(left, rmd-sheet-theme-var(static-width));
17+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import type { ReactElement } from "react";
2+
import cn from "classnames";
3+
import { CircularProgress } from "@react-md/progress";
4+
5+
import styles from "./LoadingCode.module.scss";
6+
7+
export interface LoadingCodeProps {
8+
offset: boolean;
9+
}
10+
11+
export function LoadingCode({ offset }: LoadingCodeProps): ReactElement {
12+
return (
13+
<div
14+
className={cn(styles.container, {
15+
[styles.offset]: offset,
16+
})}
17+
>
18+
<CircularProgress id="loading-code" />
19+
</div>
20+
);
21+
}
Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { useEffect, useState, useRef } from "react";
2-
import { useToggle } from "@react-md/utils";
32
import { IFiles } from "codesandbox-import-utils/lib/api/define";
43
import { ThemeMode } from "components/Theme";
54
import { getSandboxByQuery } from "utils/getSandbox";
@@ -21,43 +20,37 @@ export default function useSandbox(
2120
{ js, pkg, name, theme, pathname }: SandboxQuery
2221
): ReturnValue {
2322
const [sandbox, setSandbox] = useState(defaultSandbox);
24-
const [loading, startLoading, stopLoading] = useToggle(!sandbox);
23+
const [isLoading, setLoading] = useState(!sandbox);
2524
const prevJs = useRef(js);
26-
if (prevJs.current !== js) {
27-
prevJs.current = js;
28-
startLoading();
29-
}
25+
const loading = isLoading || prevJs.current !== js;
3026

3127
useEffect(() => {
32-
if (defaultSandbox && !loading) {
28+
if (prevJs.current === js) {
3329
return;
3430
}
3531

32+
prevJs.current = js;
3633
if (!pkg || !name || !pathname.startsWith("/sandbox")) {
37-
stopLoading();
38-
if (sandbox) {
39-
setSandbox(null);
40-
}
34+
setSandbox(null);
35+
setLoading(false);
4136
return;
4237
}
4338

4439
let cancelled = false;
45-
(async function load() {
46-
startLoading();
40+
setLoading(true);
41+
async function load(): Promise<void> {
4742
const sandbox = await getSandboxByQuery({ js, pkg, name, theme });
4843
if (!cancelled) {
44+
setLoading(false);
4945
setSandbox(sandbox);
50-
stopLoading();
5146
}
52-
})();
47+
}
48+
load();
5349

5450
return () => {
5551
cancelled = true;
56-
stopLoading();
5752
};
58-
// only want to run when these dependencies change
59-
// eslint-disable-next-line react-hooks/exhaustive-deps
60-
}, [pkg, name, pathname, js, theme]);
53+
}, [js, pkg, name, theme, pathname]);
6154

6255
return { sandbox, loading };
6356
}

packages/documentation/src/components/Demos/Alert/UpdatingMessagePriority.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,11 @@ function UpdatingMessagePriority(): ReactElement {
2525
const queue = useQueue<ExampleMessage>();
2626
const [running, setRunning] = useState(false);
2727

28-
if (running && !queue.length) {
29-
setRunning(false);
30-
}
28+
useEffect(() => {
29+
if (running && !queue.length) {
30+
setRunning(false);
31+
}
32+
}, [running, queue]);
3133

3234
const exampleNextFlow = useCallback(() => {
3335
addMessage({

packages/documentation/src/components/Demos/Chip/ActionChips/Blinds.tsx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ReactElement, useState } from "react";
1+
import { ReactElement, useEffect, useState } from "react";
22
import cn from "classnames";
33
import {
44
CSSTransitionClassNames,
@@ -21,9 +21,11 @@ const CLASSNAMES: CSSTransitionClassNames = {
2121

2222
export default function Blinds({ visible }: BlindsProps): ReactElement | null {
2323
const [exited, setExited] = useState(true);
24-
if (visible && exited) {
25-
setExited(false);
26-
}
24+
useEffect(() => {
25+
if (visible && exited) {
26+
setExited(false);
27+
}
28+
}, [visible, exited]);
2729

2830
const hide = (): void => setExited(true);
2931

packages/documentation/src/components/Demos/Form/FileInputs/ErrorModal.tsx

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ReactElement, useRef, useState } from "react";
1+
import { ReactElement, useEffect, useState } from "react";
22
import { Button } from "@react-md/button";
33
import {
44
Dialog,
@@ -20,17 +20,15 @@ export default function ErrorModal({
2020
errors,
2121
clearErrors,
2222
}: ErrorModalProps): ReactElement {
23+
// Having the visibility being derived on the `errors.length > 0` would make
24+
// it so the errors are cleared during the exit animation. To fix this, keep a
25+
// separate `visible` state and set it to `true` whenever a new error is
26+
// added. When the modal is closed, set the `visible` state to false and wait
27+
// until the modal has closed before clearing the errors.
2328
const [visible, setVisible] = useState(false);
24-
const prevErrors = useRef(errors);
25-
26-
// why?
27-
// makes it so the errors don't disappear during the exit animation
28-
if (errors !== prevErrors.current) {
29-
prevErrors.current = errors;
30-
if (!visible && errors.length) {
31-
setVisible(true);
32-
}
33-
}
29+
useEffect(() => {
30+
setVisible(errors.length > 0);
31+
}, [errors]);
3432

3533
const onRequestClose = (): void => {
3634
setVisible(false);

packages/documentation/src/components/Demos/Form/SelectFields/NativeSelectExample.tsx

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ReactElement } from "react";
1+
import { ReactElement, useEffect } from "react";
22
import {
33
Checkbox,
44
Fieldset,
@@ -50,9 +50,11 @@ export default function NativeSelectExample(): ReactElement {
5050
const [size, handleSizeChange] = useChoice("4");
5151
const [multiple, handleMultipleChange] = useChecked(false);
5252
const [optgroup, handleOptgroupChange] = useChecked(false);
53-
if (multiple && icon) {
54-
setIcon(false);
55-
}
53+
useEffect(() => {
54+
if (multiple && icon) {
55+
setIcon(false);
56+
}
57+
}, [multiple, icon, setIcon]);
5658

5759
return (
5860
<TextFieldThemeConfig
@@ -77,7 +79,9 @@ export default function NativeSelectExample(): ReactElement {
7779
>
7880
{/* eslint-disable-next-line jsx-a11y/control-has-associated-label */}
7981
{label && <option key="label" value="" disabled hidden />}
80-
{!optgroup && <States states={states} readOnly={readOnly} />}
82+
{!optgroup && (
83+
<States key="optgroup" states={states} readOnly={readOnly} />
84+
)}
8185
{optgroup &&
8286
Object.entries(grouped).map(([letter, states]) => (
8387
<optgroup key={letter} label={letter}>

packages/documentation/src/components/Demos/Form/Sliders/ColorSlider.tsx

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ReactElement, useRef } from "react";
1+
import { ReactElement, useEffect, useRef } from "react";
22
import {
33
Slider,
44
SliderRequiredProps,
@@ -43,15 +43,17 @@ export default function ColorSlider({
4343
// with it, need to also update the slider number value
4444
const prevValue = useRef(value);
4545
const prevNumberValue = useRef(numberValue);
46-
if (prevValue.current !== value && prevNumberValue.current !== value) {
47-
prevValue.current = value;
48-
prevNumberValue.current = value;
49-
setNumber(value);
50-
} else if (prevNumberValue.current !== numberValue) {
51-
prevValue.current = numberValue;
52-
prevNumberValue.current = numberValue;
53-
setValue(numberValue);
54-
}
46+
useEffect(() => {
47+
if (prevValue.current !== value && prevNumberValue.current !== value) {
48+
prevValue.current = value;
49+
prevNumberValue.current = value;
50+
setNumber(value);
51+
} else if (prevNumberValue.current !== numberValue) {
52+
prevValue.current = numberValue;
53+
prevNumberValue.current = numberValue;
54+
setValue(numberValue);
55+
}
56+
}, [value, numberValue, setNumber, setValue]);
5557

5658
return (
5759
<Slider

0 commit comments

Comments
 (0)