# Quick API Reference ⚡
A comprehensive reference for all SigPro APIs. Everything you need to build reactive web applications with signals and web components.
## 📋 API Functions Reference
| Function | Description | Example |
|----------|-------------|---------|
| **`$(initialValue)`** | Creates a reactive signal (getter/setter) | `const count = $(0)` |
| **`$(computedFn)`** | Creates a computed signal | `const full = $(() => first() + last())` |
| **`$.effect(fn)`** | Runs effect when dependencies change | `$.effect(() => console.log(count()))` |
| **`$.page(setupFn)`** | Creates a page with automatic cleanup | `$.page(() => html`
Page
`)` |
| **`$.component(tagName, setupFn, attrs, useShadow)`** | Creates reactive Web Component | `$.component('my-menu', setup, ['items'])` |
| **`$.router(routes)`** | Creates a hash-based router | `$.router([{path:'/', component:Home}])` |
| **`$.router.go(path)`** | Navigates to a route | `$.router.go('/user/42')` |
| **`$.fetch(url, data, loadingSignal)`** | Fetch wrapper with loading state | `const data = await $.fetch('/api', data, loading)` |
| **`$.storage(key, initialValue, storageType)`** | Persistent signal (local/sessionStorage) | `const theme = $.storage('theme', 'light')` |
| **`` html`...` ``** | Template literal for reactive HTML | `` html`
${count}
` `` |
### Signal Methods
| Method | Description | Example |
|--------|-------------|---------|
| **`signal()`** | Gets current value | `count()` |
| **`signal(newValue)`** | Sets new value | `count(5)` |
| **`signal(prev => new)`** | Updates using previous value | `count(c => c + 1)` |
### Component Context Properties
| Property | Description | Example |
|----------|-------------|---------|
| **`props`** | Reactive component properties | `props.title()` |
| **`slot(name)`** | Accesses slot content | `slot()` or `slot('footer')` |
| **`emit(event, data)`** | Dispatches custom event | `emit('update', value)` |
| **`onUnmount(cb)`** | Registers cleanup callback | `onUnmount(() => clearInterval(timer))` |
### Page Context Properties
| Property | Description | Example |
|----------|-------------|---------|
| **`params`** | Route parameters | `params.id`, `params.slug` |
| **`onUnmount(cb)`** | Registers cleanup callback | `onUnmount(() => clearInterval(timer))` |
### HTML Directives
| Directive | Description | Example |
|-----------|-------------|---------|
| **`@event`** | Event listener | `` @click=${handler} `` |
| **`:property`** | Two-way binding | `` :value=${signal} `` |
| **`?attribute`** | Boolean attribute | `` ?disabled=${signal} `` |
| **`.property`** | DOM property binding | `` .scrollTop=${value} `` |
| **`class:name`** | Conditional class | `` class:active=${isActive} `` |
## 📡 Signals - `$(initialValue)`
Creates a reactive value that notifies dependents when changed.
| Pattern | Example | Description |
|---------|---------|-------------|
| **Basic Signal** | `const count = $(0)` | Create signal with initial value |
| **Getter** | `count()` | Read current value |
| **Setter** | `count(5)` | Set new value directly |
| **Updater** | `count(prev => prev + 1)` | Update based on previous value |
| **Computed** | `const full = $(() => first() + last())` | Auto-updating derived signal |
### Examples
```javascript
// Basic signal
const count = $(0);
console.log(count()); // 0
count(5);
count(c => c + 1); // 6
// Computed signal
const firstName = $('John');
const lastName = $('Doe');
const fullName = $(() => `${firstName()} ${lastName()}`);
console.log(fullName()); // "John Doe"
firstName('Jane'); // fullName auto-updates to "Jane Doe"
```
## 🔄 Effects - `$.effect(fn)`
Executes a function and automatically re-runs when its dependencies change.
| Pattern | Example | Description |
|---------|---------|-------------|
| **Basic Effect** | `$.effect(() => console.log(count()))` | Run effect on dependency changes |
| **Cleanup** | `$.effect(() => { timer = setInterval(...); return () => clearInterval(timer) })` | Return cleanup function |
| **Stop Effect** | `const stop = $.effect(...); stop()` | Manually stop an effect |
### Examples
```javascript
// Auto-running effect
const count = $(0);
$.effect(() => {
console.log(`Count is: ${count()}`);
}); // Logs immediately and whenever count changes
// Effect with cleanup
const userId = $(1);
$.effect(() => {
const id = userId();
const timer = setInterval(() => fetchUser(id), 5000);
return () => clearInterval(timer); // Cleanup before re-run
});
```
## 📄 Pages - `$.page(setupFunction)`
Creates a page with automatic cleanup of all signals and effects when navigated away.
```javascript
// pages/about.js
import { $, html } from 'sigpro';
export default $.page(() => {
const count = $(0);
// Auto-cleaned on navigation
$.effect(() => {
document.title = `Count: ${count()}`;
});
return html`