# Costs Agreement

The costs agreement builder's parts: badges for how an answer resolves and where a value comes from, rule pills and kinds, the question list, the review checks, the template library row and the builder popover.

- Beta: ready to use; its API can still change in a minor release.
- Category: Patterns · Costs agreement

```tsx
import { CaBadge, CaRulePill, CaRuleKind, CaQuestionRow, CaQuestionsHeader, CaReceiptCheck, CaTemplateRow, CaPopover } from "@leap-crm/design-system";
```

## Best practices

- Do: Use each `CaBadge` tone for its one meaning: `known`, `derived` or `ask` for how an answer resolves, and `contact`, `opportunity`, `firm` or `external` for where a value comes from. Use `Badge` for labels outside the builder.
- Do: Set `open` on a `CaRulePill` while its popover is open, so the pill keeps the focus ring and screen readers hear it as expanded.
- Do: Pass the current question's answer controls as `CaQuestionRow` children, like a `Field` with Save and Leave blank, and give open and answered rows `onSelect` so the lawyer can go back to one.
- Do: Return focus to the pill or field that opened a `CaPopover` when `onClose` runs. The builder places the popover itself, so nothing else moves focus back.
- Do: Use `width="xwide"` (680 px) for the field popover and the default 340 px for a rule. `wide` is 520 px for content in between.
- Don't: Tint a section of the agreement to show a rule applies to it. Put the rule's `CaRulePill` in the page margin and its `CaRuleKind` in the popover, so colour stays in the pill and the icon.

## Keyboard

| Keys | What they do |
| --- | --- |
| Tab | Moves through its buttons in order. |
| Enter, Space | Presses the focused button. |

## Related

- [Progress](./progress.md)
- [Pilot](./pilot.md)
- [Step Header](./step-header.md)

## Examples

What the examples import:

```tsx
import { useState } from "react";
import { ActionRow, Button, CaBadge, CaPopover, CaQuestionRow, CaQuestionsHeader, CaReceiptCheck, CaRuleKind, CaRulePill, CaStatusCircle, CaTemplateRow, Field, Input, caRuleKindHints, type CaBadgeTone } from "@leap-crm/design-system";
```

Scaffolding the examples call. It lives in the docs page, not in the library:

```tsx
const tones: CaBadgeTone[] = [
  "known",
  "derived",
  "ask",
  "tag",
  "rule",
  "contact",
  "opportunity",
  "firm",
  "external",
];

function RulePillDemo() {
  const [open, setOpen] = useState(false);
  return (
    <CaRulePill open={open} onClick={() => setOpen(value => !value)}>
      Hourly rates
    </CaRulePill>
  );
}
```

### Badges

How an answer resolves (known, derived, ask), tags and rules, then where a value comes from (contact, opportunity, firm, external). Soft tones, 10 px uppercase.

```tsx
<div className="flex flex-wrap gap-2">
  {tones.map(tone => (
    <CaBadge key={tone} tone={tone}>
      {tone}
    </CaBadge>
  ))}
</div>
```

### Rules

A rule's name in the page margin opens its popover; open, it takes the focus ring. Each kind has its action hue and icon.

```tsx
<div className="flex flex-wrap items-center gap-4">
  <RulePillDemo />
  <CaRulePill open>Hourly rates</CaRulePill>
  <CaRuleKind kind="show_when" />
  <CaRuleKind kind="hide_when" />
  <CaRuleKind kind="repeat" />
  <CaRuleKind kind="else" />
</div>
```

### Questions

The sticky header with progress, then each question in document order: open, answered in green with its answer, or current in navy with its answer controls.

```tsx
<div className="flex w-104 flex-col gap-2">
  <CaQuestionsHeader answered={4} total={9} />
  <div className="flex items-center gap-3">
    <CaStatusCircle state="open" />
    <CaStatusCircle state="current" />
    <CaStatusCircle state="done" />
  </div>
  <CaQuestionRow
    state="open"
    question="Who is the responsible solicitor?"
    onSelect={() => undefined}
  />
  <CaQuestionRow
    state="done"
    question="Is the client a business?"
    answer="No"
    onSelect={() => undefined}
  />
  <CaQuestionRow
    state="current"
    question="What is the supervising partner’s hourly rate?"
    meta="Used in 3 places"
  >
    <span className="flex items-center gap-1.5">
      <Input aria-label="Hourly rate" defaultValue="$450" />
      <Button>Save</Button>
      <Button intent="outline">Leave blank</Button>
    </span>
  </CaQuestionRow>
</div>
```

