dropdown.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import DOMNavigator from "../dom-navigator";
  2. import { CustomElement } from './element.js';
  3. import { converse, api } from "@converse/headless/converse-core";
  4. import { html } from 'lit-element';
  5. import { until } from 'lit-html/directives/until.js';
  6. const u = converse.env.utils;
  7. export class BaseDropdown extends CustomElement {
  8. firstUpdated () {
  9. this.menu = this.querySelector('.dropdown-menu');
  10. this.dropdown = this.firstElementChild;
  11. this.button = this.dropdown.querySelector('button');
  12. this.dropdown.addEventListener('click', ev => this.toggleMenu(ev));
  13. this.dropdown.addEventListener('keyup', ev => this.handleKeyUp(ev));
  14. document.addEventListener('click', ev => !this.contains(ev.target) && this.hideMenu(ev));
  15. }
  16. hideMenu () {
  17. u.removeClass('show', this.menu);
  18. this.button?.setAttribute('aria-expanded', false);
  19. this.button?.blur();
  20. }
  21. showMenu () {
  22. u.addClass('show', this.menu);
  23. this.button.setAttribute('aria-expanded', true);
  24. }
  25. toggleMenu (ev) {
  26. ev.stopPropagation();
  27. ev.preventDefault();
  28. if (u.hasClass('show', this.menu)) {
  29. this.hideMenu();
  30. } else {
  31. this.showMenu();
  32. }
  33. }
  34. handleKeyUp (ev) {
  35. if (ev.keyCode === converse.keycodes.ESCAPE) {
  36. this.hideMenu();
  37. } else if (ev.keyCode === converse.keycodes.DOWN_ARROW && this.navigator && !this.navigator.enabled) {
  38. this.enableArrowNavigation(ev);
  39. }
  40. }
  41. }
  42. export default class DropdownList extends BaseDropdown {
  43. static get properties () {
  44. return {
  45. 'icon_classes': { type: String },
  46. 'items': { type: Array }
  47. }
  48. }
  49. render () {
  50. const icon_classes = this.icon_classes || "fa fa-bars";
  51. return html`
  52. <div class="dropleft">
  53. <button type="button" class="btn btn--transparent btn--standalone" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
  54. <i class="${icon_classes} only-icon"></i>
  55. </button>
  56. <div class="dropdown-menu">
  57. ${ this.items.map(b => until(b, '')) }
  58. </div>
  59. </div>
  60. `;
  61. }
  62. hideMenu () {
  63. super.hideMenu();
  64. this.navigator.disable();
  65. }
  66. firstUpdated () {
  67. super.firstUpdated();
  68. this.initArrowNavigation();
  69. }
  70. initArrowNavigation () {
  71. if (!this.navigator) {
  72. const options = {
  73. 'selector': '.dropdown-item',
  74. 'onSelected': el => el.focus()
  75. };
  76. this.navigator = new DOMNavigator(this.menu, options);
  77. }
  78. }
  79. enableArrowNavigation (ev) {
  80. if (ev) {
  81. ev.preventDefault();
  82. ev.stopPropagation();
  83. }
  84. this.navigator.enable();
  85. this.navigator.select(this.menu.firstElementChild);
  86. }
  87. handleKeyUp (ev) {
  88. super.handleKeyUp(ev);
  89. if (ev.keyCode === converse.keycodes.DOWN_ARROW && !this.navigator.enabled) {
  90. this.enableArrowNavigation(ev);
  91. }
  92. }
  93. }
  94. api.elements.define('converse-dropdown', DropdownList);