Tabs
The tabs component is part of the Gameface custom components suite. As most of the components in this suite, it uses slots to allow dynamic content.
Installation
npm i coherent-gameface-tabs
Usage
Usage with UMD:
<script src="./node_modules/coherent-gameface-tabs/dist/tabs.production.min.js"></script>
- add the tabs component to your html:
<gameface-tabs></gameface-tabs>
This is all! Load the file in Gameface to see the tabs.
Usage with JavaScript:
If you wish to import the Tabs using JavaScript you can remove the script tag and import it like this:
import { Tabs } from 'coherent-gameface-modal';
or simply
import 'coherent-gameface-modal';
Note that this approach requires a module bundler like Webpack or Rollup to resolve the modules from the node_modules folder.
Customizing the Tabs
Use the slots to put customized background or label.
The tabs component has two slots:
- tab - this is where the tab headings are added.
- panel - this is where the corresponding panels are added.
Use the <component-import>
to import the tabs. Put any custom slots as children of the <component-import>
.
<gameface-tabs>
<tab-heading slot="tab">Chapter One</tab-heading>
<tab-panel slot="panel">Chapter One Content</tab-panel>
<tab-heading slot="tab">Chapter Two</tab-heading>
<tab-panel slot="panel">Chapter Two Content</tab-panel>
<tab-heading slot="tab">Chapter Three</tab-heading>
<tab-panel slot="panel">Chapter Three Content</tab-panel>
<tab-heading slot="tab">Chapter Four</tab-heading>
<tab-panel slot="panel">Chapter Four Content</tab-panel>
</gameface-tabs>
You can put any custom styles inline or use class names and add an external file.
The tabs component registers two more custom elements - The <tab-heading>
and <tab-panel>
. Every time you create an element of one of these types an index is assigned to it. This index ensures that your heading will be linked to its corresponding panel. This means that you can add new tabs and panels like this and they will work:
const tab = document.createElement('tab-heading');
tab.innerText = 'My new tab.';
tab.slot = 'tab';
document.querySelector('[data-name="tab"]').appendChild(tab);
const panel = document.createElement('tab-panel');
panel.innerText = 'My new panel.';
panel.slot = 'panel';
document.querySelector('[data-name="panel"]').appendChild(panel);
Add the Styles
<link rel="stylesheet" href="coherent-gameface-components-theme.css">
<link rel="stylesheet" href="styles.css">
To overwrite the default styles, simply create new rules for the class names that you wish to change and include them after the default styles.
Selecting a tab
To set a tab as the default, add the selected
attribute to the tab-heading
that needs to be selected and its corresponding tab-panel
.
<gameface-tabs>
<tab-heading selected slot="tab">Chapter One</tab-heading>
<tab-panel selected slot="panel">Chapter One Content</tab-panel>
<tab-heading slot="tab">Chapter Two</tab-heading>
<tab-panel slot="panel">Chapter Two Content</tab-panel>
<tab-heading slot="tab">Chapter Three</tab-heading>
<tab-panel slot="panel">Chapter Three Content</tab-panel>
<tab-heading slot="tab">Chapter Four</tab-heading>
<tab-panel slot="panel">Chapter Four Content</tab-panel>
</gameface-tabs>
Ensure that only one tab-heading
and one tab-panel
have the selected
attribute. Adding it to multiple tabs will cause more than one tab to be marked as selected, disrupting the visual display.
To select another tab using JavaScript, use the selectTab
method. Pass a tab-heading
as an argument, and it will open the corresponding tab-panel
.
const tabs = document.querySelector('gameface-tabs');
const newTab = document.querySelectorAll('tab-heading')[1];
tabs.selectTab(newTab);