# Writing your app (/docs/guides/writing-apps)

An Octapus app is an ordinary [Octane](https://octanejs.dev) app. Everything in the Octane docs about components, state, effects and context applies. This page covers what changes when that app runs natively instead of in a browser.

## Project layout [#project-layout]

```text
my-app/
  src/
    App.tsrx      the root component (export App, or a default export)
    styles.css    optional global CSS, applied on every target
  package.json
```

You can split the app into as many `.tsrx` files as you like and import them from `App.tsrx`.

## Styling [#styling]

All of these work the same on web and native:

- **Global CSS** in `src/styles.css`, with class, type, descendant and child selectors.
- **Scoped `<style>` blocks** inside a component, which only reach that component.
- **The `style` prop** for one-off values.
- **Media queries** on width and on `prefers-color-scheme`, plus viewport units (`vw`, `vh`, `dvh`). Dark mode follows the system on native.
- **Custom properties** (`var(--accent)`).

Octapus resolves CSS the way WebKit does, starting from the same browser default stylesheet, so an unstyled `h1` or `p` looks like it does in Safari. `calc()`, `min()`, `max()`, `clamp()`, `light-dark()` and the usual units and color syntaxes work.

### CSS that has no effect on native [#css-that-has-no-effect-on-native]

These are parsed without errors but do nothing natively, so keep them to progressive enhancements:

| Feature | On native |
| --- | --- |
| `:hover`, `:focus`, `:active`, `:visited`, `:has()`, `:empty` | Never match |
| `::before`, `::after` and other pseudo-elements | Not generated |
| `@keyframes` (CSS animations), `@font-face`, `@import`, `@container` | Skipped |
| `@supports`, `@layer` | Treated as true; layer order is ignored |

## Layout and text [#layout-and-text]

Block, flex and grid layout follow browser behavior. Text wraps and aligns like inline text on the web: `line-height`, `text-align`, `letter-spacing` and text decorations are supported, and nested inline elements (`<strong>`, `<a>`, `<span>`) render as part of the surrounding text.

## Controls [#controls]

| Element | On native |
| --- | --- |
| `<button>` | A native button. If you style it with CSS, it is drawn as a box with its label, exactly as styled |
| `<input>`, `<textarea>` | A native text field |
| Inline `<svg>` | Drawn from its markup (paths, shapes, fills, strokes) |

Octapus loads a small control stylesheet before your CSS on every target, the browser included. It removes platform chrome from buttons and fields and gives them a plain background, border and padding, so a button looks the same in Safari, on macOS and on iOS. Your CSS overrides it as usual.

## Events and timers [#events-and-timers]

| Event | Fires when |
| --- | --- |
| `onClick` | A button or any element with a click handler is tapped or clicked |
| `onInput` | The text in a field changes |
| `onChange`, `onSubmit` | The user commits a field (presses Return) |

`setTimeout`, `setInterval` and their `clear*` functions work on native; the host schedules them.

## Props, host events and native code [#props-host-events-and-native-code]

The host app can pass props to your root component and exchange events with it, and Swift functions can be exposed as typed native modules:

```tsx
import { useEffect } from 'octane';
import { native, host } from 'octapus-js/native';

export function App(props) @{
  useEffect(() => host.on('deeplink', (url) => console.log(url)));

  <button onClick={() => native.Haptics.impact('light')}>{'Hello ' + props.user}</button>
}
```

See [Native modules](/docs/guides/native-modules) for defining modules, generated types, props and events. Call native functions from effects and handlers, not during render: the build-time bake has no native modules.

## Platform branches [#platform-branches]

`oct build` defines the target at compile time:

```tsx
const isWeb = import.meta.env.OCT_TARGET === 'web'; // 'web' | 'ios' | 'android' | 'mac' | 'windows'

export function Badge() @{
  <p>{isWeb ? 'Running in your browser' : 'Running natively'}</p>
}
```

Each native target is built separately, so code for other platforms is removed from the bundle.

## Not available on native yet [#not-available-on-native-yet]

- **DOM APIs.** There is no `document` or `window`. Code that touches them must sit behind an `OCT_TARGET` check.
- **Network APIs.** There is no `fetch` on native yet. Expose what you need through a [native module](/docs/guides/native-modules).
- **Images.** `<img>` is not drawn yet. Inline SVG is.

## Keep the first render deterministic [#keep-the-first-render-deterministic]

Your app's first render becomes the [first-screen snapshot](/docs/concepts/snapshot). It runs once at build time, with no props and no native modules, and again on the device, and the two results must match for the device to take over the snapshot. Avoid reading anything that changes between those two runs before the first render finishes, including props and native calls:

```tsx
export function Clock() @{
  // ✗ differs between the bake and the device:
  // const [now] = useState(Date.now());

  // ✓ render a stable placeholder, fill it in after the first commit
  const [now, setNow] = useState(null);
  useEffect(() => setNow(Date.now()));

  <p>{now === null ? 'Loading…' : new Date(now).toLocaleTimeString()}</p>
}
```

If they do differ, nothing breaks: Octapus throws the snapshot away and renders normally, and the launch metrics tell you which row differed. See [Troubleshooting](/docs/guides/troubleshooting).