Skip to content

Commit

Permalink
feat: make Layout setting cumulative
Browse files Browse the repository at this point in the history
Will amend to this commit later.
  • Loading branch information
Blankeos committed Jun 16, 2024
1 parent c664faa commit 697a0b0
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 8 deletions.
1 change: 1 addition & 0 deletions vike-solid/+config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const config = {
},
Layout: {
env: { server: true, client: true },
cumulative: true,
},
title: {
env: { server: true, client: true },
Expand Down
2 changes: 1 addition & 1 deletion vike-solid/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"solid-js": "^1.8.17",
"tslib": "^2.6.3",
"typescript": "^5.4.5",
"vike": "^0.4.174",
"vike": "^0.4.176",
"vite": "^5.2.13"
},
"exports": {
Expand Down
34 changes: 28 additions & 6 deletions vike-solid/renderer/getPageElement.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import type { PageContext } from "vike/types";
import { PageContextProvider } from "./PageContextProvider.js";
import { usePageContext } from "../hooks/usePageContext.js";
import type { JSX } from "solid-js";
import type { FlowComponent, JSX } from "solid-js";
import { createComponent, createEffect, createMemo } from "solid-js";
import { Dynamic } from "solid-js/web";
import type { Store } from "solid-js/store";
import { createStore, reconcile, unwrap, type Store } from "solid-js/store";

export function getPageElement(pageContext: Store<PageContext>): JSX.Element {
const page = (
Expand All @@ -18,11 +19,32 @@ export function getPageElement(pageContext: Store<PageContext>): JSX.Element {

function Layout(props: { children: JSX.Element }) {
const pageContext = usePageContext();
return (
<Dynamic component={pageContext.config.Layout ?? Passthrough}>
{props.children}
</Dynamic>

const [layoutStore, setLayoutStore] = createStore(
pageContext.config.Layout?.toReversed() as FlowComponent[]
);

createEffect(() => {
setLayoutStore(
reconcile(pageContext.config.Layout?.toReversed() as FlowComponent[])
);
});

const fn = (i: number) => {
let item: FlowComponent = layoutStore[i];

if (!item) return props.children;

const layoutProps: { children: JSX.Element } = {
get children() {
return fn(i + 1);
},
};

return createComponent(item, layoutProps);
};

return fn(0);
}

function Page() {
Expand Down
5 changes: 4 additions & 1 deletion vike-solid/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
export type { Component } from "solid-js";

import type { JSX } from "solid-js";
import type { Component, JSX } from "solid-js";

type Page = () => JSX.Element;

declare global {
namespace Vike {
interface ConfigResolved {
Layout?: Array<Component>;
}
interface PageContext {
// Page is undefined in onRenderHtml() when setting the `ssr` config flag to `false`
Page?: Page;
Expand Down

0 comments on commit 697a0b0

Please sign in to comment.