Skip to content

Commit c1e47b9

Browse files
cm314maradwan26heloiselui
authored
fix(menu): prevent overflow (#21006)
* fix(menu): prevent menu overflow This uses the correct menu html element for checking the size of the menu to correctly compute the position to avoid overflow. * docs(menu): add context menu example to storybook This adds an example of a context menu to the menu story to show correct automatic positioning of the menu and submenus to avoid overflows. * Update packages/web-components/src/components/menu/menu.stories.ts --------- Co-authored-by: Mahmoud <132728978+maradwan26@users.noreply.github.com> Co-authored-by: Heloise Lui <71858203+heloiselui@users.noreply.github.com>
1 parent 3c07d99 commit c1e47b9

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

packages/web-components/src/components/menu/__tests__/menu-test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,4 +90,24 @@ describe('cds-menu', () => {
9090
expect(el.context.isRoot).to.be.false;
9191
});
9292
});
93+
94+
describe('positioning', () => {
95+
it('should adjust position when the menu cannot open to the bottom right', async () => {
96+
const nearMaxX = window.innerWidth - 10;
97+
const nearMaxY = window.innerHeight - 10;
98+
99+
const el = await fixture(html`
100+
<cds-menu .x=${nearMaxX} .y=${nearMaxY} open>
101+
<cds-menu-item label="Item 1"></cds-menu-item>
102+
</cds-menu>
103+
`);
104+
105+
await el.updateComplete;
106+
107+
expect(el.position[0]).to.be.a('number');
108+
expect(el.position[1]).to.be.a('number');
109+
expect(el.position[0]).to.be.lessThan(nearMaxX);
110+
expect(el.position[1]).to.be.lessThan(nearMaxY);
111+
});
112+
});
93113
});

packages/web-components/src/components/menu/menu.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,8 @@ class CDSMenu extends HostListenerMixin(LitElement) {
313313
const { isRoot } = this.context;
314314

315315
// const isRoot = false
316-
const { width, height } = this.getBoundingClientRect();
316+
const menuElement = this.shadowRoot?.querySelector(`.${prefix}--menu`);
317+
const { width, height } = (menuElement ?? this).getBoundingClientRect();
317318
const alignment = isRoot ? 'vertical' : 'horizontal';
318319
const axes = {
319320
x: {

0 commit comments

Comments
 (0)