> For AI agents: the complete documentation index is available at /llms.txt, the full documentation bundle is available at /llms-full.txt.

# LynxShortcuts

Hears the app's shortcuts: things the app does that the person runs without opening it first. On iOS they run from Siri, Spotlight and the Shortcuts app, and on Android from the app icon's menu. `import { LynxShortcuts, type LynxShortcut, type LynxShortcutHandler } from '@blotch/angular-lynx';`

[Add shortcuts for Siri and the app icon](/guide/shortcuts.md) shows it in use.

## Set it up in angular.json

List the shortcuts in the `build` target's `shortcuts` option:

```json title="angular.json"
"options": {
  "shortcuts": [
    { "id": "refresh", "title": "Refresh", "phrases": ["Refresh {appName}"] },
    { "id": "photos", "title": "Show photos", "route": "/photos" }
  ]
}
```

`ng build --platform` checks them, says what's wrong, and builds them into the apps: App Intents on iOS, static shortcuts on Android. [The `shortcuts` option](#the-shortcuts-option) below lists every field.

## Usage

```typescript
export class Feed {
  readonly posts = resource({ loader: loadPosts });

  constructor() {
    inject(LynxShortcuts).on('refresh', () => this.posts.reload());
  }
}
```

## API

### `LynxShortcuts`

```typescript
@Injectable({ providedIn: 'root' })
class LynxShortcuts {}
```

| Host                                                                              | Where the person runs a shortcut                         |
| --------------------------------------------------------------------------------- | -------------------------------------------------------- |
| iOS 16 and later (`ng build --platform ios`)                                      | Siri, Spotlight, the Shortcuts app and the Action button |
| Android 7.1 and later (`ng build --platform android`)                             | The menu a long press on the app's icon opens            |
| Web preview, Lynx Explorer                                                        | Nowhere: no shortcut ever runs                           |
| Unit tests ([testing library](/api/testing-library/fakes/sharing.md#runshortcut)) | `runShortcut()`                                          |

The phone brings the app to the front, starting it if it has to, and then the shortcut arrives here.

- **A shortcut with a `route`** makes the router navigate to it, and then its handlers are called. That needs [`provideRouter()`](/api/angular-lynx/routing/index.md) from `@blotch/angular-lynx`.
- **A route needs no code:** `provideRouter()` creates the service as the app starts.

### `LynxShortcuts.on()`

```typescript
on(
  id: string,
  handler: LynxShortcutHandler,
  options?: { readonly injector?: Injector },
): () => void;
```

Calls `handler` each time the person runs the shortcut `id`, the `id` it has in angular.json.

- **Every handler registered for the ID is called,** in the order they were registered.
- **A handler that throws** is reported to Angular's `ErrorHandler`, and the others are still called.
- **A shortcut nothing handled waits.** One that ran before any handler was registered for its ID is kept for the first handler registered for it. That handler is called once the code that registered it has finished. It's how the shortcut that started the app reaches a component created later, such as the screen its `route` leads to.
- **The handler goes with its injection context.** Called from one, such as a constructor, the handler is removed when the component or service is destroyed. Pass `injector` to tie it to another. Outside one, it stays until it's removed.
- **Returns** a function that removes the handler.

### `LynxShortcut`

```typescript
type LynxShortcut = {
  readonly id: string;
  readonly route?: string;
};

type LynxShortcutHandler = (shortcut: LynxShortcut) => void;
```

| Field   | Description                                                                          |
| ------- | ------------------------------------------------------------------------------------ |
| `id`    | The shortcut's `id` in angular.json's `shortcuts`                                    |
| `route` | Its `route` there, if it has one. The router has already been told to navigate to it |

## The `shortcuts` option

Each entry of the `build` target's `shortcuts` in angular.json:

| Field     | Type       | Description                                                                                                                                           |
| --------- | ---------- | ----------------------------------------------------------------------------------------------------------------------------------------------------- |
| `id`      | `string`   | Required. What `on()` knows it by. Letters, digits, hyphens and underscores, starting with a letter. Two IDs can't differ only by `-`/`_` or capitals |
| `title`   | `string`   | Required. The name the phone lists it under                                                                                                           |
| `phrases` | `string[]` | What the person says to Siri. Each must contain `{appName}`, and no two shortcuts can share one. Defaults to `["<title> in {appName}"]`. iOS only     |
| `route`   | `string`   | The path the router navigates to, starting with `/`                                                                                                   |
| `symbol`  | `string`   | The SF Symbol iOS draws beside it, such as `arrow.clockwise`. iOS only: Android draws the app's icon                                                  |

iOS takes up to 10 shortcuts from an app.

## Related

- [Add shortcuts for Siri and the app icon](/guide/shortcuts.md)
- [Shortcuts example](/examples/shortcuts.md)
- [Build options](/api/build/index.md#native-app-options)
