1- import { LitElement } from 'lit' ;
1+ import { LitElement , type PropertyValues } from 'lit' ;
22import { property } from 'lit/decorators.js' ;
3-
4- import type { addAnimationController } from '../../../animations/player.js' ;
3+ import type { AnimationController } from '../../../animations/player.js' ;
54import { fadeIn , fadeOut } from '../../../animations/presets/fade/index.js' ;
65import type { AbsolutePosition } from '../../types.js' ;
76import { addInternalsController } from '../controllers/internals.js' ;
8- import { watch } from '../decorators/watch.js' ;
97
108// It'd be better to have this as a mixin rather than a base class once the analyzer
119// knows how to resolve multiple mixin chains
1210
1311export abstract class IgcBaseAlertLikeComponent extends LitElement {
14- protected _autoHideTimeout ?: number ;
12+ declare protected abstract readonly _player : AnimationController ;
1513
16- declare protected abstract _animationPlayer : ReturnType <
17- typeof addAnimationController
18- > ;
14+ protected _autoHideTimeout ?: ReturnType < typeof setTimeout > ;
1915
2016 /**
2117 * Whether the component is in shown state.
@@ -56,28 +52,33 @@ export abstract class IgcBaseAlertLikeComponent extends LitElement {
5652 } ) ;
5753 }
5854
59- @ watch ( 'displayTime' , { waitUntilFirstUpdate : true } )
60- protected displayTimeChange ( ) {
61- this . setAutoHideTimer ( ) ;
62- }
55+ protected override updated ( props : PropertyValues < this > ) : void {
56+ if ( props . has ( 'displayTime' ) ) {
57+ this . _setAutoHideTimer ( ) ;
58+ }
6359
64- @ watch ( 'keepOpen' , { waitUntilFirstUpdate : true } )
65- protected keepOpenChange ( ) {
66- clearTimeout ( this . _autoHideTimeout ) ;
60+ if ( props . has ( 'keepOpen' ) ) {
61+ clearTimeout ( this . _autoHideTimeout ) ;
62+ }
6763 }
6864
69- private async toggleAnimation ( state : 'open' | 'close' ) {
70- const animation = state === 'open' ? fadeIn : fadeOut ;
71-
72- const [ _ , event ] = await Promise . all ( [
73- this . _animationPlayer . stopAll ( ) ,
74- this . _animationPlayer . play ( animation ( ) ) ,
75- ] ) ;
65+ private async _setOpenState ( open : boolean ) : Promise < boolean > {
66+ let state : boolean ;
67+
68+ if ( open ) {
69+ this . open = open ;
70+ state = await this . _player . playExclusive ( fadeIn ( ) ) ;
71+ this . _setAutoHideTimer ( ) ;
72+ } else {
73+ clearTimeout ( this . _autoHideTimeout ) ;
74+ state = await this . _player . playExclusive ( fadeOut ( ) ) ;
75+ this . open = open ;
76+ }
7677
77- return event . type === 'finish' ;
78+ return state ;
7879 }
7980
80- private setAutoHideTimer ( ) {
81+ private _setAutoHideTimer ( ) : void {
8182 clearTimeout ( this . _autoHideTimeout ) ;
8283 if ( this . open && this . displayTime > 0 && ! this . keepOpen ) {
8384 this . _autoHideTimeout = setTimeout ( ( ) => this . hide ( ) , this . displayTime ) ;
@@ -86,26 +87,12 @@ export abstract class IgcBaseAlertLikeComponent extends LitElement {
8687
8788 /** Opens the component. */
8889 public async show ( ) : Promise < boolean > {
89- if ( this . open ) {
90- return false ;
91- }
92-
93- this . open = true ;
94- await this . toggleAnimation ( 'open' ) ;
95- this . setAutoHideTimer ( ) ;
96- return true ;
90+ return this . open ? false : this . _setOpenState ( true ) ;
9791 }
9892
9993 /** Closes the component. */
10094 public async hide ( ) : Promise < boolean > {
101- if ( ! this . open ) {
102- return false ;
103- }
104-
105- clearTimeout ( this . _autoHideTimeout ) ;
106- await this . toggleAnimation ( 'close' ) ;
107- this . open = false ;
108- return true ;
95+ return this . open ? this . _setOpenState ( false ) : false ;
10996 }
11097
11198 /** Toggles the open state of the component. */
0 commit comments