-
Notifications
You must be signed in to change notification settings - Fork 973
feat(DocumentContext): document provider and hook #3516
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
Open
chaance
wants to merge
15
commits into
main
Choose a base branch
from
pr/3357
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
89a3183
feat(DocumentContext): document provider and hook
arturbien 70fd89c
use providedDocument instead of document
arturbien 6c0c407
add stories and SSR test page
arturbien 0b46e24
rename document to providedDocument
arturbien 0a1effa
remove unused stuff
arturbien 14197d9
add unit tests
arturbien 2baccd9
replace window with providedDocument.defaultView
arturbien 6862ec3
update casing
arturbien 3d6da65
address document-context comments
arturbien 4f9b688
fixup for updated repo config
chaance 7b79b61
fix ssr testing
chaance 59c4a20
minor storybook tweaks
chaance 24b989e
add missing deps
chaance 131569d
use globals for timers
chaance 881d2ef
revert signature change for useEscapeKeydown
chaance 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 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,11 @@ | ||
'use client'; | ||
import * as React from 'react'; | ||
import { DocumentContext } from 'radix-ui/internal'; | ||
|
||
export function Child() { | ||
const document = DocumentContext.useDocument(); | ||
React.useEffect(() => { | ||
console.log(document); | ||
}, [document]); | ||
return <p>Open the console to see the document context.</p>; | ||
} |
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,13 @@ | ||
import * as React from 'react'; | ||
|
||
import { Parent } from './parent'; | ||
import { Child } from './child'; | ||
|
||
export default function Page() { | ||
return ( | ||
<Parent> | ||
<h1>Document Context</h1> | ||
<Child /> | ||
</Parent> | ||
); | ||
} |
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,29 @@ | ||
'use client'; | ||
import * as React from 'react'; | ||
import { DocumentContext } from 'radix-ui/internal'; | ||
|
||
export function Parent({ children }: { children: React.ReactNode }) { | ||
const [document, setDocument] = React.useState<Document | null>(null); | ||
React.useEffect(() => { | ||
console.log(document); | ||
}, [document]); | ||
return ( | ||
<DocumentContext.DocumentProvider document={document}> | ||
{children} | ||
<div> | ||
<iframe | ||
onLoad={(event) => { | ||
const iframeDocument = event.currentTarget.contentDocument; | ||
setDocument(iframeDocument); | ||
}} | ||
title="Radix UI webiste" | ||
src="https://www.radix-ui.com" | ||
style={{ | ||
width: '100%', | ||
height: '1000px', | ||
}} | ||
/> | ||
</div> | ||
</DocumentContext.DocumentProvider> | ||
); | ||
} |
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
64 changes: 64 additions & 0 deletions
64
apps/storybook/stories/document-context.stories.module.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,64 @@ | ||
.trigger { | ||
border: 1px solid black; | ||
border-radius: 6px; | ||
background-color: transparent; | ||
padding: 5px 10px; | ||
font-family: apple-system, BlinkMacSystemFont, helvetica, arial, sans-serif; | ||
font-size: 13px; | ||
|
||
&:focus { | ||
outline: none; | ||
box-shadow: 0 0 0 2px rgb(0 0 0 / 0.5); | ||
} | ||
} | ||
|
||
.content { | ||
display: inline-block; | ||
box-sizing: border-box; | ||
min-width: 130px; | ||
background-color: var(--color-white); | ||
border: 1px solid var(--color-gray100); | ||
border-radius: 6px; | ||
padding: 5px; | ||
box-shadow: 0 5px 10px 0 rgba(0, 0, 0, 0.1); | ||
font-family: apple-system, BlinkMacSystemFont, helvetica, arial, sans-serif; | ||
font-size: 13px; | ||
&:focus-within { | ||
border-color: var(--color-black); | ||
} | ||
} | ||
|
||
.item { | ||
display: flex; | ||
align-items: center; | ||
justify-content: space-between; | ||
line-height: 1; | ||
cursor: default; | ||
user-select: none; | ||
white-space: nowrap; | ||
height: 25px; | ||
padding: 0 10px; | ||
color: var(--color-black); | ||
border-radius: 3px; | ||
|
||
outline: none; | ||
|
||
&[data-highlighted] { | ||
background-color: var(--color-black); | ||
color: var(--color-white); | ||
} | ||
|
||
&[data-disabled] { | ||
color: var(--color-gray100); | ||
} | ||
} | ||
|
||
.dialog { | ||
position: fixed; | ||
background: white; | ||
border: 1px solid black; | ||
top: 50%; | ||
left: 50%; | ||
transform: translate(-50%, -50%); | ||
padding: 30px; | ||
} |
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,146 @@ | ||
import * as React from 'react'; | ||
import { createPortal } from 'react-dom'; | ||
import { DocumentContext } from 'radix-ui/internal'; | ||
import { Dialog, DropdownMenu, Tooltip } from 'radix-ui'; | ||
import styles from './document-context.stories.module.css'; | ||
|
||
export default { title: 'Utilities/DocumentContext' }; | ||
|
||
export const Default = () => { | ||
const [portalElement, setPortalElement] = React.useState<HTMLElement | null>(null); | ||
const [count, setCount] = React.useState(0); | ||
|
||
const openContentInPopup = async () => { | ||
const popup = window.open( | ||
'', | ||
'Popup Test', | ||
'height=600,width=600,left=300,top=300,resizable=yes,scrollbars=yes,toolbar=no,menubar=no,location=no,directories=no,status=no' | ||
); | ||
if (!popup) return; | ||
|
||
// Copy all parent window styles and fonts | ||
// https://developer.chrome.com/docs/web-platform/document-picture-in-picture/#copy-style-sheets-to-the-picture-in-picture-window | ||
[...document.styleSheets].forEach((styleSheet) => { | ||
try { | ||
const cssRules = [...styleSheet.cssRules].map((rule) => rule.cssText).join(''); | ||
const style = document.createElement('style'); | ||
|
||
style.textContent = cssRules; | ||
popup.document.head.appendChild(style); | ||
} catch (e) { | ||
console.error(e); | ||
const link = document.createElement('link'); | ||
if (styleSheet.href === null) { | ||
return; | ||
} | ||
|
||
link.rel = 'stylesheet'; | ||
link.type = styleSheet.type; | ||
link.media = styleSheet.media.toString(); | ||
link.href = styleSheet.href; | ||
popup.document.head.appendChild(link); | ||
} | ||
}); | ||
|
||
setPortalElement(popup.document.body); | ||
|
||
// Detect when window is closed by user | ||
popup.addEventListener('pagehide', () => { | ||
setPortalElement(null); | ||
}); | ||
}; | ||
|
||
const content = ( | ||
<div style={{ display: 'flex', flexDirection: 'column', gap: 40 }}> | ||
<div | ||
style={{ | ||
display: 'flex', | ||
flexDirection: 'column', | ||
gap: 32, | ||
padding: 32, | ||
background: 'yellow', | ||
}} | ||
> | ||
<h1>This section will be portalled to another document/window</h1> | ||
<button onClick={() => setCount(count + 1)}>Increment</button> | ||
<DropdownMenu.Root> | ||
<DropdownMenu.Trigger className={styles.trigger}> | ||
Dropdown with dialog test | ||
</DropdownMenu.Trigger> | ||
<DropdownMenu.Portal> | ||
<DropdownMenu.Content className={styles.content} sideOffset={5}> | ||
<Dialog.Root> | ||
<Dialog.Trigger className={styles.item} asChild> | ||
<DropdownMenu.Item onSelect={(event) => event.preventDefault()}> | ||
Open dialog | ||
</DropdownMenu.Item> | ||
</Dialog.Trigger> | ||
|
||
<Dialog.Portal> | ||
<Dialog.Content className={styles.dialog}> | ||
<Dialog.Title>Nested dropdown</Dialog.Title> | ||
<DropdownMenu.Root> | ||
<DropdownMenu.Trigger | ||
className={styles.trigger} | ||
style={{ width: '100%', marginBottom: 20 }} | ||
> | ||
Open | ||
</DropdownMenu.Trigger> | ||
<DropdownMenu.Portal> | ||
<DropdownMenu.Content className={styles.content} sideOffset={5}> | ||
<DropdownMenu.Item | ||
className={styles.item} | ||
onSelect={() => console.log('undo')} | ||
> | ||
Undo | ||
</DropdownMenu.Item> | ||
<DropdownMenu.Item | ||
className={styles.item} | ||
onSelect={() => console.log('redo')} | ||
> | ||
Redo | ||
</DropdownMenu.Item> | ||
<DropdownMenu.Arrow /> | ||
</DropdownMenu.Content> | ||
</DropdownMenu.Portal> | ||
</DropdownMenu.Root> | ||
<Dialog.Close>Close</Dialog.Close> | ||
</Dialog.Content> | ||
</Dialog.Portal> | ||
</Dialog.Root> | ||
<DropdownMenu.Item className={styles.item}>Test</DropdownMenu.Item> | ||
<DropdownMenu.Arrow /> | ||
</DropdownMenu.Content> | ||
</DropdownMenu.Portal> | ||
</DropdownMenu.Root> | ||
|
||
<Tooltip.Provider> | ||
<Tooltip.Root> | ||
<Tooltip.Trigger asChild> | ||
<button>Tooltip test</button> | ||
</Tooltip.Trigger> | ||
<Tooltip.Content>Tooltip content</Tooltip.Content> | ||
</Tooltip.Root> | ||
</Tooltip.Provider> | ||
</div> | ||
</div> | ||
); | ||
|
||
return ( | ||
<div> | ||
<button onClick={openContentInPopup} type="button"> | ||
Open in Popup | ||
</button> | ||
<mark>{count}</mark> | ||
|
||
{portalElement | ||
? createPortal( | ||
<DocumentContext.DocumentProvider document={portalElement.ownerDocument}> | ||
{content} | ||
</DocumentContext.DocumentProvider>, | ||
portalElement | ||
) | ||
: content} | ||
</div> | ||
); | ||
}; |
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
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
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.
@chaance afaik browsers throttle setTimeout and setInterval by 1000ms when browser tab is inactive or hidden. So if the timeout is set on the parent window, and user opens Document PIP from it with an avatar in it, then navigates to a different tab or app, these timeouts might get delayed or paused. that's why I've attached all timeouts to the provided document window