TTabs is a layout system that allows users to create resizable and rearrangeable interfaces with tiling tabs. Like in VSCode.
You create an instance of TTabs, register your components (can be any Svelte component), create a layout, and add instances of your components to tabs and columns.
npm install ttabs-svelte
import { createTtabs, TtabsRoot } from 'ttabs-svelte';
// Create a ttabs instance
const ttabs = createTtabs({
// Optional: provide initial state
tiles: savedData?.tiles,
focusedTab: savedData?.focusedTab
});
<TtabsRoot {ttabs} />
TtabsRoot is the container component that renders your layout and manages the root grid.
First, register your components:
// Register components that will be used in tabs
import MyComponent from './MyComponent.svelte';
ttabs.registerComponent('my-component', MyComponent);
Then create your layout using method chaining:
// Create a new grid and build the layout using method chaining
ttabs
.newGrid()
.newRow()
.newColumn()
.newPanel()
.newTab("My Tab", true)
.setComponent("my-component", { prop1: "value1" })
.setFocused();
Once you've registered components, you can add them to tabs in various ways:
// Add component to a tab using method chaining
panel
.newTab("Tab Name", true)
.setComponent("my-component", {
prop1: "value1"
});
// Or set a component on an existing tab
const tab = ttabs.getTabObject(tabId);
tab.setComponent("my-component", { prop1: "value1" });
// Get the active panel
const activePanel = ttabs.getActivePanel();
const panel = ttabs.getPanelObject(activePanel);
// Create a new tab in a panel
panel
.newTab("New Tab", true)
.setFocused();
// Reset the entire layout
ttabs.resetTiles();
// Create a new layout from scratch
ttabs
.newGrid()
.newRow()
.newColumn()
.newPanel();
import { createTtabs, LocalStorageAdapter } from 'ttabs-svelte';
import { onMount } from 'svelte';
// Create a storage adapter with optional debounce time in ms
const storageAdapter = new LocalStorageAdapter("my-app-layout", 500);
// First, try to load saved state
const savedData = storageAdapter.load();
// Initialize ttabs with the loaded state
const ttabs = createTtabs({
// Use saved tiles if available, otherwise use empty state
tiles: savedData?.tiles,
// Use saved focused tab if available
focusedTab: savedData?.focusedTab,
});
// Connect the storage adapter to save changes
const unsubscribe = ttabs.subscribe((state) => {
storageAdapter.save(state);
});
// Register cleanup on component destroy
onMount(() => {
return () => {
// Unsubscribe from state changes when component is destroyed
unsubscribe();
};
});
TileGrid -> TileRow[] -> TileColumn[] -> TileContent | TilePanel | TileGrid
TilePanel -> TileTab[] -> TileContnet
Each level follows strict containment rules:
$state
, $effect
) for state management