---
mode: agent
---
# AppRun Component Creation Rules
When creating AppRun components, follow these guidelines for consistent, maintainable code.
## Core Component Architecture
- **HTML templates**: Always use `html` tagged literals for UI rendering
- **State management**: Handle state through event handlers that return new state objects
- **Event handling**: Use `event-name` syntax in templates with handlers `(state, param) => newState`
- **Local events**: Use `run()` for component-local events (no registration needed)
- **Immutable updates**: Always return new state objects, never mutate existing state
## Component Creation Patterns
### 1. Functional Components (Pure UI Components)
**When to use**: For reusable UI pieces that don't manage their own state.
```js
// Create pure function components that take props and return HTML
export const AgentModal = (agent, onClose) => {
return html`
e.stopPropagation()}>
${agent.status ? html`
${agent.name}
` : html`
agent.name = e.target.value}>
`}
`;
};
```
**Rules for functional components**:
- Export as pure functions
- Accept props as parameters
- Return HTML template literals
- Handle events through callback props
- No internal state management
### 2. Stateful Page Components (Full Components)
**When to use**: For main application views that manage state and handle complex interactions.
**Step 1: Create async state initialization**
```js
// Always use async state function for API calls and data loading
const state = async () => {
const data = await api.getData();
return {
...initialState,
data,
loading: false
};
};
```
**Step 2: Define event handlers as separate functions**
```js
// Create specific handlers for each user interaction
const selectWorld = async (state, worldName) => {
if (worldName === state.worldName) return state;
const agents = await api.getAgents(worldName);
return ({ ...state, worldName, agents });
};
const openModal = (state, item = null) => {
return ({
...state,
editingItem: item || { name: 'New Item' },
showModal: true
});
};
```
**Step 3: Create view function with proper rendering patterns**
```js
// View function should be pure - only render, never mutate
const view = (state) => {
return html`