Component
Dialog

Dialog

A window overlaid on either the primary window or another dialog window, rendering the content underneath inert.

Installation

Install the following dependencies

npm i @radix-ui/react-dialog

Copy and paste the following code into your project

components/ui/dialog/index.tsx
'use client'
 
import * as React from 'react'
import * as DialogPrimitive from '@radix-ui/react-dialog'
import { X } from 'lucide-react'
import { createStyleContext } from '@shadow-panda/style-context'
import { styled } from '@shadow-panda/styled-system/jsx'
import { css } from '@shadow-panda/styled-system/css'
import { dialog, icon } from '@shadow-panda/styled-system/recipes'
 
const { withProvider, withContext } = createStyleContext(dialog)
 
const DialogPortal = withContext(styled(DialogPrimitive.Portal), 'portal')
const DialogOverlay = withContext(styled(DialogPrimitive.Overlay), 'overlay')
const DialogClose = withContext(styled(DialogPrimitive.Close), 'close')
 
const Content = React.forwardRef<
  React.ElementRef<typeof DialogPrimitive.Content>,
  React.ComponentPropsWithoutRef<typeof DialogPrimitive.Content>
>(({ children, ...props }, ref) => (
  <DialogPortal>
    <DialogOverlay />
    <DialogPrimitive.Content ref={ref} {...props}>
      {children}
      <DialogClose>
        <X className={icon()} />
        <span className={css({ srOnly: true })}>Close</span>
      </DialogClose>
    </DialogPrimitive.Content>
  </DialogPortal>
))
Content.displayName = DialogPrimitive.Content.displayName
 
export const Dialog = withProvider(styled(DialogPrimitive.Root), 'root')
export const DialogTrigger = withContext(styled(DialogPrimitive.Trigger), 'trigger')
export const DialogContent = withContext(styled(Content), 'content')
export const DialogHeader = withContext(styled('div'), 'header')
export const DialogFooter = withContext(styled('div'), 'footer')
export const DialogTitle = withContext(styled(DialogPrimitive.Title), 'title')
export const DialogDescription = withContext(styled(DialogPrimitive.Description), 'description')

Update the import paths to match your project setup

Usage

import {
  Dialog,
  DialogContent,
  DialogDescription,
  DialogHeader,
  DialogTitle,
  DialogTrigger,
} from '@/components/ui/dialog'
<Dialog>
  <DialogTrigger>Open</DialogTrigger>
  <DialogContent>
    <DialogHeader>
      <DialogTitle>Are you sure absolutely sure?</DialogTitle>
      <DialogDescription>
        This action cannot be undone. This will permanently delete your account and remove your data
        from our servers.
      </DialogDescription>
    </DialogHeader>
  </DialogContent>
</Dialog>

Notes

To activate the Dialog component from within a Context Menu or Dropdown Menu, you must encase the Context Menu or Dropdown Menu component in the Dialog component. For more information, refer to the linked issue here.

<Dialog>
  <ContextMenu>
    <ContextMenuTrigger>Right click</ContextMenuTrigger>
    <ContextMenuContent>
      <ContextMenuItem>Open</ContextMenuItem>
      <ContextMenuItem>Download</ContextMenuItem>
      <DialogTrigger asChild>
        <ContextMenuItem>
          <span>Delete</span>
        </ContextMenuItem>
      </DialogTrigger>
    </ContextMenuContent>
  </ContextMenu>
  <DialogContent>
    <DialogHeader>
      <DialogTitle>Are you sure absolutely sure?</DialogTitle>
      <DialogDescription>
        This action cannot be undone. Are you sure you want to permanently delete this file from our
        servers?
      </DialogDescription>
    </DialogHeader>
    <DialogFooter>
      <Button type="submit">Confirm</Button>
    </DialogFooter>
  </DialogContent>
</Dialog>