-
Notifications
You must be signed in to change notification settings - Fork 432
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(core): use context selector for DocumentPaneProvider
- Loading branch information
Showing
5 changed files
with
69 additions
and
35 deletions.
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
6 changes: 4 additions & 2 deletions
6
packages/sanity/src/_singletons/context/DocumentPaneContext.ts
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 |
---|---|---|
@@ -1,9 +1,11 @@ | ||
import {createContext} from 'sanity/_createContext' | ||
// import {createContext} from 'sanity/_createContext' | ||
|
||
import {createContext} from 'use-context-selector' | ||
|
||
import type {DocumentPaneContextValue} from '../../structure/panes/document/DocumentPaneContext' | ||
|
||
/** @internal */ | ||
export const DocumentPaneContext = createContext<DocumentPaneContextValue | null>( | ||
'sanity/_singletons/context/document-pane', | ||
// 'sanity/_singletons/context/document-pane', | ||
null, | ||
) |
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
16 changes: 9 additions & 7 deletions
16
packages/sanity/src/structure/panes/document/useDocumentPane.tsx
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 |
---|---|---|
@@ -1,15 +1,17 @@ | ||
import {useContext} from 'react' | ||
import {DocumentPaneContext} from 'sanity/_singletons' | ||
import {type Context, useContextSelector} from 'use-context-selector' | ||
|
||
import {type DocumentPaneContextValue} from './DocumentPaneContext' | ||
|
||
/** @internal */ | ||
export function useDocumentPane(): DocumentPaneContextValue { | ||
const documentPane = useContext(DocumentPaneContext) | ||
|
||
if (!documentPane) { | ||
throw new Error('DocumentPane: missing context value') | ||
} | ||
export function useDocumentPane<T = DocumentPaneContextValue>( | ||
selector?: (value: DocumentPaneContextValue) => T, | ||
): T { | ||
const selectorFn = selector || ((value: DocumentPaneContextValue) => value as T) | ||
const documentPane = useContextSelector<DocumentPaneContextValue, T>( | ||
DocumentPaneContext as Context<DocumentPaneContextValue>, | ||
selectorFn, | ||
) | ||
|
||
return documentPane | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.