{"name":"gatsby-plugin-layout","version":"4.14.0","keywords":["gatsby","gatsby-plugin"],"author":"","license":"MIT","_id":"gatsby-plugin-layout@4.14.0","homepage":"https://github.com/gatsbyjs/gatsby/tree/master/packages/gatsby-plugin-layout#readme","bugs":{"url":"https://github.com/gatsbyjs/gatsby/issues"},"dist":{"shasum":"0598fab385b73dd48be968c857878c56b500bc17","tarball":"http://registry-npm.ywwl.com/gatsby-plugin-layout/download/gatsby-plugin-layout-4.14.0.tgz","fileCount":10,"integrity":"sha512-DG7Rva3hgbqgDxNT7JbJAGxwAPyhOppAorK1ioeyToLqa4BPWBWggIYD65Uuetkh5633Sx44q1L5ygpiB0QJkw==","signatures":[{"sig":"MEQCIFK6rMvvlQD0jcZohQfUDb38GcfqqsTNw+G84O14qspxAiATAW3NMK99B7sW8sJjxKZSWPVsjF7c0ARXjQlpcaz8XA==","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"unpackedSize":43440,"size":8214,"noattachment":false,"key":"/gatsby-plugin-layout/-/gatsby-plugin-layout-4.14.0.tgz"},"main":"index.js","engines":{"node":">=18.0.0"},"gitHead":"5397fbf5554ec3d937cc5a9042f2141b698d9eab","scripts":{"build":"babel src --out-dir . --ignore \"**/__tests__\"","watch":"babel -w src --out-dir . --ignore \"**/__tests__\"","prepare":"cross-env NODE_ENV=production npm run build"},"_npmUser":{"name":"serhalp-netlify","email":"philippe.serhal@netlify.com"},"repository":{"url":"git+https://github.com/gatsbyjs/gatsby.git","type":"git","directory":"packages/gatsby-plugin-layout"},"_npmVersion":"lerna/3.22.1/node@v20.11.1+arm64 (darwin)","description":"Reimplements the behavior of layout components in gatsby@1, which was removed in version 2.","directories":{},"_nodeVersion":"20.11.1","dependencies":{"@babel/runtime":"^7.20.13"},"_hasShrinkwrap":false,"devDependencies":{"cross-env":"^7.0.3","@babel/cli":"^7.20.7","@babel/core":"^7.20.12","babel-preset-gatsby-package":"^3.14.0"},"peerDependencies":{"gatsby":"^5.0.0-next"},"_npmOperationalInternal":{"tmp":"tmp/gatsby-plugin-layout_4.14.0_1730923208962_0.39064685311413583","host":"s3://npm-registry-packages"},"readme":"# gatsby-plugin-layout\n\nThis plugin enables adding components which live above the page components and persist across page changes.\n\nThis can be helpful for:\n\n- Persisting layout between page changes for e.g. animating navigation\n- Storing state when navigating pages\n- Custom error handling using componentDidCatch\n- Inject additional data into pages using React Context.\n\nThis plugin reimplements the behavior of layout components in `gatsby@1`, which [was removed in version 2](https://github.com/gatsbyjs/rfcs/blob/master/text/0002-remove-special-layout-components.md).\n\n## Install\n\n```shell\nnpm install gatsby-plugin-layout\n```\n\n## How to use\n\nAdd the plugin to your `gatsby-config.js`:\n\nBy default plugin will try to use Layout component located in `src/layouts/index.js` (same as Gatsby v1)\n\n```js\nmodule.exports = {\n  plugins: [`gatsby-plugin-layout`],\n}\n```\n\nIf you prefer to keep layout in different place, you can use `component` option:\n\n```js\nmodule.exports = {\n  plugins: [\n    {\n      resolve: `gatsby-plugin-layout`,\n      options: {\n        component: require.resolve(`./relative/path/to/layout/component`),\n      },\n    },\n  ],\n}\n```\n\nOnce the plugin is added, you don't need to manually wrap your pages with the Layout component. The plugin does this automatically.\n\n## Why would you want to reimplement the V1 layout behavior?\n\nThere are a few scenarios where it makes sense to reimplement the V1 layout handling:\n\n1.  You have a large or complex V1 site and [refactoring to the new layout component](https://www.gatsbyjs.com/docs/migrating-from-v1-to-v2/#remove-or-refactor-layout-components) is not feasible\n2.  Your site uses page transitions or other transitions that break if the layout component is unmounted and remounted when routes change\n3.  Your site attaches global state in the layout that doesn't persist if the component is unmounted and remounted\n\n### How layouts worked in version 1\n\nIn the original implementation, the layout component was wrapped around the outside of the page component, which, in pseudo-code, looked something like this:\n\n```jsx\n<Root>\n  <Layout>\n    {/* layout is not affected when the page template changes */}\n    <PageElement>{/* page content here */}</PageElement>\n  </Layout>\n</Root>\n```\n\nThis meant that the layout component could manage things like transitions and persistent state without any special workarounds, because it never rerendered.\n\n### How layouts work in version 2\n\nIn version 2, the layout component is no longer special, and it's included in every page that wants to display it. This means that it _does_ rerender on every route change:\n\n```jsx\n<Root>\n  <PageElement>\n    {/* layout will rerender each time the page template changes */}\n    <Layout>{/* page content here */}</Layout>\n  </PageElement>\n</Root>\n```\n\nThis can make it complicated to support transitions or state without using the [`wrapPageElement` browser API](https://gatsbyjs.com/docs/browser-apis/#wrapPageElement) (and the [SSR equivalent](https://gatsbyjs.com/docs/ssr-apis/#wrapPageElement)). This plugin implements those APIs for you, which reimplements the behavior of Gatsby V1.\n\n## Troubleshooting\n\n### Passing data from Layout to Page / from Page to Layout\n\nUse [React Context](https://reactjs.org/docs/context.html) to pass data both ways.\n\nFor example you can use this boilerplate:\n\n```js\n// Context.js\nimport React from \"react\"\n\nconst defaultContextValue = {\n  data: {\n    // set your initial data shape here\n    menuOpen: false,\n  },\n  set: () => {},\n}\n\nconst { Provider, Consumer } = React.createContext(defaultContextValue)\n\nclass ContextProviderComponent extends React.Component {\n  constructor() {\n    super()\n\n    this.setData = this.setData.bind(this)\n    this.state = {\n      ...defaultContextValue,\n      set: this.setData,\n    }\n  }\n\n  setData(newData) {\n    this.setState(state => ({\n      data: {\n        ...state.data,\n        ...newData,\n      },\n    }))\n  }\n\n  render() {\n    return <Provider value={this.state}>{this.props.children}</Provider>\n  }\n}\n\nexport { Consumer as default, ContextProviderComponent }\n```\n\nUse Provider in Layout Component:\n\n```js\nimport { ContextProviderComponent } from \"./Context\"\n\nexport default ({ children }) => (\n  <ContextProviderComponent>\n    <Header />\n    {children}\n    <Footer />\n  </ContextProviderComponent>\n)\n```\n\nAnd then you can use it anywhere:\n\n- To read state:\n\n```js\nimport ContextConsumer from \"./Context\"\n\nconst ComponentThatReadState = () => (\n  <ContextConsumer>\n    {({ data }) => {\n      data.menuOpen ? <Menu /> : null\n    }}\n  </ContextConsumer>\n)\n```\n\n- To read and set state:\n\n```js\nimport ContextConsumer from \"./Context\"\n\nconst ComponentThatChangeState = () => (\n  <ContextConsumer>\n    {({ data, set }) => (\n      <div onClick={() => set({ menuOpen: !data.menuOpen })}>\n        {data.menuOpen ? `Opened Menu` : `Closed Menu`}\n      </div>\n    )}\n  </ContextConsumer>\n)\n```\n\n### Handling multiple layouts\n\nIf you want to use different layouts for different pages, you can pass this information in the context of the pages you create, and then conditionally render in your layout file.\n\nIn `gatsby-node.js`:\n\n```js\nexports.onCreatePage = ({ page, actions }) => {\n  const { createPage } = actions\n\n  if (page.path.match(/special-page/)) {\n    page.context.layout = \"special\"\n    createPage(page)\n  }\n}\n```\n\nAnd then in `src/layouts/index.js`:\n\n```js\nexport default ({ children, pageContext }) => {\n  if (pageContext.layout === \"special\") {\n    return <AlternativeLayout>{children}</AlternativeLayout>\n  }\n  return <RegularLayout>{children}</RegularLayout>\n}\n```\n","publish_time":1730923209217,"maintainers":[{"name":"mlgualtieri-gatsby","email":"mike.gualtieri@netlify.com"},{"name":"serhalp-netlify","email":"philippe.serhal@netlify.com"},{"name":"kathmbeck","email":"katherine.beck@netlify.com"},{"name":"pieh","email":"misiek.piechowiak@gmail.com"},{"name":"tylerbarnes","email":"tyler@known.design"},{"name":"daniellewgatsby","email":"daniel.lew@gatsbyjs.com"},{"name":"fk","email":"sechskilo@gmail.com"}],"_cnpm_publish_time":1730923209217,"dist-tags":{"alpha-9689ff":"3.23.0-alpha-9689ff.25","alpha-a5-peer":"3.23.0-alpha-a5-peer.70","alpha-qe-sm":"2.14.0-alpha-qe-sm.46","alpha-transformer-json":"3.14.0-alpha-transformer-json.26","alpha-ts-jit":"3.6.0-alpha-ts-jit.60","alpha-v5":"4.0.0-alpha-v5.d20221012t101120.57","drupal-proxyurl":"4.0.0-alpha-drupal-proxyurl.14","drupal-self-reference":"3.18.0-alpha-drupal-self-reference.18","image-helpers":"1.5.0-image-helpers.58","image-service":"3.8.0-alpha-image-service.24","latest":"4.16.0","latest-v3":"2.15.0","latest-v4":"3.25.0","next":"4.17.0-next.0","otp-test":"1.1.6-otp-test.69","preview-gh-api":"3.23.0-alpha-preview-gh-api.26","reach-router":"2.0.0-reach-router.186","react19":"4.17.0-react19.1","unifiedroutes":"1.2.3-unifiedroutes.76","v3rc":"2.0.0-v3rc.0","wp-image-cdn-auth":"3.15.0-alpha-wp-image-cdn-auth.48"}}