0.0.14 • Published 3 years ago

@guyathomas/layout-for-path v0.0.14

Weekly downloads
-
License
ISC
Repository
github
Last release
3 years ago

Layout For Path

A declarative, react-router style API for wrapping your application in layouts, depending on the current route.

Core Principles

  • The first match in the array will be applied
  • Nested routes will recursively be applied ( top level layouts being provided at the outermost level )
  • A match doesn't need to have a layout element, and can serve as matching a prefix for child routes
  • Wildcard routes /*, param routes /:uuid, and exact match routes /driver/name are supported

Example

import { useRouter } from "next/router";
import LayoutForPage, { LayoutSpec } from "@guyathomas/layout-for-path";
/* ... Layout Imports Omitted ... */

const layoutSpec: LayoutSpec[] = [
  {
    pattern: "/dashboard", // my-app.com/dashboard/* would be wrapped in this
    layout: ({ children }) => (
      <AuthGuard>
        <DashboardLayout>{children}</DashboardLayout>
      </AuthGuard>
    )
  },
  {
    pattern: "/*", // Everything that doesn't match my-app.com/dashboard will render <MainLayout>{children}</MainLayout>
    layout: MainLayout,
    children: [
      {
        pattern: "/", // my-app.com/ will render <MainLayout><HomePageLayout>{children}</HomePageLayout></MainLayout>
        layout: HomePageLayout,
      },
      {
        pattern: "/browse/*", // my-app.com/browse/some-route will render <MainLayout><BrowseLayout>{children}</BrowseLayout></MainLayout>
        layout: BrowseLayout,
      },
    ],
  },
];


const AppLayout: React.FC = ({ children }) => {
  const router = useRouter();
  return (
    <LayoutForPage path={router.asPath} layoutSpec={layoutSpec}>
      {children}
    </LayoutForPage>
  );
};
0.0.12

3 years ago

0.0.13

3 years ago

0.0.14

3 years ago

0.0.10

3 years ago

0.0.11

3 years ago

0.0.9

3 years ago

0.0.8

3 years ago

0.0.7

3 years ago

0.0.6

3 years ago

0.0.5

3 years ago

0.0.4

3 years ago

0.0.3

3 years ago

0.0.2

3 years ago

0.0.1

3 years ago