Skip to content

Commit

Permalink
mirror createDynamic on SSR
Browse files Browse the repository at this point in the history
  • Loading branch information
ryansolid committed Feb 21, 2025
1 parent 86ae8a9 commit 9431b88
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .changeset/giant-goats-press.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"solid-js": patch
---

Mirror createDynamic for SSR
27 changes: 19 additions & 8 deletions packages/solid/web/server/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ssrElement } from "./server.js";
import { splitProps, Component, JSX } from "solid-js";
import { splitProps, type JSX, type ValidComponent, type ComponentProps } from "solid-js";

export * from "./server";

Expand All @@ -19,21 +19,32 @@ export {
export const isServer: boolean = true;
export const isDev: boolean = false;

export function Dynamic<T>(
props: T & { children?: any; component?: Component<T> | string | keyof JSX.IntrinsicElements }
) {
const [p, others] = splitProps(props, ["component"]);
const comp = p.component,
export function createDynamic<T extends ValidComponent>(
component: () => T | undefined,
props: ComponentProps<T>
): JSX.Element {
const comp = component(),
t = typeof comp;

if (comp) {
if (t === "function") return (comp as Function)(others);
if (t === "function") return (comp as Function)(props);
else if (t === "string") {
return ssrElement(comp as string, others, undefined, true);
return ssrElement(comp as string, props, undefined, true);
}
}
}

export type DynamicProps<T extends ValidComponent, P = ComponentProps<T>> = {
[K in keyof P]: P[K];
} & {
component: T | undefined;
};

export function Dynamic<T extends ValidComponent>(props: DynamicProps<T>): JSX.Element {
const [, others] = splitProps(props, ["component"]);
return createDynamic(() => props.component, others as ComponentProps<T>);
}

export function Portal(props: { mount?: Node; useShadow?: boolean; children: JSX.Element }) {
return "";
}
8 changes: 4 additions & 4 deletions packages/solid/web/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,10 @@ export type DynamicProps<T extends ValidComponent, P = ComponentProps<T>> = {
* @description https://docs.solidjs.com/reference/components/dynamic
*/
export function createDynamic<T extends ValidComponent>(
component: () => T,
component: () => T | undefined,
props: ComponentProps<T>
): JSX.Element {
const cached = createMemo<Function | string>(component);
const cached = createMemo<Function | string | undefined>(component);
return createMemo(() => {
const component = cached();
switch (typeof component) {
Expand Down Expand Up @@ -158,6 +158,6 @@ export function createDynamic<T extends ValidComponent>(
* @description https://docs.solidjs.com/reference/components/dynamic
*/
export function Dynamic<T extends ValidComponent>(props: DynamicProps<T>): JSX.Element {
const [p, others] = splitProps(props, ["component"]);
return createDynamic(() => p.component, others);
const [, others] = splitProps(props, ["component"]);
return createDynamic(() => props.component, others as ComponentProps<T>);
}

0 comments on commit 9431b88

Please sign in to comment.