### Full SSR + Hydration Example with State
Source: https://ilha.build/llms-full.txt
A comprehensive example showing island definition with state management, SSR rendering with snapshot, and client-side hydration.
```typescript
// server.ts
import ilha, { html } from "ilha";
import { mount } from "ilha";
const Counter = ilha
.state("count", 0)
.on("button@click", ({ state }) => state.count(state.count() + 1))
.render(
({ state }) => html`
Count: ${state.count()}
`,
);
// Server — render with snapshot
const body = await Counter.hydratable(
{ count: 10 },
{ name: "Counter", snapshot: true, skipOnMount: true },
);
// Client — hydrate in place
mount({ Counter });
```
--------------------------------
### File-System Routing Setup
Source: https://ilha.build/llms-full.txt
Configuration for enabling file-system routing with Vite.
```APIDOC
## File-System Routing
`@ilha/router` ships a Vite plugin that scans `src/pages/`, resolves layout and error boundary chains, and generates a ready-to-use router with zero manual route registration.
### Setup
```ts
// vite.config.ts
import { defineConfig } from "vite";
import { pages } from "@ilha/router/vite";
export default defineConfig({
plugins: [pages()],
});
```
Add `.ilha/` to `.gitignore`.
```
--------------------------------
### Ilha Store Installation
Source: https://ilha.build/llms-full.txt
Install the @ilha/store package using your preferred package manager.
```APIDOC
## Install
```
--------------------------------
### Install Ilha using Package Manager
Source: https://ilha.build/llms-full.txt
Install the Ilha framework using your preferred package manager. This command is typically run in your project's terminal.
```bash
npm install ilha
```
--------------------------------
### Full Pokemon Picker Component Example
Source: https://ilha.build/llms-full.txt
This example demonstrates a complete Ilha.js component that fetches and displays Pokemon data. It utilizes `.input()` for initial configuration, `.state()` for managing component data, `.onMount()` for initial data fetching, and `.effect()` for handling side effects like API requests with cancellation.
```javascript
import dedent from "dedent";
import { Sandbox } from "$src/sandbox";
export const script = dedent`
import "./styles.css";
import ilha, { html, mount, type } from "ilha";
const PokemonPicker = ilha
.input<{ defaultPokemon: string }>()
.state('pokemon', ({ defaultPokemon }) => defaultPokemon)
.state('pokemonList', [])
.state('pokemonData', null)
.onMount(({ state }) => {
const fetchList = async () => {
const req = await fetch('https://pokeapi.co/api/v2/pokemon');
const list = await req.json();
state.pokemonList(list.results);
};
fetchList();
})
.effect(({ state }) => {
const controller = new AbortController();
const fetchPokemon = async () => {
const pokemon = state.pokemon();
try {
const req = await fetch(
`https://pokeapi.co/api/v2/pokemon/${pokemon}`,
{ signal: controller.signal }
);
const data = await req.json();
// Only update if this request wasn't aborted (still the latest)
if (!controller.signal.aborted) {
state.pokemonData(data);
}
} catch (err) {
// Ignore abort errors
if (err.name !== 'AbortError') throw err;
}
};
fetchPokemon();
return () => controller.abort();
})
.bind('#pokemon', 'pokemon')
.render(({ state }) => {
const currentPokemon = state.pokemon();
const options = state.pokemonList().map(
({ name }) => html`
`
);
const card = state.pokemonData()
? html`
${state.pokemonData().name}
`
: html`
Loading...
`
return html`
${card}
`;
});
const Pokedex = ilha.render(() => html`
${PokemonPicker({ defaultPokemon: "charizard" })}
`);
mount({ Pokedex });
`;
export const template = ``;
```
--------------------------------
### Example Usage - createForm
Source: https://ilha.build/llms-full.txt
An example demonstrating how to use the createForm function with Zod schema validation.
```APIDOC
## Example
```ts
import { z } from "zod";
import { createForm, issuesToErrors } from "@ilha/form";
const form = createForm({
el: document.querySelector("form")!,
schema: z.object({
email: z.string().email("Invalid email"),
name: z.string().min(1, "Name is required"),
}),
onSubmit(values) {
console.log(values.email);
},
onError(issues) {
console.log(issuesToErrors(issues));
},
validateOn: "input",
});
const unmount = form.mount();
```
```
--------------------------------
### One-time Setup with `.onMount()`
Source: https://ilha.build/llms-full.txt
Register a function with `.onMount()` to execute once after the island is mounted. This is ideal for initializing third-party libraries or performing DOM manipulations that only need to happen initially.
```typescript
import ilha from "ilha";
const Island = ilha
.onMount(({ host }) => {
console.log("mounted", host);
})
.render(() => `
hello
`);
```
--------------------------------
### Vite Plugin Setup for File-System Routing
Source: https://ilha.build/llms-full.txt
Configure the Vite build process to use the `@ilha/router/vite` plugin for automatic route generation based on the file system.
```typescript
// vite.config.ts
import { defineConfig } from "vite";
import { pages } from "@ilha/router/vite";
export default defineConfig({
plugins: [pages()],
});
```
--------------------------------
### Full SSR + Hydration Example
Source: https://ilha.build/guide/island/hydratable
A comprehensive example showing a stateful island component rendered with SSR and configured for client-side hydration. Note the use of `snapshot: true` for server-side state capture and `skipOnMount: true`.
```typescript
// server.ts
import ilha, { html } from "ilha";
import { mount } from "ilha";
const Counter = ilha
.state("count", 0)
.on("button@click", ({ state }) => state.count(state.count() + 1))
.render(
({ state }) => html`
Count: ${state.count()}
`,
);
// Server — render with snapshot
const body = await Counter.hydratable(
{ count: 10 },
{ name: "Counter", snapshot: true, skipOnMount: true },
);
// Client — hydrate in place
mount({ Counter });
```
--------------------------------
### Create Basic Ilha Store
Source: https://ilha.build/llms-full.txt
Initializes a simple global store with a count state. Demonstrates setting and getting the state.
```typescript
import { createStore } from "@ilha/store";
const store = createStore({ count: 0 });
store.setState({ count: 1 });
store.getState(); // → { count: 1 }
```
--------------------------------
### Create Ilha Store with State and Actions
Source: https://ilha.build/llms-full.txt
Illustrates creating a store with both initial state and a set of actions. The actions creator receives set, get, and getInitialState functions.
```typescript
// State + actions
const store = createStore({ count: 0 }, (set, get, getInitialState) => ({
increment() {
set({ count: get().count + 1 });
},
reset() {
set(getInitialState());
},
}));
```
--------------------------------
### Server-side Rendering with Ilha Router
Source: https://ilha.build/llms-full.txt
Implement server-side rendering by using the router to render the HTML for a given request URL. This example shows a basic SSR setup without hydration.
```typescript
import { router } from "@ilha/router";
import { homePage, aboutPage, notFound } from "./pages";
const html = router()
.route("/", homePage)
.route("/about", aboutPage)
.route("/**", notFound)
.render(request.url);
return new Response(`${html}`, {
headers: { "content-type": "text/html" },
});
```
--------------------------------
### Selector Syntax Examples
Source: https://ilha.build/guide/island/on
Demonstrates various ways to specify CSS selectors and event names for attaching listeners. Omit the selector to target the island host.
```typescript
.on("@click", handler) // host click
.on("button@click", handler) // any