Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions css/_tabs.scss
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,28 @@
@include tabs-icon;
}

/// Tab full-width styles
/// @access private
/// @group components
@mixin tabs-full-width {
.#{$prefix}--tabs--full-width .#{$prefix}--tabs__nav {
width: 100%;
}

.#{$prefix}--tabs--full-width .#{$prefix}--tabs__nav-item {
flex: 1;
}

.#{$prefix}--tabs--full-width .#{$prefix}--tabs__nav-link {
width: 100%;
max-width: none;
}
}

@include exports('tabs-full-width') {
@include tabs-full-width;
}

/// Tab container panel background
/// @access private
/// @group components
Expand Down
2 changes: 1 addition & 1 deletion css/all.css

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion css/g10.css

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion css/g100.css

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion css/g80.css

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion css/g90.css

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion css/white.css

Large diffs are not rendered by default.

15 changes: 15 additions & 0 deletions docs/src/pages/components/Tabs.svx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,21 @@ By default, each tab has a fixed width of `10rem`. Set `autoWidth` to `true` for
</svelte:fragment>
</Tabs>

## Full-width

Set `fullWidth` to `true` for tabs to evenly span the full width of the container.

<Tabs fullWidth>
<Tab label="Tab label 1" />
<Tab label="Tab label 2" />
<Tab label="Tab label 3" />
<svelte:fragment slot="content">
<TabContent>Content 1</TabContent>
<TabContent>Content 2</TabContent>
<TabContent>Content 3</TabContent>
</svelte:fragment>
</Tabs>

## Reactive example

Use `bind:selected` to create a two-way binding with the selected tab index. This allows you to programmatically control the selected tab and react to tab changes.
Expand Down
13 changes: 10 additions & 3 deletions src/Tabs/Tab.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,15 @@

import { getContext, onMount } from "svelte";

const { selectedTab, useAutoWidth, add, remove, update, change } =
getContext("carbon:Tabs");
const {
selectedTab,
useAutoWidth,
useFullWidth,
add,
remove,
update,
change,
} = getContext("carbon:Tabs");

add({ id, label, disabled });

Expand Down Expand Up @@ -87,7 +94,7 @@
{id}
{href}
class:bx--tabs__nav-link={true}
style:width={$useAutoWidth ? "auto" : undefined}
style:width={$useFullWidth ? "100%" : $useAutoWidth ? "auto" : undefined}
>
<slot>{label}</slot>
{#if icon}
Expand Down
10 changes: 10 additions & 0 deletions src/Tabs/Tabs.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
/** Set to `true` for tabs to have an auto-width */
export let autoWidth = false;

/** Set to `true` for tabs to span the full width of the container */
export let fullWidth = false;

/**
* Specify the ARIA label for the chevron icon.
* @type {string}
Expand Down Expand Up @@ -44,6 +47,10 @@
* @type {import("svelte/store").Writable<boolean>}
*/
const useAutoWidth = writable(autoWidth);
/**
* @type {import("svelte/store").Writable<boolean>}
*/
const useFullWidth = writable(fullWidth);
/**
* @type {import("svelte/store").Writable<string | undefined>}
*/
Expand Down Expand Up @@ -152,6 +159,7 @@
selectedTab,
selectedContent,
useAutoWidth,
useFullWidth,
add,
remove,
addContent,
Expand Down Expand Up @@ -228,13 +236,15 @@
dropdownHidden = true;
}
$: useAutoWidth.set(autoWidth);
$: useFullWidth.set(fullWidth);
</script>

<div
bind:this={refRoot}
role="navigation"
class:bx--tabs={true}
class:bx--tabs--container={type === "container"}
class:bx--tabs--full-width={fullWidth}
{...$$restProps}
>
<div
Expand Down
2 changes: 2 additions & 0 deletions tests/Tabs/Tabs.test.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
export let selected = 0;
export let type: "default" | "container" = "default";
export let autoWidth = false;
export let fullWidth = false;
export let iconDescription = "Show menu options";
export let triggerHref = "#";
export let customClass = "";
Expand All @@ -14,6 +15,7 @@
{selected}
{type}
{autoWidth}
{fullWidth}
{iconDescription}
{triggerHref}
class={customClass}
Expand Down
12 changes: 12 additions & 0 deletions tests/Tabs/Tabs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ describe("Tabs", () => {
expect(screen.getByText("Content 1")).toBeVisible();
expect(screen.getByText("Content 2")).not.toBeVisible();
expect(screen.getByText("Content 3")).not.toBeVisible();

const tabsContainer = screen.getByRole("navigation");
expect(tabsContainer).not.toHaveClass("bx--tabs--full-width");
});

it("should select initial tab based on selected prop", () => {
Expand Down Expand Up @@ -105,6 +108,15 @@ describe("Tabs", () => {
}
});

it("should render full-width tabs when fullWidth is true", () => {
render(Tabs, {
props: { fullWidth: true },
});

const tabsContainer = screen.getByRole("navigation");
expect(tabsContainer).toHaveClass("bx--tabs--full-width");
});

it("should show dropdown on trigger click", async () => {
render(Tabs);

Expand Down
Loading