|
| 1 | +--- |
| 2 | +group: javascript-developer-guide |
| 3 | +subgroup: 3_Widgets |
| 4 | +title: Dropdown widget |
| 5 | +functional_areas: |
| 6 | + - Frontend |
| 7 | + - Theme |
| 8 | +--- |
| 9 | + |
| 10 | +## Overview |
| 11 | + |
| 12 | +Use the dropdown widget to display custom content on the storefront. |
| 13 | + |
| 14 | +Widget source file is [lib/web/mage/dropdowns.js]. |
| 15 | + |
| 16 | + |
| 17 | +**Usages:** |
| 18 | +- [Shipping policy] |
| 19 | +- [Customer menu] |
| 20 | +- [UI tooltip] |
| 21 | + |
| 22 | +[lib/web/mage/dropdowns.js]: {{ site.mage2bloburl }}/{{ page.guide_version }}/lib/web/mage/dropdowns.js |
| 23 | +[Shipping policy]: {{ site.mage2bloburl }}/{{ page.guide_version }}/app/code/Magento/Shipping/view/frontend/web/template/checkout/shipping/shipping-policy.html |
| 24 | +[Customer menu]: {{ site.mage2bloburl }}/{{ page.guide_version }}/app/code/Magento/Customer/view/frontend/templates/account/customer.phtml |
| 25 | +[UI tooltip]: {{ site.mage2bloburl }}/{{ page.guide_version }}/app/code/Magento/Ui/view/frontend/web/templates/form/element/helper/tooltip.html |
| 26 | + |
| 27 | +### HTML markup |
| 28 | + |
| 29 | +```html |
| 30 | +<div class="magento__dropdown-widget"> |
| 31 | + <span class="action toggle" data-toggle="dropdown" aria-haspopup="true"> |
| 32 | + <span>Dropdown Opener Button</span> |
| 33 | + </span> |
| 34 | + <ul class="dropdown-options" data-target="dropdown"> |
| 35 | + <li> |
| 36 | + <span class="item">Dropdown Item One</span> |
| 37 | + </li> |
| 38 | + <li> |
| 39 | + <span class="item"><a href="#">Dropdown Item Two, With Link</a></span> |
| 40 | + </li> |
| 41 | + <li> |
| 42 | + <span class="item">Dropdown Item Three</span> |
| 43 | + </li> |
| 44 | + </ul> |
| 45 | +</div> |
| 46 | +``` |
| 47 | + |
| 48 | +### Styles |
| 49 | + |
| 50 | +Because the dropdown widget is not formatted by default, use [LESS mixin `.lib-dropdown()`] to customize it. |
| 51 | + |
| 52 | +```less |
| 53 | +// |
| 54 | +// Simple dropdown |
| 55 | +// --------------------------------------------- |
| 56 | + |
| 57 | +.lib-dropdown( |
| 58 | + @_toggle-selector : ~".action.toggle", |
| 59 | + @_options-selector : ~"ul.dropdown", |
| 60 | + |
| 61 | + @_dropdown-actions-padding: @dropdown-actions__padding, |
| 62 | + @_dropdown-list-min-width: @dropdown-list__min-width, |
| 63 | + @_dropdown-list-width: @dropdown-list__width, |
| 64 | + @_dropdown-list-height: @dropdown-list__height, |
| 65 | + @_dropdown-list-margin-top: @dropdown-list__margin-top, |
| 66 | + @_dropdown-list-position-top: @dropdown-list__position-top, |
| 67 | + @_dropdown-list-position-bottom: @dropdown-list__position-bottom, |
| 68 | + @_dropdown-list-position-left: @dropdown-list__position-left, |
| 69 | + @_dropdown-list-position-right: @dropdown-list__position-right, |
| 70 | + @_dropdown-list-background: @dropdown-list__background, |
| 71 | + @_dropdown-list-border: @dropdown-list__border, |
| 72 | + @_dropdown-list-pointer: @dropdown-list__pointer, |
| 73 | + @_dropdown-list-pointer-border: @dropdown-list-pointer__border, |
| 74 | + @_dropdown-list-pointer-position: @dropdown-list-pointer__position, |
| 75 | + @_dropdown-list-pointer-position-top: @dropdown-list-pointer__position-top, |
| 76 | + @_dropdown-list-pointer-position-left-right: @dropdown-list-pointer__position-left-right, |
| 77 | + @_dropdown-list-item-border: @dropdown-list-item__border, |
| 78 | + @_dropdown-list-item-padding: @dropdown-list-item__padding, |
| 79 | + @_dropdown-list-item-margin: @dropdown-list-item__margin, |
| 80 | + @_dropdown-list-item-hover: @dropdown-list-item__hover, |
| 81 | + @_dropdown-list-shadow: @dropdown-list__shadow, |
| 82 | + @_dropdown-list-z-index: @dropdown-list__z-index, |
| 83 | + |
| 84 | + @_dropdown-toggle-icon-content: @dropdown-toggle-icon__content, |
| 85 | + @_dropdown-toggle-active-icon-content: @dropdown-toggle-icon__active__content, |
| 86 | + |
| 87 | + @_icon-font: @dropdown-toggle-icon__font, |
| 88 | + @_icon-font-size: @dropdown-toggle-icon__font-size, |
| 89 | + @_icon-font-line-height: @dropdown-toggle-icon__font-line-height, |
| 90 | + @_icon-font-color: @dropdown-toggle-icon__font-color, |
| 91 | + @_icon-font-color-hover: @dropdown-toggle-icon__font-color-hover, |
| 92 | + @_icon-font-color-active: @dropdown-toggle-icon__font-color-active, |
| 93 | + @_icon-font-margin: @dropdown-toggle-icon__font-margin, |
| 94 | + @_icon-font-position: @dropdown-toggle-icon__position, |
| 95 | + @_icon-font-vertical-align: @dropdown-toggle-icon__font-vertical-align, |
| 96 | + @_icon-font-text-hide: @dropdown-toggle-icon__text-hide |
| 97 | +) { |
| 98 | + // ... |
| 99 | +} |
| 100 | +``` |
| 101 | + |
| 102 | +[LESS mixin `.lib-dropdown()`]: {{ site.mage2bloburl }}/{{ page.guide_version }}/lib/web/css/source/lib/_dropdowns.less |
| 103 | + |
| 104 | +## Initialize the dropdown widget {#dropdown_init} |
| 105 | + |
| 106 | +The dropdown widget is initialized as described in [JavaScript initialization]({{ page.baseurl }}/javascript-dev-guide/javascript/js_init.html). |
| 107 | + |
| 108 | +### Initialize with `data-mage-init` attribute |
| 109 | + |
| 110 | +```html |
| 111 | +<div class="magento__dropdown-widget"> |
| 112 | + <span class="action toggle" |
| 113 | + data-toggle="dropdown" |
| 114 | + aria-haspopup="true" |
| 115 | + data-mage-init='{"dropdown":{}}'> |
| 116 | + <span>Dropdown Opener Button</span> |
| 117 | + </span> |
| 118 | + <ul class="dropdown-options" data-target="dropdown" /> |
| 119 | +</div> |
| 120 | +``` |
| 121 | + |
| 122 | +### Initialize in `.js` file with options |
| 123 | + |
| 124 | +```js |
| 125 | +$('.magento__dropdown-widget').dropdown(); |
| 126 | +``` |
| 127 | + |
| 128 | +## Options {#dropdown_options} |
| 129 | + |
| 130 | +- [parent](#d_parent) |
| 131 | +- [autoclose](#d_autoclose) |
| 132 | +- [btnArrow](#d_btnArrow) |
| 133 | +- [menu](#d_menu) |
| 134 | +- [activeClass](#d_activeClass) |
| 135 | + |
| 136 | + |
| 137 | +### `parent` {#d_parent} |
| 138 | + |
| 139 | +The parent element that initialized the widget. If not specified, the widget locates the element using the `.parent()` jQuery method. |
| 140 | + |
| 141 | +**Type**: |
| 142 | + |
| 143 | +- jQuery object |
| 144 | +- HTML |
| 145 | +- String |
| 146 | + |
| 147 | +**Default value**: `null` |
| 148 | + |
| 149 | + |
| 150 | +### `autoclose` {#d_autoclose} |
| 151 | + |
| 152 | +Determines whether to close the dropdown menu when the user clicks outside of the dropdown scope. |
| 153 | + |
| 154 | +**Type**: Boolean |
| 155 | + |
| 156 | +**Default value**: `true` |
| 157 | + |
| 158 | + |
| 159 | +### `btnArrow` {#d_btnArrow} |
| 160 | + |
| 161 | +(Optional) A jQuery selector that updates the arrow to `+` or `-` based on the state of the dropdown. |
| 162 | + |
| 163 | +**Type**: String |
| 164 | + |
| 165 | +**Default value**: `.arrow` |
| 166 | + |
| 167 | + |
| 168 | +### `menu` {#d_menu} |
| 169 | + |
| 170 | +A jQuery selector that represents the dropdown's menu element. `menu` must be inside the `parent` element. |
| 171 | + |
| 172 | +**Type**: String |
| 173 | + |
| 174 | +**Default value**: `[data-target="dropdown"]` |
| 175 | + |
| 176 | + |
| 177 | +### `activeClass` {#d_activeClass} |
| 178 | + |
| 179 | +Given class is added to the widget element when the menu is visible. |
| 180 | + |
| 181 | +**Type**: String |
| 182 | + |
| 183 | +**Default value**: `active` |
| 184 | + |
| 185 | + |
| 186 | +## Result |
| 187 | + |
| 188 | +The result is a custom select dropdown with custom content as options. |
| 189 | + |
| 190 | + |
0 commit comments