dropdown.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import DOMNavigator from "shared/dom-navigator.js";
  2. import { converse, api } from "@converse/headless/core";
  3. import { html } from 'lit';
  4. import { until } from 'lit/directives/until.js';
  5. import DropdownBase from 'shared/components/dropdownbase.js';
  6. export default class Dropdown extends DropdownBase {
  7. static get properties () {
  8. return {
  9. 'icon_classes': { type: String },
  10. 'items': { type: Array }
  11. }
  12. }
  13. render () {
  14. const icon_classes = this.icon_classes || "fa fa-bars";
  15. return html`
  16. <button type="button" class="btn btn--transparent btn--standalone" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
  17. <i class="${icon_classes} only-icon"></i>
  18. </button>
  19. <div class="dropdown-menu">
  20. ${ this.items.map(b => until(b, '')) }
  21. </div>
  22. `;
  23. }
  24. firstUpdated () {
  25. super.firstUpdated();
  26. this.initArrowNavigation();
  27. }
  28. hideMenu () {
  29. super.hideMenu();
  30. this.navigator?.disable();
  31. }
  32. initArrowNavigation () {
  33. if (!this.navigator) {
  34. const options = {
  35. 'selector': '.dropdown-item',
  36. 'onSelected': el => el.focus()
  37. };
  38. this.navigator = new DOMNavigator(this.menu, options);
  39. }
  40. }
  41. enableArrowNavigation (ev) {
  42. if (ev) {
  43. ev.preventDefault();
  44. ev.stopPropagation();
  45. }
  46. this.navigator.enable();
  47. this.navigator.select(this.menu.firstElementChild);
  48. }
  49. handleKeyUp (ev) {
  50. super.handleKeyUp(ev);
  51. if (ev.keyCode === converse.keycodes.DOWN_ARROW && !this.navigator.enabled) {
  52. this.enableArrowNavigation(ev);
  53. }
  54. }
  55. }
  56. api.elements.define('converse-dropdown', Dropdown);