# Spotlight

Crate: `libero`
Import: `use libero::components::{SpotlightAction, SpotlightHandle, SpotlightOptions, spotlight_filter, use_spotlight};`
Feature: `spotlight` (off by default, turns on `combobox`, `dialog`, `modal`, `kbd`, `loader`; [features.md](features.md))
Source: <https://github.com/tdymel/libero/tree/main/libero/src/components/overlay/spotlight>
Index: [index.md](index.md) lists every other page
Description: A command palette. A modal search box over your actions, with groups, arrow-key highlight and a Ctrl/Cmd+K hotkey.

A command palette, a modal search box over a list of actions. `use_spotlight`
returns a `Copy` handle, like `use_modal`, so call it in a component that
outlives every trigger. `actions` is called with the query
and returns the rows, so a fixed list and search results are the same prop.
`spotlight_filter` covers the common case. Label hits come first, exact and
prefix matches before matches inside a word, so "table" puts Table above
Sortable.

## Usage

```rust
use dioxus::prelude::*;
use libero::components::{
    Button, Kbd, SpotlightAction, SpotlightOptions, Text, spotlight_filter, use_spotlight,
};

fn commands(mut last: Signal<String>) -> Vec<SpotlightAction> {
    let run = move |name: &'static str| move |_| last.set(name.to_string());
    vec![
        SpotlightAction::new("Home")
            .group("Pages")
            .description("The start page")
            .onclick(run("Home")),
        SpotlightAction::new("Components")
            .group("Pages")
            .onclick(run("Components")),
        SpotlightAction::new("Changelog")
            .group("Pages")
            .keywords(["releases", "news"])
            .onclick(run("Changelog")),
        SpotlightAction::new("New file")
            .group("Commands")
            .shortcut("Ctrl N")
            .onclick(run("New file")),
        SpotlightAction::new("Toggle sidebar")
            .group("Commands")
            .shortcut("Ctrl B")
            .onclick(run("Toggle sidebar")),
        SpotlightAction::new("Sign out")
            .group("Account")
            .onclick(run("Sign out")),
    ]
}

#[component]
fn Demo() -> Element {
    let last = use_signal(|| String::from("nothing yet"));
    let all = use_hook(|| commands(last));
    let commands = use_spotlight(SpotlightOptions {
        actions: Some(Callback::new(move |query: String| spotlight_filter(&query, &all))),
        aria_label: Some("Commands".into()),
        shortcut: Some('j'),
        ..Default::default()
    });

    rsx! {
        Button { variant: "outlined", onclick: move |_| commands.open(), "Show" }
        Text { size: "sm", Kbd { "Ctrl" } " + " Kbd { "J" } " toggles it" }
        Text { size: "sm", role: "status", "Last run: {last()}" }
    }
}
```

## API

```rust,ignore
pub fn use_spotlight(options: SpotlightOptions) -> SpotlightHandle;
pub fn spotlight_filter(query: &str, actions: &[SpotlightAction]) -> Vec<SpotlightAction>;
```

### SpotlightOptions

| Prop | Type | Default | Description |
|---|---|---|---|
| `actions` | `Callback<String, Vec<SpotlightAction>>` | - | Called with the query, returns the rows. Capture a `Signal`, not a `Vec`, if the list changes. Unset warns and shows nothing. |
| `placeholder` | `String` | `"Search..."` | The search box's placeholder, and its name once you set it. Unset, the box is named "Search commands". |
| `nothing_found` | `Element` | - | Shown and announced when a query matches nothing. Unset, the localization's text. |
| `limit` | `usize` | - | The most rows drawn, counted across groups. A screen reader still hears how many actions matched. The list is not virtualized: every drawn row mounts on open. Sixty rows open in about 13 ms on a desktop, so set a limit for lists in the hundreds. |
| `close_on_action` | `bool` | `true` | Closes after running an action. |
| `clear_on_close` | `bool` | `true` | Starts every opening with an empty query. |
| `aria_label` | `String` | `"Command palette"` | Names the dialog and its list. |
| `shortcut` | `Option<char>` | `Some('k')` | Ctrl (Cmd on a Mac) plus this key toggles the palette. `None` for no hotkey. Works on the web, a desktop WebView and native (Blitz); a server render binds nothing. A key the browser already uses, such as L, T or W, warns in a debug build. Two palettes on one page should not share a key. |
| `highlight_first_on_query` | `bool` | `true` | Highlights the first row after every keystroke, so Enter runs it. Off, Enter does nothing until the arrows pick a row. |
| `loading` | `bool` | `false` | The results are still coming. A loader replaces the rows, and a screen reader hears "Searching". |
| `onquery` | `Callback<String>` | - | Called with the query on every keystroke. Set `loading` and start the search here, cancelling the previous one first: a newer query supersedes it, and a slow answer to an older one would overwrite the newer rows. Return the results from `actions` out of a signal. |
| `sx` | `Sx` | - | Styles the dialog box. |
| `parts` | `Parts<SpotlightPart>` | - | Styles for the inner parts in the Style API tab, under `sx`. |

