concept Layout in category nextjs

appears as: Layout
Next.js in Action

This is an excerpt from Manning's book Next.js in Action.

We’re going to a Layout component for the messenger application that we worked on in chapter 3. If you haven’t read chapter 3 yet, don’t worry, we’ll recap what we’re changing in the app as we move along. Every page in the application has some global CSS styles applied to it. There are two problems with this:

Our Layout component takes a children property – this is the page content for each page that uses the Layout component. Inside of a <Fragment> element we’re rendering children along with the global styles that we found in the home page. Now, we can update the home page to use the new Layout component, remove the global styles, and set the style scope to local:

Listing 4.3: The home page using the new Layout component and scoped styles
import React from "react";
import Link from "next/link";
import Layout from "../components/Layout";
 
export default function Home({ messages, friends }) {
  return (
    <Layout>
      <div className="container">
            ...
      </div>
      <style jsx>{`
        main {
          display: flex;
          justify-content: space-between;
        }
        ...
      `}</style>
    </Layout>
  );
}

The changes to use the new Layout component are highlighted in listing 4.3. The Layout component is imported from components/Layout and <Layout> is used where <Fragment> used to be. We’ve removed the global styles and the global flag from the <style> element. Since all the pages in our application follow this same pattern, we can walk through each page and make the same changes to use Layout while removing global styles. Although this involves updating every file, once every page has the Layout component, we don’t have to make a significant change like this again.

Listing 4.6: The home page content with the page header and navigation moved into Layout
export default function Home({ messages, friends }) {
  return (
    <Layout title="Home">
      <section>
        <h2>Latest Messages</h2>
        ...
      </section>
      <section>
        <h2>Online</h2>
        ...
      </section>
      <style jsx>{`
        ... 
      `}</style>
    </Layout>
  );
}

The home page now only has to define the content that goes in the <main> element, in this case the two <section> elements, as well as the <style> element for any styles that are scoped to this page. Now that every page in the application is using the Layout component to get the global styles in place, we can follow the same pattern of deleting any page content or styles that now lives in Layout.

In some cases, you’re going to find places where what you’ve defined in Layout isn’t compatible with what you’re expecting to see. For example, here’s what the new message page looks like after removing the repetitive page structure and styles:

Figure 4.2: The layout of the new message page breaks when using the Layout component
A screenshot of a cell phone Description automatically generated

This isn’t how the layout of the form fields on this page looked before we added the Layout component. When we’re refactoring page content to include shared components, it’s important to catch issues like these as we go so that we can address them. The problem here is that the <main> element in the Layout component uses flex as its display, and the default flex direction is row. The styles on this page, before we refactored to use the Layout component, used column as the flex direction. This means that we’ve found a variant that the Layout component needs to support. Instead of only allowing for the default row flex direction, we need to allow individual pages to tell Layout that <main> should use column as the flex direction:

Listing 4.7: Updating Layout to add a flexDirection property so pages can override the default
export default function Layout({ children, title, flexDirection }) {
  return (
    <Fragment>
      <div className="container">
        ...
      </div>
      <style jsx global>{`
        ...
        main {
          display: flex;
          justify-content: space-between;
          flex-direction: ${flexDirection};
        }
      `}</style>
    </Fragment>
  );
}
 
Layout.defaultProps = {
  flexDirection: "row"
};