-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(util): add useDocument hook (#528)
Migrates useDocument from the VE starter to this repo. Also moves DocumentProvider. Documentation added to README.md Co-authored-by: Matt Kilpatrick <[email protected]>
- Loading branch information
1 parent
8fd95b1
commit f27199a
Showing
4 changed files
with
64 additions
and
1 deletion.
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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export { getRuntime } from "./runtime.js"; | ||
export { isProduction } from "./env.js"; | ||
export { dynamic, type DynamicOptions } from "./dynamic.js"; | ||
export { DocumentProvider, useDocument } from "./useDocument.js"; |
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 |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import * as React from "react"; | ||
|
||
const DocumentContext = React.createContext<any | undefined>(undefined); | ||
|
||
type DocumentProviderProps<T> = { | ||
value: T; | ||
children: React.ReactNode; | ||
}; | ||
|
||
const DocumentProvider = <T,>({ | ||
value, | ||
children, | ||
}: DocumentProviderProps<T>) => { | ||
return ( | ||
<DocumentContext.Provider value={value}> | ||
{children} | ||
</DocumentContext.Provider> | ||
); | ||
}; | ||
|
||
const useDocument = <T,>(): T => { | ||
const context = React.useContext(DocumentContext); | ||
if (!context) { | ||
throw new Error("useDocument must be used within a DocumentProvider"); | ||
} | ||
|
||
return context as T; | ||
}; | ||
|
||
export { DocumentProvider, useDocument }; |