1- import {
2- openOverlay ,
3- closeOverlay ,
4- addOverlayClickListener ,
5- } from "./overlay.js" ;
6-
71function getCloseButton ( dialog ) {
82 return dialog . querySelector ( ".o-dialog__header > .a-button" ) ;
93}
@@ -12,31 +6,23 @@ function openDialog(dialogId) {
126 const dialog = document . getElementById ( dialogId ) ;
137 if ( ! dialog ) return ;
148
15- dialog . show ( ) ;
16- openOverlay ( "dialog" ) ;
17-
18- const closeButton = getCloseButton ( dialog ) ;
19- if ( closeButton ) {
20- closeButton . addEventListener ( "click" , ( ) => closeDialog ( dialog ) ) ;
21- }
9+ dialog . showModal ( ) ;
2210}
2311
24- function closeDialog ( dialog ) {
25- const closeButton = getCloseButton ( dialog ) ;
26- if ( closeButton ) {
27- closeButton . replaceWith ( closeButton . cloneNode ( true ) ) ;
28- }
29- dialog . close ( ) ;
30- closeOverlay ( ) ;
31- }
12+ function initDialogs ( ) {
13+ document . querySelectorAll ( "dialog" ) . forEach ( ( dialog ) => {
14+ dialog . addEventListener ( "click" , ( e ) => {
15+ if ( e . target === dialog ) {
16+ dialog . close ( ) ;
17+ }
18+ } ) ;
3219
33- function closeAllDialogs ( ) {
34- document . querySelectorAll ( "dialog[open]" ) . forEach ( ( dialog ) => {
35- closeDialog ( dialog ) ;
20+ const closeButton = getCloseButton ( dialog ) ;
21+ if ( closeButton ) {
22+ closeButton . addEventListener ( "click" , ( ) => dialog . close ( ) ) ;
23+ }
3624 } ) ;
37- }
3825
39- function initDialogs ( ) {
4026 document . querySelectorAll ( "[data-dialog-trigger]" ) . forEach ( ( trigger ) => {
4127 const handler = ( e ) => {
4228 if ( e . type === "click" || ( e . type === "keydown" && e . key === "Enter" ) ) {
@@ -49,16 +35,6 @@ function initDialogs() {
4935 trigger . addEventListener ( "click" , handler ) ;
5036 trigger . addEventListener ( "keydown" , handler ) ;
5137 } ) ;
52-
53- addOverlayClickListener ( ( ) => {
54- closeAllDialogs ( ) ;
55- } ) ;
56-
57- document . addEventListener ( "keydown" , ( e ) => {
58- if ( e . key === "Escape" ) {
59- closeAllDialogs ( ) ;
60- }
61- } ) ;
6238}
6339
6440if ( document . readyState === "loading" ) {
0 commit comments