blob: 3af6c75f607963abe83b3fc4ae13d46eb03b4ee1 [file] [log] [blame]
Copybara botbe50d492023-11-30 00:16:42 +01001<!DOCTYPE html>
2<html>
3<meta name="viewport" content="initial-scale=1" />
4<meta charset="UTF-8" />
5<head>
6 <script src="../dist/dialog-polyfill.js"></script>
7 <link rel="stylesheet" type="text/css" href="../dist/dialog-polyfill.css">
8 <style>
9
10 dialog.actionbar {
11 position: fixed;
12 border: 0;
13 box-sizing: border-box;
14 width: 100%;
15 bottom: 0;
16 background: #fff;
17 transform: translate(0, 100%);
18 transition: transform 0.25s ease-in-out;
19 margin: 0;
20 padding: 0;
21 box-shadow: 0 0 4px rgba(0, 0, 0, 0.25);
22 }
23 dialog.actionbar.appear {
24 transform: translate(0);
25 }
26
27 dialog.actionbar .holder {
28 display: flex;
29 justify-content: space-around;
30 flex-flow: wrap;
31 }
32 dialog.actionbar .holder button {
33 flex-grow: 1;
34 min-width: 200px;
35 margin: 8px;
36 border: 0;
37 font-family: Roboto, Arial, Sans-Serif;
38 font-size: 23px;
39 line-height: 33px;
40 font-weight: 400;
41 color: #666;
42 border-radius: 2px;
43 background: #ccc;
44 display: block;
45 box-sizing: border-box;
46 border: 2px solid transparent;
47 }
48 dialog.actionbar .holder button:focus {
49 outline: 0;
50 border-color: rgba(0, 0, 0, 0.125);
51 }
52 dialog.actionbar .holder button.cancel {
53 background: #fcc;
54 }
55
56 dialog.actionbar::backdrop {
57 background: rgba(0, 0, 0, 0.25);
58 opacity: 0;
59 transition: opacity 0.25s ease-in-out;
60 }
61 dialog.actionbar + .backdrop {
62 background: rgba(0, 0, 0, 0.25);
63 opacity: 0;
64 transition: opacity 0.25s ease-in-out;
65 }
66 dialog.actionbar.appear::backdrop {
67 opacity: 1;
68 }
69 dialog.actionbar.appear + .backdrop {
70 opacity: 1;
71 }
72
73 </style>
74</head>
75<body>
76
77<p>Builds an action bar from a modal dialog.</p>
78
79<button id="show">Show</button>
80
81<script>
82
83var ActionBar = function() {
84 this.options_ = [];
85 this.cancelButton_ = true;
86 this.dialog_ = null;
87};
88
89ActionBar.prototype.enableCancelButton = function(value) {
90 this.cancelButton_ = vaue;
91};
92
93ActionBar.prototype.addOption = function(name, opt_callback) {
94 this.options_.push({name: name, callback: opt_callback || null});
95};
96
97ActionBar.prototype.show = function(opt_callback) {
98 if (this.dialog_ != null) {
99 throw "Can't show ActionBar, already visible";
100 }
101
102 var dialog = document.createElement('dialog');
103 if (!('showModal' in dialog)) {
104 dialogPolyfill.registerDialog(dialog);
105 }
106 dialog.className = 'actionbar';
107
108 var holder = document.createElement('div');
109 holder.className = 'holder';
110 dialog.appendChild(holder);
111
112 dialog.addEventListener('click', function(ev) {
113 if (ev.target == dialog) {
114 dialog.close();
115 }
116 });
117
118 dialog.addEventListener('close', function() {
119 opt_callback && opt_callback(dialog.returnValue);
120
121 var cloneDialog = dialog.cloneNode(true);
122 if (!('showModal' in cloneDialog)) {
123 dialogPolyfill.registerDialog(cloneDialog);
124 }
125
126 document.body.appendChild(cloneDialog);
127 cloneDialog.showModal();
128 cloneDialog.classList.remove('appear');
129 window.setTimeout(function() {
130 cloneDialog.close(); // TODO: needed until DOM removal is safe
131 cloneDialog.parentNode.removeChild(cloneDialog);
132 }, 250);
133
134 if (dialog.parentNode) {
135 dialog.parentNode.removeChild(dialog);
136 }
137 if (this.dialog_ == dialog) {
138 this.dialog_ = null;
139 }
140 }.bind(this));
141
142 this.options_.forEach(function(option) {
143 var button = document.createElement('button');
144 button.textContent = option.name;
145 button.addEventListener('click', function() {
146 if (option.callback) {
147 window.setTimeout(option.callback, 0);
148 }
149 dialog.close(option.name);
150 });
151 holder.appendChild(button);
152 });
153
154 if (this.cancelButton_) {
155 var cancelButton = document.createElement('button');
156 cancelButton.className = 'cancel';
157 cancelButton.textContent = 'Cancel';
158 cancelButton.addEventListener('click', function() {
159 dialog.close();
160 });
161 holder.appendChild(cancelButton);
162 }
163
164 document.body.appendChild(dialog);
165 dialog.showModal();
166 dialog.classList.add('appear');
167
168 this.dialog_ = dialog;
169};
170
171////// DEMO
172
173show.addEventListener('click', function() {
174 var ab = new ActionBar();
175 ab.addOption('Display');
176 ab.addOption('Options');
177 ab.addOption('Longer Choice');
178 ab.addOption('Alert', function() {
179 alert('you hit Alert');
180 });
181 ab.show(function(choice) {
182 console.info('choice was', choice);
183 });
184});
185
186</script>
187</body>
188</html>