summaryrefslogtreecommitdiffstats
path: root/browser/components/newtab/content-src/asrouter/components/SnippetBase/SnippetBase.jsx
blob: fd25337fbf3d5d73638f173a908e3b47c33806f7 (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
/* 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 React from "react";

export class SnippetBase extends React.PureComponent {
  constructor(props) {
    super(props);
    this.onBlockClicked = this.onBlockClicked.bind(this);
    this.onDismissClicked = this.onDismissClicked.bind(this);
    this.setBlockButtonRef = this.setBlockButtonRef.bind(this);
    this.onBlockButtonMouseEnter = this.onBlockButtonMouseEnter.bind(this);
    this.onBlockButtonMouseLeave = this.onBlockButtonMouseLeave.bind(this);
    this.state = { blockButtonHover: false };
  }

  componentDidMount() {
    if (this.blockButtonRef) {
      this.blockButtonRef.addEventListener(
        "mouseenter",
        this.onBlockButtonMouseEnter
      );
      this.blockButtonRef.addEventListener(
        "mouseleave",
        this.onBlockButtonMouseLeave
      );
    }
  }

  componentWillUnmount() {
    if (this.blockButtonRef) {
      this.blockButtonRef.removeEventListener(
        "mouseenter",
        this.onBlockButtonMouseEnter
      );
      this.blockButtonRef.removeEventListener(
        "mouseleave",
        this.onBlockButtonMouseLeave
      );
    }
  }

  setBlockButtonRef(element) {
    this.blockButtonRef = element;
  }

  onBlockButtonMouseEnter() {
    this.setState({ blockButtonHover: true });
  }

  onBlockButtonMouseLeave() {
    this.setState({ blockButtonHover: false });
  }

  onBlockClicked() {
    if (this.props.provider !== "preview") {
      this.props.sendUserActionTelemetry({
        event: "BLOCK",
        id: this.props.UISurface,
      });
    }

    this.props.onBlock();
  }

  onDismissClicked() {
    if (this.props.provider !== "preview") {
      this.props.sendUserActionTelemetry({
        event: "DISMISS",
        id: this.props.UISurface,
      });
    }

    this.props.onDismiss();
  }

  renderDismissButton() {
    if (this.props.footerDismiss) {
      return (
        <div className="footer">
          <div className="footer-content">
            <button
              className="ASRouterButton secondary"
              onClick={this.onDismissClicked}
            >
              {this.props.content.scene2_dismiss_button_text}
            </button>
          </div>
        </div>
      );
    }

    const label = this.props.content.block_button_text || "Remove this";
    return (
      <button
        className="blockButton"
        title={label}
        aria-label={label}
        onClick={this.onBlockClicked}
        ref={this.setBlockButtonRef}
      />
    );
  }

  render() {
    const { props } = this;
    const { blockButtonHover } = this.state;

    const containerClassName = `SnippetBaseContainer${
      props.className ? ` ${props.className}` : ""
    }${blockButtonHover ? " active" : ""}`;

    return (
      <div className={containerClassName} style={this.props.textStyle}>
        <div className="innerWrapper">{props.children}</div>
        {this.renderDismissButton()}
      </div>
    );
  }
}