11import '../colors/colors.js' ;
22import '../loading-spinner/loading-spinner.js' ;
3- import { css , html , LitElement } from 'lit' ;
3+ import { css , html , LitElement , nothing } from 'lit' ;
44import { getOffsetParent } from '../../helpers/dom.js' ;
55
66const BACKDROP_DELAY_MS = 800 ;
7- const FADE_IN_DURATION_MS = 500 ;
8- const FADE_OUT_DURATION_MS = 500 ;
9- const SPINNER_DELAY_MS = BACKDROP_DELAY_MS + FADE_IN_DURATION_MS ;
7+ const FADE_DURATION_MS = 500 ;
8+ const SPINNER_DELAY_MS = FADE_DURATION_MS ;
109
1110const reduceMotion = matchMedia ( '(prefers-reduced-motion: reduce)' ) . matches ;
1211
@@ -28,52 +27,47 @@ class LoadingBackdrop extends LitElement {
2827
2928 static get styles ( ) {
3029 return css `
31- :host, .backdrop, d2l-loading-spinner {
32- height: 0%;
33- position: absolute;
34- width: 0%;
35- }
36-
37- .backdrop, d2l-loading-spinner {
38- opacity: 0;
39- }
40-
4130 :host {
31+ display: none;
32+ height: 100%;
33+ justify-content: center;
34+ position: absolute;
4235 top: 0;
36+ width: 100%;
4337 z-index: 999;
4438 }
39+ :host([_state="showing"]),
40+ :host([_state="shown"]),
41+ :host([_state="hiding"]) {
42+ display: flex;
43+ }
4544
4645 .backdrop {
4746 background-color: var(--d2l-color-regolith);
47+ height: 100%;
48+ opacity: 0;
49+ position: absolute;
50+ top: 0;
51+ transition: opacity ${ FADE_DURATION_MS } ms ease-in;
52+ width: 100%;
53+ }
54+ :host([_state="shown"]) .backdrop {
55+ opacity: 0.7;
4856 }
4957
5058 d2l-loading-spinner {
59+ opacity: 0;
60+ position: absolute;
5161 top: 100px;
62+ transition: opacity ${ FADE_DURATION_MS } ms ease-in ${ SPINNER_DELAY_MS } ms;
5263 }
53-
54- :host([_state="showing"]),
55- :host([_state="hiding"]),
56- d2l-loading-spinner[_state="showing"],
57- d2l-loading-spinner[_state="hiding"],
58- .backdrop[_state="showing"],
59- .backdrop[_state="hiding"] {
60- height: 100%;
61- width: 100%;
62- }
63-
64- d2l-loading-spinner[_state="showing"] {
64+ :host([_state="shown"]) d2l-loading-spinner {
6565 opacity: 1;
66- transition: opacity ${ FADE_IN_DURATION_MS } ms ease-in ${ SPINNER_DELAY_MS } ms;
6766 }
68-
69- .backdrop[_state="showing"] {
70- opacity: 0.7;
71- transition: opacity ${ FADE_IN_DURATION_MS } ms ease-in ${ BACKDROP_DELAY_MS } ms;
72- }
73-
74- d2l-loading-spinner[_state="hiding"],
75- .backdrop[_state="hiding"] {
76- transition: opacity ${ FADE_OUT_DURATION_MS } ms ease-out;
67+
68+ :host([_state="hiding"]) .d2l-backdrop,
69+ :host([_state="hiding"]) d2l-loading-spinner {
70+ transition: opacity ${ FADE_DURATION_MS } ms ease-out;
7771 }
7872
7973 @media (prefers-reduced-motion: reduce) {
@@ -85,16 +79,27 @@ class LoadingBackdrop extends LitElement {
8579 constructor ( ) {
8680 super ( ) ;
8781 this . shown = false ;
88- this . _state = null ;
82+ this . _state = 'hidden' ;
8983 }
9084
9185 render ( ) {
86+ if ( this . _state === 'hidden' ) return nothing ;
9287 return html `
93- < div class ="backdrop " _state = ${ this . _state } @transitionend =${ this . #handleTransitionEnd} @transitioncancel=${ this . #hide} > </ div >
94- < d2l-loading-spinner _state = ${ this . _state } size =" ${ this . _state === null ? '0' : '50' } " > </ d2l-loading-spinner >
88+ < div class ="backdrop " @transitionend =" ${ this . #handleTransitionEnd} " @transitioncancel =" ${ this . #hide} " > </ div >
89+ < d2l-loading-spinner > </ d2l-loading-spinner >
9590 ` ;
9691 }
9792
93+ updated ( changedProperties ) {
94+ if ( changedProperties . has ( '_state' ) ) {
95+ if ( this . _state === 'showing' ) {
96+ setTimeout ( ( ) => {
97+ if ( this . _state === 'showing' ) this . _state = 'shown' ;
98+ } , BACKDROP_DELAY_MS ) ;
99+ }
100+ }
101+ }
102+
98103 willUpdate ( changedProperties ) {
99104 if ( changedProperties . has ( 'shown' ) ) {
100105 if ( this . shown ) {
@@ -106,7 +111,7 @@ class LoadingBackdrop extends LitElement {
106111 }
107112
108113 #fade( ) {
109- if ( reduceMotion ) {
114+ if ( reduceMotion || this . _state === 'showing' ) {
110115 this . #hide( ) ;
111116 } else {
112117 this . _state = 'hiding' ;
@@ -120,15 +125,15 @@ class LoadingBackdrop extends LitElement {
120125 }
121126
122127 #hide( ) {
123- this . _state = null ;
128+ this . _state = 'hidden' ;
124129
125130 const containingBlock = getOffsetParent ( this ) ;
126131
127132 if ( containingBlock . dataset . initiallyInert !== '1' ) containingBlock . removeAttribute ( 'inert' ) ;
128133 }
129134
130135 #show( ) {
131- this . _state = 'showing' ;
136+ this . _state = reduceMotion ? 'shown' : 'showing' ;
132137
133138 const containingBlock = getOffsetParent ( this ) ;
134139
0 commit comments