Files
webawesome/source/docs/dropdowns.md

165 lines
4.9 KiB
Markdown
Raw Normal View History

2017-08-08 17:41:40 -04:00
---
layout: default.html
title: Dropdowns
description: Add beautiful menus to your app with dropdowns.
---
## Dropdowns
Dropdowns can be created using the markup below. You can use a `<button>` or an `<a>` as a trigger. Dropdown indicators (i.e. carets) are added for you. Menu items are simply `<a>` elements. Dividers are simply `<hr>` elements.
Note the class names used for the main container, the trigger, and the menu. Additionally, menu items can be disabled by adding the `disabled` class. Menu items can also be given a checked state using the `checked` class.
2017-08-18 14:27:07 -04:00
To disable a dropdown entirely, add the `disabled` property to the dropdown trigger if its a button. If its a link, add the `disabled` class instead. When a dropdown is activated, it will receive the `active` class and the menu will show.
2017-08-08 17:41:40 -04:00
2017-08-31 12:23:47 -04:00
Headings can be created with the `dropdown-heading` class.
2017-08-08 17:41:40 -04:00
```html
<div class="dropdown">
<button type="button" class="dropdown-trigger">Dropdown</button>
<div class="dropdown-menu">
<a href="#">Item 1</a>
<a href="#">Item 2</a>
<a href="#">Item 3</a>
<a href="#" class="checked">Checked</a>
<a href="#" class="disabled">Disabled</a>
<hr>
2017-08-31 12:23:47 -04:00
<div class="dropdown-heading">Heading</div>
2017-08-08 17:41:40 -04:00
<a href="#">More...</a>
</div>
</div>
```
2017-08-23 09:59:22 -04:00
<div class="input-field">
2017-08-08 17:41:40 -04:00
<div class="dropdown">
<button type="button" class="dropdown-trigger">Dropdown</button>
<div class="dropdown-menu">
<a href="#">Item 1</a>
<a href="#">Item 2</a>
<a href="#">Item 3</a>
<a href="#" class="checked">Checked</a>
<a href="#" class="disabled">Disabled</a>
<hr>
2017-08-31 12:23:47 -04:00
<div class="dropdown-heading">Heading</div>
2017-08-08 17:41:40 -04:00
<a href="#">More...</a>
</div>
</div>
</div>
Use the `dropdown-top` and `dropdown-left` modifiers to change the positioning of the menu. You can combine these modifiers as needed.
```html
2017-08-13 17:08:21 -04:00
<div class="dropdown dropdown-top">
2017-08-08 17:41:40 -04:00
...
</div>
2017-08-13 17:08:21 -04:00
<div class="dropdown dropdown-left">
2017-08-08 17:41:40 -04:00
...
</div>
<div class="dropdown dropdown-top dropdown-left">
...
</div>
```
2017-08-23 09:59:22 -04:00
<div class="input-field">
2017-08-13 17:08:21 -04:00
<div class="dropdown dropdown-top">
<button type="button" class="dropdown-trigger">Top</button>
2017-08-08 17:41:40 -04:00
<div class="dropdown-menu">
<a href="#">Item 1</a>
<a href="#">Item 2</a>
<a href="#">Item 3</a>
</div>
</div>
2017-08-13 17:08:21 -04:00
<div class="dropdown dropdown-left">
<button type="button" class="dropdown-trigger">Left</button>
2017-08-08 17:41:40 -04:00
<div class="dropdown-menu">
<a href="#">Item 1</a>
<a href="#">Item 2</a>
<a href="#">Item 3</a>
</div>
</div>
<div class="dropdown dropdown-top dropdown-left">
<button type="button" class="dropdown-trigger">Top Left</button>
<div class="dropdown-menu">
<a href="#">Item 1</a>
<a href="#">Item 2</a>
<a href="#">Item 3</a>
</div>
</div>
</div>
Dropdowns with button triggers can be used inside input groups.
2017-08-13 15:41:38 -04:00
```html
2017-08-08 17:41:40 -04:00
<div class="input-group">
<span class="input-addon">$</span>
<input type="text" placeholder="10.00">
2017-08-13 15:41:38 -04:00
<div class="dropdown dropdown-left">
<button type="button" class="dropdown-trigger">Currency</button>
<div class="dropdown-menu">
<a href="#" class="checked">USD</a>
<a href="#">AUD</a>
<a href="#">CAD</a>
<a href="#">GBP</a>
</div>
</div>
</div>
```
<div class="input-group">
<span class="input-addon">$</span>
<input type="text" placeholder="10.00">
2017-08-08 17:41:40 -04:00
<div class="dropdown dropdown-left">
<button type="button" class="dropdown-trigger">Currency</button>
<div class="dropdown-menu">
<a href="#" class="checked">USD</a>
<a href="#">AUD</a>
<a href="#">CAD</a>
<a href="#">GBP</a>
</div>
</div>
</div>
2017-08-29 17:53:59 -04:00
### Interactivity
2017-08-08 17:41:40 -04:00
2017-08-29 17:53:59 -04:00
Dropdowns require `shoelace.js` for interactivity. You dont need to initialize anything. Just include the script and everything “just works.”
2017-08-08 17:41:40 -04:00
2017-08-29 17:53:59 -04:00
There is no JavaScript API. Shoelaces philosophy believes that custom components should act like native components as much as possible. You can, however, listen for various events.
2017-08-08 17:41:40 -04:00
- `show`  Fires when a dropdown is shown.
- `hide`  Fires when a dropdown is hidden.
- `select`  Fires when a dropdown menu item is selected. The second callback argument is a reference to the respective menu item.
This example will log all three events for a dropdown with an id of `my-dropdown`.
```javascript
$('#my-dropdown')
.on('show', function(event) {
console.log('show', event.target);
})
.on('hide', function(event) {
console.log('hide', event.target);
})
.on('select', function(event, item) {
console.log('select', event.target, item);
2017-09-01 17:13:46 -04:00
// Tip: Use event.preventDefault() to
// intercept the original click event.
2017-08-08 17:41:40 -04:00
});
```
2017-08-29 17:53:59 -04:00
To show or hide a dropdown programmatically, just add or remove the `active` class to the dropdown.
```javascript
// Show the dropdown
$('#my-dropdown').addClass('active');
// Hide the dropdown
$('#my-dropdown').removeClass('active');
```