summaryrefslogtreecommitdiffstats
path: root/browser/components/firefoxview/card-container.mjs
blob: b58f42204ab927ae2d94add91214ec1886b30d87 (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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/* 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 {
  classMap,
  html,
  ifDefined,
  when,
} from "chrome://global/content/vendor/lit.all.mjs";
import { MozLitElement } from "chrome://global/content/lit-utils.mjs";

/**
 * A collapsible card container to be used throughout Firefox View
 *
 * @property {string} sectionLabel - The aria-label used for the section landmark if the header is hidden with hideHeader
 * @property {boolean} hideHeader - Optional property given if the card container should not display a header
 * @property {boolean} isEmptyState - Optional property given if the card is used within an empty state
 * @property {boolean} isInnerCard - Optional property given if the card a nested card within another card and given a border rather than box-shadow
 * @property {boolean} preserveCollapseState - Whether or not the expanded/collapsed state should persist
 * @property {string} shortPageName - Page name that the 'View all' link will navigate to and the preserveCollapseState pref will use
 * @property {boolean} showViewAll - True if you need to display a 'View all' header link to navigate
 * @property {boolean} toggleDisabled - Optional property given if the card container should not be collapsible
 * @property {boolean} removeBlockEndMargin - True if you need to remove the block end margin on the card container
 */
class CardContainer extends MozLitElement {
  constructor() {
    super();
    this.initiallyExpanded = true;
    this.isExpanded = false;
    this.visible = false;
  }

  static properties = {
    sectionLabel: { type: String },
    hideHeader: { type: Boolean },
    isExpanded: { type: Boolean },
    isEmptyState: { type: Boolean },
    isInnerCard: { type: Boolean },
    preserveCollapseState: { type: Boolean },
    shortPageName: { type: String },
    showViewAll: { type: Boolean },
    toggleDisabled: { type: Boolean },
    removeBlockEndMargin: { type: Boolean },
    visible: { type: Boolean },
  };

  static queries = {
    detailsEl: "details",
    mainSlot: "slot[name=main]",
    summaryEl: "summary",
    viewAllLink: ".view-all-link",
  };

  get detailsExpanded() {
    return this.detailsEl.hasAttribute("open");
  }

  get detailsOpenPrefValue() {
    const prefName = this.shortPageName
      ? `browser.tabs.firefox-view.ui-state.${this.shortPageName}.open`
      : null;
    if (prefName && Services.prefs.prefHasUserValue(prefName)) {
      return Services.prefs.getBoolPref(prefName);
    }
    return null;
  }

  connectedCallback() {
    super.connectedCallback();
    this.isExpanded = this.detailsOpenPrefValue ?? this.initiallyExpanded;
  }

  onToggleContainer() {
    if (this.isExpanded == this.detailsExpanded) {
      return;
    }
    this.isExpanded = this.detailsExpanded;

    this.updateTabLists();

    if (!this.shortPageName) {
      return;
    }

    if (this.preserveCollapseState) {
      const prefName = this.shortPageName
        ? `browser.tabs.firefox-view.ui-state.${this.shortPageName}.open`
        : null;
      Services.prefs.setBoolPref(prefName, this.isExpanded);
    }

    // Record telemetry
    Services.telemetry.recordEvent(
      "firefoxview_next",
      this.isExpanded ? "card_expanded" : "card_collapsed",
      "card_container",
      null,
      {
        data_type: this.shortPageName,
      }
    );
  }

  viewAllClicked() {
    this.dispatchEvent(
      new CustomEvent("card-container-view-all", {
        bubbles: true,
        composed: true,
      })
    );
  }

  willUpdate(changes) {
    if (changes.has("visible")) {
      this.updateTabLists();
    }
  }

  updateTabLists() {
    let tabLists = this.querySelectorAll("fxview-tab-list");
    if (tabLists) {
      tabLists.forEach(tabList => {
        tabList.updatesPaused = !this.visible || !this.isExpanded;
      });
    }
  }

  render() {
    return html`
      <link
        rel="stylesheet"
        href="chrome://browser/content/firefoxview/card-container.css"
      />
      <section
        aria-labelledby="header"
        aria-label=${ifDefined(this.sectionLabel)}
      >
        ${when(
          this.toggleDisabled,
          () => html`<div
            class=${classMap({
              "card-container": true,
              inner: this.isInnerCard,
              "empty-state": this.isEmptyState && !this.isInnerCard,
            })}
          >
            <span
              id="header"
              class="card-container-header"
              ?hidden=${ifDefined(this.hideHeader)}
              toggleDisabled
              ?withViewAll=${this.showViewAll}
            >
              <slot name="header"></slot>
              <slot name="secondary-header"></slot>
            </span>
            <a
              href="about:firefoxview#${this.shortPageName}"
              @click=${this.viewAllClicked}
              class="view-all-link"
              data-l10n-id="firefoxview-view-all-link"
              ?hidden=${!this.showViewAll}
            ></a>
            <slot name="main"></slot>
            <slot name="footer" class="card-container-footer"></slot>
          </div>`,
          () => html`<details
            class=${classMap({
              "card-container": true,
              inner: this.isInnerCard,
              "empty-state": this.isEmptyState && !this.isInnerCard,
            })}
            ?open=${this.isExpanded}
            ?isOpenTabsView=${this.removeBlockEndMargin}
            @toggle=${this.onToggleContainer}
          >
            <summary
              id="header"
              class="card-container-header"
              ?hidden=${ifDefined(this.hideHeader)}
              ?withViewAll=${this.showViewAll}
            >
              <span
                class="icon chevron-icon"
                aria-role="presentation"
                data-l10n-id="firefoxview-collapse-button-${this.isExpanded
                  ? "hide"
                  : "show"}"
              ></span>
              <slot name="header"></slot>
            </summary>
            <a
              href="about:firefoxview#${this.shortPageName}"
              @click=${this.viewAllClicked}
              class="view-all-link"
              data-l10n-id="firefoxview-view-all-link"
              ?hidden=${!this.showViewAll}
            ></a>
            <slot name="main"></slot>
            <slot name="footer" class="card-container-footer"></slot>
          </details>`
        )}
      </section>
    `;
  }
}
customElements.define("card-container", CardContainer);