Skip to content

Commit c3e5419

Browse files
committed
docs: customization with examples
1 parent c6e4561 commit c3e5419

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed

README.md

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,77 @@ The element also emits **events** as a user interacts with it. This is how you c
114114
|`change` |`string` |Dispatched with every keystroke as the user types (not debounced).|
115115
|`error` |`Error` |Dispatched if an error occures during the request (for example if the rate limit was exceeded.)|
116116

117+
118+
119+
## Customization
120+
121+
We did our best to design the element in such a way that it can be used as is in a lot of different contexts without needing to adjust how it looks. However, there certainly can be situations where customization is necessary. The element supports three different customization APIs:
122+
123+
1. Custom CSS (variables)
124+
2. A string template as well as
125+
3. A row template
126+
127+
### 1. Custom CSS
128+
129+
We use CSS variables for almost all properties that you would want to customize. This includes the font family, background or shadow of the input and the hover state for a result just to name a few. For a list of all available variables please check the source CSS file directly ([`/src/autocomplete/autocomplete.css`](src/autocomplete/autocomplete.css)).
130+
131+
You can adjust these variables by placing a `<style>` tag _inside_ the element, like so:
132+
133+
```html
134+
<ge-autocomplete api_key="">
135+
<style>
136+
:host {
137+
--input-bg: salmon;
138+
--input-color: green;
139+
--loading-color: hotpink;
140+
}
141+
</style>
142+
</ge-autocomplete>
143+
```
144+
145+
**Important:** While it is technically possible to override the actual classnames the element uses internally, we do not consider those part of the public API. That means they can change without a new major version, which could break your customization. The CSS variables on the other hand are specifically meant to be customized, which is why they will stay consistent even if the internal markup changes.
146+
147+
If you would like to customize a property for which there is no variable we’d be happy to accept a pull-request or issue about it.
148+
149+
### 2. String Template
150+
151+
If you want to customize how a feature is turned into a string for rendering (in the results as well as the input field after it was selected), you can define a custom string template. This allows you to use the [lodash template language][_template] to access every property of the feature to build a custom string.
152+
153+
```html
154+
<ge-autocomplete api_key="">
155+
<template string>
156+
${feature.properties.name} (${feature.properties.id}, ${feature.properties.source})
157+
</template>
158+
</ge-autocomplete>
159+
```
160+
161+
**Important:** Make sure to return a plain string here, no HTML. The reason for this is that this template will also be used in the input field itself after a result has been selected, which doesn’t support HTML.
162+
163+
### 3. Row Template
164+
165+
Similar to the string template mentioned above, you can use the row template to define how a single row in the results is rendered. The key here is that this supports full HTML:
166+
167+
```html
168+
<ge-autocomplete api_key="">
169+
<template row>
170+
<div class="custom-row ${feature.active ? 'custom-row--active' : null}">
171+
<img src="/flags/${feature.properties.country_a.png" alt="${feature.country_a}">
172+
<span>${feature.properties.label}</span>
173+
</div>
174+
</template>
175+
</ge-autocomplete>
176+
```
177+
178+
**Pro Tip™:** Use the `active` property to check if the current row is being hovered over or activated via arrow keys.
179+
180+
The example above could render a little flag icon for the result’s country, for example. You can customize the styling by defining custom classes in the same way you would customize the CSS variables. It’s best to prefix your classes to avoid conflicts with internal classnames of the element.
181+
182+
The [lodash template language][_template] supports much more than just straight variables. Please refer to their docs directly to understand how it works. It’s pretty powerful.
183+
184+
[_template]: https://lodash.com/docs/4.17.15#template
185+
186+
187+
117188
## Example
118189

119190
Please see the `example` folder. You can follow the steps in the [**Development**](#development) section to run them directly, too.

0 commit comments

Comments
 (0)