# Composition

import { Callout } from "fumadocs-ui/components/callout";
import { Tabs, Tab } from "fumadocs-ui/components/tabs";
import { Step, Steps } from "fumadocs-ui/components/steps";
import {
  Badge,
  Card,
  CardContent,
  CardHeader,
  CardTitle,
} from "@pycolors/ui";
import {
  ArrowRight,
  Boxes,
  CheckCircle,
  Layers3,
  LayoutDashboard,
  LayoutTemplate,
  PanelTop,
  PanelsTopLeft,
  Rocket,
  Rows3,
  ShieldCheck,
  Sparkles,
  XCircle,
} from "lucide-react";
import { PreferAvoid } from '@/components/docs/prefer-avoid';
import { DocsLinks } from "@/components/docs/docs-links";
import { DocsFeatureGrid } from "@/components/docs/docs-feature-grid";
import { DocsConceptTabs } from "@/components/docs/docs-concept-tabs";

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

  <Badge>
    Composition
  </Badge>
</div>

### Compose product surfaces from small primitives

PyColors UI components are intentionally small.

They are not designed to own your whole product flow.

They are designed to be composed into clear, maintainable product surfaces:

* dashboards
* settings pages
* billing panels
* forms
* tables
* overlays
* admin tools
* empty and error states

The goal is not to create the biggest component possible.

The goal is to compose smaller primitives into interfaces that remain understandable as the product grows.

<Callout title="Core idea">
  Components should stay small. Product meaning comes from composition.
</Callout>

## Why composition matters

Most UI systems start clean and become fragile later.

The usual cause is not the design.

It is the abstraction strategy.

Products become hard to maintain when teams:

* create one giant component for every feature
* duplicate the same layout pattern across pages
* mix business logic inside UI primitives
* add one-off variants instead of composing states
* hide product decisions behind unclear props

Composition prevents that.

It lets you keep primitives reusable while still building real SaaS experiences.

## The composition model

PyColors uses a layered mental model.

<DocsConceptTabs
  items={[
  {
    value: 'Primitive',
    title: 'Primitive',
    icon: Boxes,
    description: (
      <>
        <p>
          A small reusable component with one clear responsibility.
        </p>

        <div className="mt-4 flex flex-wrap gap-2">
          <Badge variant="outline">Button</Badge>
          <Badge variant="outline">Card</Badge>
          <Badge variant="outline">Input</Badge>
          <Badge variant="outline">Badge</Badge>
        </div>
      </>
    ),
  },
  {
    value: 'Pattern',
    title: 'Pattern',
    icon: LayoutTemplate,
    description: (
      <>
        <p>
          A reusable product behavior made from multiple primitives,
          state, and UX rules.
        </p>

        <div className="mt-4 flex flex-wrap gap-2">
          <Badge variant="outline">Data table</Badge>
          <Badge variant="outline">Overlays</Badge>
          <Badge variant="outline">Async actions</Badge>
        </div>
      </>
    ),
  },
  {
    value: 'Product',
    title: 'Product surface',
    icon: LayoutDashboard,
    description: (
      <>
        <p>
          A real feature or page where UI, copy, data, auth, billing,
          and product decisions meet.
        </p>

        <div className="mt-4 flex flex-wrap gap-2">
          <Badge variant="outline">Dashboard</Badge>
          <Badge variant="outline">Billing</Badge>
          <Badge variant="outline">Settings</Badge>
        </div>
      </>
    ),
  },
]}
/>

<Callout type="info" title="Rule of thumb">
  If the component describes visual structure, keep it in UI. If it describes product behavior, move it into Guides or Patterns.
</Callout>

### Composition principles

<DocsFeatureGrid
  columns={3}
  tone="platform"
  items={[
  {
    title: 'Structure first',
    description:
      'Start with page structure, hierarchy, and content purpose before styling details.',
    icon: Layers3,
  },
  {
    title: 'Meaning over decoration',
    description:
      'Use components to communicate product intent instead of decorating every surface.',
    icon: Sparkles,
  },
  {
    title: 'Clear ownership',
    description:
      'UI renders state. Product logic, auth, billing, and backend rules live outside primitives.',
    icon: ShieldCheck,
  },
]}
/>

### Page composition

A production SaaS page usually has four responsibilities.

| Area     | Responsibility                   | Typical primitives                         |
| -------- | -------------------------------- | ------------------------------------------ |
| Header   | Context and primary action       | `Button`, `Badge`                          |
| Content  | Main product information         | `Card`, `Table`, `Tabs`                    |
| Feedback | Loading, empty, error, success   | `Skeleton`, `EmptyState`, `Alert`, `Toast` |
| Overlays | Focused or progressive workflows | `Dialog`, `Sheet`, `DropdownMenu`          |

This keeps the page readable.

The page coordinates product intent. Components render the pieces.

## Examples

These examples illustrate how composition turns small UI primitives into real product surfaces.

Each example highlights how structure, state, and user intent are organized to create interfaces that are scalable, predictable, and easy to maintain.

Do not focus on the components alone. Focus on how the page coordinates behavior.

### Example: settings page

A settings page is not one component.

It is a composition of surfaces, navigation, inputs, and confirmations.

