Skip to content

Commit 23e8af1

Browse files
authored
refactor(banner): Banner component cleanups (#1940)
* Added slot controller to support updates for dynamic content * Dropped the obsolete 'toggleAnimation' method
1 parent ebfc92f commit 23e8af1

File tree

2 files changed

+24
-35
lines changed

2 files changed

+24
-35
lines changed

src/components/banner/banner.ts

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { html, LitElement } from 'lit';
22
import { property } from 'lit/decorators.js';
3-
import { createRef, type Ref, ref } from 'lit/directives/ref.js';
4-
3+
import { createRef, ref } from 'lit/directives/ref.js';
54
import { addAnimationController } from '../../animations/player.js';
65
import { growVerIn, growVerOut } from '../../animations/presets/grow/index.js';
76
import { addThemingController } from '../../theming/theming-controller.js';
87
import IgcButtonComponent from '../button/button.js';
98
import { addInternalsController } from '../common/controllers/internals.js';
9+
import { addSlotController, setSlots } from '../common/controllers/slot.js';
1010
import { registerComponent } from '../common/definitions/register.js';
1111
import type { Constructor } from '../common/mixins/constructor.js';
1212
import { EventEmitterMixin } from '../common/mixins/event-emitter.js';
@@ -50,11 +50,8 @@ export default class IgcBannerComponent extends EventEmitterMixin<
5050
registerComponent(IgcBannerComponent, IgcButtonComponent);
5151
}
5252

53-
private readonly _bannerRef: Ref<HTMLElement> = createRef();
54-
private readonly _animationPlayer = addAnimationController(
55-
this,
56-
this._bannerRef
57-
);
53+
private readonly _bannerRef = createRef<HTMLElement>();
54+
private readonly _player = addAnimationController(this, this._bannerRef);
5855

5956
/**
6057
* Determines whether the banner is being shown/hidden.
@@ -67,7 +64,7 @@ export default class IgcBannerComponent extends EventEmitterMixin<
6764
super();
6865

6966
addThemingController(this, all);
70-
67+
addSlotController(this, { slots: setSlots('prefix', 'actions') });
7168
addInternalsController(this, {
7269
initialARIA: {
7370
role: 'status',
@@ -76,14 +73,21 @@ export default class IgcBannerComponent extends EventEmitterMixin<
7673
});
7774
}
7875

76+
private async _handleClick(): Promise<void> {
77+
if (this.emitEvent('igcClosing', { cancelable: true })) {
78+
await this.hide();
79+
this.emitEvent('igcClosed');
80+
}
81+
}
82+
7983
/** Shows the banner if not already shown. Returns `true` when the animation has completed. */
8084
public async show(): Promise<boolean> {
8185
if (this.open) {
8286
return false;
8387
}
8488

8589
this.open = true;
86-
return await this.toggleAnimation('open');
90+
return this._player.playExclusive(growVerIn());
8791
}
8892

8993
/** Hides the banner if not already hidden. Returns `true` when the animation has completed. */
@@ -92,26 +96,14 @@ export default class IgcBannerComponent extends EventEmitterMixin<
9296
return false;
9397
}
9498

95-
await this.toggleAnimation('close');
99+
await this._player.playExclusive(growVerOut());
96100
this.open = false;
97101
return true;
98102
}
99103

100104
/** Toggles between shown/hidden state. Returns `true` when the animation has completed. */
101105
public async toggle(): Promise<boolean> {
102-
return this.open ? await this.hide() : await this.show();
103-
}
104-
105-
private async toggleAnimation(dir: 'open' | 'close') {
106-
const animation = dir === 'open' ? growVerIn : growVerOut;
107-
return this._animationPlayer.playExclusive(animation());
108-
}
109-
110-
private async handleClick() {
111-
if (this.emitEvent('igcClosing', { cancelable: true })) {
112-
await this.hide();
113-
this.emitEvent('igcClosed');
114-
}
106+
return this.open ? this.hide() : this.show();
115107
}
116108

117109
protected override render() {
@@ -131,7 +123,7 @@ export default class IgcBannerComponent extends EventEmitterMixin<
131123
<igc-button
132124
type="button"
133125
variant="flat"
134-
@click=${this.handleClick}
126+
@click=${this._handleClick}
135127
>OK</igc-button
136128
>
137129
</slot>

stories/banner.stories.ts

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ const checkIcon =
5656

5757
registerIconFromText('success', checkIcon, 'material');
5858

59-
const BasicTemplate = ({ open }: IgcBannerArgs) => {
60-
return html`
59+
export const Basic: Story = {
60+
render: (args) => html`
6161
<style>
6262
igc-button {
6363
margin-block-start: 12px;
@@ -66,21 +66,21 @@ const BasicTemplate = ({ open }: IgcBannerArgs) => {
6666
<igc-navbar style="height:30px">
6767
<h2>Title</h2>
6868
</igc-navbar>
69-
<igc-banner id="banner" .open=${open}>
69+
<igc-banner id="banner" ?open=${args.open}>
7070
You are currently not logged in! Please, log into your account first.
7171
</igc-banner>
7272
<igc-button onclick="banner.toggle()">Toggle Banner</igc-button>
73-
`;
73+
`,
7474
};
7575

76-
const SlottedContentTemplate = ({ open }: IgcBannerArgs) => {
77-
return html`
76+
export const SlottedContent: Story = {
77+
render: (args) => html`
7878
<style>
7979
igc-banner[open] + igc-button {
8080
margin-block-start: 12px;
8181
}
8282
</style>
83-
<igc-banner id="banner" .open=${open}>
83+
<igc-banner id="banner" ?open=${args.open}>
8484
<igc-icon name="success" collection="material" slot="prefix"></igc-icon>
8585
8686
Build f58a1815-c069-429d-ab20-860849e96a59 completed!
@@ -91,8 +91,5 @@ const SlottedContentTemplate = ({ open }: IgcBannerArgs) => {
9191
</div>
9292
</igc-banner>
9393
<igc-button onclick="banner.toggle()">Toggle Banner</igc-button>
94-
`;
94+
`,
9595
};
96-
97-
export const Basic: Story = BasicTemplate.bind({});
98-
export const SlottedContent: Story = SlottedContentTemplate.bind({});

0 commit comments

Comments
 (0)