summaryrefslogtreecommitdiffstats
path: root/toolkit/components/reader/color-input.mjs
blob: 1c460dac3e8d972036949ca4cf99db70c894a56d (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
/* 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";

/**
 * @tagname color-input
 * @property {string} color - The initial color value as a hex code.
 * @property {string} propName - The property that the color input sets.
 * @property {string} l10nId - l10nId for label text.
 */
export default class ColorInput extends MozLitElement {
  static properties = {
    color: { type: String },
    propName: { type: String, attribute: "prop-name" },
    l10nId: { type: String, attribute: "data-l10n-id" },
  };

  static queries = {
    inputEl: "#color-swatch",
  };

  handleColorInput(event) {
    this.color = event.target.value;
    this.dispatchEvent(
      new CustomEvent("color-picked", {
        detail: this.color,
      })
    );
  }

  /* Function to launch color picker when the user clicks anywhere in the container. */
  handleClick(event) {
    // If the user directly clicks the color swatch, no need to propagate click.
    if (event.target.matches("input")) {
      return;
    }
    this.inputEl.click();
  }

  render() {
    return html`
      <link
        rel="stylesheet"
        href="chrome://global/content/reader/color-input.css"
      />
      <div class="color-input-container" @click="${this.handleClick}">
        <input
          type="color"
          name="${this.propName}"
          .value="${this.color}"
          id="color-swatch"
          @input="${this.handleColorInput}"
        />
        <label for="color-swatch" data-l10n-id=${this.l10nId}></label>
        <div class="icon-container">
          <img
            class="icon"
            role="presentation"
            src="chrome://global/skin/icons/edit-outline.svg"
          />
        </div>
      </div>
    `;
  }
}
customElements.define("color-input", ColorInput);