summaryrefslogtreecommitdiffstats
path: root/toolkit/content/widgets/message-bar.js
blob: d38347b40a9f0f2c4905de75c95fc5863699b559 (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
/* 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/. */

"use strict";

// This is loaded into chrome windows with the subscript loader. Wrap in
// a block to prevent accidentally leaking globals onto `window`.
{
  class MessageBarElement extends HTMLElement {
    constructor() {
      super();
      const shadowRoot = this.attachShadow({ mode: "open" });
      window.MozXULElement?.insertFTLIfNeeded(
        "toolkit/global/notification.ftl"
      );
      document.l10n.connectRoot(this.shadowRoot);
      const content = this.constructor.template.content.cloneNode(true);
      shadowRoot.append(content);
      this.closeButton.addEventListener("click", () => this.dismiss(), {
        once: true,
      });
    }

    disconnectedCallback() {
      this.dispatchEvent(new CustomEvent("message-bar:close"));
    }

    get closeButton() {
      return this.shadowRoot.querySelector("button.close");
    }

    static get template() {
      const template = document.createElement("template");

      const commonStyles = document.createElement("link");
      commonStyles.rel = "stylesheet";
      commonStyles.href = "chrome://global/skin/in-content/common.css";
      const messageBarStyles = document.createElement("link");
      messageBarStyles.rel = "stylesheet";
      messageBarStyles.href =
        "chrome://global/content/elements/message-bar.css";
      template.content.append(commonStyles, messageBarStyles);

      // A container for the entire message bar content,
      // most of the css rules needed to provide the
      // expected message bar layout is applied on this
      // element.
      const container = document.createElement("div");
      container.part = "container";
      container.classList.add("container");
      template.content.append(container);

      const icon = document.createElement("span");
      icon.classList.add("icon");
      icon.part = "icon";
      container.append(icon);

      const barcontent = document.createElement("span");
      barcontent.classList.add("content");
      barcontent.append(document.createElement("slot"));
      container.append(barcontent);

      const spacer = document.createElement("span");
      spacer.classList.add("spacer");
      container.append(spacer);

      const closeIcon = document.createElement("button");
      closeIcon.classList.add("close", "ghost-button");
      document.l10n.setAttributes(closeIcon, "notification-close-button");
      container.append(closeIcon);

      Object.defineProperty(this, "template", {
        value: template,
      });

      return template;
    }

    dismiss() {
      this.dispatchEvent(new CustomEvent("message-bar:user-dismissed"));
      this.close();
    }

    close() {
      this.remove();
    }
  }

  customElements.define("message-bar", MessageBarElement);
}