-
Notifications
You must be signed in to change notification settings - Fork 49.6k
[SuspenseTab] Scuffed version of Suspense rects #34188
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
15 changes: 15 additions & 0 deletions
15
packages/react-devtools-shared/src/devtools/views/SuspenseTab/SuspenseRects.css
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
.SuspenseRectsContainer { | ||
padding: .25rem; | ||
} | ||
|
||
.SuspenseRect { | ||
fill: transparent; | ||
stroke: var(--color-background-selected); | ||
stroke-width: 1px; | ||
vector-effect: non-scaling-stroke; | ||
paint-order: stroke; | ||
} | ||
|
||
[data-highlighted='true'] > .SuspenseRect { | ||
fill: var(--color-selected-tree-highlight-active); | ||
} |
200 changes: 200 additions & 0 deletions
200
packages/react-devtools-shared/src/devtools/views/SuspenseTab/SuspenseRects.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,200 @@ | ||
/** | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @flow | ||
*/ | ||
|
||
import type Store from 'react-devtools-shared/src/devtools/store'; | ||
import type { | ||
SuspenseNode, | ||
Rect, | ||
} from 'react-devtools-shared/src/frontend/types'; | ||
|
||
import * as React from 'react'; | ||
import {useContext} from 'react'; | ||
import { | ||
TreeDispatcherContext, | ||
TreeStateContext, | ||
} from '../Components/TreeContext'; | ||
import {StoreContext} from '../context'; | ||
import {useHighlightHostInstance} from '../hooks'; | ||
import styles from './SuspenseRects.css'; | ||
import {SuspenseTreeStateContext} from './SuspenseTreeContext'; | ||
|
||
function SuspenseRect({rect}: {rect: Rect}): React$Node { | ||
return ( | ||
<rect | ||
className={styles.SuspenseRect} | ||
x={rect.x} | ||
y={rect.y} | ||
width={rect.width} | ||
height={rect.height} | ||
/> | ||
); | ||
} | ||
|
||
function SuspenseRects({ | ||
suspenseID, | ||
}: { | ||
suspenseID: SuspenseNode['id'], | ||
}): React$Node { | ||
const dispatch = useContext(TreeDispatcherContext); | ||
const store = useContext(StoreContext); | ||
|
||
const {inspectedElementID} = useContext(TreeStateContext); | ||
|
||
const {highlightHostInstance, clearHighlightHostInstance} = | ||
useHighlightHostInstance(); | ||
|
||
const suspense = store.getSuspenseByID(suspenseID); | ||
if (suspense === null) { | ||
console.warn(`<Element> Could not find suspense node id ${suspenseID}`); | ||
return null; | ||
} | ||
|
||
function handleClick(event: SyntheticMouseEvent<>) { | ||
if (event.defaultPrevented) { | ||
// Already clicked on an inner rect | ||
return; | ||
} | ||
event.preventDefault(); | ||
dispatch({type: 'SELECT_ELEMENT_BY_ID', payload: suspenseID}); | ||
} | ||
|
||
function handlePointerOver(event: SyntheticPointerEvent<>) { | ||
if (event.defaultPrevented) { | ||
// Already hovered an inner rect | ||
return; | ||
} | ||
event.preventDefault(); | ||
highlightHostInstance(suspenseID); | ||
} | ||
|
||
function handlePointerLeave(event: SyntheticPointerEvent<>) { | ||
if (event.defaultPrevented) { | ||
// Already hovered an inner rect | ||
return; | ||
} | ||
event.preventDefault(); | ||
clearHighlightHostInstance(); | ||
} | ||
|
||
// TODO: Use the nearest Suspense boundary | ||
const selected = inspectedElementID === suspenseID; | ||
|
||
return ( | ||
<g | ||
data-highlighted={selected} | ||
onClick={handleClick} | ||
onPointerOver={handlePointerOver} | ||
onPointerLeave={handlePointerLeave}> | ||
<title>{suspense.name}</title> | ||
{suspense.rects !== null && | ||
suspense.rects.map((rect, index) => { | ||
return <SuspenseRect key={index} rect={rect} />; | ||
})} | ||
{suspense.children.map(childID => { | ||
return <SuspenseRects key={childID} suspenseID={childID} />; | ||
})} | ||
</g> | ||
); | ||
} | ||
|
||
function getDocumentBoundingRect( | ||
store: Store, | ||
shells: $ReadOnlyArray<SuspenseNode['id']>, | ||
): Rect { | ||
if (shells.length === 0) { | ||
return {x: 0, y: 0, width: 0, height: 0}; | ||
} | ||
|
||
let minX = Number.POSITIVE_INFINITY; | ||
let minY = Number.POSITIVE_INFINITY; | ||
let maxX = Number.NEGATIVE_INFINITY; | ||
let maxY = Number.NEGATIVE_INFINITY; | ||
|
||
for (let i = 0; i < shells.length; i++) { | ||
const shellID = shells[i]; | ||
const shell = store.getSuspenseByID(shellID); | ||
if (shell === null) { | ||
continue; | ||
} | ||
|
||
const rects = shell.rects; | ||
if (rects === null) { | ||
continue; | ||
} | ||
for (let j = 0; j < rects.length; j++) { | ||
const rect = rects[j]; | ||
minX = Math.min(minX, rect.x); | ||
minY = Math.min(minY, rect.y); | ||
maxX = Math.max(maxX, rect.x + rect.width); | ||
maxY = Math.max(maxY, rect.y + rect.height); | ||
} | ||
} | ||
|
||
if (minX === Number.POSITIVE_INFINITY) { | ||
// No rects found, return empty rect | ||
return {x: 0, y: 0, width: 0, height: 0}; | ||
} | ||
|
||
return { | ||
x: minX, | ||
y: minY, | ||
width: maxX - minX, | ||
height: maxY - minY, | ||
}; | ||
} | ||
|
||
function SuspenseRectsShell({ | ||
shellID, | ||
}: { | ||
shellID: SuspenseNode['id'], | ||
}): React$Node { | ||
const store = useContext(StoreContext); | ||
const shell = store.getSuspenseByID(shellID); | ||
if (shell === null) { | ||
console.warn(`<Element> Could not find suspense node id ${shellID}`); | ||
return null; | ||
} | ||
|
||
return ( | ||
<g> | ||
{shell.children.map(childID => { | ||
return <SuspenseRects key={childID} suspenseID={childID} />; | ||
})} | ||
</g> | ||
); | ||
} | ||
|
||
function SuspenseRectsContainer(): React$Node { | ||
const store = useContext(StoreContext); | ||
// TODO: This relies on a full re-render of all children when the Suspense tree changes. | ||
const {shells} = useContext(SuspenseTreeStateContext); | ||
|
||
const boundingRect = getDocumentBoundingRect(store, shells); | ||
|
||
const width = '100%'; | ||
const boundingRectWidth = boundingRect.width; | ||
const height = | ||
(boundingRectWidth === 0 ? 0 : boundingRect.height / boundingRect.width) * | ||
100 + | ||
'%'; | ||
|
||
return ( | ||
<div className={styles.SuspenseRectsContainer}> | ||
<svg | ||
style={{width, height}} | ||
viewBox={`${boundingRect.x} ${boundingRect.y} ${boundingRect.width} ${boundingRect.height}`}> | ||
{shells.map(shellID => { | ||
return <SuspenseRectsShell key={shellID} shellID={shellID} />; | ||
})} | ||
</svg> | ||
</div> | ||
); | ||
} | ||
|
||
export default SuspenseRectsContainer; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This only picks the rects from the roots but the suspense boundaries within the root can overflow the root and the inner suspense boundaries themselves and overflow.
For example, if you just have an absolutely positioned child inside body it's likely that the body element is smaller than the child.
So if the last boundary is below the size of the root then it's not clickable in this mode. This should really be the max of every rect recursively.
However, another way is to just make it like:
Since then the overflow of the "document" div will account for the max of all the children automatically.
Another reason to maybe favor divs.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Although maybe we need to keep track of the min/max of the whole document anyway to allow the UI to scale down to fit the width of the smaller devtools window automatically.