Skip to content

Commit

Permalink
feat(banner): Expose layout method.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 339502866
  • Loading branch information
EstebanG23 authored and copybara-github committed Oct 28, 2020
1 parent dbc449b commit 4794b25
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/mdc-banner/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ Method Signature | Description
`setPrimaryActionText(actionButtonText: string) => void` | Sets the banner's primary action text.
`getSecondaryActionText() => string|null` | Gets the banner's secondary action text. Returns null if the banner has no secondary action.
`setSecondaryActionText(actionButtonText: string) => void` | Sets the banner's secondary action text.
`layout() => void` | Recalculates layout. With height being calculated dynamically recommended to call on window `resize` events.

### Usage Within Frameworks

Expand Down
4 changes: 4 additions & 0 deletions packages/mdc-banner/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ export class MDCBanner extends MDCComponent<MDCBannerFoundation> {
this.deregisterContentClickHandler(this.handleContentClick);
}

layout() {
this.foundation.layout();
}

/**
* Opens the banner and fires events.OPENING to indicate the beginning of its
* opening animation and then events.OPENED once the animation finishes.
Expand Down
5 changes: 5 additions & 0 deletions packages/mdc-banner/foundation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,11 @@ export class MDCBannerFoundation extends MDCFoundation<MDCBannerAdapter> {
this.close(CloseReason.SECONDARY);
}

layout() {
const contentHeight = this.adapter.getContentHeight();
this.adapter.setStyleProperty('height', `${contentHeight}px`);
}

private handleAnimationTimerEnd() {
this.animationTimer = 0;
this.adapter.removeClass(OPENING);
Expand Down
11 changes: 11 additions & 0 deletions packages/mdc-banner/test/component.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,17 @@ describe('MDCBanner', () => {
expect(fixture.offsetHeight).toEqual(0);
});

it('#layout sets the root element height to the content element height',
() => {
const {component, contentEl} = setupTest(fixture);

(contentEl as HTMLElement).style.height = '50px';
component.layout();
jasmine.clock().tick(1);
expect(fixture.offsetHeight)
.toEqual((contentEl as HTMLElement).offsetHeight);
});

it('get isOpen returns true when open', () => {
const {component} = setupTest(fixture);

Expand Down
9 changes: 9 additions & 0 deletions packages/mdc-banner/test/foundation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,15 @@ describe('MDCBannerFoundation', () => {
expect(mockAdapter.addClass).not.toHaveBeenCalledWith(cssClasses.OPEN);
});

it('#layout sets the root element height to the content height', () => {
const {foundation, mockAdapter} = setupTest();

foundation.layout();
expect(mockAdapter.getContentHeight).toHaveBeenCalled();
// 0px since nothing is being generated.
expect(mockAdapter.setStyleProperty).toHaveBeenCalledWith('height', '0px');
});

it('#open adds CSS classes after rAF', () => {
const {foundation, mockAdapter} = setupTest();

Expand Down

0 comments on commit 4794b25

Please sign in to comment.