Nested Layout
Most times it is desirable to nest layouts into each other. A page's content can be nested in numerous wrapping layouts, which is determined by the directory structure.
src/
โโโ routes/
โโโ layout.tsx # Parent layout
โโโ about/
โโโ layout.tsx # Child layout
โโโ index.tsx # https://example.com/about
In the above example, there are two layouts that apply themselves around the /about
page component.
src/routes/layout.tsx
src/routes/about/layout.tsx
In this case, the layouts will nest each other with the page within each of them.
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ src/routes/layout.tsx โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ src/routes/about/layout.tsx โ โ
โ โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ โ
โ โ โ src/routes/about/index.tsx โ โ โ
โ โ โ โ โ โ
โ โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ โ
โ โ โ โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ โ
โ โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
// File: src/routes/layout.tsx
export default component$(() => {
return (
<main>
<Slot /> {/* <== Child layout/route inserted here */}
</main>
);
});
// File: src/routes/about/layout.tsx
export default component$(() => {
return (
<section>
<Slot /> {/* <== Child layout/route inserted here */}
</section>
);
});
// File: src/routes/about/index.tsx
export default component$(() => {
return <h1>About</h1>;
});
The above example would render the html:
<main>
<section>
<h1>About</h1>
</section>
</main>