Skip to content

add loading component #19

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

Merged
merged 4 commits into from
Jul 30, 2025
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
4 changes: 4 additions & 0 deletions registry.json
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,10 @@
"path": "src/components/admin/error.tsx",
"type": "registry:component"
},
{
"path": "src/components/admin/loading.tsx",
"type": "registry:component"
},
{
"path": "src/lib/i18nProvider.ts",
"type": "registry:lib"
Expand Down
34 changes: 34 additions & 0 deletions src/components/admin/loading.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { Translate, useTimeout } from "ra-core";
import { Spinner } from "./spinner";

export const Loading = (props: LoadingProps) => {
const {
loadingPrimary = "ra.page.loading",
loadingSecondary = "ra.message.loading",
delay = 1000,
...rest
} = props;
const oneSecondHasPassed = useTimeout(delay);
return oneSecondHasPassed ? (
<div
className={"flex flex-col justify-center items-center h-full"}
{...rest}
>
<div className={"text-center font-sans color-muted pt-1 pb-1"}>
<Spinner size="large" className="width-9 height-9" />
<h5 className="mt-3 text-2xl text-secondary-foreground">
<Translate i18nKey={loadingPrimary}>{loadingPrimary}</Translate>
</h5>
<p className="text-primary">
<Translate i18nKey={loadingSecondary}>{loadingSecondary}</Translate>
</p>
</div>
</div>
) : null;
};

export interface LoadingProps {
loadingPrimary?: string;
loadingSecondary?: string;
delay?: number;
}
42 changes: 42 additions & 0 deletions src/components/admin/spinner.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { cn } from "@/lib/utils";
import { VariantProps, cva } from "class-variance-authority";
import { Loader2 } from "lucide-react";

const spinnerVariants = cva("flex-col items-center justify-center", {
variants: {
show: {
true: "flex",
false: "hidden",
},
},
defaultVariants: {
show: true,
},
});

const loaderVariants = cva("animate-spin text-primary", {
variants: {
size: {
small: "size-6",
medium: "size-8",
large: "size-12",
},
},
defaultVariants: {
size: "medium",
},
});

interface SpinnerContentProps
extends VariantProps<typeof spinnerVariants>,
VariantProps<typeof loaderVariants> {
className?: string;
}

export function Spinner({ size, show, className }: SpinnerContentProps) {
return (
<span className={spinnerVariants({ show })}>
<Loader2 className={cn(loaderVariants({ size }), className)} />
</span>
);
}
84 changes: 84 additions & 0 deletions src/stories/loading.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import polyglotI18nProvider from "ra-i18n-polyglot";
import englishMessages from "ra-language-english";
import { I18nContextProvider } from "ra-core";
import { Loading } from "@/components/admin/loading";
import { ReactNode } from "react";
import { ThemeProvider } from "@/components/admin";

export default {
title: "Layout/Loading",
parameters: {
docs: {
// 👇 Enable Code panel for all stories in this file
codePanel: true,
},
},
};

const StoryWrapper = ({
children,
theme,
}: {
children: ReactNode;
theme: "system" | "light" | "dark";
}) => <ThemeProvider defaultTheme={theme}>{children}</ThemeProvider>;

const i18nProvider = polyglotI18nProvider(() => englishMessages, "en");

export const Basic = ({
theme,
delay,
}: {
theme: "system" | "light" | "dark";
delay?: number;
}) => (
<StoryWrapper theme={theme}>
<Loading delay={delay} />
</StoryWrapper>
);

Basic.args = {
theme: "system",
};

Basic.argTypes = {
theme: {
type: "select",
options: ["light", "dark", "system"],
},
delay: {
type: "number",
defaultValue: 1000,
},
};

export const I18N = ({
theme,
delay,
}: {
theme: "system" | "light" | "dark";
delay?: number;
}) => {
return (
<StoryWrapper theme={theme}>
<I18nContextProvider value={i18nProvider}>
<Loading delay={delay} />
</I18nContextProvider>
</StoryWrapper>
);
};

I18N.args = {
theme: "system",
};

I18N.argTypes = {
theme: {
type: "select",
options: ["light", "dark", "system"],
},
delay: {
type: "number",
defaultValue: 1000,
},
};