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 14, 2024
1 parent c664faa commit e43ac4c
Show file tree
Hide file tree
Showing 4 changed files with 33 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
33 changes: 27 additions & 6 deletions vike-solid/renderer/getPageElement.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import type { PageContext } from "vike/types";
import { PageContextProvider } from "./PageContextProvider.js";
import { usePageContext } from "../hooks/usePageContext.js";
import type { JSX } from "solid-js";
import { createComponent, createMemo } from "solid-js";
import { Dynamic } from "solid-js/web";
import type { Store } from "solid-js/store";
import { type Store } from "solid-js/store";

export function getPageElement(pageContext: Store<PageContext>): JSX.Element {
const page = (
Expand All @@ -18,11 +19,31 @@ 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 layouts = createMemo(() => {
return pageContext.config.Layout?.toReversed() ?? [];
});

const fn = (i: number) => {
let item: any = layouts()[i];

if (!item) return props.children;

const layoutProps: { value?: any; children: JSX.Element } = {
get children() {
return fn(i + 1);
},
};
if (Array.isArray(item)) {
layoutProps.value = item[1];
item = item[0];
if (typeof item !== "function") item = Passthrough;
}

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 e43ac4c

Please sign in to comment.