File tree Expand file tree Collapse file tree 4 files changed +49
-3
lines changed
apps/ssr-testing/app/document-context
packages/react/document-context/src Expand file tree Collapse file tree 4 files changed +49
-3
lines changed Original file line number Diff line number Diff line change
1
+ 'use client' ;
2
+ import * as React from 'react' ;
3
+ import { DocumentContext } from 'radix-ui/internal' ;
4
+
5
+ export function Child ( ) {
6
+ const document = DocumentContext . useDocument ( ) ;
7
+ React . useEffect ( ( ) => {
8
+ console . log ( document ) ;
9
+ } , [ document ] ) ;
10
+ return < p > Open the console to see the document context.</ p > ;
11
+ }
Original file line number Diff line number Diff line change 1
1
import * as React from 'react' ;
2
2
3
- import * as DocumentContext from '@radix-ui/react-document-context' ;
3
+ import { Parent } from './parent' ;
4
+ import { Child } from './child' ;
4
5
5
6
export default function Page ( ) {
6
- return < DocumentContext . DocumentProvider > Document Context</ DocumentContext . DocumentProvider > ;
7
+ return (
8
+ < Parent >
9
+ < h1 > Document Context</ h1 >
10
+ < Child />
11
+ </ Parent >
12
+ ) ;
7
13
}
Original file line number Diff line number Diff line change
1
+ 'use client' ;
2
+ import * as React from 'react' ;
3
+ import { DocumentContext } from 'radix-ui/internal' ;
4
+
5
+ export function Parent ( { children } : { children : React . ReactNode } ) {
6
+ const [ document , setDocument ] = React . useState < Document | null > ( null ) ;
7
+ React . useEffect ( ( ) => {
8
+ console . log ( document ) ;
9
+ } , [ document ] ) ;
10
+ return (
11
+ < DocumentContext . DocumentProvider document = { document } >
12
+ { children }
13
+ < div >
14
+ < iframe
15
+ onLoad = { ( event ) => {
16
+ const iframeDocument = event . currentTarget . contentDocument ;
17
+ setDocument ( iframeDocument ) ;
18
+ } }
19
+ title = "Radix UI webiste"
20
+ src = "https://www.radix-ui.com"
21
+ style = { {
22
+ width : '100%' ,
23
+ height : '1000px' ,
24
+ } }
25
+ />
26
+ </ div >
27
+ </ DocumentContext . DocumentProvider >
28
+ ) ;
29
+ }
Original file line number Diff line number Diff line change @@ -6,7 +6,7 @@ const DocumentContext = React.createContext<Document | null>(null);
6
6
DocumentContext . displayName = 'DocumentContext' ;
7
7
8
8
interface DocumentProviderProps {
9
- document : Document ;
9
+ document : Document | null ;
10
10
children : React . ReactNode ;
11
11
}
12
12
You can’t perform that action at this time.
0 commit comments