summaryrefslogtreecommitdiffstats
path: root/browser/components/newtab/content-src/components/DiscoveryStreamComponents/FeatureHighlight/FeatureHighlight.jsx
blob: 792be40ba3e9803f2d59dc1b8bad158a804b70a8 (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
/* 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, { useState, useCallback, useRef, useEffect } from "react";
import { actionCreators as ac } from "common/Actions.sys.mjs";

export function FeatureHighlight({
  message,
  icon,
  toggle,
  position = "top-left",
  title,
  ariaLabel,
  feature = "FEATURE_HIGHLIGHT_DEFAULT",
  dispatch = () => {},
  windowObj = global,
}) {
  const [opened, setOpened] = useState(false);
  const ref = useRef(null);

  useEffect(() => {
    const handleOutsideClick = e => {
      if (!ref?.current?.contains(e.target)) {
        setOpened(false);
      }
    };

    windowObj.document.addEventListener("click", handleOutsideClick);
    return () => {
      windowObj.document.removeEventListener("click", handleOutsideClick);
    };
  }, [windowObj]);

  const onToggleClick = useCallback(() => {
    if (!opened) {
      dispatch(
        ac.DiscoveryStreamUserEvent({
          event: "CLICK",
          source: "FEATURE_HIGHLIGHT",
          value: {
            feature,
          },
        })
      );
    }
    setOpened(!opened);
  }, [dispatch, feature, opened]);

  const openedClassname = opened ? `opened` : `closed`;
  return (
    <div ref={ref} className="feature-highlight">
      <button
        title={title}
        aria-haspopup="true"
        aria-label={ariaLabel}
        className="toggle-button"
        onClick={onToggleClick}
      >
        {toggle}
      </button>
      <div className={`feature-highlight-modal ${position} ${openedClassname}`}>
        <div className="message-icon">{icon}</div>
        <p>{message}</p>
        <button
          title="Dismiss"
          aria-label="Close sponsored content more info popup"
          className="icon icon-dismiss"
          onClick={() => setOpened(false)}
        ></button>
      </div>
    </div>
  );
}