Files

57 lines
1.9 KiB
TypeScript
Raw Permalink Normal View History

import React, { Suspense } from 'react';
2026-10-05 18:10:18 -04:00
import { useLocation, useRouter } from '@tanstack/react-router';
import ErrorBoundary from '@/components/elements/ErrorBoundary';
2026-10-12 09:04:03 -04:00
import { replaceable, useSkin } from '@/themes/skins';
2019-06-29 16:14:32 -07:00
2020-07-04 12:39:55 -07:00
export type SpinnerSize = 'small' | 'base' | 'large';
2026-10-12 09:04:03 -04:00
export interface SpinnerProps {
2019-12-22 14:53:27 -08:00
size?: SpinnerSize;
centered?: boolean;
2020-07-04 12:39:55 -07:00
isBlue?: boolean;
2026-10-05 18:10:18 -04:00
children?: React.ReactNode;
2019-12-22 14:53:27 -08:00
}
2026-10-12 09:04:03 -04:00
type SpinnerComponentType = ((props: SpinnerProps) => React.JSX.Element) & {
2026-10-05 18:10:18 -04:00
displayName?: string;
Size: Record<'SMALL' | 'BASE' | 'LARGE', SpinnerSize>;
2026-10-12 09:04:03 -04:00
Suspense: ((props: SpinnerProps) => React.JSX.Element) & { displayName?: string };
2026-10-05 18:10:18 -04:00
};
2026-10-12 09:04:03 -04:00
function CoreSpinner({ size = 'base', centered = false, isBlue = false }: SpinnerProps) {
const ui = useSkin('Spinner', { size, blue: isBlue, centered });
const spinner = <span {...ui('root')} />;
2026-10-12 09:04:03 -04:00
return centered ? <div {...ui('container')}>{spinner}</div> : spinner;
}
2026-10-08 16:53:20 -04:00
2026-10-12 09:04:03 -04:00
const Spinner = replaceable('Spinner', CoreSpinner) as SpinnerComponentType;
2020-02-08 15:23:08 -08:00
Spinner.Size = {
SMALL: 'small',
BASE: 'base',
LARGE: 'large',
};
2026-10-05 18:10:18 -04:00
const LocationErrorBoundary = ({ children }: { children?: React.ReactNode }) => {
const href = useLocation({ select: (location) => location.href });
return <ErrorBoundary resetKeys={[href]}>{children}</ErrorBoundary>;
};
const SuspenseErrorBoundary = ({ children }: { children?: React.ReactNode }) =>
useRouter({ warn: false }) ? (
<LocationErrorBoundary>{children}</LocationErrorBoundary>
) : (
<ErrorBoundary>{children}</ErrorBoundary>
);
Spinner.Suspense = ({ children, centered = true, size = Spinner.Size.LARGE, ...props }) => (
<Suspense fallback={<Spinner centered={centered} size={size} {...props} />}>
2026-10-05 18:10:18 -04:00
<SuspenseErrorBoundary>{children}</SuspenseErrorBoundary>
</Suspense>
);
Spinner.Suspense.displayName = 'Spinner.Suspense';
2020-02-08 15:23:08 -08:00
export default Spinner;