2020-10-30 14:08:56 -04:00
---
stage: none
group: unassigned
2020-11-26 01:09:20 -05:00
info: To determine the technical writer assigned to the Stage/Group associated with this page, see https://about.gitlab.com/handbook/engineering/ux/technical-writing/#assignments
2020-10-30 14:08:56 -04:00
---
2017-04-19 11:16:58 -04:00
# DropLab
A generic dropdown for all of your custom dropdown needs.
## Usage
2020-11-16 13:09:15 -05:00
DropLab can be used by adding a `data-dropdown-trigger` HTML attribute. This
attribute allows us to find the "trigger" _(toggle)_ for the dropdown, whether
it's a button, link or input.
2017-04-19 11:16:58 -04:00
2020-11-16 13:09:15 -05:00
The value of the `data-dropdown-trigger` should be a CSS selector that DropLab
can use to find the trigger's dropdown list.
2017-04-19 11:16:58 -04:00
You should also add the `data-dropdown` attribute to declare the dropdown list.
The value is irrelevant.
2020-11-16 13:09:15 -05:00
The DropLab class has no side effects, so you must always call `.init` when the
DOM is ready. `DropLab.prototype.init` takes the same arguments as `DropLab.prototype.addHook` .
2020-12-14 13:09:48 -05:00
If you don't provide any arguments, it globally queries and instantiates all
2020-11-16 13:09:15 -05:00
DropLab-compatible dropdowns.
2017-04-19 11:16:58 -04:00
```html
< a href = "#" data-dropdown-trigger = "#list" > Toggle< / a >
< ul id = "list" data-dropdown >
<!-- ... -->
< ul >
```
2019-07-12 04:09:23 -04:00
2020-05-19 14:08:11 -04:00
```javascript
2017-04-19 11:16:58 -04:00
const droplab = new DropLab();
droplab.init();
```
2020-11-16 13:09:15 -05:00
As noted, we have a "Toggle" link that's declared as a trigger. It provides a
selector to find the dropdown list it should control.
2017-04-19 11:16:58 -04:00
### Static data
You can add static list items.
```html
< a href = "#" data-dropdown-trigger = "#list" > Toggle< / a >
< ul id = "list" data-dropdown >
< li > Static value 1< / li >
< li > Static value 2< / li >
< ul >
```
2019-07-12 04:09:23 -04:00
2020-05-19 14:08:11 -04:00
```javascript
2017-04-19 11:16:58 -04:00
const droplab = new DropLab();
droplab.init();
```
### Explicit instantiation
2020-11-16 13:09:15 -05:00
You can pass the trigger and list elements as constructor arguments to return a
non-global instance of DropLab using the `DropLab.prototype.init` method.
2017-04-19 11:16:58 -04:00
```html
< a href = "#" id = "trigger" data-dropdown-trigger = "#list" > Toggle< / a >
< ul id = "list" data-dropdown >
<!-- ... -->
< ul >
```
2019-07-12 04:09:23 -04:00
2020-05-19 14:08:11 -04:00
```javascript
2017-04-19 11:16:58 -04:00
const trigger = document.getElementById('trigger');
const list = document.getElementById('list');
const droplab = new DropLab();
droplab.init(trigger, list);
```
You can also add hooks to an existing DropLab instance using `DropLab.prototype.addHook` .
```html
< a href = "#" data-dropdown-trigger = "#auto-dropdown" > Toggle< / a >
< ul id = "auto-dropdown" data-dropdown > <!-- ... --> < ul >
< a href = "#" id = "trigger" data-dropdown-trigger = "#list" > Toggle< / a >
< ul id = "list" data-dropdown > <!-- ... --> < ul >
```
2019-07-12 04:09:23 -04:00
2020-05-19 14:08:11 -04:00
```javascript
2017-04-19 11:16:58 -04:00
const droplab = new DropLab();
droplab.init();
const trigger = document.getElementById('trigger');
const list = document.getElementById('list');
droplab.addHook(trigger, list);
```
### Dynamic data
2020-12-14 13:09:48 -05:00
Adding `data-dynamic` to your dropdown element enables dynamic list
2020-11-16 13:09:15 -05:00
rendering.
2017-04-19 11:16:58 -04:00
2020-11-16 13:09:15 -05:00
You can template a list item using the keys of the data object provided. Use the
handlebars syntax `{{ value }}` to HTML escape the value. Use the `<%= value %>`
syntax to interpolate the value. Use the `<%= value %>` syntax to evaluate the
value.
2017-04-19 11:16:58 -04:00
2020-12-14 13:09:48 -05:00
Passing an array of objects to `DropLab.prototype.addData` renders that data
2017-04-19 11:16:58 -04:00
for all `data-dynamic` dropdown lists tracked by that DropLab instance.
```html
< a href = "#" data-dropdown-trigger = "#list" > Toggle< / a >
< ul id = "list" data-dropdown data-dynamic >
< li > < a href = "#" data-id = "{{id}}" > {{text}}< / a > < / li >
< / ul >
```
2019-07-12 04:09:23 -04:00
2020-05-19 14:08:11 -04:00
```javascript
2017-04-19 11:16:58 -04:00
const droplab = new DropLab();
droplab.init().addData([{
id: 0,
text: 'Jacob',
}, {
id: 1,
text: 'Jeff',
}]);
```
2020-11-16 13:09:15 -05:00
Alternatively, you can specify a specific dropdown to add this data to by
passing the data as the second argument and the `id` of the trigger element as
the first argument.
2017-04-19 11:16:58 -04:00
```html
< a href = "#" data-dropdown-trigger = "#list" id = "trigger" > Toggle< / a >
< ul id = "list" data-dropdown data-dynamic >
< li > < a href = "#" data-id = "{{id}}" > {{text}}< / a > < / li >
< / ul >
```
2019-07-12 04:09:23 -04:00
2020-05-19 14:08:11 -04:00
```javascript
2017-04-19 11:16:58 -04:00
const droplab = new DropLab();
droplab.init().addData('trigger', [{
id: 0,
text: 'Jacob',
}, {
id: 1,
text: 'Jeff',
}]);
```
2020-11-16 13:09:15 -05:00
This allows you to mix static and dynamic content, even with one trigger.
2017-04-19 11:16:58 -04:00
Note the use of scoping regarding the `data-dropdown` attribute to capture both
dropdown lists, one of which is dynamic.
```html
< input id = "trigger" data-dropdown-trigger = "#list" >
< div id = "list" data-dropdown >
< ul >
< li > < a href = "#" > Static item 1< / a > < / li >
< li > < a href = "#" > Static item 2< / a > < / li >
< / ul >
< ul data-dynamic >
< li > < a href = "#" data-id = "{{id}}" > {{text}}< / a > < / li >
< / ul >
< / div >
```
2019-07-12 04:09:23 -04:00
2020-05-19 14:08:11 -04:00
```javascript
2017-04-19 11:16:58 -04:00
const droplab = new DropLab();
droplab.init().addData('trigger', [{
id: 0,
text: 'Jacob',
}, {
id: 1,
text: 'Jeff',
}]);
```
## Internal selectors
DropLab adds some CSS classes to help lower the barrier to integration.
2018-11-13 01:07:16 -05:00
For example:
2017-04-19 11:16:58 -04:00
2020-11-16 13:09:15 -05:00
- The `droplab-item-selected` CSS class is added to items that have been
selected either by a mouse click or by enter key selection.
2020-03-25 23:07:43 -04:00
- The `droplab-item-active` CSS class is added to items that have been selected
2018-11-13 01:07:16 -05:00
using arrow key navigation.
2020-11-16 13:09:15 -05:00
- You can add the `droplab-item-ignore` CSS class to any item that you don't
want to be selectable. For example, an `<li class="divider"></li>` list
divider element that shouldn't be interactive.
2017-04-19 11:16:58 -04:00
## Internal events
DropLab uses some custom events to help lower the barrier to integration.
2018-11-13 01:07:16 -05:00
For example:
2017-04-19 11:16:58 -04:00
2020-11-16 13:09:15 -05:00
- The `click.dl` event is fired when an `li` list item has been clicked. It's
also fired when a list item has been selected with the keyboard. It's also
fired when a `HookButton` button is clicked (a registered `button` tag or `a`
tag trigger).
- The `input.dl` event is fired when a `HookInput` (a registered `input` tag
trigger) triggers an `input` event.
- The `mousedown.dl` event is fired when a `HookInput` triggers a `mousedown`
event.
2018-11-13 01:07:16 -05:00
- The `keyup.dl` event is fired when a `HookInput` triggers a `keyup` event.
- The `keydown.dl` event is fired when a `HookInput` triggers a `keydown` event.
2017-04-19 11:16:58 -04:00
2020-11-16 13:09:15 -05:00
These custom events add a `detail` object to the vanilla `Event` object that
provides some potentially useful data.
2017-04-19 11:16:58 -04:00
## Plugins
2020-11-16 13:09:15 -05:00
Plugins are objects that are registered to be executed when a hook is added (when
a DropLab trigger and dropdown are instantiated).
2017-04-19 11:16:58 -04:00
2020-12-14 13:09:48 -05:00
If no modules API is detected, the library falls back as it does with
`window.DropLab` and adds `window.DropLab.plugins.PluginName` .
2017-04-19 11:16:58 -04:00
### Usage
2020-11-16 13:09:15 -05:00
To use plugins, you can pass them in an array as the third argument of
`DropLab.prototype.init` or `DropLab.prototype.addHook` . Some plugins require
2020-12-11 01:10:17 -05:00
configuration values; the configuration object can be passed as the fourth argument.
2017-04-19 11:16:58 -04:00
```html
< a href = "#" id = "trigger" data-dropdown-trigger = "#list" > Toggle< / a >
< ul id = "list" data-dropdown > <!-- ... --> < ul >
```
2019-07-12 04:09:23 -04:00
2020-05-19 14:08:11 -04:00
```javascript
2017-04-19 11:16:58 -04:00
const droplab = new DropLab();
const trigger = document.getElementById('trigger');
const list = document.getElementById('list');
droplab.init(trigger, list, [droplabAjax], {
droplabAjax: {
endpoint: '/some-endpoint',
method: 'setData',
},
});
```
### Documentation
2020-11-16 13:09:15 -05:00
Refer to the list of available [DropLab plugins ](plugins/index.md ) for
information about their use.
2017-04-19 11:16:58 -04:00
### Development
2020-11-16 13:09:15 -05:00
When plugins are initialised for a DropLab trigger+dropdown, DropLab calls the
plugins' `init` function, so this must be implemented in the plugin.
2017-04-19 11:16:58 -04:00
2020-05-19 14:08:11 -04:00
```javascript
2017-04-19 11:16:58 -04:00
class MyPlugin {
static init() {
this.someProp = 'someProp';
this.someMethod();
}
static someMethod() {
this.otherProp = 'otherProp';
}
}
export default MyPlugin;
```