```tsx title="settings-page.tsx"
export function SettingsPage() {
  return (
    <div className="space-y-6">
      <SettingsHeader />

      <Tabs defaultValue="profile">
        <TabsList>
          <TabsTrigger value="profile">Profile</TabsTrigger>
          <TabsTrigger value="security">Security</TabsTrigger>
          <TabsTrigger value="billing">Billing</TabsTrigger>
        </TabsList>

        <TabsContent value="profile">
          <ProfileSettingsCard />
        </TabsContent>

        <TabsContent value="security">
          <SecuritySettingsCard />
        </TabsContent>

        <TabsContent value="billing">
          <BillingSettingsCard />
        </TabsContent>
      </Tabs>
    </div>
  );
}
```

This page uses primitives, but the product meaning comes from the composition:

* tabs separate concerns
* cards group content
* inputs collect values
* dialogs confirm destructive actions
* alerts communicate blocking states

### Example: billing panel

Billing UI needs clear hierarchy because it affects trust and revenue.

```tsx title="billing-panel.tsx"
export function BillingPanel() {
  return (
    <Card>
      <CardHeader>
        <CardTitle>Current plan</CardTitle>
      </CardHeader>

      <CardContent className="space-y-4">
        <Badge variant="success">Active</Badge>
        <p className="text-sm text-muted-foreground">
          Your subscription is active and renews next month.
        </p>
        <Button>Manage billing</Button>
      </CardContent>
    </Card>
  );
}
```

The primitives stay simple.

The product layer decides:

* what the billing state means
* what action is available
* what copy reassures the user
* when access should be gated

### Example: data-heavy page

Data-heavy pages should not make the table responsible for everything.

```tsx title="projects-page.tsx"
export function ProjectsPage() {
  return (
    <div className="space-y-6">
      <ProjectsHeader />
      <ProjectsToolbar />
      <ProjectsTable />
      <ProjectsPagination />
    </div>
  );
}
```

The table renders rows.

The page coordinates:

* filters
* loading state
* empty state
* error recovery
* pagination
* row actions

<Callout title="Why this matters">
  A table should not own your product flow. It should render structured data inside a larger page composition.
</Callout>

### Composition workflow

<Steps>
  <Step>
    #### Start with the user goal

    Define what the page helps the user do. Good composition starts with purpose, not components.
  </Step>

  <Step>
    #### Choose the page structure

    Decide the major regions: header, content, controls, feedback, overlays.
  </Step>

  <Step>
    #### Use primitives for each responsibility

    Use Button for actions, Card for surfaces, Input for values, Badge for metadata, and Alert for blocking feedback.
  </Step>

  <Step>
    #### Move repeated behavior into Patterns

    If multiple pages repeat the same flow, document it as a pattern.
  </Step>

  <Step>
    #### Keep business logic outside UI primitives

    Auth, billing, permissions, data fetching, and backend rules should not live inside low-level UI components.
  </Step>
</Steps>

### When to extract a component

Extract a component when:

* the visual structure repeats
* the component has a clear responsibility
* it makes the page easier to read
* the abstraction does not hide important product logic

Do not extract when:

* the component exists only once
* the abstraction creates too many props
* the component mixes unrelated responsibilities
* the name does not describe a clear concept

<Callout type="info" title="Extraction rule">
  Extract for clarity, not for cleverness.
</Callout>

### When to create a Pattern

Create a Pattern when the repeated need includes behavior.

Examples:

| Repeated need                                      | Better as             |
| -------------------------------------------------- | --------------------- |
| Table with loading, empty, error, filters, actions | Data table pattern    |
| Save / retry / undo / optimistic feedback          | Async actions pattern |
| Dropdown vs Dialog vs Sheet decisions              | Overlays pattern      |
| Form structure with validation and error handling  | Guide / Pattern       |
| SaaS dashboard shell                               | Guide / Starter       |

A pattern can include multiple primitives, state rules, copywriting rules, and accessibility guidance.

### Prefer / avoid

<PreferAvoid
  prefer={[
  'small primitives with clear responsibility',
  'page-level composition for product meaning',
  'patterns for repeated behavior',
  'semantic tokens over raw values',
  'explicit structure over hidden magic',
]}
  avoid={[
  'giant configurable components',
  'business logic inside primitives',
  'duplicated product flows across pages',
  'custom styles that bypass tokens',
  'abstracting before the pattern is clear',
]}
/>

### Composition checklist

Before shipping a composed UI surface, confirm:

* the page has a clear user goal
* each component has one clear responsibility
* semantic tokens are used for visual roles
* loading, empty, and error states are handled
* actions have clear feedback
* overlays are used only when needed
* repeated behavior is moved into a pattern
* business logic is not hidden inside primitives
* the layout still makes sense on mobile

<Callout title="Product quality rule">
  A premium UI is not more decorated. It is easier to understand, easier to maintain, and harder to misuse.
</Callout>

## Next steps

<DocsLinks
  variant="grid"
  tone="platform"
  items={[
  {
    title: 'Why PyColors UI',
    href: '/docs/ui/why-pycolors-ui',
    description:
      'Understand the philosophy behind small primitives and product-level patterns.',
    icon: Sparkles,
  },
  {
    title: 'Patterns',
    href: '/docs/patterns',
    description:
      'Move from components to production-ready feature blocks.',
    icon: LayoutTemplate,
  },
  {
    title: 'Building a SaaS layout',
    href: '/docs/guides/building-a-saas-layout',
    description:
      'Compose primitives into real dashboard structure.',
    icon: PanelsTopLeft,
  },
  {
    title: 'Starter Pro',
    href: '/docs/starter-pro',
    description:
      'See how UI composition connects to auth, billing, backend, and production flows.',
    icon: Rocket,
  },
]}
/>