-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathicon-container.tsx
More file actions
86 lines (73 loc) · 2.1 KB
/
icon-container.tsx
File metadata and controls
86 lines (73 loc) · 2.1 KB
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
"use client";
import React, { useState } from "react";
import { IconType } from "react-icons";
import { MdFormatListBulleted, MdHistory, MdOutlineWarningAmber } from "react-icons/md";
import { RiErrorWarningLine } from "react-icons/ri";
import { InternalError } from "../error-boundary";
import styles from "./icon-container.module.css";
/**
* React Client Component that renders the icon for the provided
* `iconName` inside a styled `<div>`.
*
* Currently supports `"history"` and `"bullet-list"` as `iconName`
* values; if provided with something else, will throw an
* `InternalError`
*/
export default function IconContainer({
color,
handleClick,
hoverColor,
iconName,
text,
}: {
/** string value for CSS `color` */
color?: string;
/** callback for `onClick` handler */
handleClick?: () => void;
/** string value for CSS `color` when element is hovered over */
hoverColor?: string;
/** name of icon to display */
iconName: string;
/** text to display below icon */
text: string;
}): React.ReactElement {
const [hovered, setHovered] = useState(false);
const defaultColor = "#aaa";
const defaultHoverColor = "rgb(79, 75, 80)";
const hasOnClick = typeof handleClick === "function";
const col = hovered ? hoverColor || defaultHoverColor : color || defaultColor;
const cursor = hasOnClick ? "pointer" : "auto";
const iconProps = { size: "1.2em", color: col };
let Icon: IconType;
switch (iconName) {
case "history":
Icon = MdHistory;
break;
case "bullet-list":
Icon = MdFormatListBulleted;
break;
case "restricted":
Icon = RiErrorWarningLine;
break;
case "out-of-date":
Icon = MdOutlineWarningAmber;
break;
default:
throw new InternalError(`${iconName} is not a valid icon name`);
}
return (
<div
className={styles.iconWrapper}
onClick={hasOnClick ? handleClick : undefined}
onMouseOut={() => setHovered(false)}
onMouseOver={() => setHovered(true)}
style={{
color: col,
cursor,
}}
>
<Icon {...iconProps} />
{text}
</div>
);
}