summaryrefslogtreecommitdiffstats
path: root/browser/components/firefoxview/fxview-category-navigation.mjs
blob: a8ac6838f8a1ba698dbbfe599120a4fd53cb2e9a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

import { html } from "chrome://global/content/vendor/lit.all.mjs";
import { MozLitElement } from "chrome://global/content/lit-utils.mjs";

export default class FxviewCategoryNavigation extends MozLitElement {
  // Use a relative URL in storybook to get faster reloads on style changes.
  static stylesheetUrl = window.IS_STORYBOOK
    ? "./fxview-category-navigation.css"
    : "chrome://browser/content/firefoxview/fxview-category-navigation.css";

  static properties = {
    currentCategory: { type: String },
  };

  static queries = {
    categoryButtonsSlot: "slot[name=category-button]",
  };

  get categoryButtons() {
    return this.categoryButtonsSlot
      .assignedNodes()
      .filter(node => !node.hidden);
  }

  onChangeCategory(e) {
    this.currentCategory = e.target.name;
  }

  handleFocus(e) {
    if (e.key == "ArrowDown" || e.key == "ArrowRight") {
      e.preventDefault();
      this.focusNextCategory();
    } else if (e.key == "ArrowUp" || e.key == "ArrowLeft") {
      e.preventDefault();
      this.focusPreviousCategory();
    }
  }

  focusPreviousCategory() {
    let categoryButtons = this.categoryButtons;
    let currentIndex = categoryButtons.findIndex(b => b.selected);
    let prev = categoryButtons[currentIndex - 1];
    if (prev) {
      prev.activate();
      prev.focus();
    }
  }

  focusNextCategory() {
    let categoryButtons = this.categoryButtons;
    let currentIndex = categoryButtons.findIndex(b => b.selected);
    let next = categoryButtons[currentIndex + 1];
    if (next) {
      next.activate();
      next.focus();
    }
  }

  render() {
    return html`
      <link rel="stylesheet" href=${this.constructor.stylesheetUrl} />
      <nav>
        <div class="category-nav-header">
          <slot name="category-nav-header"></slot>
        </div>
        <div
          class="category-nav-buttons"
          role="tablist"
          aria-orientation="vertical"
        >
          <slot
            name="category-button"
            @change-category=${this.onChangeCategory}
            @keydown=${this.handleFocus}
          ></slot>
        </div>
        <div class="category-nav-footer">
          <slot name="category-nav-footer"></slot>
        </div>
      </nav>
    `;
  }

  updated() {
    let categorySelected = false;
    let assignedCategories = this.categoryButtons;
    for (let button of assignedCategories) {
      button.selected = button.name == this.currentCategory;
      categorySelected = categorySelected || button.selected;
    }
    if (!categorySelected && assignedCategories.length) {
      // Current category has no matching category, reset to the first category.
      assignedCategories[0].activate();
    }
  }
}
customElements.define("fxview-category-navigation", FxviewCategoryNavigation);

export class FxviewCategoryButton extends MozLitElement {
  // Use a relative URL in storybook to get faster reloads on style changes.
  static stylesheetUrl = window.IS_STORYBOOK
    ? "./fxview-category-button.css"
    : "chrome://browser/content/firefoxview/fxview-category-button.css";

  static properties = {
    selected: { type: Boolean },
  };

  static queries = {
    buttonEl: "button",
  };

  connectedCallback() {
    super.connectedCallback();
    this.setAttribute("role", "tab");
  }

  get name() {
    return this.getAttribute("name");
  }

  activate() {
    this.dispatchEvent(
      new CustomEvent("change-category", {
        bubbles: true,
        composed: true,
      })
    );
  }

  render() {
    return html`
      <link rel="stylesheet" href=${this.constructor.stylesheetUrl} />
      <button tabindex="-1" ?selected=${this.selected} @click=${this.activate}>
        <span class="category-icon" part="icon"></span>
        <slot></slot>
      </button>
    `;
  }

  updated() {
    this.setAttribute("aria-selected", this.selected);
    this.setAttribute("tabindex", this.selected ? 0 : -1);
  }
}
customElements.define("fxview-category-button", FxviewCategoryButton);