2.5.2 • Published 1 month ago
@sinm/react-chrome-tabs v2.5.2
React Chrome Tabs
Installation
yarn add @sinm/react-chrome-tabs
Usage
import { Tabs, TabProperties } from "@sinm/react-chrome-tabs";
import '@sinm/react-chrome-tabs/css/chrome-tabs.css';
const [tabs, setTabs] = useState<TabProperties[]>([
{ id: "abc", favicon: fb, title: "测试", active: true },
]);
<Tabs
onTabClose={close}
onTabReorder={reorder}
onTabActive={active}
tabs={tabs}
/>
Example
import React, { useEffect } from "react";
import { useState } from "react";
import { Tabs, TabProperties } from "@sinm/react-chrome-tabs";
import '@sinm/react-chrome-tabs/css/chrome-tabs.css';
// for dark mode
import '@sinm/react-chrome-tabs/css/chrome-tabs-dark-theme.css';
import fb from "./images/facebook-favicon.ico";
import google from "./images/google-favicon.ico";
let id = 1;
function App() {
const [tabs, setTabs] = useState<TabProperties[]>([
{ id: "abc", favicon: fb, title: "测试", active: true },
]);
const [draggable, setDraggable] = useState(true);
const addTab = () => {
id++;
setTabs([
...tabs,
{
id: `tab-id-${id}`,
title: `New Tabs ${id}`,
favicon: tabs.length % 2 ? fb : google,
},
]);
};
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);
setTabs(newTabs);
};
const closeAll = () => setTabs([]);
return (
<div>
<Tabs
darkMode={false}
onTabClose={close}
draggable={draggable}
onTabReorder={reorder}
onTabActive={active}
tabs={tabs}
pinnedRight={<button onClick={addTabWithIcon}>+</button>}
></Tabs>
<button onClick={addTab}>Add Tab</button>
<button onClick={closeAll}>Close All</button>
</div>
);
}
More Examples see
Component Props
name | type | description | |
---|---|---|---|
darkMode | boolean | Enables or disables dark mode. | |
className | string | Custom class name for the tabs container. | |
tabs | TabProperties | Array of tab data to be rendered. | |
draggable | boolean | Determines whether tabs can be dragged for reordering,default true. | |
onTabActive | Function | Callback function triggered when a tab becomes active. | |
onTabClose | Function | Callback function triggered when a tab is closed. | |
onTabReorder | Function | Callback function triggered when tabs are reordered via drag-and-drop. | |
onContextMenu | Function | Callback function triggered when the context menu event is invoked. |
TabProperties
export interface TabProperties {
id: string;
title: string;
active?: boolean;
// favicon background image
favicon?: boolean | string;
// favicon class
faviconClass?: string;
// default true
isCloseIconVisible?: boolean;
}
Run Demo
git clone https://github.com/pansinm/react-chrome-tabs.git
cd react-chrome-tabs
yarn start
# visit http://localhost:8080/
2.5.0
1 month ago
2.5.2
1 month ago
2.5.1
1 month ago
2.4.5
7 months ago
2.4.6
7 months ago
2.4.4
8 months ago
2.4.1
8 months ago
2.4.0
8 months ago
2.4.3
8 months ago
2.4.2
8 months ago
2.3.0
1 year ago
2.3.2
1 year ago
2.3.1
1 year ago
2.3.3
1 year ago
2.2.0
1 year ago
2.1.2
3 years ago
2.1.1
3 years ago
2.1.3
3 years ago
2.1.0
3 years ago
2.0.0
3 years ago
1.2.13
4 years ago
1.2.16
4 years ago
1.2.14
4 years ago
1.2.15
4 years ago
1.2.12
4 years ago
1.2.10
4 years ago
1.2.11
4 years ago
1.2.6
4 years ago
1.2.9
4 years ago
1.2.0
4 years ago
1.1.0
4 years ago
1.2.5
4 years ago
1.2.4
4 years ago
1.2.3
4 years ago
1.2.1
4 years ago
1.0.2
4 years ago
1.0.1
4 years ago
1.0.0
4 years ago