Skip to content

Commit e28ffa6

Browse files
committed
chore: polish the example and update article content
1 parent e6b4284 commit e28ffa6

File tree

1 file changed

+60
-63
lines changed

1 file changed

+60
-63
lines changed

knowledge-base/tabstrip-add-remove-tabs.md

Lines changed: 60 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -24,44 +24,25 @@ previous_url: /knowledge-base/tabstrip-dynamic-tabs
2424

2525
## Description
2626

27-
I have a collection of items representing separate tabs. I am iterating through that collection to render a tab for each item as shown [here](slug:tabstrip-tabs-collection). I want to allow the user to add and remove tabs, how to achieve that?
27+
I have a collection of items representing separate tabs. I am iterating through that collection to render a tab for each item as shown in the [Tabs Collection article](slug:tabstrip-tabs-collection). I want to allow the user to add and remove tabs. How to achieve that?
2828

29-
This KB article answers the following questions:
29+
This KB article also answers the following questions:
3030

31-
* How to remove a tab with a "X" button in the tab header?
32-
* How to use a button to add tabs and position this button next to the last tab header (similar to the "+" button in the browser)?
31+
* How to implement add and remove tab functionality with the Telerik TabStrip component.
32+
* How to remove a tab using an "X" button in the tab header.
33+
* How to add a new tab with a "+" button, similar to browser tab controls.
34+
* How to position the add ("+") button next to the last tab header.
3335

3436
## Solution
3537

36-
The example below shows how to:
37-
* Use a [`HeaderTemplate`](slug:tabstrip-header-template) for the tab to add a button that removes the tab.
38-
* Conditionally display the "X" button based on the tabs' count.
39-
* Declare a button for adding tabs.
38+
* Use a [`HeaderTemplate`](slug:tabstrip-header-template) for the tab to add an "X" button.
39+
* Display the "X" button only when there is more than one tab.
40+
* Declare a "+" button for adding tabs.
4041
* Use custom styling and JavaScript to position the "+" button next to the last tab header.
4142

