CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
sagemathinc

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/frontend/app/nav-tab.tsx
Views: 687
1
/*
2
* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.
3
* License: MS-RSL – see LICENSE.md for details
4
*/
5
6
import { Tooltip } from "antd";
7
8
import { CSS, React, useActions } from "@cocalc/frontend/app-framework";
9
import { Icon, IconName } from "@cocalc/frontend/components";
10
import track from "@cocalc/frontend/user-tracking";
11
import { COLORS } from "@cocalc/util/theme";
12
import { TOP_BAR_ELEMENT_CLASS } from "./top-nav-consts";
13
14
const ACTIVE_BG_COLOR = COLORS.TOP_BAR.ACTIVE;
15
16
interface Props {
17
//close?: boolean;
18
active_top_tab?: string;
19
add_inner_style?: CSS;
20
children?: React.ReactNode;
21
hide_label?: boolean;
22
icon?: IconName | JSX.Element;
23
is_project?: boolean;
24
label_class?: string;
25
label?: string | JSX.Element;
26
name?: string;
27
on_click?: () => void;
28
style?: CSS;
29
tooltip?: string;
30
}
31
32
export const NavTab: React.FC<Props> = React.memo((props: Props) => {
33
const {
34
//close,
35
active_top_tab,
36
add_inner_style = {},
37
children,
38
hide_label,
39
icon,
40
is_project,
41
label_class,
42
label,
43
name,
44
on_click,
45
style = {},
46
tooltip,
47
} = props;
48
const page_actions = useActions("page");
49
50
function render_label() {
51
if (!hide_label && label != null) {
52
return (
53
<span
54
style={{ marginLeft: "5px" }}
55
className={label_class}
56
cocalc-test={name}
57
>
58
{label}
59
</span>
60
);
61
}
62
}
63
64
function render_icon() {
65
if (icon != null) {
66
if (typeof icon === "string") {
67
return <Icon name={icon} style={{ fontSize: "20px" }} />;
68
} else {
69
return icon;
70
}
71
}
72
}
73
74
function onClick() {
75
on_click?.();
76
77
if (is_project) {
78
track("top_nav", {
79
name: "project",
80
project_id: name,
81
});
82
} else {
83
track("top_nav", {
84
name: name ?? label,
85
});
86
}
87
88
if (name != null) {
89
page_actions.set_active_tab(name);
90
}
91
}
92
93
const is_active = active_top_tab === name;
94
95
const outer_style: CSS = {
96
fontSize: "14px",
97
cursor: "pointer",
98
float: "left",
99
flex: "0 0 auto",
100
display: "flex",
101
border: "none",
102
...style,
103
...(is_active && { backgroundColor: ACTIVE_BG_COLOR }),
104
};
105
106
const inner_style: CSS = {
107
padding: "12px",
108
display: "flex",
109
flexDirection: "row",
110
verticalAlign: "middle",
111
alignItems: "center",
112
whiteSpace: "nowrap",
113
...add_inner_style,
114
};
115
116
function renderInner(): JSX.Element {
117
const inner = (
118
<div style={inner_style}>
119
{render_icon()}
120
{render_label()}
121
{children}
122
</div>
123
);
124
if (tooltip != null) {
125
return (
126
<Tooltip
127
title={tooltip}
128
mouseEnterDelay={1}
129
mouseLeaveDelay={0}
130
placement="bottom"
131
>
132
{inner}
133
</Tooltip>
134
);
135
} else {
136
return inner;
137
}
138
}
139
140
return (
141
<div
142
onClick={onClick}
143
style={outer_style}
144
className={TOP_BAR_ELEMENT_CLASS}
145
>
146
{renderInner()}
147
</div>
148
);
149
});
150
151