Skip to content

Commit f50b577

Browse files
committed
refactor(inspect): Rename Inspector to RawInspector and wrap with portal
1 parent 73e7ff3 commit f50b577

File tree

3 files changed

+28
-11
lines changed

3 files changed

+28
-11
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ or, use the `/inspect` subpackage to graphically visualize the fiber tree:
3333
```jsx
3434
import { Inspector } from 'bippy/inspect';
3535

36-
<Inspector enabled={true} />
36+
<Inspector enabled={true} />;
3737
```
3838

3939
<table>
@@ -158,7 +158,6 @@ or, use via script tag:
158158
159159
next, you can use the api to get data about the fiber tree. below is a (useful) subset of the api. for the full api, read the [source code](https://github.com/aidenybai/bippy/blob/main/src/core.ts).
160160

161-
162161
### onCommitFiberRoot
163162

164163
a utility function that wraps the `instrument` function and sets the `onCommitFiberRoot` hook.
@@ -303,7 +302,6 @@ traverseContexts(fiber, (next, prev) => {
303302
});
304303
```
305304

306-
307305
### setFiberId / getFiberId
308306

309307
set and get a persistent identity for a fiber. by default, fibers are anonymous and have no identity.
@@ -621,6 +619,8 @@ instrument(
621619

622620
## development
623621

622+
pre-requisite: you should understand how react works internally. if you don't, please give this [series of articles](https://jser.dev/series/react-source-code-walkthrough) a read.
623+
624624
we use a pnpm monorepo, get started by running:
625625

626626
```shell

packages/bippy/src/inspect.tsx

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
ObjectRootLabel,
2424
ObjectLabel,
2525
} from 'react-inspector';
26+
import ReactDOM from 'react-dom';
2627

2728
const FIBER_PROP_EXPLANATIONS: Record<string, string> = {
2829
tag: 'Numeric type identifier for this fiber (e.g. 1=FunctionComponent, 5=HostComponent)',
@@ -234,7 +235,7 @@ const CloseButton = React.memo(({ onClick }: { onClick: () => void }) => (
234235
</button>
235236
));
236237

237-
export const Inspector = React.memo(
238+
export const RawInspector = React.memo(
238239
({ enabled = true, dangerouslyRunInProduction = false }: InspectorProps) => {
239240
const [element, setElement] = useState<Element | null>(null);
240241
const [rect, setRect] = useState<DOMRect | null>(null);
@@ -685,4 +686,26 @@ export const Inspector = React.memo(
685686
},
686687
);
687688

689+
export const Inspector = React.memo(({ ...props }: InspectorProps) => {
690+
const [root, setRoot] = useState<ShadowRoot | null>(null);
691+
692+
useEffect(() => {
693+
const div = document.createElement('div');
694+
document.body.appendChild(div);
695+
const shadowRoot = div.attachShadow({ mode: 'open' });
696+
setRoot(shadowRoot);
697+
698+
return () => {
699+
document.body.removeChild(div);
700+
};
701+
}, []);
702+
703+
if (!root) return null;
704+
705+
return ReactDOM.createPortal(
706+
<RawInspector {...props} />,
707+
root
708+
);
709+
});
710+
688711
export default Inspector;

packages/kitchen-sink/src/main.tsx

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { instrument } from 'bippy/dist/index';
1+
import 'bippy/dist/index';
22

33
import { StrictMode } from 'react';
44
import ReactDOM from 'react-dom/client';
@@ -7,12 +7,6 @@ import './main.css';
77

88
import { routeTree } from './routeTree.gen';
99

10-
instrument({
11-
onActive() {
12-
console.log('active');
13-
},
14-
});
15-
1610
const router = createRouter({ routeTree });
1711

1812
declare module '@tanstack/react-router' {

0 commit comments

Comments
 (0)