Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add spinner to playground when loading #2698

Merged
merged 3 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@typespec/playground",
"comment": "Allow standalone playground to show a loading fallback",
"type": "none"
}
],
"packageName": "@typespec/playground"
}
14 changes: 12 additions & 2 deletions packages/playground/src/react/standalone.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,15 @@ import {
useToastController,
webLightTheme,
} from "@fluentui/react-components";
import { FunctionComponent, useCallback, useEffect, useId, useMemo, useState } from "react";
import {
FunctionComponent,
ReactNode,
useCallback,
useEffect,
useId,
useMemo,
useState,
} from "react";
import { createRoot } from "react-dom/client";
import { createBrowserHost } from "../browser-host.js";
import { LibraryImportOptions } from "../core.js";
Expand All @@ -19,6 +27,8 @@ import { Playground, PlaygroundProps, PlaygroundSaveData } from "./playground.js
export interface ReactPlaygroundConfig extends Partial<PlaygroundProps> {
readonly libraries: readonly string[];
readonly importConfig?: LibraryImportOptions;
/** Content to show while the playground data is loading(Libraries) */
readonly fallback?: ReactNode;
}

interface StandalonePlaygroundContext {
Expand Down Expand Up @@ -80,7 +90,7 @@ export const StandalonePlayground: FunctionComponent<ReactPlaygroundConfig> = (c
[context]
);
if (context === undefined || fixedOptions === undefined) {
return <></>;
return config.fallback;
}

const options: PlaygroundProps = {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.container {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100%;
width: 100%;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Spinner } from "@fluentui/react-components";
import style from "./loading-spinner.module.css";

export interface LoadingSpinnerProps {
message?: string;
}
export const LoadingSpinner = ({ message }: LoadingSpinnerProps) => {
return (
<div className={style["container"]}>
<Spinner />
<div>{message}</div>
</div>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
import { SwaggerUIViewer } from "@typespec/playground/react/viewers";
import { FunctionComponent, useMemo } from "react";
import { VersionData } from "./import-map";
import { LoadingSpinner } from "./loading-spinner";

import "@typespec/playground/style.css";

Expand Down Expand Up @@ -52,6 +53,7 @@ export const WebsitePlayground = ({ versionData }: WebsitePlaygroundProps) => {
importConfig={{ useShim: true }}
editorOptions={editorOptions}
footer={<PlaygroundFooter versionData={versionData} />}
fallback={<LoadingSpinner message="Loading libraries..." />}
/>
);
};
Expand Down
7 changes: 6 additions & 1 deletion packages/website/src/pages/playground.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { useEffect, useState } from "react";
import "@typespec/playground/style.css";
import { FluentLayout } from "../components/fluent-layout/fluent-layout";
import { VersionData, loadImportMap } from "../components/playground-component/import-map";
import { LoadingSpinner } from "../components/playground-component/loading-spinner";

export default function PlaygroundPage() {
return (
Expand Down Expand Up @@ -34,5 +35,9 @@ const AsyncPlayground = () => {
});
}, []);

return mod && <mod.WebsitePlayground versionData={mod.versionData} />;
return mod ? (
<mod.WebsitePlayground versionData={mod.versionData} />
) : (
<LoadingSpinner message="Loading playground..." />
);
};