1
1
import { html } from '@aegisjsproject/core/parsers/html.js' ;
2
- import { css } from '@aegisjsproject/core/parsers/css.js' ;
3
2
import { AegisComponent } from '@aegisjsproject/component/base.js' ;
4
3
import { SYMBOLS , TRIGGERS } from '@aegisjsproject/component/consts.js' ;
5
- import { dark , light } from '@aegisjsproject/styles/palette/gnome.js' ;
6
- import { registerCallback } from '@aegisjsproject/core/callbackRegistry.js' ;
7
- import { EVENTS , AEGIS_EVENT_HANDLER_CLASS } from '@aegisjsproject/core/events.js' ;
8
- import { closeIcon } from './icons.js' ;
9
-
10
- const inertMap = new WeakMap ( ) ;
11
-
12
- const tabElsSelector = 'a[href]:not([inert]), input:not([inert]), select:not([inert]), textarea:not([inert]), button:not([inert]), iframe:not([inert]), audio:not([inert]), video:not([inert]), [tabindex]:not([inert]), [contenteditable="true"]:not([inert], [is]:defined:not([inert])' ;
13
-
14
- const getCustomElements = ( ) => Array . from ( document . querySelectorAll ( ':defined' ) )
15
- . filter ( el => el . tagName . includes ( '-' ) || el . hasAttribute ( 'is' ) ) ;
16
-
17
- function getOtherElements ( modal ) {
18
- return [
19
- ...Array . from ( document . body . querySelectorAll ( tabElsSelector ) ) ,
20
- ...getCustomElements ( modal ) ,
21
- ] . filter ( el => ! ( modal . isSameNode ( el ) || modal . contains ( el ) || el . contains ( modal ) ) ) ;
22
- }
23
-
24
- function toggleInert ( modal , inert ) {
25
- if ( inert ) {
26
- const els = getOtherElements ( modal ) ;
27
- inertMap . set ( modal , els ) ;
28
- els . forEach ( el => el . inert = true ) ;
29
- } else if ( inertMap . has ( modal ) ) {
30
- inertMap . get ( modal ) . forEach ( el => el . inert = false ) ;
31
- inertMap . delete ( modal ) ;
32
- }
33
- }
34
-
35
- const styles = css `
36
- :host(:not([open])) {
37
- display: none;
38
- }
39
-
40
- :host([open]:not([theme])), :host([open][theme]) {
41
- background-color: transparent;
42
- }
43
-
44
- :host([open]) {
45
- position: fixed;
46
- z-index: 2147483647;
47
- top: 0;
48
- left: 0;
49
- bottom: 0;
50
- right: 0;
51
- isolation: isolate;
52
- }
53
-
54
- :host([open]) .backdrop {
55
- width: 100%;
56
- height: 100%;
57
- background-color: rgba(0, 0, 0, 0.7);
58
- display: flex;
59
- align-items: center;
60
- justify-content: center;
61
- }
62
-
63
- .sticky {
64
- position: sticky;
65
- top: 0;
66
- }
67
-
68
- .flex {
69
- display: flex;
70
- }
71
-
72
- .flex.row {
73
- flex-direction: row;
74
- }
75
-
76
- .flex.no-wrap {
77
- flex-wrap: no-wrap;
78
- }
79
-
80
- .container {
81
- display: flex;
82
- min-width: 85%;
83
- max-width: 95%;
84
- min-height: 50%;
85
- max-height: 95%;
86
- overflow: auto;
87
- isolation: isolate;
88
- flex-direction: column;
89
- }
90
-
91
- .header {
92
- background-color: ${ dark [ 3 ] } ;
93
- color: ${ light [ 1 ] } ;
94
- padding: 0.7rem;
95
- }
96
-
97
- .header-container {
98
- flex-grow: 1;
99
- }
100
-
101
- .close-btn {
102
- display: inline-block;
103
- width: 1.3rem;
104
- height: 1.3rem;
105
- cursor: pointer;
106
- background-color: transparent;
107
- border: none;
108
- box-sizing: content-box;
109
- color: inherit;
110
- }
111
-
112
- .body {
113
- background-color: ${ light [ 1 ] } ;
114
- color: ${ dark [ 4 ] } ;
115
- flex-grow: 1;
116
- padding: 0.4rem;
117
- }
118
-
119
- :host([theme="dark"]) .body {
120
- background-color: ${ dark [ 2 ] } ;
121
- color: ${ light [ 1 ] } ;
122
- }
123
-
124
- @media (prefers-color-scheme: dark) {
125
- :host(:not([theme="light"])) .body {
126
- background-color: ${ dark [ 2 ] } ;
127
- color: ${ light [ 1 ] } ;
128
- }
129
- }
130
- ` ;
131
-
132
- const closeHandler = registerCallback (
133
- 'aegis-modal:close' ,
134
- event => event . target . getRootNode ( ) . host . close ( ) ,
135
- ) ;
136
-
137
-
138
- const template = html `
139
- < div part ="backdrop " class ="backdrop ">
140
- < div part ="container " id ="container " class ="container ">
141
- < div part ="header " id ="header " class ="header sticky top flex row no-wrap ">
142
- < div class ="header-container ">
143
- < slot name ="header "> </ slot >
144
- </ div >
145
- < button type ="button " title ="Close modal " ${ EVENTS . onClick } ="${ closeHandler } " id="close" class="btn close-btn ${ AEGIS_EVENT_HANDLER_CLASS } " part="btn close" aria-label="Close Modal" aria-keyshortcuts="Escape">
146
- < slot name ="close-icon "> X</ slot >
147
- </ button >
148
- </ div >
149
- < div part ="body " id ="body " class ="body ">
150
- < slot id ="content "> </ slot >
151
- </ div >
152
- </ div >
153
- </ div >
154
- ` ;
4
+ import { styles } from './styles.js' ;
5
+ import { template } from './template.js' ;
6
+ import { toggleInert , renable } from './functions.js' ;
155
7
156
8
export class AegisModalElement extends AegisComponent {
157
9
#removeController;
@@ -168,7 +20,6 @@ export class AegisModalElement extends AegisComponent {
168
20
internals . ariaHidden = this . open ? 'false' : 'true' ;
169
21
internals . ariaLabel = 'Aegis Modal Dialog' ;
170
22
internals . ariaModal = 'true' ;
171
- shadow . getElementById ( 'close' ) . replaceChildren ( closeIcon . cloneNode ( true ) ) ;
172
23
setTimeout ( ( ) => console . log ( internals ) , 2000 ) ;
173
24
this . hidden = ! this . open ;
174
25
break ;
@@ -184,10 +35,8 @@ export class AegisModalElement extends AegisComponent {
184
35
this . close ( ) ;
185
36
}
186
37
187
- if ( inertMap . has ( this ) ) {
188
- toggleInert ( this , false ) ;
189
- inertMap . delete ( this ) ;
190
- }
38
+ renable ( this ) ;
39
+
191
40
break ;
192
41
193
42
case TRIGGERS . slotChanged :
0 commit comments