# Overlays Pattern

import { Callout } from "fumadocs-ui/components/callout";
import { Tabs, Tab } from "fumadocs-ui/components/tabs";
import { Badge } from "@pycolors/ui";
import {
  CheckCircle,
  LayoutPanelTop,
  PanelRight,
  ShieldCheck,
  Sparkles,
} from "lucide-react";

import { PreferAvoid } from "@/components/docs/prefer-avoid";
import { DocsConceptTabs } from "@/components/docs/docs-concept-tabs";
import { DocsDecisionGrid } from "@/components/docs/docs-decision-grid";
import { DocsFeatureGrid } from "@/components/docs/docs-feature-grid";
import { DocsUpgradeCTA } from "@/components/docs/docs-upgrade-cta";

import {
  OverlayDecisionDemo,
  SheetFiltersDemo,
  DialogCreateDemo,
  DropdownRowActionsDemo,
} from "../previews/overlays";

<div className="flex flex-wrap items-center gap-2">
  <Badge variant="outline">
    Patterns
  </Badge>

  <Badge>
    Overlays
  </Badge>
</div>

## Overlays define interaction quality

Overlays are not just UI components.

They define how users interact with:

* workflows
* actions
* confirmations
* navigation
* product state
* contextual decisions

Choosing the wrong overlay creates:

* broken flows
* accessibility problems
* unclear user intent
* inconsistent interaction patterns
* fragile UX behavior

Choosing the right one makes the product feel:

* faster
* safer
* clearer
* more intentional

<Callout title="Mental model">
  Overlays are interaction contracts, not visual components.
</Callout>

### Overlay system

<DocsConceptTabs
  items={[
  {
    value: 'Dropdown',
    title: 'DropdownMenu',
    icon: LayoutPanelTop,
    description: (
      <>
        <p>
          Lightweight contextual actions that should remain fast, compact, and close to the trigger.
        </p>

        <div className="mt-4 flex flex-wrap gap-2">
          <Badge variant="outline">Row actions</Badge>
          <Badge variant="outline">Quick actions</Badge>
          <Badge variant="outline">Compact menus</Badge>
          <Badge variant="outline">Contextual actions</Badge>
        </div>
      </>
    ),
  },
  {
    value: 'Dialog',
    title: 'Dialog',
    icon: ShieldCheck,
    description: (
      <>
        <p>
          Focused blocking interactions that require explicit user attention before continuing.
        </p>

        <div className="mt-4 flex flex-wrap gap-2">
          <Badge variant="outline">Create flows</Badge>
          <Badge variant="outline">Edit forms</Badge>
          <Badge variant="outline">Confirmations</Badge>
          <Badge variant="outline">Critical actions</Badge>
        </div>
      </>
    ),
  },
  {
    value: 'Sheet',
    title: 'Sheet',
    icon: PanelRight,
    description: (
      <>
        <p>
          Progressive disclosure for filters, mobile navigation, side panels, and secondary workflows.
        </p>

        <div className="mt-4 flex flex-wrap gap-2">
          <Badge variant="outline">Filters</Badge>
          <Badge variant="outline">Mobile navigation</Badge>
          <Badge variant="outline">Side workflows</Badge>
          <Badge variant="outline">Progressive disclosure</Badge>
        </div>
      </>
    ),
  },
]}
/>

### Decision matrix

<DocsDecisionGrid
  columns={3}
  items={[
  {
    eyebrow: 'Micro interactions',
    title: 'Use DropdownMenu',
    icon: LayoutPanelTop,
    description:
      'Use DropdownMenu for compact contextual actions that should stay lightweight and fast.',
    items: [
      'row actions',
      'quick contextual choices',
      'compact menus',
      'non-blocking actions',
    ],
  },
  {
    eyebrow: 'Focused tasks',
    title: 'Use Dialog',
    icon: ShieldCheck,
    tone: 'pro',
    description:
      'Use Dialog when the user must complete or cancel a focused interaction intentionally.',
    items: [
      'forms',
      'create flows',
      'edit flows',
      'destructive confirmations',
    ],
  },
  {
    eyebrow: 'Side workflows',
    title: 'Use Sheet',
    icon: PanelRight,
    description:
      'Use Sheet for secondary workflows that should stay connected to the current page.',
    items: [
      'filters',
      'mobile navigation',
      'configuration panels',
      'secondary workflows',
    ],
  },
]}
/>

### Rule of thumb

* Dropdown → micro actions
* Dialog → focused task
* Sheet → side workflow

