Course contentsShow
← React
Lesson 1137 of 2,39629. Composition: SlotsFree lesson

Real-World Slot Patterns: Layout Components

Common slot patterns in layout components: sidebar slots, modal slots, and shell components with multiple regions.

Real-World Slot Patterns: Layout Components

What you'll learn: How to use slot patterns to build flexible layout components with multiple, well-defined content regions like sidebars, modals, and application shells.

Why Layout Components Need Slots

Layout components are perfect candidates for the slot pattern because they manage structure (positioning, spacing, responsive behavior) while delegating content to the parent. Think of them as picture frames with multiple openings—you control the frame's shape, but users choose what goes inside each opening.

Common Slot Patterns

Sidebar Layout

interface SidebarLayoutProps {
  sidebar: React.ReactNode;
  main: React.ReactNode;
  header?: React.ReactNode;
}

function SidebarLayout({ sidebar, main, header }: SidebarLayoutProps) {
  return (
    <div className="layout">
      {header && <header className="layout-header">{header}</header>}
      <div className="layout-body">
        <aside className="layout-sidebar">{sidebar}</aside>
        <main className="layout-main">{main}</main>
      </div>
    </div>
  );
}

Modal Slots

interface ModalProps {
  title: React.ReactNode;
  content: React.ReactNode;
  footer?: React.ReactNode;
}

function Modal({ title, content, footer }: ModalProps) {
  return (
    <div className="modal">
      <div className="modal-title">{title}</div>
      <div className="modal-content">{content}</div>
      {footer && <div className="modal-footer">{footer}</div>}
    </div>
  );
}

Application Shell

interface AppShellProps {
  nav: React.ReactNode;
  sidebar?: React.ReactNode;
  main: React.ReactNode;
  footer?: React.ReactNode;
}

Why This Works

Each slot has a clear semantic purpose—you're not just passing "children," you're saying "this is the navigation" or "this is the footer." The layout component handles responsive breakpoints, accessibility landmarks, and consistent spacing, while consumers focus purely on content.

Key Takeaway: Slot-based layout components separate structural concerns from content, creating reusable, semantic UI shells with multiple well-defined regions that consumers fill independently.