How to create a tabs component in SvelteJS: Step-by-step Tutorial

Mark Caggiano
3 min readApr 30, 2023

Sure, I can help you with that. Here’s a step-by-step tutorial with code examples on how to create a tabs component in SvelteJS:

Step 1: Create the Tab Component

First, let’s create a Tab component that will represent a single tab. This component should accept a title prop that will be displayed as the tab's label, and a selected prop that will be used to determine if the tab is currently selected.

<!-- Tab.svelte -->
<div class="tab" class:selected={selected} on:click={handleClick}>
<span>{title}</span>
</div>

<script>
export let title;
export let selected;
export let index;

function handleClick() {
// Emit an event to the parent component indicating that this tab was clicked
const event = new CustomEvent('tabClick', {
detail: { index }
});
dispatchEvent(event);
}
</script>

<style>
.tab {
cursor: pointer;
padding: 10px 15px;
border: 1px solid #ccc;
border-bottom: none;
}
.tab:last-child {
border-bottom: 1px solid #ccc;
}
.tab.selected {
background-color: #eee;
}
</style>

Sure, I can help you with that. Here’s a step-by-step tutorial with code examples on how to create a tabs component in SvelteJS:

Step 1: Create the Tab Component

--

--