2.5.2 • Published 1 month ago

@sinm/react-chrome-tabs v2.5.2

Weekly downloads
-
License
MIT
Repository
github
Last release
1 month ago

React Chrome Tabs

npm.io

codesandbox

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

nametypedescription
darkModebooleanEnables or disables dark mode.
classNamestringCustom class name for the tabs container.
tabsTabPropertiesArray of tab data to be rendered.
draggablebooleanDetermines whether tabs can be dragged for reordering,default true.
onTabActiveFunctionCallback function triggered when a tab becomes active.
onTabCloseFunctionCallback function triggered when a tab is closed.
onTabReorderFunctionCallback function triggered when tabs are reordered via drag-and-drop.
onContextMenuFunctionCallback 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