Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inspector improvements for dynamic <Select> and <Menu> support. #292

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions packages/material/src/MenuItem/MenuItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import menuItemClasses from "./menuItemClasses";
import createComponentFactory from "@suid/base/createComponentFactory";
import { alpha } from "@suid/system";
import createRef from "@suid/system/createRef";
import { createInspector } from "@suid/system/inspect";
import { InPropsOf } from "@suid/types";
import clsx from "clsx";
import {
Expand Down Expand Up @@ -167,7 +168,7 @@ const MenuItemRoot = styled(ButtonBase, {
* - [MenuItem API](https://mui.com/api/menu-item/)
* - inherits [ButtonBase API](https://mui.com/api/button-base/)
*/
const MenuItem = $.defineComponent(function MenuItem(inProps) {
const MenuItemComponent = $.defineComponent(function MenuItem(inProps) {
const menuItemRef = createRef<HTMLElement>(inProps);
const props = $.useThemeProps({ props: inProps });
const [, other] = splitProps(props, [
Expand Down Expand Up @@ -234,10 +235,17 @@ const MenuItem = $.defineComponent(function MenuItem(inProps) {
}
};

const inspector = createInspector<any>(MenuItemComponent, inProps);

const handleRef = (el: HTMLElement) => {
menuItemRef(el);
inspector(el);
};

return (
<ListContext.Provider value={childContext}>
<MenuItemRoot
ref={menuItemRef}
ref={handleRef}
role={baseProps.role}
tabIndex={tabIndex()}
component={baseProps.component}
Expand All @@ -252,5 +260,4 @@ const MenuItem = $.defineComponent(function MenuItem(inProps) {
</ListContext.Provider>
);
});

export default MenuItem;
export default MenuItemComponent;
25 changes: 22 additions & 3 deletions packages/system/src/inspect.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Component, createMemo, JSX } from "solid-js";
import { Component, createMemo, onCleanup, JSX } from "solid-js";

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

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

export type InspectResult = JSX.Element | ComponentObject;

const nodeToComponent = new WeakMap();

let inspectionEnabled = false;

export function createInspector<T>(fn: Component<T>, props: T) {
let elementRef: HTMLElement | null;
const cleanup = () => {
if (elementRef) {
nodeToComponent.delete(elementRef);
elementRef = null;
}
};
onCleanup(cleanup);
return (newElementRef: HTMLElement) => {
cleanup();
elementRef = newElementRef;
nodeToComponent.set(elementRef, { Component: fn, props, $INSPECT });
};
}

export function inspect(fn: () => JSX.Element): InspectResult[] {
try {
inspectionEnabled = true;
const result = fn();
return Array.isArray(result) ? result : [result];
const ret = fn();
const result = Array.isArray(ret) ? ret : [ret];
return result.map((v: any) => nodeToComponent.get(v) || v);
} finally {
inspectionEnabled = false;
}
Expand Down