npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

next-super-layout

v3.3.2

Published

Convenient layout routes & React hooks for Next.js

Downloads

35

Readme

🗺 next-super-layout

Next.js conveniently solves many of the headaches involved with modern React development. However, one of the fundamental pieces still missing from Next.js is the ability to create clean, composable, data-infused layouts. There is some advice to be found in Next.js docs, but no sufficient out-of-the-box abstraction. This problem only worsens when component-level data becomes necessary anywhere in your application; at which point your only option is to "drill props" and deal with increasing amounts of /pages boilerplate. This project tries to solve the layouts problem with a simple, opinionated abstraction that plays nicely with existing Next.js conventions.

📦 Installation

Using NPM:

npm install next-super-layout

Using Yarn:

yarn add next-super-layout

🛠 Usage

Note These instructions are written for >=v3.x. For V2 docs, start here!

Bootstrapping new layouts is a cinch using the createLayout function. Simply give your layout a name and describe some UI with getLayout:

// layouts/my-layout.tsx
import { createLayout } from 'next-super-layout';

export const myLayout = createLayout({
  name: 'myLayout', // choose something unique from amongst all your layouts

  getLayout: (page, data) => {
    // `page` is the React element being wrapped.
    // `data` is the data returned from the `getData` function.
    return (<>
      <MyHeader />
      {page}
      <MyFooter />
    </>);
  },
});

Note If you encounter messages from ESLint about "rules of hooks" violations because "React component names must start with an uppercase letter", you can resolve this by changing getLayout to GetLayout (note the capitalization).

Once we've created our layout, we'll connect it to a Next.js page using createPageWrapper:

// pages/some/path.tsx
import { createPageWrapper } from 'next-super-layout';
import { myLayout } from './layouts/my-layout';

const wrapPage = createPageWrapper(myLayout);

export default wrapPage((props) => {
  return <>{...}</>;
});

Fetching layout data

We can also connect our layout to a static data source wrapping getStaticProps or getServerSideProps. First, we'll create a data fetcher using Layout.createDataFetcher:

// layouts/my-layout.data.tsx
import { myLayout } from './my-layout';

export const myLayoutData = myLayout.createDataFetcher(async (ctx) => {
  // `ctx` is the `GetStaticPropsContext` object passed to `getStaticProps`.
  return { ... };
});

Then, we'll revist our Next.js page to inject our data with createDataWrapper:

  // pages/some/path.tsx
- import { createPageWrapper } from 'next-super-layout';
+ import { createPageWrapper, createDataWrapper } from 'next-super-layout';
  import { myLayout } from './layouts/my-layout';
+ import { myLayoutData} from './layouts/my-layout.data';

  const wrapPage = createPageWrapper(myLayout);
+ const dataWrapper = createDataWrapper(myLayoutData);

  export default wrapPage((props) => {
    return <>{...}</>;
  });

+ export const getStaticProps = dataWrapper.wrapGetStaticProps(...);
  // or...
+ export const getServerSideProps = dataWrapper.wrapGetServerSideProps(...);

Should you need to fetch additional data for your page, you can define a page-specific getStaticProps or getServerSideProps function, then pass it to wrapGetStaticProps or wrapGetServerSideProps, respectively.

Note Shorthands for wrapGetStaticProps and wrapGetServerSideProps are also available: gSP and gSSP, respectively.

(Optional) Connecting next-super-layout to your Next.js _app

To ensure that layout-specific state is persisted between route changes, we can choose to define a custom Next.js _app component onto which we'll connect our layout-wrapped pages. Take a look:

// pages/_app
import { LayoutProvider } from 'next-super-layout';

export default function App(props) {
  return <LayoutProvider {...props} />;
}

Voila!

Composing layouts

With next-super-layout, it's effortless to compose multiple layouts together using variadic parameters given to thecreatePageWrapper and createDataWapper functions:

  // pages/some/path.tsx
  import { createPageWrapper, createDataWrapper } from 'next-super-layout';
  import { myLayout } from './layouts/my-layout';
  import { myLayoutData} from './layouts/my-layout.data';

+ import { myOtherLayout } from './layouts/my-other-layout';
+ import { myOtherLayoutData} from './layouts/my-other-layout.data';

- const wrapPage = createPageWrapper(myLayout);
+ const wrapPage = createPageWrapper(myLayout, myOtherLayout);
- const dataWrapper = createDataWrapper(myLayoutData);
+ const dataWrapper = createDataWrapper(myLayoutData, myOtherLayoutData);

  export default wrapPage((props) => {
    return <>{...}</>;
  });

  export const getStaticProps = dataWrapper.wrapGetStaticProps(...);
  // or...
  export const getServerSideProps = dataWrapper.wrapGetServerSideProps(...);

Using layout data

Layouts contain a useData hook that easily connects any component within the React tree of a page to the data retrieved by that page's getData fetcher.

// components/my-component.tsx
import { myLayout } from './layouts/my-layout';

function MyComponent() {
  const myLayoutData = myLayout.useData();

  // ...

  return <>{...}</>
}

⚖️ License

MIT