# Web Component

> <findort-search> is a ready-made address field with suggestions. No framework, no build step, usable with keyboard and screen readers.

Source: https://findort.ch/en/docs/web-component/

## Adding it

With a script tag straight from Findort:

```html

<findort-search key="pk_…" country="ch" lang="en" name="address"></findort-search>
```

Or from the npm package, for example with Vite or Webpack:

```js
```

The element works in React, Vue, Svelte, Angular and plain HTML. It needs a [public key](https://findort.ch/en/docs/authentication/) with your domain.

### Versions

The URL contains the version, so you get improvements without anything changing under your feet:

| URL | Behaviour |
|---|---|
| `/sdk/v1/search.js` | Newest 1.x version. Fixes arrive by themselves, breaking changes never do. Recommended. |
| `/sdk/1.0.0/search.js` | Exactly this version, never changes. Good if you want to review everything yourself or pin it with `integrity`. |

A new major version (v2) gets a new URL. `/sdk/v1/` stays.

## Attributes

| Attribute | Default | Description |
|---|---|---|
| `key` | | Public key `pk_…` |
| `base-url` | `https://api.findort.ch` | API address. An empty value calls your own domain. |
| `lang` | page language | `de`, `fr`, `it` or `en`. Without it, `<html lang>` applies. |
| `country` | all countries | `ch` or `ch,li` |
| `types` | all types | for example `address,poi`, see [types](https://findort.ch/en/docs/response/#types) |
| `limit` | `6` | Number of suggestions, 1 to 20 |
| `near` | | `lat,lon`, prefers nearby results |
| `placeholder` | `Adresse oder Ort suchen` | Placeholder text. Set your own for English pages. |
| `value` | | Initial value, such as a saved address |
| `name` | | Form field name |
| `min-length` | `2` | Search starts at this many characters |
| `debounce` | `150` | Wait time in milliseconds after the last keystroke |
| `disabled` | | Disables the field |

`types`, `limit`, `near` and `min-length` take effect immediately, even when changed later. `key`, `base-url`, `lang`, `country` and `debounce` are read when the element is inserted into the page. To change them afterwards, set the attribute and insert the element again. `placeholder`, `value` and `disabled` can change at any time.

## Events

| Event | `detail` | When |
|---|---|---|
| `findort-select` | `{ place }` | Someone picked a suggestion. `place` is a [Place](https://findort.ch/en/docs/response/#place). |
| `change` | | Right after `findort-select`, like a normal field |
| `findort-error` | `{ error }` | A request failed, see [errors](https://findort.ch/en/docs/errors/#web-component) |

The events bubble and leave the shadow DOM, so you can also listen on the `<form>`.

```js

field.addEventListener('findort-select', (e) => {
  const { label, address, location } = e.detail.place;
  console.log(label, address.postcode, location.lat, location.lon);
});
```

## Properties and methods

| Name | Description |
|---|---|
| `selected` | The picked Place, or `null` as soon as someone types again |
| `value` | Text in the field. Setting it replaces the text and clears `selected`. |
| `clear()` | Clears the field and the suggestions |
| `focus()` | Moves focus into the input |

## In a form

With `name` the element becomes a form field. On submit it sends the text in the field, which after a pick is the label. If you need coordinates or single address parts, fill hidden inputs in the `findort-select` event:

```html
  <label for="address">Delivery address</label>
  <findort-search id="address" key="pk_…" name="address" country="ch" types="address,poi" placeholder="Search an address"></findort-search>
  <input type="hidden" name="postcode">
  <input type="hidden" name="lat">
  <input type="hidden" name="lon">
  <button>Order</button>
</form>

<script>
  const form = document.querySelector('form');
  form.querySelector('findort-search').addEventListener('findort-select', (e) => {
    const p = e.detail.place;
    form.postcode.value = p.address.postcode ?? '';
    form.lat.value = p.location.lat;
    form.lon.value = p.location.lon;
  });
</script>
```

## Styling

The element inherits the page's font and colour. Everything else is controlled by CSS custom properties:

| Property | Default | Affects |
|---|---|---|
| `--findort-accent` | `#ffc531` | Focus ring and highlighted suggestion |
| `--findort-bg` | `#fff` | Background of field and list |
| `--findort-fg` | `#14161a` | Text colour |
| `--findort-muted` | `#8a8a84` | Icons, second line, source notice |
| `--findort-border` | `#d6d6cf` | Borders |
| `--findort-radius` | `10px` | Corner radius |
| `--findort-font` | `inherit` | Font |

For more control, these parts are available to `::part()`:

| Part | Element |
|---|---|
| `input` | the input field |
| `list` | the suggestion list |
| `option` | a single suggestion |

```css
  --findort-accent: #1f6f4a;
  --findort-radius: 4px;
  font-size: 1.1rem;
}
findort-search::part(input) {
  padding-block: 14px;
}
findort-search::part(option) {
  border-bottom: 1px solid #eee;
}
```

## Keyboard and accessibility

| Key | Action |
|---|---|
| <kbd>↓</kbd> | Open the list, next suggestion |
| <kbd>↑</kbd> | Previous suggestion |
| <kbd>Enter</kbd> | Take the highlighted suggestion |
| <kbd>Esc</kbd> | Close the list |
| <kbd>Tab</kbd> | Leave the field, the list closes |

The field follows the WAI-ARIA combobox pattern: screen readers announce the highlighted suggestion. A `<label for="…">` gives the field its name. The first suggestion is highlighted after every search, so <kbd>Enter</kbd> takes it right away.

## Source notice

Below the suggestions it says "© OpenStreetMap". The notice is required by the [ODbL](https://findort.ch/en/docs/limits/#attribution). Do not hide it.