<OverlayDecisionDemo />

## Core UX rules

<DocsFeatureGrid
  columns={3}
  tone="platform"
  items={[
  {
    title: 'Focus management',
    description:
      'Trap focus correctly, restore focus to the trigger, and support complete keyboard navigation.',
    icon: ShieldCheck,
  },
  {
    title: 'Scroll behavior',
    description:
      'Lock background scrolling while preserving internal overlay scrolling.',
    icon: Sparkles,
  },
  {
    title: 'Interaction clarity',
    description:
      'Use explicit actions, predictable closing behavior, and intentional workflow boundaries.',
    icon: CheckCircle,
  },
]}
/>

### Focus and keyboard

* Trap focus in Dialog and Sheet
* Restore focus to the trigger on close
* Support Escape to close safely
* Ensure full keyboard navigation

### Scroll behavior

* Lock page scroll when Dialog or Sheet is open
* Allow scrolling inside the overlay
* Avoid nested scroll containers

### Layering

* Avoid deep overlay nesting
* Dropdown → Dialog is acceptable when intentional
* Ensure overlays stack correctly with portals and z-index

### Closing behavior

* Always provide explicit actions such as **Cancel** and **Save**
* Do not auto-close on error
* Require confirmation for destructive actions

<Callout type="info" title="Production rule">
  Overlay quality directly affects product trust. Weak focus handling and inconsistent interaction patterns immediately make the UI feel fragile.
</Callout>

## Common flows

### Create flow → Dialog

Use Dialog when the user starts a focused task that must be completed or canceled intentionally.

<DialogCreateDemo />

<br />

### Filters and side workflow → Sheet

Use Sheet for progressive disclosure and secondary workflows that should stay connected to the current page.

<SheetFiltersDemo />

<br />

### Row actions → DropdownMenu

Use DropdownMenu for contextual actions that should remain lightweight and fast.

<DropdownRowActionsDemo />

## Accessibility checklist

Before shipping overlays:

* Dialog and Sheet have a visible or accessible title
* Focus is trapped correctly
* Focus returns to trigger on close
* Escape closes safely
* Buttons have clear labels
* Icon-only triggers include `aria-label`
* Dropdown items are keyboard accessible
* Overlay state changes are predictable

<Callout title="Accessibility is infrastructure">
  Accessibility is not optional for overlays. It defines whether the interaction is usable at all.
</Callout>

## Examples

<Tabs items={["Dialog", "Sheet"]}>
  <Tab value="Dialog">
    ```tsx title="create-dialog.tsx"
    <Dialog>
      <DialogTrigger asChild>
        <Button>Create</Button>
      </DialogTrigger>

      <DialogContent>
        <DialogHeader>
          <DialogTitle>Create project</DialogTitle>

          <DialogDescription>
            Give it a name and status.
          </DialogDescription>
        </DialogHeader>

        {/* form */}
      </DialogContent>
    </Dialog>
    ```
  </Tab>

  <Tab value="Sheet">
    ```tsx title="filters-sheet.tsx"
    <Sheet>
      <SheetTrigger asChild>
        <Button variant="outline">
          Filters
        </Button>
      </SheetTrigger>

      <SheetContent>
        <SheetHeader>
          <SheetTitle>Filters</SheetTitle>

          <SheetDescription>
            Narrow results and apply advanced options.
          </SheetDescription>
        </SheetHeader>

        {/* filters */}
      </SheetContent>
    </Sheet>
    ```
  </Tab>
</Tabs>

## Decision guide

<PreferAvoid
  prefer={[
  'one overlay per interaction',
  'clear user intent',
  'accessible focus handling',
  'progressive disclosure',
  'predictable closing behavior',
]}
  avoid={[
  'stacking multiple overlays',
  'using Dialog for navigation',
  'using DropdownMenu for forms',
  'implicit destructive actions',
  'breaking keyboard navigation',
]}
/>

## Why this matters

Overlays are where interaction quality becomes visible.

A strong overlay system makes the product feel:

* faster
* safer
* clearer
* more deliberate
* more production-ready

A weak overlay system makes the entire product feel fragile.

<DocsUpgradeCTA title="Build overlays that feel production-ready." description="PyColors Patterns help standardize accessibility, interaction quality, and scalable product behavior across your SaaS surface." productSlug="starter-pro" buyLabel="Explore Starter Pro" secondaryHref="/docs/patterns" secondaryLabel="Browse Patterns" />