Skip to content

Commit b3f7ea5

Browse files
committed
feat(ui5-toolbar-item): expose :state(overflowed) CSS custom state
Add CSS custom state that allows styling toolbar items differently when they are in the overflow popover. The state is managed via the existing isOverflowed property using ElementInternals.states. - Add getter/setter for isOverflowed to manage the custom state - Document the CSS custom state in class JSDoc - Add test sample and Cypress test
1 parent 98b851b commit b3f7ea5

File tree

3 files changed

+95
-6
lines changed

3 files changed

+95
-6
lines changed

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import Toolbar from "../../src/Toolbar.js";
22
import ToolbarItem from "../../src/ToolbarItem.js";
3+
import ToolbarButton from "../../src/ToolbarButton.js";
34
import Button from "../../src/Button.js";
45
import Switch from "../../src/Switch.js";
56
import MultiComboBox from "../../src/MultiComboBox.js";
@@ -9,6 +10,8 @@ import ComboBoxItem from "../../src/ComboBoxItem.js";
910
import DatePicker from "../../src/DatePicker.js";
1011
import Select from "../../src/Select.js";
1112
import Option from "../../src/Option.js";
13+
import CheckBox from "../../src/CheckBox.js";
14+
import add from "@ui5/webcomponents-icons/dist/add.js";
1215

1316
describe("Toolbar Item Properties", () => {
1417
it("Should render ui5-toolbar-item with correct properties and not suppress events", () => {
@@ -764,4 +767,27 @@ describe("Toolbar Item Closing Events - overflowCloseEvents functionality (IOver
764767
expect(closingEvents).to.include("extra-event");
765768
});
766769
});
770+
});
771+
772+
describe("ToolbarItem CSS Custom State", () => {
773+
it("Should set :state(overflowed) when item is in overflow", () => {
774+
cy.mount(
775+
<div style={{ width: "200px" }}>
776+
<Toolbar>
777+
<ToolbarButton text="Button 1" icon={add}></ToolbarButton>
778+
<ToolbarItem id="test-item" overflow-priority="AlwaysOverflow">
779+
<CheckBox text="Check"></CheckBox>
780+
</ToolbarItem>
781+
</Toolbar>
782+
</div>
783+
);
784+
785+
cy.wait(500);
786+
787+
cy.get("#test-item").then($item => {
788+
const item = $item[0] as ToolbarItem;
789+
// @ts-ignore
790+
expect(item._internals.states.has("overflowed")).to.be.true;
791+
});
792+
});
767793
});

packages/main/src/ToolbarItem.ts

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,25 @@ interface IOverflowToolbarItem extends HTMLElement {
4242
* @class
4343
*
4444
* Represents an abstract class for items, used in the `ui5-toolbar`.
45+
*
46+
* ### CSS Custom States
47+
*
48+
* The `ui5-toolbar-item` exposes the following CSS custom states for styling:
49+
*
50+
* | State | Description |
51+
* |-------|-------------|
52+
* | `:state(overflowed)` | Applied when the item is displayed in the overflow popover. Use this state to apply different styles when the item is overflowed. |
53+
*
54+
* **Example:**
55+
* ```css
56+
* ui5-toolbar-item:state(overflowed) {
57+
* flex-direction: column;
58+
* }
59+
* ```
60+
*
4561
* @constructor
4662
* @extends UI5Element
4763
* @public
48-
* @experimental This module is experimental and its API might change significantly in future.
4964
* @since 1.17.0
5065
*/
5166
class ToolbarItem extends UI5Element {
@@ -78,13 +93,24 @@ class ToolbarItem extends UI5Element {
7893
* @protected
7994
* @since 2.11.0
8095
*/
96+
get isOverflowed(): boolean {
97+
return this._isOverflowed;
98+
}
8199

82-
@property({ type: Boolean })
83-
isOverflowed: boolean = false;
100+
set isOverflowed(value: boolean) {
101+
this._isOverflowed = value;
102+
103+
if (value) {
104+
this._internals.states.add("overflowed");
105+
} else {
106+
this._internals.states.delete("overflowed");
107+
}
108+
}
84109

85110
_isRendering = true;
86111
_maxWidth = 0;
87112
_wrapperChecked = false;
113+
_isOverflowed: boolean = false;
88114
fireCloseOverflowRef = this.fireCloseOverflow.bind(this);
89115

90116
closeOverflowSet = {

packages/main/test/pages/ToolbarItems.html

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -183,12 +183,49 @@
183183
</ui5-toolbar-item>
184184

185185
<!-- Toolbar Item with Date Picker -->
186-
<ui5-toolbar-item>
186+
<ui5:toolbar-item>
187187
<ui5-date-picker placeholder="Select a date"></ui5-date-picker>
188-
</ui5-toolbar-item>
188+
</ui5:toolbar-item>
189189
</ui5-toolbar>
190190
</section>
191+
192+
<!-- Test section for CSS custom state :state(overflowed) -->
193+
<ui5-title level="H3">Toolbar with :state(overflowed) CSS Custom State (resize to test)</ui5-title>
194+
<br />
195+
<style>
196+
/* Use CSS custom state :state(overflowed) to style items when in overflow popover */
197+
#overflow-state-toolbar [ui5-toolbar-item]:state(overflowed) {
198+
/* When overflowed, change checkboxes and radio buttons to column layout */
199+
display: flex;
200+
flex-direction: column;
201+
}
202+
203+
/* Visual indicator for the test */
204+
#overflow-state-toolbar [ui5-toolbar-item]:state(overflowed)::before {
205+
content: "Overflowed:";
206+
font-size: 0.75rem;
207+
color: var(--sapNeutralTextColor);
208+
margin-bottom: 0.25rem;
209+
}
210+
</style>
211+
<section>
212+
<ui5-toolbar id="overflow-state-toolbar" style="max-width: 400px;">
213+
<ui5-toolbar-button icon="add" text="Add"></ui5-toolbar-button>
214+
<!-- Toolbar Item with Radio Buttons - should display in column when overflowed -->
215+
<ui5-toolbar-item id="overflow-state-radio-item">
216+
<ui5-radio-button name="overflow-group" text="Option A" selected></ui5-radio-button>
217+
<ui5-radio-button name="overflow-group" text="Option B"></ui5-radio-button>
218+
<ui5-radio-button name="overflow-group" text="Option C"></ui5-radio-button>
219+
</ui5-toolbar-item>
220+
<!-- Toolbar Item with Checkboxes - should display in column when overflowed -->
221+
<ui5-toolbar-item id="overflow-state-checkbox-item">
222+
<ui5-checkbox text="Check A"></ui5-checkbox>
223+
<ui5-checkbox text="Check B" checked></ui5-checkbox>
224+
<ui5-checkbox text="Check C"></ui5-checkbox>
225+
</ui5-toolbar-item>
226+
<ui5-toolbar-button icon="decline" text="Remove"></ui5-toolbar-button>
227+
</ui5-toolbar>
228+
</section>
191229
</div>
192230
</body>
193-
194231
</html>

0 commit comments

Comments
 (0)