Skip to content

Commit 0bd1cce

Browse files
committed
feat(tab): add icon-only prop
1 parent 4a96a1c commit 0bd1cce

File tree

6 files changed

+46
-16
lines changed

6 files changed

+46
-16
lines changed

packages/core/src/components.d.ts

+14
Original file line numberDiff line numberDiff line change
@@ -1140,6 +1140,13 @@ export namespace Components {
11401140
* If this tab is currently active if multiple `go-tab` are active in the same `go-tabs`, first one is active.
11411141
*/
11421142
"active": boolean;
1143+
/**
1144+
* Only show `icon` and `icon-active` slot content "Label" prop is still required for screen reader announcement, but will not be shown visually
1145+
*/
1146+
"iconOnly"?: boolean;
1147+
/**
1148+
* Position of the icon, either 'before' or 'after'
1149+
*/
11431150
"iconPosition"?: TabIconPosition;
11441151
/**
11451152
* Label displayed on the tab
@@ -3060,6 +3067,13 @@ declare namespace LocalJSX {
30603067
* If this tab is currently active if multiple `go-tab` are active in the same `go-tabs`, first one is active.
30613068
*/
30623069
"active"?: boolean;
3070+
/**
3071+
* Only show `icon` and `icon-active` slot content "Label" prop is still required for screen reader announcement, but will not be shown visually
3072+
*/
3073+
"iconOnly"?: boolean;
3074+
/**
3075+
* Position of the icon, either 'before' or 'after'
3076+
*/
30633077
"iconPosition"?: TabIconPosition;
30643078
/**
30653079
* Label displayed on the tab

packages/core/src/components/go-tabs/go-tab.tsx

+9
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,17 @@ export class GoTab {
3535
*/
3636
@Prop({ mutable: true }) panelId?: string;
3737

38+
/**
39+
* Position of the icon, either 'before' or 'after'
40+
*/
3841
@Prop() iconPosition?: TabIconPosition = 'before';
3942

43+
/**
44+
* Only show `icon` and `icon-active` slot content
45+
* "Label" prop is still required for screen reader announcement, but will not be shown visually
46+
*/
47+
@Prop() iconOnly?: boolean = false;
48+
4049
@Method()
4150
async setActive(active: boolean) {
4251
this.active = active;

packages/core/src/components/go-tabs/go-tablist.tsx

+1-2
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,6 @@ export class GoTablist {
247247
// target index out of range
248248
if (pressed === 'ArrowLeft' || pressed === 'ArrowUp') {
249249
if (!this.auto) {
250-
console.log('yo!!');
251250
this.focusLastTab();
252251
return;
253252
}
@@ -313,7 +312,7 @@ export class GoTablist {
313312
'has-active-icon': !!tab.iconActiveSlot,
314313
}}
315314
ref={(el) => this.tabEls.push(el)}>
316-
<span>{tab.label}</span>
315+
<span class={{ 'visually-hidden': tab.iconOnly }}>{tab.label}</span>
317316
</button>
318317
);
319318
})

packages/core/src/components/go-tabs/go-tabs.tsx

+16-9
Original file line numberDiff line numberDiff line change
@@ -81,20 +81,27 @@ export class GoTabs {
8181
}
8282

8383
this.tabChildren = children.map((goTab) => {
84-
const tabId = uniqueId('tab-');
85-
const panelId = tabId + '-panel';
86-
goTab.tabId = tabId;
87-
goTab.panelId = panelId;
84+
if (!goTab.tabId) {
85+
const tId = uniqueId('tab-');
86+
goTab.tabId = tId;
87+
}
88+
if (!goTab.panelId) {
89+
const pId = goTab.tabId + '-panel';
90+
goTab.panelId = pId;
91+
}
8892
const iconSlot = this.initIconSlot(goTab, 'icon');
8993
const iconActiveSlot = this.initIconSlot(goTab, 'icon-active');
94+
95+
const { tabId, panelId, label, active, iconPosition, iconOnly } = goTab;
9096
return {
91-
tabId: goTab.tabId || tabId,
92-
panelId: goTab.panelId || panelId,
93-
label: goTab.label,
94-
active: goTab.active,
97+
tabId,
98+
panelId,
99+
label,
100+
active,
101+
iconPosition,
102+
iconOnly,
95103
iconSlot,
96104
iconActiveSlot,
97-
iconPosition: goTab.iconPosition,
98105
};
99106
});
100107
this.panels = children;

packages/core/src/components/go-tabs/tabs.type.ts

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export interface TabItem {
66
iconSlot?: Element;
77
iconActiveSlot?: Element;
88
iconPosition?: TabIconPosition;
9+
iconOnly?: boolean;
910
}
1011

1112
export interface ActiveTab {

packages/core/src/index.html

+5-5
Original file line numberDiff line numberDiff line change
@@ -29,22 +29,22 @@
2929

3030
<!-- keep this sandbox empty before pushing changes -->
3131
<div class="container">
32-
<go-tabs fill vertical>
33-
<go-tab label="Icon tab" icon-position="before">
32+
<go-tabs fill>
33+
<go-tab label="Icon tab" icon-position="before" icon-only>
3434
<go-icon name="home" slot="icon"></go-icon>
3535
<p>
3636
You can use <code>slot="icon"</code> to add an icon to tab (by default, icon is placed before the label
3737
text)
3838
</p>
3939
</go-tab>
40-
<go-tab label="Icon tab" icon-position="after">
41-
<go-icon name="star" slot="icon-active"></go-icon>
40+
<go-tab label="Icon tab" icon-position="after" icon-only>
41+
<go-icon name="star" slot="icon"></go-icon>
4242
<p>
4343
<code>icon-position="after"</code> puts icon after the label, you can use <code>slot="icon-active"</code> to
4444
specify the icon that will be shown when the tab is active
4545
</p>
4646
</go-tab>
47-
<go-tab label="Third tab">
47+
<go-tab label="Third tab" icon-only>
4848
<svg fill="currentColor" width="2rem" height="2rem" slot="icon" viewBox="0 0 500 500">
4949
<path
5050
d="M302.5 131.9c-29.3-28.1-69.1-45.3-112.9-45.3-90.3 0-163.4 73.1-163.4 163.4s73.2 163.4 163.4 163.4c43.8 0 83.6-17.2 112.9-45.3L184.1 250l118.4-118.1zm-171.2 24.6c0-20.8 16.9-37.7 37.7-37.7s37.7 16.9 37.7 37.7c0 20.8-16.9 37.7-37.7 37.7s-37.7-16.8-37.7-37.7z" />

0 commit comments

Comments
 (0)