Skip to content

fix(site): update Spinner component to avoid UI edge cases #16497

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

Closed
wants to merge 23 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
8da41df
refactor: clean up existing code
Parkreiner Feb 7, 2025
c9841f2
fix: update API for component
Parkreiner Feb 7, 2025
2f356bc
wip: get majority of component updated
Parkreiner Feb 7, 2025
87fd0c2
chore: get initial version of spinner update in place
Parkreiner Feb 7, 2025
e9b0e5e
docs: switch main comment to JSDoc
Parkreiner Feb 7, 2025
150370d
fix: update state definitions
Parkreiner Feb 7, 2025
b2870a6
fix: add delay safety nets
Parkreiner Feb 7, 2025
90df622
refactor: clean up current code
Parkreiner Feb 7, 2025
d082834
docs: fix typo
Parkreiner Feb 7, 2025
e094eb0
fix: remove infinite render loops
Parkreiner Feb 7, 2025
c8ba1d0
fix: more adjustments to avoid infinite re-renders
Parkreiner Feb 7, 2025
b0d020e
refactor: split up nasty state logic to make main component more read…
Parkreiner Feb 7, 2025
4560c24
fix: more render stabilization
Parkreiner Feb 7, 2025
7a668d2
docs: update comments for clarity
Parkreiner Feb 7, 2025
a69d576
refactor: clean up code more
Parkreiner Feb 7, 2025
3e0db15
docs: rewrite docs for clarity again
Parkreiner Feb 7, 2025
3567280
docs: remove typo
Parkreiner Feb 7, 2025
6ad1cf2
docs: add link reference for cursed React techniques
Parkreiner Feb 7, 2025
fc44cdb
fix: add edge-case protection for certain delay values
Parkreiner Feb 7, 2025
638ccf5
fix: make first effort towards preserving state when content for load…
Parkreiner Feb 10, 2025
ce8e555
refactor: make it harder to misuse render cache
Parkreiner Feb 10, 2025
3b058df
refactor: clean up current implementation
Parkreiner Feb 11, 2025
815c71d
docs: fix links
Parkreiner Feb 11, 2025
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
Prev Previous commit
Next Next commit
fix: update API for component
  • Loading branch information
Parkreiner committed Feb 7, 2025
commit c9841f203afbb9330a51e7b1b0f9f43e30bda20a
45 changes: 36 additions & 9 deletions site/src/components/Spinner/Spinner.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import isChromatic from "chromatic/isChromatic";
import { type VariantProps, cva } from "class-variance-authority";
import type { FC, ReactNode } from "react";
import { type FC, useEffect, useState } from "react";
import { cn } from "utils/cn";

const SPINNER_LEAF_COUNT = 8;
Expand All @@ -26,35 +26,62 @@ const spinnerVariants = cva("", {
type SpinnerProps = Readonly<
React.SVGProps<SVGSVGElement> &
VariantProps<typeof spinnerVariants> & {
children?: ReactNode;
loading?: boolean;
loading: boolean;
unmountedWhileLoading?: boolean;
spinnerStartDelayMs?: number;
}
>;

const leavesIterable = Array.from({ length: SPINNER_LEAF_COUNT }, (_, i) => i);

export const Spinner: FC<SpinnerProps> = ({
className,
size,
loading,
children,
...props
spinnerStartDelayMs = 0,
unmountedWhileLoading = false,
...delegatedProps
}) => {
if (!loading) {
// Doing some mid-render state syncs to minimize re-renders and risks of
// contradictory states. It's ugly, but it's what the React team recommends
const noDelay = spinnerStartDelayMs === 0;
const [mountSpinner, setMountSpinner] = useState(noDelay);
const unmountImmediatelyOnRerender = mountSpinner && !loading;
if (unmountImmediatelyOnRerender) {
setMountSpinner(false);
}
const mountImmediatelyOnRerender = !mountSpinner && noDelay;
if (mountImmediatelyOnRerender) {
setMountSpinner(true);
}
useEffect(() => {
if (spinnerStartDelayMs === 0) {
return;
}

const delayId = window.setTimeout(() => {
setMountSpinner(true);
}, spinnerStartDelayMs);
return () => window.clearTimeout(delayId);
}, [spinnerStartDelayMs]);

// Past this point, only showSpinner should need to be referenced
const showSpinner = loading && mountSpinner;
if (!showSpinner) {
return children;
}

return (
<svg
// Fill is the only prop that should be allowed to be overridden;
// all other props must come after destructuring
fill="currentColor"
{...delegatedProps}
viewBox="0 0 24 24"
xmlns="http://www.w3.org/2000/svg"
fill="currentColor"
className={cn(className, spinnerVariants({ size }))}
{...props}
>
<title>Loading spinner</title>
<title>Loading&hellip;</title>
{leavesIterable.map((leafIndex) => (
<rect
key={leafIndex}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ export const CreateOrganizationPageView: FC<
</fieldset>
<div className="flex flex-row gap-2">
<Button type="submit" disabled={form.isSubmitting}>
{form.isSubmitting && <Spinner />}
<Spinner loading={form.isSubmitting} />
Save
</Button>
<Button
Expand Down