brrd / electron-tabs

Tab component for Electron
MIT License
697 stars 130 forks source link
electron tab tabs typescript webcomponent webview

electron-tabs is discontinued

Thank you for your support and contributions all these years.


electron-tabs

Simple tabs for Electron applications

Electron Tab Demo

Features

Installation

npm install --save electron-tabs

Getting started

Define the following webPreferences options in the main process:

const mainWindow = new electron.BrowserWindow({
  webPreferences: {
    webviewTag: true
  }
});

Then add the following markup where you want the tabs to display:

<tab-group></tab-group>

<script src="https://github.com/brrd/electron-tabs/raw/master/node_modules/electron-tabs/dist/electron-tabs.js"></script>

Options

You can add options by setting <tab-group> element attributes:

<tab-group new-tab-button="true" sortable="true"></tab-group>

The following attributes are supported:

Methods

Use TabGroup methods and manipulate tabs in a script after calling electron-tabs.js.

<tab-group new-tab-button="true"></tab-group>

<script src="https://github.com/brrd/electron-tabs/raw/master/path/to/electron-tabs.js"></script>

<script>
  // Select tab-group
  const tabGroup = document.querySelector("tab-group");

  // Setup the default tab which is created when the "New Tab" button is clicked
  tabGroup.setDefaultTab({
    title: "New Page",
    src: "path/to/new-page.html",
    active: true
  });

  // Do stuff
  const tab = tabGroup.addTab({
    title: "electron-tabs on NPM",
    src: "https://www.npmjs.com/package/electron-tabs"
  });
  const pos = tab.getPosition();
  console.log("Tab position is " + pos);
</script>

TabGroup

tabGroup.addTab(options)

Add a new tab and returns the related Tab instance.

tabGroup.setDefaultTab(options)

Define default options to use for creating the tab when the "New Tab" button is clicked or when calling tabGroup.addTab() with no parameter.

tabGroup.setDefaultTab({
  title: "New Page",
  src: "path/to/new-page.html",
  active: true
});

tabGroup.getTab(id)

Retrieve an instance of Tab from this id (return null if not found).

tabGroup.getTabByPosition(position)

Retrieve an instance of Tab from this position (return null if not found). A negative value is an offset from the right.

To get the tab in the leftmost position:

tabGroup.getTabByPosition(1);

To get the tab in the rightmost position:

tabGroup.getTabByPosition(-1);

tabGroup.getTabByRelPosition(position)

Retrieve an instance of Tab from this position relative to the active tab (return null if not found).

tabGroup.getNextTab() is an alias to tabGroup.getTabByRelPosition(1).

tabGroup.getPreviousTab() is an alias to tabGroup.getTabByRelPosition(-1).

tabGroup.getActiveTab()

Return the active tab (return null if none).

tabGroup.getTabs()

Return all registered tabs.

tabGroup.eachTab(fn, thisArg)

Loop through the list of tabs in tabGroup and execute the fn function for each tab. fn is called with the following parameters:

thisArg (optional) is the value to use as this when executing fn.

Tab

Instances of Tab are returned by the tabGroup.addTab() method.

tab.setTitle(title)

Set tab title.

tab.getTitle()

Get current tab title.

tab.setBadge(badge)

Set tab badge.

tab.getBadge()

Get current tab badge.

tab.setIcon (iconURL, icon)

Set tab icon (a iconURL or an icon must be given).

tab.getIcon()

Get current tab icon URL / icon.

tab.setPosition(newPosition)

Move tab to the specified position. See tabGroup.getTabByPosition for information about positions.

tab.getPosition(fromRight)

Get the tab position. If fromRight is true the index returned is negative and is the offset from the right.

tab.activate()

Activate this tab. The class "active" is added to the active tab.

tab.show(flag)

Toggle the "visible" class on the tab. tab.hide() is an alias to tab.show(false).

tab.hasClass(classname)

Return true if the tab element has the specified classname. Useful for checking if a tab is "active" or "visible".

tab.close(force)

Close the tab (and activate another tab if relevant). When force is set to true the tab will be closed even if it is not closable.

Events

The following events are emitted:

You can also use tab.once to automatically remove the listener when invoked:

Access Electron webview element

You can access the webview element and use its methods with through the Tab.webview attribute. See webview documentation.

let webview = tab.webview;
webview.loadURL("file://path/to/new/page.html");

Custom styles

To customize tab-group styles, set new values to electron-tabs CSS variables in your application stylesheet.

Since TabGroup is a Web Component you won't be able to change its styles directly from your app stylesheet. If you need more control over it then you can add a <style> tag inside the <tab-group > element:

<tab-group new-tab-button="true" sortable="true">
  <style>
    /* Write your own CSS rules here... */
  </style>
</tab-group>

This method is particularly useful when you need to define custom badges or tab styles:

<tab-group new-tab-button="true" sortable="true">
  <style>
    /* Add custom styles */
    .my-badge {
      background-color: orange;
    }
    .my-custom-tab {
      color: red;
      font-weight: bold;
    }
  </style>
</tab-group>

<script src="https://github.com/brrd/electron-tabs/raw/master/path/to/electron-tabs.js"></script>

<script>
  const tabGroup = document.querySelector("tab-group");

  tabGroup.addTab({
    title: "Tab with custom badge",
    src: "page.html",
    badge: {
      text: "5",
      classname: "my-badge"
    }
  });

  tabGroup.addTab({
    title: "Tab with custom style",
    src: "page.html",
    ready: function(tab) {
      tab.element.classList.add("my-custom-tab");
    }
  });
</script>

Development

electron-tabs uses TypeScript and Parcel under the hood.

Requirements

Git and Node 12+.

Build

# Clone this repo
git clone git@github.com:brrd/electron-tabs.git
cd electron-tabs

# Install dependencies
npm install

# Build
npm run build

# ...or watch
npm run watch

Demo

npm run demo

License

The MIT License (MIT) - Copyright (c) 2022 Thomas Brouard