forked from adamschwartz/chrome-tabs
-
Notifications
You must be signed in to change notification settings - Fork 13
/
index.tsx
105 lines (92 loc) · 2.67 KB
/
index.tsx
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
import React, { useEffect } from "react";
import { useState } from "react";
import { render } from "react-dom";
import { Tabs } from "../src";
import { TabProperties } from "../src/chrome-tabs";
import "../css/chrome-tabs.css";
import "../css/chrome-tabs-dark-theme.css";
import "./css/demo.css";
import fb from "./images/facebook-favicon.ico";
import google from "./images/google-favicon.ico";
let id = 0;
function App() {
const [tabs, setTabs] = useState<TabProperties[]>([
{ id: "abc", favicon: fb, title: "测试", active: true },
]);
const [darkMode, setDarkMode] = useState(false);
const addTabWithIcon = () => {
id++;
setTabs([
...tabs.map((i) => ({ ...i, active: false })),
{
id: `tab-id-${id}`,
title: `New Tabs ${id}`,
favicon: tabs.length % 2 ? fb : google,
active: true,
},
]);
};
const addTabWithIconClass = () => {
id++;
setTabs([
...tabs.map((i) => ({ ...i, active: false })),
{
id: `tab-id-${id}`,
title: `New Tabs ${id}`,
faviconClass: " emoji",
active: true,
},
]);
};
const addTabWithoutClose = () => {
id++;
setTabs([
...tabs.map((i) => ({ ...i, active: false })),
{
id: `tab-id-${id}`,
title: `New Tabs ${id}`,
faviconClass: " emoji",
isCloseIconVisible: false,
active: true,
},
]);
};
const active = (id: string) => {
setTabs(tabs.map((tab) => ({ ...tab, active: id === tab.id })));
};
const close = (id: string) => {
setTabs(tabs.filter((tab) => tab.id !== id));
};
const reorder = (tabId: string, fromIndex: number, toIndex: number) => {
const beforeTab = tabs.find((tab) => tab.id === tabId);
if (!beforeTab) {
return;
}
let newTabs = tabs.filter((tab) => tab.id !== tabId);
newTabs.splice(toIndex, 0, beforeTab);
console.log(newTabs);
setTabs(newTabs);
};
const closeAll = () => setTabs([]);
const toggleDarkMode = () => {
setDarkMode((darkMode) => !darkMode);
};
return (
<div>
<Tabs
darkMode={darkMode}
onTabClose={close}
onTabReorder={reorder}
onTabActive={active}
tabs={tabs}
pinnedRight={<button onClick={addTabWithIcon}>+</button>}
></Tabs>
<button onClick={addTabWithIcon}>Add Tab with icon</button>
<button onClick={addTabWithIconClass}>Add Tab with iconClass</button>
<button onClick={addTabWithoutClose}>Add Tab without close icon</button>
<button onClick={closeAll}>Close All</button>
<button onClick={toggleDarkMode}>Dark Mode</button>
</div>
);
}
render(<App />, document.getElementById("root"));