### Review checks

One check on the review step: fine in the known green, or needs a look in the ask amber. Screen readers hear which.

```tsx
<div className="flex flex-wrap gap-4">
  <CaReceiptCheck
    status="fine"
    title="Every question answered"
    detail="9 answers written into the agreement"
  />
  <CaReceiptCheck
    status="look"
    title="2 sections removed by rules"
    detail="Show when · Matter type is Family law"
  />
</div>
```

### Template row

A template in the library: a page thumbnail, its title and what it covers. Hover for the subtle fill.

```tsx
<div className="w-88">
  <CaTemplateRow
    title="Standard costs agreement"
    meta="Family law · 12 questions"
  />
</div>
```

### Popover

Every builder popover: the rule pill and kind, a Close, what the rule does, its fields, and an action row. 340, 520 or 680 px wide.

```tsx
<CaPopover
  label="Hourly rates rule"
  onClose={() => undefined}
  header={
    <>
      <CaRulePill open>Hourly rates</CaRulePill>
      <CaRuleKind kind="show_when" />
    </>
  }
  description={caRuleKindHints.show_when}
  footer={
    <ActionRow size="sm">
      <Button intent="outline">Remove rule</Button>
      <Button>Save</Button>
    </ActionRow>
  }
>
  <Field label="Matter type is">
    <Input defaultValue="Family law" />
  </Field>
</CaPopover>
```

## Props

### CaBadge

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `tone` | `\| "known" \| "derived" \| "ask" \| "tag" \| "rule" \| "contact" \| "opportunity" \| "firm" \| "external"` | `"known"` | Where the value comes from: known, derived or asked, each with its own colour. |
| `children` (required) | `ReactNode` |  | What it shows inside. |

### CaRulePill

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `open` | `boolean` | `false` | Its popover is open: the pill takes the focus ring. |
| `children` (required) | `ReactNode` |  | What it shows inside. |

### CaRuleKind

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `kind` (required) | `CaRuleKind` |  | show_when, hide_when, repeat or else: the rule's icon, colour and label. |

### CaStatusCircle

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `state` (required) | `"open" \| "current" \| "done"` |  | Where the question stands; the circle fills as it moves on. |

### CaReceiptCheck

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `status` (required) | `"fine" \| "look"` |  | fine in the known green, or look in the ask amber when it needs attention. |
| `title` (required) | `ReactNode` |  | What was checked, like "Fees match the matter type". |
| `detail` | `ReactNode` |  | A grey line under the title on why, or what to look at. |
| `children` | `ReactNode` |  | What it shows inside. |

### CaTemplateRow

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `title` (required) | `string` |  | The template's name. |
| `meta` | `string` |  | What it covers, like "Family law · 12 questions". |

### CaQuestionRow

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `state` (required) | `"open" \| "done" \| "current"` |  | open, current (the one being answered) or done. |
| `question` (required) | `ReactNode` |  | The question as the client will read it. |
| `answer` | `ReactNode` |  | The answer, shown in green once done. |
| `meta` | `ReactNode` |  | A note under the current question, like "Used in 3 places". |
| `children` | `ReactNode` |  | The current question's answer controls: a Field with Save and Leave blank, or Yes / No toggles. |
| `onSelect` | `() => void` |  | Makes an open or answered question the current one. |

### CaQuestionsHeader

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `answered` (required) | `number` |  | How many questions have an answer. |
| `total` (required) | `number` |  | How many questions there are. |

### CaPopover

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `label` (required) | `string` |  | Names the popover for screen readers. |
| `header` | `ReactNode` |  | What leads the header, like a rule pill and its kind. |
| `description` | `ReactNode` |  | A muted line under the header, like what the rule does. |
| `children` | `ReactNode` |  | The popover's fields. |
| `footer` | `ReactNode` |  | The action row at the foot, like Remove rule and Save. |
| `onClose` (required) | `() => void` |  | Runs when the popover's close button is pressed. |
| `width` | `"default" \| "wide" \| "xwide"` | `"default"` | 340 px by default, 520 wide, 680 for the field dialog. |
