Skip to content

Commit 7e2a07d

Browse files
authored
feat(ui5-toolbar-button): add showOverflowText property for conditional text display (#12542)
Introduces showOverflowText boolean property that controls text visibility based on overflow state: - When true: icon-only in toolbar, icon+text in overflow popover - When false/default: consistent icon+text display in both states Related to: #12381
1 parent d1c6917 commit 7e2a07d

File tree

4 files changed

+132
-2
lines changed

4 files changed

+132
-2
lines changed

packages/main/cypress/specs/Toolbar.cy.tsx

Lines changed: 75 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -544,7 +544,7 @@ describe("ToolbarButton", () => {
544544
cy.get("[ui5-toolbar-button][accessible-name]").shadow().find(".ui5-tb-button")
545545
.should("have.prop", "accessibilityAttributes")
546546
.should("deep.include", { expanded: "true", controls: "btn", hasPopup: "dialog" });
547-
});
547+
});
548548

549549
it("Should not recalculate overflow when button state changes without affecting width", () => {
550550
cy.mount(
@@ -589,4 +589,78 @@ describe("ToolbarButton", () => {
589589
});
590590
});
591591
});
592+
593+
it("Should display text only in overflow when showOverflowText is true", () => {
594+
cy.mount(
595+
<Toolbar>
596+
<ToolbarButton
597+
icon={add}
598+
text="Add Document"
599+
showOverflowText={true}
600+
></ToolbarButton>
601+
602+
<ToolbarButton
603+
icon={employee}
604+
text="Employee"
605+
showOverflowText={false}
606+
></ToolbarButton>
607+
608+
<ToolbarButton
609+
icon={decline}
610+
text="Decline Item"
611+
></ToolbarButton>
612+
</Toolbar>
613+
);
614+
615+
cy.viewport(800, 600);
616+
617+
cy.get("[ui5-toolbar-button][text='Add Document']")
618+
.should("have.prop", "isOverflowed", false)
619+
.shadow()
620+
.find("[ui5-button]")
621+
.should("not.contain.text", "Add Document");
622+
623+
cy.get("[ui5-toolbar-button][text='Employee']")
624+
.should("have.prop", "isOverflowed", false)
625+
.shadow()
626+
.find("[ui5-button]")
627+
.should("contain.text", "Employee");
628+
629+
cy.get("[ui5-toolbar-button][text='Decline Item']")
630+
.should("have.prop", "isOverflowed", false)
631+
.shadow()
632+
.find("[ui5-button]")
633+
.should("contain.text", "Decline Item");
634+
635+
cy.viewport(100, 600);
636+
637+
cy.get("[ui5-toolbar]")
638+
.shadow()
639+
.find(".ui5-tb-overflow-btn")
640+
.should("not.have.class", "ui5-tb-overflow-btn-hidden")
641+
.realClick();
642+
643+
cy.get("[ui5-toolbar]")
644+
.shadow()
645+
.find(".ui5-overflow-popover")
646+
.should("have.attr", "open", "open");
647+
648+
cy.get("[ui5-toolbar-button][text='Add Document']")
649+
.should("have.prop", "isOverflowed", true)
650+
.shadow()
651+
.find("[ui5-button]")
652+
.should("contain.text", "Add Document");
653+
654+
cy.get("[ui5-toolbar-button][text='Employee']")
655+
.should("have.prop", "isOverflowed", true)
656+
.shadow()
657+
.find("[ui5-button]")
658+
.should("contain.text", "Employee");
659+
660+
cy.get("[ui5-toolbar-button][text='Decline Item']")
661+
.should("have.prop", "isOverflowed", true)
662+
.shadow()
663+
.find("[ui5-button]")
664+
.should("contain.text", "Decline Item");
665+
});
592666
});

packages/main/src/ToolbarButton.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,20 @@ class ToolbarButton extends ToolbarItem {
145145
@property()
146146
text?: string;
147147

148+
/**
149+
* Defines whether the button text should only be displayed in the overflow popover.
150+
*
151+
* When set to `true`, the button appears as icon-only in the main toolbar,
152+
* but shows both icon and text when moved to the overflow popover.
153+
*
154+
* **Note:** This property only takes effect when the `text` property is also set.
155+
*
156+
* @default false
157+
* @public
158+
*/
159+
@property({ type: Boolean })
160+
showOverflowText = false;
161+
148162
/**
149163
* Defines the width of the button.
150164
*
@@ -162,6 +176,23 @@ class ToolbarButton extends ToolbarItem {
162176
};
163177
}
164178

179+
/**
180+
* Returns the effective text to display based on overflow state and showOverflowText property.
181+
*
182+
* When showOverflowText is true:
183+
* - Normal state: returns empty string (icon-only)
184+
* - Overflow state: returns text
185+
*
186+
* When showOverflowText is false:
187+
* - Returns text in both states (normal behavior)
188+
*/
189+
get effectiveText(): string | undefined {
190+
if (this.showOverflowText) {
191+
return this.isOverflowed ? this.text : "";
192+
}
193+
return this.text;
194+
}
195+
165196
onClick(e: Event) {
166197
e.stopImmediatePropagation();
167198
const prevented = !this.fireDecoratorEvent("click", { targetRef: e.target as HTMLElement });

packages/main/src/ToolbarButtonTemplate.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export default function ToolbarButtonTemplate(this: ToolbarButton) {
2222
data-ui5-stable={this.stableDomRef}
2323
onClick={(...args) => this.onClick(...args)}
2424
>
25-
{this.text}
25+
{this.effectiveText}
2626
</Button>
2727
);
2828
}

packages/main/test/pages/Toolbar.html

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,31 @@
316316
</div>
317317
</section>
318318

319+
<br /><br />
320+
<ui5-title level="H3">Toolbar with showOverflowText property</ui5-title>
321+
<ui5-label>Resize the viewport to see the overflow behavior. Buttons with showOverflowText show icon-only in the toolbar but display text when overflowed.</ui5-label>
322+
<br /><br />
323+
<section>
324+
<ui5-toolbar id="otb_overflow_text_only">
325+
<ui5-toolbar-button icon="save" text="Save" show-overflow-text tooltip="Save Document"></ui5-toolbar-button>
326+
<ui5-toolbar-button icon="edit" text="Edit" show-overflow-text tooltip="Edit Document"></ui5-toolbar-button>
327+
<ui5-toolbar-button icon="copy" text="Copy" show-overflow-text tooltip="Copy Document"></ui5-toolbar-button>
328+
329+
<ui5-toolbar-separator></ui5-toolbar-separator>
330+
331+
<ui5-toolbar-button icon="add" text="Add New Item"></ui5-toolbar-button>
332+
<ui5-toolbar-button icon="decline" text="Delete Item"></ui5-toolbar-button>
333+
334+
<ui5-toolbar-spacer></ui5-toolbar-spacer>
335+
336+
<ui5-toolbar-button icon="download" text="Download" show-overflow-text tooltip="Download File"></ui5-toolbar-button>
337+
<ui5-toolbar-button icon="share" text="Share" show-overflow-text tooltip="Share Document"></ui5-toolbar-button>
338+
<ui5-toolbar-button icon="refresh" text="Refresh" show-overflow-text tooltip="Refresh Page"></ui5-toolbar-button>
339+
</ui5-toolbar>
340+
</section>
341+
342+
<br /><br />
343+
319344
<section id="testEventpreventClosing">
320345
<div style="width: 250px;">
321346
<ui5-toolbar id="testEventpreventClosing-toolbar">

0 commit comments

Comments
 (0)