Skip to content

About

A collection of useful Next.js code snippets for common tasks. Copy paste ready.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

2 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 

Repository files navigation

Next.js Useful Snippets

A collection of copy paste ready code snippets for Next.js projects. Things I use all the time when building websites at Impeccify.

Table of Contents

SEO Metadata Template

A reusable metadata template for Next.js App Router pages.

export const metadata = {
  title: "Your Page Title - Your Brand",
  description: "Your page description goes here. Keep it under 160 characters.",
  keywords: ["keyword1", "keyword2", "keyword3"],
  openGraph: {
    title: "Your Page Title",
    description: "Your page description",
    url: "https://yoursite.com/page",
    siteName: "Your Site Name",
    type: "website",
  },
  twitter: {
    card: "summary_large_image",
    title: "Your Page Title",
    description: "Your page description",
  },
  alternates: {
    canonical: "https://yoursite.com/page",
  },
};
`` `

## Dark Mode Toggle

Simple dark mode toggle using Tailwind CSS and localStorage.

```jsx
"use client";
import { useState, useEffect } from "react";

export default function DarkModeToggle() {
  const [dark, setDark] = useState(false);

  useEffect(() => {
    const saved = localStorage.getItem("theme");
    if (saved === "dark") {
      setDark(true);
      document.documentElement.classList.add("dark");
    }
  }, []);

  const toggle = () => {
    setDark(!dark);
    if (!dark) {
      document.documentElement.classList.add("dark");
      localStorage.setItem("theme", "dark");
    } else {
      document.documentElement.classList.remove("dark");
      localStorage.setItem("theme", "light");
    }
  };

  return (
    <button onClick={toggle}>
      {dark ? "Light Mode" : "Dark Mode"}
    </button>
  );
}
`` `

## Scroll to Top Button

A smooth scroll to top button that appears when you scroll down.

```jsx
"use client";
import { useState, useEffect } from "react";

export default function ScrollToTop() {
  const [visible, setVisible] = useState(false);

  useEffect(() => {
    const onScroll = () => setVisible(window.scrollY > 300);
    window.addEventListener("scroll", onScroll);
    return () => window.removeEventListener("scroll", onScroll);
  }, []);

  if (!visible) return null;

  return (
    <button
      onClick={() => window.scrollTo({ top: 0, behavior: "smooth" })}
      style={{
        position: "fixed",
        bottom: "2rem",
        right: "2rem",
        padding: "0.75rem",
        borderRadius: "50%",
        background: "#333",
        color: "#fff",
        border: "none",
        cursor: "pointer",
      }}
    >
      Top
    </button>
  );
}
`` `

## FAQ Accordion

A simple FAQ accordion component. No external libraries needed.

```jsx
"use client";
import { useState } from "react";

function FAQItem({ question, answer }) {
  const [open, setOpen] = useState(false);

  return (
    <div style={{ borderBottom: "1px solid #ddd", padding: "1rem 0" }}>
      <button
        onClick={() => setOpen(!open)}
        style={{
          width: "100%",
          textAlign: "left",
          background: "none",
          border: "none",
          fontSize: "1.1rem",
          fontWeight: "600",
          cursor: "pointer",
          display: "flex",
          justifyContent: "space-between",
        }}
      >
        {question}
        <span>{open ? "-" : "+"}</span>
      </button>
      {open && (
        <p style={{ marginTop: "0.5rem", color: "#666" }}>{answer}</p>
      )}
    </div>
  );
}

export default function FAQ({ items }) {
  return (
    <div>
      {items.map((item, i) => (
        <FAQItem key={i} question={item.q} answer={item.a} />
      ))}
    </div>
  );
}
`` `

---

Built by [Impeccify](https://impeccify.com) - We build custom websites and web apps with Next.js.# nextjs-useful-snippets
A collection of useful Next.js code snippets for common tasks. Copy paste ready.

About

A collection of useful Next.js code snippets for common tasks. Copy paste ready.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors