File Tabs
For multitasking UI/UX
by
Read Write Tools May 4, 2020
Abstract |
---|
The rwt-file-tabs web component is a thin bar containing named file tabs. It is typically used with apps that allow multiple files to be worked on simultaneously. |
Motivation
The rwt-file-tabs web component can be used in scenarios
with multiple views or multiple work tasks.
This component can be configured with a fixed set of tabs which are slotted into
the component when designing the document. Alternatively, its programming API
allows tabs to be created and removed as needed, when working with a dynamic set
of tabs.
Tabs can be configured using CSS to adjust their size and typography.
The tab bar occupies a fixed width. When the size of all tabs exceeds that
width, scroll buttons are automatically enabled.
Dynamic tabs can be closable
or not, based on an attribute set on
the web component by the developer.
Prerequisites
The rwt-file-tabs web component works in any browser that
supports modern W3C standards. Templates are written using BLUE
PHRASE notation, which can be compiled into HTML using the free Read Write View
desktop app. It has no other prerequisites. Distribution and installation are
done with either NPM or via Github.
Installation using NPM
If you are familiar with Node.js and the package.json
file, you'll be
comfortable installing the component just using this command:
npm install rwt-file-tabs
If you are a front-end Web developer with no prior experience with NPM, follow
these general steps:
- Install Node.js/NPM
on your development computer.
- Create a
package.json
file in the root of your web project using the command:
npm init
- Download and install the web component using the command:
npm install rwt-file-tabs
Important note: This web component uses Node.js and NPM and package.json
as a
convenient distribution and installation mechanism. The web component itself
does not need them.
Installation using Github
If you are more comfortable using Github for installation, follow these steps:
- Create a directory
node_modules
in the root of your web project. - Clone the rwt-file-tabs web component into it using the
command:
git clone https://github.com/readwritetools/rwt-file-tabs.git
Using the web component
After installation, you need to add a few things to your HTML page to make use
of it.
- Add a
script
tag to load the component's rwt-file-tabs.js
file:
<script src='/node_modules/rwt-file-tabs/rwt-file-tabs.js' type=module></script>
Slotted usage
If the component is to be used with a predetermined set of tabs, they can be
slotted in. Here's an example:
<rwt-file-tabs role=contentinfo>
<button slot=tabitem id=tab1 title='Read only view'>READ</button>
<button slot=tabitem id=tab2 title='Text editing view'>WRITE</button>
<button slot=tabitem id=tab3 title='Web browser preview'>PREVIEW</button>
</rwt-file-tabs>
Programmatic API
If the component is to be used with a dynamic set of tabs, they can be added and
removed using these methods.
insertTab(id, value, title)
- The
id
should be a unique identifier. It will be used for manipulating the tab.
- The
value
is the text to display on the tab. Text that is too long will be ellipsed.
- The
title
parameter is optional, and if provided will be used as the hover tooltip.
removeTab(id)
- The
id
is the identifier of the tab to remove.
getCurrentTab()
- Returns an object with two values:
currentTabId
and currentTabValue
.
setCurrentTab(id)
- Changes the current tab to be the one identified with
id
.
getScrollableOverflow()
- The size, in pixels, of the tabs that are hidden when the component isn't wide enough for all of them.
setScrollPosition(left)
- The
left
parameter is the number of pixels to programmatically scroll the tabs. It should be an integer from 0 to scrollableOverflow
.
scrollMaxRight()
- Scroll the tabs all the way to the right. This is only meaningful when there is scrollable overflow.
Events
The component issues life-cycle events.
component-loaded
- Sent when the component is fully loaded and ready to be used.
tab-activated
- Sent when the user switches to a new tab. The
event
argument has a detail
property containing the currentTabId
and currentTabValue
of the newly activated tab.
tab-closing
- Sent when the user clicks on the 'x' to close a tab. This event allows the developer to determine if it's safe to close the tab. Use
preventDefault()
to cancel the operation. The event
argument has a detail
property containing the currentTabId
and currentTabValue
of the item about to be closed.
Styling
Tab styling
The component can be styled with these CSS variables:
rwt-file-tabs {
--width: 100%;
--height: 2rem;
--nav-button-size: 1.6rem;
}
The tabs can be styled with these CSS variables:
rwt-file-tabs {
--font-weight: normal;
--letter-spacing: 0px;
--text-align: left;
--min-width: 2rem;
--max-width: 6rem;
}
Dialog color scheme
The default color palette for the component uses a dark mode theme. You can use
CSS to override the variables' defaults:
rwt-file-tabs {
--color: var(--white);
--accent-color1: var(--pure-white);
--background: var(--black);
--accent-background1: var(--pure-black);
--accent-background2: var(--nav-black);
--accent-background3: var(--medium-black);
--accent-background4: var(--gray);
}
Vertically oriented tabs
The component can be oriented vertically by wrapping it in a positioned element
and using a CSS transform. Here's an example of how to do it:
HTML
<div id=viewtabs-area>
<rwt-file-tabs id=viewtabs scroll-side=left anchor-side=right>
<button id=id4 slot=tabitem title='Delta view'>Delta</button>
<button id=id3 slot=tabitem title='Gamma view'>Gamma</button>
<button id=id2 slot=tabitem title='Beta view'>Beta</button>
<button id=id1 slot=tabitem title='Alpha view'>Alpha</button>
</rwt-file-tabs>
</div>
CSS
div#viewtabs-area {
position: absolute;
top: 2.3rem;
left: 0;
bottom: 0;
width: 2.0rem;
border-top: 1px solid #000;
border-bottom: 1px solid #000;
}
#viewtabs {
position: absolute;
transform: rotate(-90deg);
transform-origin: left top;
width: 34rem;
top: 34rem;
}
JavaScript
function onResize() {
var viewTabsArea = document.getElementById('viewtabs-area');
var viewTabs = document.getElementById('viewtabs');
var height = viewTabsArea.offsetHeight;
viewTabs.style.width = `${height}px`;
viewTabs.style.top = `${height}px`;
}
window.addEventListener('resize', onResize);
License
The rwt-file-tabs web component is licensed under the MIT
License.
MIT License
Copyright © 2020 Read Write Tools.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Availability