| name | add-card |
| description | Create a new card part within a section with factory/class pattern, Card wrapper, and proper cleanup |
| aliases | ["new-card","create-card"] |
Add Card Skill
Usage
/add-card <SectionName>/<CardName>
Example: /add-card Pets/InventoryCard or /add-card Alerts/WeatherCard
What is a Card?
A Card is a self-contained UI part within a Section. It:
- Wraps content in the
Card component (expandable/collapsible container)
- Has a specific purpose (logs viewer, team manager, item list, etc.)
- Lives in
src/ui/sections/<SectionName>/parts/<cardName>/
- Can be either class-based (complex state) or factory-based (simpler)
Cards vs Components
| Aspect | Card (Part) | Component |
|---|
| Location | sections/<Section>/parts/ | components/ |
| Scope | Section-specific | Reusable across sections |
| State | Often section-specific state | Props-driven, minimal internal state |
| Complexity | Higher (orchestrates components) | Lower (single purpose) |
Step 1: Ask Questions
1. Parent Section
Which section will contain this card?
→ Must exist in src/ui/sections/
2. Card Purpose
Brief description (1 sentence):
What does this card display/manage?
3. Pattern Type
A) Factory pattern (recommended for simpler cards)
- createXxxCard(options): XxxCardHandle
- Better for cards with minimal internal state
- Example: ShopsCard, PublicCard
B) Class pattern (for complex cards)
- class XxxCardPart { build(), destroy(), render() }
- Better for cards with complex state, multiple modes, drag handlers
- Example: TeamCard, TrackerCard, AbilityLogsCard
4. Card Features
[ ] Expandable (collapsible header with chevron)
[ ] Filterable (search bar, selects)
[ ] Scrollable list (virtual scrolling if >50 items)
[ ] Modes toggle (SegmentedControl for overview/manage/etc.)
[ ] Refresh capability (manual refresh button)
5. Data Sources
A) Globals (reactive) → subscribe + cleanup
B) MGData (static) → MGData.get('plants')
C) Feature state → feature.getState()
D) API fetch → async loading
E) Section state → state.ts
F) Combination of above
6. Child Components
Check existing components before creating new:
Layout:
[ ] Card (always used - the wrapper)
Inputs/Controls:
[ ] SearchBar - text filtering
[ ] Select - dropdown filter
[ ] SegmentedControl - mode toggle
[ ] Checkbox - multi-select
[ ] Button - actions
Display:
[ ] Table - tabular data with sorting
[ ] Badge - status indicators
[ ] ProgressBar - progress display
[ ] TeamListItem - team rows
Utility:
[ ] Modal - popups
[ ] SoundPicker - audio selection
7. Sprites
Does it display game sprites? → MGSprite.toCanvas()
Step 2: Create Structure
src/ui/sections/<SectionName>/parts/<cardName>/
├── <CardName>.ts # Main card logic (class or factory)
├── <cardName>.css.ts # Scoped styles (optional)
├── index.ts # Barrel exports
└── <cardName>Data.ts # Data processing helpers (optional, for complex data)
└── <cardName>Table.ts # Table configuration (optional, if using Table)
Read existing cards for templates:
- Factory pattern:
src/ui/sections/Alerts/parts/shop/shopsCard.ts
- Factory pattern:
src/ui/sections/Room/parts/public.ts
- Class pattern:
src/ui/sections/Pets/parts/ability/AbilityLogsCard.ts
- Class pattern:
src/ui/sections/Pets/parts/team/TeamCard.ts
Step 3: File Templates
Factory Pattern (Recommended for simpler cards)
<CardName>.ts
import { Card } from "../../../../components/Card/Card";
import { element } from "../../../../styles/helpers";
export interface <CardName>Options {
defaultExpanded?: boolean;
onExpandChange?: (expanded: boolean) => void;
}
export interface <CardName>Handle {
root: HTMLElement;
refresh?(): void;
destroy(): void;
}
export function create<CardName>(options: <CardName>Options = {}): <CardName>Handle {
const { defaultExpanded = true, onExpandChange } = options;
let root: HTMLElement | null = null;
const cleanups: (() => void)[] = [];
function buildCard(): HTMLElement {
const content = element("div", {
style: "display: flex; flex-direction: column; gap: 12px;",
}) as HTMLDivElement;
root = Card(
{
title: "<Card Title>",
subtitle: "<Card subtitle description>",
expandable: true,
defaultExpanded,
padding: "md",
onExpandChange,
},
content
);
return root;
}
function refresh(): void {
}
function destroy(): void {
cleanups.forEach(fn => fn());
cleanups.length = 0;
root = null;
}
return {
root: buildCard(),
refresh,
destroy,
};
}
Class Pattern (For complex cards with state)
<CardName>.ts
import { Card } from "../../../../components/Card/Card";
import { element } from "../../../../styles/helpers";
export interface <CardName>PartOptions {
onSomeEvent?: () => void;
}
export class <CardName>Part {
private card: HTMLDivElement | null = null;
private content: HTMLDivElement | null = null;
private options: <CardName>PartOptions;
private cleanups: (() => void)[] = [];
constructor(options: <CardName>PartOptions = {}) {
this.options = options;
}
build(): HTMLDivElement {
if (this.card) return this.card;
return this.createCard();
}
destroy(): void {
this.cleanups.forEach(fn => fn());
this.cleanups.length = 0;
this.card = null;
this.content = null;
}
render(): void {
if (!this.card) return;
this.renderContent();
}
private createCard(): HTMLDivElement {
const wrapper = element("div", {
className: "<card-name>-wrapper",
});
this.content = element("div", {
className: "<card-name>__content",
});
wrapper.appendChild(this.content);
this.card = Card(
{
title: "<Card Title>",
subtitle: "<Card subtitle description>",
expandable: true,
defaultExpanded: true,
},
wrapper
);
return this.card;
}
private renderContent(): void {
if (!this.content) return;
this.content.replaceChildren();
}
}
index.ts (Barrel exports)
export { create<CardName> } from "./<CardName>";
export type { <CardName>Options, <CardName>Handle } from "./<CardName>";
export { <CardName>Part } from "./<CardName>";
export type { <CardName>PartOptions } from "./<CardName>";
export { <cardName>CardCss } from "./<cardName>.css";
<cardName>.css.ts (Optional)
export const <cardName>CardCss = `
/* Scoped to card */
.<card-name>-wrapper {
display: flex;
flex-direction: column;
gap: 12px;
}
.<card-name>__content {
/* Content styles */
}
.<card-name>__list {
max-height: 400px;
overflow-y: auto;
}
.<card-name>__empty {
padding: 24px;
text-align: center;
color: color-mix(in oklab, var(--fg) 60%, #9ca3af);
font-size: 14px;
}
`;
Step 4: Register in Section
Update parts/index.ts
export { create<CardName> } from "./<cardName>/<CardName>";
export type { <CardName>Options, <CardName>Handle } from "./<cardName>/<CardName>";
export { <CardName>Part } from "./<cardName>/<CardName>";
export type { <CardName>PartOptions } from "./<cardName>/<CardName>";
Use in section.ts
import { create<CardName> } from "./parts";
import { <CardName>Part } from "./parts";
const card = create<CardName>({
defaultExpanded: true,
onExpandChange: (expanded) => { ... },
});
container.appendChild(card.root);
cleanups.push(() => card.destroy());
const cardPart = new <CardName>Part({ ... });
container.appendChild(cardPart.build());
cardPart.render();
cleanups.push(() => cardPart.destroy());
Step 5: Validate
Structure
API
Card Wrapper
Cleanup
Styling
Data (if applicable)
Components (if applicable)
Existing Cards Reference
Factory Pattern (simpler)
Alerts/parts/shop/shopsCard.ts - Table with filters
Alerts/parts/weather/weatherCard.ts - Weather alerts
Room/parts/public.ts - Rooms list with API fetch
Class Pattern (complex)
Pets/parts/ability/AbilityLogsCard.ts - Virtual scrolling list
Pets/parts/team/TeamCard.ts - CRUD with drag-drop
Pets/parts/teamDetails/TeamDetailsCard.ts - Expansion panels
Trackers/parts/TrackerCard.ts - Team list with expansion
Common Patterns
Filters Row
const filters = element("div", {
className: "<card>-filters",
style: "display: flex; gap: 8px; margin-bottom: 12px;",
});
const select = Select({
options: [...],
onChange: (value) => applyFilters(),
});
const search = SearchBar({
placeholder: "Search...",
onSearch: (value) => applyFilters(),
});
filters.append(select.root, search.root);
Scrollable List
const list = element("div", {
className: "<card>__list",
style: "max-height: 400px; overflow-y: auto;",
});
Empty State
if (items.length === 0) {
const empty = element("div", {
className: "<card>__empty",
style: "padding: 24px; text-align: center; color: color-mix(in oklab, var(--fg) 60%, #9ca3af);",
}, "No items yet");
content.appendChild(empty);
return;
}
Mode Toggle
const modeControl = SegmentedControl({
segments: [
{ id: "simple", label: "Simple" },
{ id: "detailed", label: "Detailed" },
],
selected: "simple",
onChange: (id) => {
mode = id;
renderContent();
},
});
Subscribe to Globals
const unsub = getMyInventory().subscribe((inventory) => {
items = inventory.items;
renderList();
});
cleanups.push(unsub);
References
- Rules:
.claude/rules/ui/sections.md
- Card component:
src/ui/components/Card/Card.ts
- Existing cards:
src/ui/sections/*/parts/*/
- UI Components:
src/ui/components/
- Globals:
src/globals/variables/
- Section workflow:
.claude/workflows/ui/section/add-section.md