Skip to content

Commit

Permalink
feat: make Layout setting cumulative
Browse files Browse the repository at this point in the history
BREAKING CHANGE: The `Layout` setting cannot be overriden anymore because it's now cumulative, see:
 - https://vike.dev/Layout#multiple-layouts
 - https://vike.dev/Layout#nested-layouts
  • Loading branch information
brillout committed Jun 13, 2024
1 parent 35f68a0 commit 92f8da4
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 12 deletions.
3 changes: 2 additions & 1 deletion packages/vike-react/src/+config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ export default {
env: { server: true }
},
Layout: {
env: { server: true, client: true }
env: { server: true, client: true },
cumulative: true
},
title: {
env: { server: true, client: true }
Expand Down
19 changes: 9 additions & 10 deletions packages/vike-react/src/renderer/getPageElement.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
export { getPageElement }

import React, { type ReactNode } from 'react'
import React from 'react'
import type { PageContext } from 'vike/types'
import { PageContextProvider } from '../hooks/usePageContext.js'

function getPageElement(pageContext: PageContext): JSX.Element {
// Main component
const Layout = pageContext.config.Layout ?? PassThrough
const { Page } = pageContext
let page = <Layout>{Page ? <Page /> : null}</Layout>
let page = Page ? <Page /> : null

// Wrapper components
;(pageContext.config.Wrapper || []).forEach((Wrapper) => {
// Wrapping
;[
// Inner wrapping
...(pageContext.config.Layout || []),
// Outer wrapping
...(pageContext.config.Wrapper || [])
].forEach((Wrapper) => {
page = <Wrapper>{page}</Wrapper>
})
page = <PageContextProvider pageContext={pageContext}>{page}</PageContextProvider>
Expand All @@ -21,7 +24,3 @@ function getPageElement(pageContext: PageContext): JSX.Element {

return page
}

function PassThrough({ children }: { children: ReactNode }) {
return <>{children}</>
}
4 changes: 3 additions & 1 deletion packages/vike-react/src/types/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ declare global {
*
* https://vike.dev/Layout
*/
Layout?: (props: { children: React.ReactNode }) => React.ReactNode
Layout?: Layout

/**
* A component wrapping the the root component `<Page>`.
Expand Down Expand Up @@ -93,8 +93,10 @@ declare global {
}
interface ConfigResolved {
Wrapper?: Wrapper[]
Layout?: Layout[]
}
}
}

type Wrapper = (props: { children: React.ReactNode }) => React.ReactNode
type Layout = Wrapper

0 comments on commit 92f8da4

Please sign in to comment.