@@ -8,7 +8,6 @@ import 'package:web/web.dart';
88
99import '_focusability.dart' ;
1010import 'deferred/markdown.dart' deferred as md;
11- import 'mdc/mdc_dialog.dart' ;
1211import 'web_util.dart' ;
1312
1413/// Displays a message via the modal window.
@@ -51,6 +50,13 @@ Future<bool> modalWindow({
5150 allowedComponents: [content],
5251 );
5352 final c = Completer <bool >();
53+
54+ void cancel () {
55+ if (! c.isCompleted) {
56+ c.complete (false );
57+ }
58+ }
59+
5460 final root = _buildDialog (
5561 titleText: titleText,
5662 content: content,
@@ -61,21 +67,31 @@ Future<bool> modalWindow({
6167 c.complete (pushedOk);
6268 }
6369 },
70+ onDismiss: cancel,
6471 cancelButtonText: cancelButtonText,
6572 okButtonText: okButtonText,
6673 );
74+
75+ // Close the dialog when the escape key was hit.
76+ final keySubscription = window.onKeyDown.listen ((event) {
77+ if (event.key == 'Escape' ) {
78+ event.preventDefault ();
79+ cancel ();
80+ }
81+ });
82+
6783 document.body! .append (root);
68- final dialog = MdcDialog .attachTo (root);
84+ document.body! .classList.add ('pub-dialog-scroll-lock' );
85+ // Note: this forces a layout event, so that the open transition animates from the hidden state.
86+ root.getBoundingClientRect ();
87+ root.classList.add ('pub-dialog--open' );
6988 try {
70- dialog.open ();
71- dialog.listenOnClose (() => c.complete (false ));
7289 await c.future;
7390 } finally {
91+ await keySubscription.cancel ();
7492 restoreFocusabilityFn ();
75- dialog.destroy ();
7693 root.remove ();
77- // Note: This seems to be a bug in the JS library
78- document.body! .classList.remove ('mdc-dialog-scroll-lock' );
94+ document.body! .classList.remove ('pub-dialog-scroll-lock' );
7995 }
8096 return await c.future;
8197}
@@ -90,27 +106,26 @@ Element _buildDialog({
90106 /// The callback will be called with `true` when "OK" was clicked, and `false`
91107 /// when "Cancel" was clicked.
92108 required void Function (bool ) closing,
109+
110+ /// Called when the dialog is dismissed without pressing a button (e.g. click outside).
111+ required void Function () onDismiss,
93112}) {
94113 final title = document.createElement ('h2' ) as HTMLElement
95- ..classList.add ('mdc-dialog__title ' )
114+ ..classList.add ('pub-dialog-title ' )
96115 ..id = 'pub-dialog-title'
97116 ..innerText = titleText;
98117
99118 final contentDiv = HTMLDivElement ()
100- ..classList.add ('mdc-dialog__content ' )
119+ ..classList.add ('pub-dialog-content ' )
101120 ..id = 'pub-dialog-content'
102121 ..append (content);
103122
104- final footer = document.createElement ('footer' );
105- footer. classList.add ('mdc-dialog__actions ' );
123+ final footer = document.createElement ('footer' )
124+ .. classList.add ('pub-dialog-actions ' );
106125
107126 if (isQuestion) {
108127 final cancelBtn = HTMLButtonElement ()
109- ..classList.addAll ([
110- 'pub-button' ,
111- 'mdc-dialog__button' ,
112- '-pub-dom-dialog-cancel-button' ,
113- ])
128+ ..classList.addAll (['pub-button' , '-pub-dom-dialog-cancel-button' ])
114129 ..tabIndex = 2
115130 ..innerText = cancelButtonText ?? 'Cancel' ;
116131 cancelBtn.onClick.listen ((e) {
@@ -121,11 +136,7 @@ Element _buildDialog({
121136 }
122137
123138 final okBtn = HTMLButtonElement ()
124- ..classList.addAll ([
125- 'pub-button' ,
126- 'mdc-dialog__button' ,
127- '-pub-dom-dialog-ok-button' ,
128- ])
139+ ..classList.addAll (['pub-button' , '-pub-dom-dialog-ok-button' ])
129140 ..tabIndex = 1
130141 ..innerText = okButtonText ?? 'Ok' ;
131142 okBtn.onClick.listen ((e) {
@@ -135,23 +146,29 @@ Element _buildDialog({
135146 footer.append (okBtn);
136147
137148 final surface = HTMLDivElement ()
138- ..classList.add ('mdc-dialog__surface ' )
149+ ..classList.add ('pub-dialog-surface ' )
139150 ..append (title)
140151 ..append (contentDiv)
141152 ..append (footer);
142153
143154 final container = HTMLDivElement ()
144- ..classList.add ('mdc-dialog__container ' )
155+ ..classList.add ('pub-dialog-container ' )
145156 ..append (surface);
146157
158+ final scrim = HTMLDivElement ()..classList.add ('pub-dialog-scrim' );
159+ scrim.onClick.listen ((e) {
160+ e.preventDefault ();
161+ onDismiss ();
162+ });
163+
147164 final root = HTMLDivElement ()
148- ..classList.add ('mdc -dialog' )
165+ ..classList.add ('pub -dialog' )
149166 ..setAttribute ('role' , 'alertdialog' )
150- ..setAttribute ('aria-model ' , 'true' )
167+ ..setAttribute ('aria-modal ' , 'true' )
151168 ..setAttribute ('aria-labelledby' , 'pub-dialog-title' )
152169 ..setAttribute ('aria-describedby' , 'pub-dialog-content' )
153- ..append (container )
154- ..append (HTMLDivElement ()..classList. add ( 'mdc-dialog__scrim' ) );
170+ ..append (scrim )
171+ ..append (container );
155172
156173 return root;
157174}
0 commit comments