-
Notifications
You must be signed in to change notification settings - Fork 435
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
fix: preload documents on hover #8110
Merged
+34
−13
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
This file contains 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 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 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 |
---|---|---|
|
@@ -9,10 +9,11 @@ import {Box, type CardProps, Text} from '@sanity/ui' | |
import { | ||
type ComponentType, | ||
type MouseEvent, | ||
type ReactNode, | ||
startTransition, | ||
useCallback, | ||
useEffect, | ||
useMemo, | ||
useRef, | ||
useState, | ||
} from 'react' | ||
import { | ||
|
@@ -22,6 +23,7 @@ import { | |
SanityDefaultPreview, | ||
useDocumentPresence, | ||
useDocumentPreviewStore, | ||
useEditState, | ||
useSchema, | ||
} from 'sanity' | ||
|
||
|
@@ -124,14 +126,6 @@ export function PaneItem(props: PaneItemProps) { | |
documentPresence, | ||
]) | ||
|
||
const Link = useMemo( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Defining components during render should be avoided whenever possible 🙌 |
||
() => | ||
function LinkComponent(linkProps: {children: ReactNode}) { | ||
return <ChildLink {...linkProps} childId={id} /> | ||
}, | ||
[ChildLink, id], | ||
) | ||
|
||
const handleClick = useCallback((e: MouseEvent<HTMLElement>) => { | ||
if (e.metaKey) { | ||
setClicked(false) | ||
|
@@ -144,16 +138,31 @@ export function PaneItem(props: PaneItemProps) { | |
// Reset `clicked` state when `selected` prop changes | ||
useEffect(() => setClicked(false), [selected]) | ||
|
||
// Preloads the edit state on hover, using concurrent rendering with `startTransition` so preloads can be interrupted and not block rendering | ||
const [preloading, setPreload] = useState(false) | ||
const timeoutRef = useRef<ReturnType<typeof setTimeout> | null>(null) | ||
const handleMouseEnter = useCallback(() => { | ||
timeoutRef.current = setTimeout(() => startTransition(() => setPreload(true)), 400) | ||
}, []) | ||
const handleMouseLeave = useCallback(() => { | ||
if (timeoutRef.current) clearTimeout(timeoutRef.current) | ||
startTransition(() => setPreload(false)) | ||
}, []) | ||
|
||
return ( | ||
<PreviewCard | ||
data-testid={`pane-item-${title}`} | ||
__unstable_focusRing | ||
as={Link as FIXME} | ||
as={ChildLink as FIXME} | ||
// @ts-expect-error - `childId` is a valid prop on `ChildLink` | ||
childId={id} | ||
bjoerge marked this conversation as resolved.
Show resolved
Hide resolved
|
||
data-as="a" | ||
margin={margin} | ||
marginBottom={marginBottom} | ||
marginTop={marginTop} | ||
onClick={handleClick} | ||
onMouseEnter={handleMouseEnter} | ||
onMouseLeave={handleMouseLeave} | ||
pressed={pressed} | ||
radius={2} | ||
selected={selected || clicked} | ||
|
@@ -162,6 +171,18 @@ export function PaneItem(props: PaneItemProps) { | |
tone="inherit" | ||
> | ||
{preview} | ||
{preloading && schemaType?.name && value && isSanityDocument(value) && ( | ||
<PreloadDocumentPane documentId={id} documentType={schemaType.name} /> | ||
)} | ||
</PreviewCard> | ||
) | ||
} | ||
|
||
function PreloadDocumentPane(props: {documentId: string; documentType: string}) { | ||
const {documentId, documentType} = props | ||
// Preload the edit state for the document, and keep it alive until mouse leave | ||
useEditState(documentId, documentType) | ||
|
||
return null | ||
} | ||
PreloadDocumentPane.displayName = 'PreloadDocumentPane' |
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 fixes a long standing quirk where clicking on a document sees a weird initial layout shift before the panel is mounted 😮💨