### SpotlightAction

Every field has a builder method of the same name.

| Prop | Type | Default | Description |
|---|---|---|---|
| `label` | `String` | required | The row's text, and the first thing `spotlight_filter` matches. |
| `description` | `String` | - | A second line, matched after the label. |
| `keywords` | `Vec<String>` | - | Matched, never drawn. |
| `group` | `String` | - | A section header. Groups keep the order they first appear in. |
| `icon` | `Element` | - | Drawn before the label. |
| `shortcut` | `String` | - | A hint, one `Kbd` per key, split on spaces and `+`. Never bound. |
| `onclick` | `Callback<()>` | - | Runs on Enter or a click. |

### SpotlightHandle

| Method | Returns | Description |
|---|---|---|
| `open()` | `()` | Opens with focus in the search box. Call it from the trigger's handler, so focus returns there. |
| `close()` | `()` | Closes. |
| `toggle()` | `()` | Opens or closes. |
| `is_open()` | `bool` | Whether it is open. |

## Style API

Style a part with `SpotlightOptions::parts`, or address it as `[data-slot='…']`
in your own CSS. The names are stable. [Style API in Styling](styling.md#style-api)
explains how parts work.

| Part | `data-slot` | Description |
|---|---|---|
| `SpotlightPart::Body` | `body` | Holds the search box, the list and the status line. |
| `SpotlightPart::Search` | `search` | The search box. |
| `SpotlightPart::List` | `list` | The `listbox`, a `ScrollArea`. |
| `SpotlightPart::Group` | `group` | A named group of rows. |
| `SpotlightPart::GroupLabel` | `group-label` | A group's visible name. |
| `SpotlightPart::Option` | `option` | One action row. The highlighted one has `data-active`. |
| `SpotlightPart::Icon` | `icon` | A row's icon. |
| `SpotlightPart::Text` | `text` | The column holding the label and the description. |
| `SpotlightPart::Label` | `label` | A row's label. |
| `SpotlightPart::Description` | `description` | A row's second line. |
| `SpotlightPart::Shortcut` | `shortcut` | A row's key hint, one `Kbd` per key. |
| `SpotlightPart::Status` | `status` | The status line: "nothing found" or the loader, and the result count for a reader only. |

## Accessibility

### Keyboard

| Key | Action |
|---|---|
| `Down` or `Up` | Moves the highlight, wrapping at both ends. |
| `Enter` | Runs the highlighted action. After typing, the first row is highlighted; right after opening, none is. |
| `Escape` | Closes, as a click outside does. Focus goes back to what opened it. |

### Libero handles

- Focus stays in the search box.
- A polite status region says how many actions a query matched
  (`SpotlightLabels::results`, "2 results"), also those past `limit`,
  "nothing found" when none, and "searching" while `loading`.
- The search box is named by your `placeholder`, so a files palette and a
  commands palette sound different.
- The hotkey is ignored while you type in another text field, and while a
  dialog or popover is open.

### You must

- Turn `highlight_first_on_query` off for a palette whose actions change
  things. Then nothing is highlighted until you press Down.
- Call `open()` from the trigger's handler, so focus returns there.

### Example

A command palette opened from a "Search" button that calls `open()`: typing
"the" says "2 results", Enter runs the first one, and Escape puts focus back
on Search.

### Limits

- In a desktop WebView or on Android, Tab and Shift+Tab can leave the palette
  for the page behind it.

## Theme defaults

`SpotlightDefaults` on the theme holds `width` (`"600px"`), `top_offset`
(`"80px"`), `max_list_height` (`"400px"`), `radius` (`Md`), `padding`
(`"4px"`), `search_font_size` (`"1.125rem"`), `group_color` and
`description_color` (`muted.7`). The words are `SpotlightLabels` in the
[localization](localization.md).

## CSS variables

`--lsx-spotlight-width`, `--lsx-spotlight-top-offset`,
`--lsx-spotlight-max-list-height`, `--lsx-spotlight-padding`,
`--lsx-spotlight-search-font-size`, `--lsx-spotlight-group-color` and
`--lsx-spotlight-description-color`.

## Data attributes

The highlighted row carries `data-active`, and every part its `data-slot`.
