Skip to content

Commit 229d282

Browse files
gerjanvangeestdaKmoR
authored andcommitted
fix(overlays): add inheritsReferenceObjectWidth parameter
1 parent 9412d6a commit 229d282

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

packages/overlays/src/LocalOverlayController.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export class LocalOverlayController {
1111
this.constructor.popperModule = __preloadPopper();
1212
this.__mergePopperConfigs(params.popperConfig || {});
1313

14+
this.inheritsReferenceObjectWidth = params.inheritsReferenceObjectWidth;
1415
this.hidesOnEsc = params.hidesOnEsc;
1516
this.hidesOnOutsideClick = params.hidesOnOutsideClick;
1617
this.trapsKeyboardFocus = params.trapsKeyboardFocus;
@@ -148,6 +149,19 @@ export class LocalOverlayController {
148149
this.contentNode.style.zIndex = 1;
149150
this.invokerNode.setAttribute('aria-expanded', true);
150151

152+
if (this.inheritsReferenceObjectWidth) {
153+
const referenceObjectWidth = `${this.invokerNode.clientWidth}px`;
154+
switch (this.inheritsReferenceObjectWidth) {
155+
case 'max':
156+
this.contentNode.style.maxWidth = referenceObjectWidth;
157+
break;
158+
case 'full':
159+
this.contentNode.style.width = referenceObjectWidth;
160+
break;
161+
default:
162+
this.contentNode.style.minWidth = referenceObjectWidth;
163+
}
164+
}
151165
if (this.trapsKeyboardFocus) this._setupTrapsKeyboardFocus();
152166
if (this.hidesOnOutsideClick) this._setupHidesOnOutsideClick();
153167
if (this.hidesOnEsc) this._setupHidesOnEsc();

packages/overlays/test/LocalOverlayController.test.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,27 @@ describe('LocalOverlayController', () => {
499499
'Popper positioning Y value should be 32 less than previous, due to the added 32px offset',
500500
);
501501
});
502+
503+
it('can set the contentNode minWidth as the invokerNode width', () => {
504+
const controller = new LocalOverlayController({
505+
inheritsReferenceObjectWidth: 'min',
506+
});
507+
expect(controller.contentNode.style.minWidth).to.equal(controller.invokerNode.style.width);
508+
});
509+
510+
it('can set the contentNode maxWidth as the invokerNode width', () => {
511+
const controller = new LocalOverlayController({
512+
inheritsReferenceObjectWidth: 'max',
513+
});
514+
expect(controller.contentNode.style.maxWidth).to.equal(controller.invokerNode.style.width);
515+
});
516+
517+
it('can set the contentNode width as the invokerNode width', () => {
518+
const controller = new LocalOverlayController({
519+
inheritsReferenceObjectWidth: 'full',
520+
});
521+
expect(controller.contentNode.style.width).to.equal(controller.invokerNode.style.width);
522+
});
502523
});
503524

504525
describe('a11y', () => {

0 commit comments

Comments
 (0)