Logo

dev-resources.site

for different kinds of informations.

Handling Local Storage for Toaster Notifications

Published at
1/2/2025
Categories
webdev
javascript
shadcn
typescript
Author
brokarim
Author
8 person written this
brokarim
open
Handling Local Storage for Toaster Notifications

Demo :

Source code :

============= layout.tsx 
<body>
   <ThemeProvider attribute="class" defaultTheme="dark">
     {children}
      <DevToaster />
   </ThemeProvider>
</body>

============= dev-toaster.tsx 
export function TemplateToaster() {
  const [isVisibile, setIsVisible] = useState(true);
  const [notificationStatus, setNotificationStatus] = useState<string | null>(null);
  const path = usePathname();
  const [hydrated, setHydrated] = useState(false);

  // Run this effect only once to set hydrated to true
  useEffect(() => {
    setHydrated(true);
  }, []);

  // Run this effect only on client side
  useEffect(() => {
    if (hydrated) {
      // Remove the saved notification status to reset it
      localStorage.removeItem("behindui-notification");
      // Check localStorage after clearing
      setNotificationStatus(localStorage.getItem("behindui-notification"));
    }
  }, [hydrated]);

  if (!hydrated) {
    return null;
  }

  const templates = path.includes("templates");
  const isIframe = isInIframe();
  if (isIframe || notificationStatus === "off") {
    return <div></div>;
  }

  return (
    isVisibile &&
    !templates && (
      <section className="relative z-50">
        <div className="font-geist fixed bottom-4 right-4">
          <Card className={cn(" w-[350px] bg-background [border:1px_solid_rgba(255,255,255,.1)]")}>
            <CardHeader>
              <div className="font-mono font-bold  uppercase tracking-tight">
                ๐ŸšงWebsite Under Development ๐Ÿšง <br />
              </div>
              <CardDescription className="mt-4 text-black/90 dark:text-white/70">This website is currently in active development. Some features may be incomplete or not working as expected</CardDescription>
            </CardHeader>
            <CardFooter className="flex justify-end gap-4">
              <Button
                size="sm"
                onClick={() => {
                  localStorage.setItem("behindui-notification", "off");
                  setIsVisible(false);
                }}
              >
                Understand
              </Button>
            </CardFooter>
          </Card>
        </div>
      </section>
    )
  );
}

function isInIframe() {
  try {
    return window.self !== window.top;
  } catch (e) {
    return true;
  }
}

Enter fullscreen mode Exit fullscreen mode
shadcn Article's
30 articles in total
Favicon
Made FOSS for simplifying NextJS dev with OAuth And Postgres
Favicon
.gitkeep file in Shadcn/ui source code
Favicon
Introducing the Zod Schema Designer: A Visual Tool for Creating and Editing Zod Schemas
Favicon
Handling Local Storage for Toaster Notifications
Favicon
I created a visually pleasing HTML color viewer
Favicon
AISEKA Color Tool Website
Favicon
Color Palette is served with ShadCn Theme Editor
Favicon
Create React project with Vite and set up Tailwind, shadcn/ui
Favicon
Why Developers Love the Shadcn Sidebar Component
Favicon
A comparison of metadata configurations between Lobechat and Shadcn/ui
Favicon
Forms in seconds with ShardCn forms
Favicon
What is the Difference Between Radix UI and ShadCN?
Favicon
How Shadcn CLI uses error constants to improve code readability
Favicon
Exploring the ShadCN UI Library: A Comprehensive Guide
Favicon
10 Essential Shadcn Components Every Developer Should Know About
Favicon
The beginning of my open source journey
Favicon
Install ShadCN and Tailwind in Vite React Project
Favicon
How to add shadcn to existing project
Favicon
How to Use Shadcn UI with a React Project
Favicon
How to Install Shadcn with Next.js 14: A Step-by-Step Guide
Favicon
Shadcn Date Picker with custom implementation
Favicon
How to Fix Shadcn UI Adding Wrong Folder for Components
Favicon
Material UI vs Shadcn
Favicon
Building UIs with Franken UI, a Shadcn alternative
Favicon
Implement ShadCn form with Validation
Favicon
Top 5 Coolest shadcn/ui Extensions
Favicon
Shadcn UI: Must-Have Tools & Resources
Favicon
shadcn-ui/ui codebase analysis: examples route explained.
Favicon
Two ways I discovered to pass dynamic parameters in (arrow and non-arrow) function.
Favicon
Creating a User Nav bar in Next.js 14 using Shadcn UI and Tailwindย CSS

Featured ones: