Skip to content

Commit d7905f1

Browse files
Kilianpacocoursey
andauthored
Allow setting a container for Radix.Portal (#58)
* Allow setting container to Radix Portal * add documentation for container prop * Add note explaining container prop * Replace `querySelector` in example with useRef * prettier formatting * fix up jsdoc Co-authored-by: Paco <34928425+pacocoursey@users.noreply.github.com>
1 parent 994dea6 commit d7905f1

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,19 @@ return (
151151
)
152152
```
153153

154+
You can provide a `container` prop that accepts an HTML element that is forwarded to Radix UI's Dialog Portal component to specify which element the Dialog should portal into (defaults to `body`). See the [Radix Documentation](https://www.radix-ui.com/docs/primitives/components/dialog#portal) for more information.
155+
156+
```tsx
157+
const containerElement = React.useRef(null)
158+
159+
return (
160+
<>
161+
<Command.Dialog container={containerElement.current} />
162+
<div ref={containerElement} />
163+
</>
164+
)
165+
```
166+
154167
### Input `[cmdk-input]`
155168

156169
All props are forwarded to the underlying `input` element. Can be controlled with the `value` and `onValueChange` props.

cmdk/src/index.tsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@ type SeparatorProps = DivProps & {
1414
/** Whether this separator should always be rendered. Useful if you disable automatic filtering. */
1515
alwaysRender?: boolean
1616
}
17-
type DialogProps = RadixDialog.DialogProps & CommandProps
17+
type DialogProps = RadixDialog.DialogProps &
18+
CommandProps & {
19+
/** Provide a custom element the Dialog should portal into. */
20+
container?: HTMLElement
21+
}
1822
type ListProps = Children & DivProps & {}
1923
type ItemProps = Children &
2024
Omit<DivProps, 'disabled' | 'onSelect' | 'value'> & {
@@ -764,10 +768,10 @@ const List = React.forwardRef<HTMLDivElement, ListProps>((props, forwardedRef) =
764768
* Renders the command menu in a Radix Dialog.
765769
*/
766770
const Dialog = React.forwardRef<HTMLDivElement, DialogProps>((props, forwardedRef) => {
767-
const { open, onOpenChange, ...etc } = props
771+
const { open, onOpenChange, container, ...etc } = props
768772
return (
769773
<RadixDialog.Root open={open} onOpenChange={onOpenChange}>
770-
<RadixDialog.Portal>
774+
<RadixDialog.Portal container={container}>
771775
<RadixDialog.Overlay cmdk-overlay="" />
772776
<RadixDialog.Content aria-label={props.label} cmdk-dialog="">
773777
<Command ref={forwardedRef} {...etc} />

0 commit comments

Comments
 (0)