4243
````RAZOR
4344
@inject IJSRuntime JS
4445
45-
<style>
46-
.dynamic-tabstrip-wrapper {
47-
--add-tab-button-size: 32.8px;
48-
}
49-
50-
.dynamic-tabstrip-wrapper .k-tabstrip-items {
51-
max-width: calc(100% - var(--add-tab-button-size));
52-
}
53-
54-
.dynamic-tabstrip-wrapper .add-tab-button {
55-
width: var(--add-tab-button-size);
56-
padding-block: 6px;
57-
z-index: 10000;
58-
}
59-
60-
.remove-tab-button {
61-
padding: 0 !important;
62-
}
63-
</style>
64-
6546
<div class="dynamic-tabstrip-wrapper k-relative">
6647
<TelerikButton OnClick="@AddTab"
6748
Class="add-tab-button !k-absolute k-z-10 k-ratio-1"
@@ -94,10 +75,52 @@ The example below shows how to:
9475
</TelerikTabStrip>
9576
</div>
9677
78+
<style>
79+
.dynamic-tabstrip-wrapper {
80+
--add-tab-button-size: 32.8px;
81+
}
82+
83+
.dynamic-tabstrip-wrapper .k-tabstrip-items {
84+
max-width: calc(100% - var(--add-tab-button-size));
85+
}
86+
87+
.dynamic-tabstrip-wrapper .add-tab-button {
88+
width: var(--add-tab-button-size);
89+
padding-block: 6px;
90+
z-index: 10000;
91+
}
92+
93+
.remove-tab-button {
94+
padding: 0 !important;
95+
}
96+
</style>
97+
98+
<script suppress-error="BL9992">
99+
window.positionAddTabButton = () => {
100+
const tabStripItems = Array.from(document.querySelectorAll(".dynamic-tabstrip-wrapper .k-tabstrip-item"));
101+
const tabStripWrapperWidth = document.querySelector(".dynamic-tabstrip-wrapper").scrollWidth;
102+
103+
let totalWidth = !tabStripItems ? 0 : tabStripItems.reduce(
104+
(accumulator, currentItem) => accumulator + parseFloat(currentItem.getBoundingClientRect().width),
105+
0,
106+
);
107+
108+
const addTabButton = document.querySelector(".add-tab-button");
109+
const addTabButtonWidth = addTabButton.getBoundingClientRect().width;
110+
111+
// Assure button is never positioned outside the boundaries of the wrapper
112+
if (totalWidth + addTabButtonWidth > tabStripWrapperWidth) {
113+
totalWidth = tabStripWrapperWidth - addTabButtonWidth;
114+
}
115+
116+
addTabButton.style.left = `${totalWidth}px`;
117+
};
118+
</script>
119+
97120
@code {
98-
private string ActiveTabId { get; set; }
121+
private string ActiveTabId { get; set; } = string.Empty;
99122
100-
private List<Tab> Tabs = new List<Tab>()
123+
private List<Tab> Tabs = new List<Tab>
101124
{
102125
new Tab { Id = Guid.NewGuid().ToString(), Title = "Tab 1" },
103126
new Tab { Id = Guid.NewGuid().ToString(), Title = "Tab 2" },
@@ -106,11 +129,10 @@ The example below shows how to:
106129
107130
private void AddTab()
108131
{
109-
Tab tabToAdd = new Tab { Id = Guid.NewGuid().ToString(), Title = $"New Tab" };
110-
132+
var tabToAdd = new Tab { Id = Guid.NewGuid().ToString(), Title = "New Tab" };
111133
Tabs.Add(tabToAdd);
112134
113-
//In this example, we are always activating the newly added tab. Adjust the logic to activate a different tab if needed.
135+
// Activate the newly added tab
114136
ActiveTabId = tabToAdd.Id;
115137
}
116138
@@ -119,51 +141,26 @@ The example below shows how to:
119141
if (Tabs.Count <= 1)
120142
{
121143
return;
122-
}
123-
144+
}
145+
124146
Tabs.Remove(tab);
125147
126-
//In this example, we are always activating the first tab. Adjust the logic to determine which tab to activate after removal.
148+
// Activate the first tab after removal
127149
ActiveTabId = Tabs[0].Id;
128150
}
129151
130152
protected override async Task OnAfterRenderAsync(bool firstRender)
131153
{
132154
await JS.InvokeVoidAsync("positionAddTabButton");
133-
134155
await base.OnAfterRenderAsync(firstRender);
135156
}
136157
137158
public class Tab
138159
{
139-
public string Id { get; set; }
140-
141-
public string Title { get; set; }
160+
public string Id { get; set; } = string.Empty;
161+
public string Title { get; set; } = string.Empty;
142162
}
143-
144163
}
145-
146-
<script suppress-error="BL9992">
147-
window.positionAddTabButton = () => {
148-
const tabStripItems = Array.from(document.querySelectorAll(".dynamic-tabstrip-wrapper .k-tabstrip-item"));
149-
const tabStripWrapperWidth = document.querySelector(".dynamic-tabstrip-wrapper").scrollWidth;
150-
151-
let totalWidth = !tabStripItems ? 0 : tabStripItems.reduce(
152-
(accumulator, currentItem) => accumulator + parseFloat(currentItem.getBoundingClientRect().width),
153-
0,
154-
);
155-
156-
const addTabButton = document.querySelector(".add-tab-button");
157-
const addTabButtonWidth = addTabButton.getBoundingClientRect().width;
158-
159-
// assure button is never positioned outside the boundaries of the wrapper
160-
if (totalWidth + addTabButtonWidth > tabStripWrapperWidth) {
161-
totalWidth = tabStripWrapperWidth - addTabButtonWidth;
162-
}
163-
164-
addTabButton.style.left = `${totalWidth}px`;
165-
};
166-
</script>
167164
````
168165

169166
## See Also

0 commit comments

Comments
 (0)