Skip to content

Commit

Permalink
fix(system): inspect dynamic components
Browse files Browse the repository at this point in the history
Closes #223
  • Loading branch information
juanrgm committed Sep 9, 2024
1 parent 9940922 commit b196443
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 13 deletions.
5 changes: 5 additions & 0 deletions .changeset/real-hairs-bow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@suid/system": patch
---

Fix `inspectChildren` function
32 changes: 19 additions & 13 deletions packages/system/src/inspect.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { Component, createMemo, JSX } from "solid-js";
import {
Component,
createComponent,
createContext,
createMemo,
JSX,
useContext,
} from "solid-js";

export const $INSPECT = Symbol("solid-inspect");

Expand All @@ -13,21 +20,22 @@ type PrivateComponentObject<T = any> = ComponentObject<T> & {

export type InspectResult = JSX.Element | ComponentObject;

let inspectionEnabled = false;
export const InspectContext = createContext<{ enabled: boolean }>();

export function inspect(fn: () => JSX.Element): InspectResult[] {
try {
inspectionEnabled = true;
const result = fn();
return Array.isArray(result) ? result : [result];
} finally {
inspectionEnabled = false;
}
const cb = createComponent(InspectContext.Provider, {
value: { enabled: true },
get children() {
return fn();
},
}) as any as () => any;
const result = cb();
return Array.isArray(result) ? result : [result];
}

export function componentTrap<T>(fn: Component<T>): Component<T> {
function Component(props: T) {
if (inspectionEnabled)
if (useContext(InspectContext)?.enabled)
return {
Component,
props,
Expand Down Expand Up @@ -74,9 +82,7 @@ export function resolveChildren(children: any): unknown {

export function inspectChildren(fn: () => any) {
const children = createMemo(() => inspect(fn));
const memo = createMemo(() =>
inspect(() => resolveChildren(children()) as any)
);
const memo = createMemo(() => resolveChildren(children()) as any);
(memo as any).toArray = () => {
const c = memo();
return Array.isArray(c) ? c : c != null ? [c] : [];
Expand Down

0 comments on commit b196443

Please sign in to comment.