Skip to content
This repository was archived by the owner on Sep 22, 2022. It is now read-only.

Commit 6127ed5

Browse files
committed
Autoreset forms on close
1 parent 30178c5 commit 6127ed5

File tree

3 files changed

+50
-11
lines changed

3 files changed

+50
-11
lines changed

dist/index-umd.js

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,6 @@
6868
;
6969
Object.setPrototypeOf(_CustomElement.prototype, HTMLElement.prototype);
7070
Object.setPrototypeOf(_CustomElement, HTMLElement);
71-
var tmpl = document.createElement('template');
72-
tmpl.innerHTML = '\n <style>\n .close-button {\n background: none;\n position: absolute;\n right: 0;\n top: 0;\n padding: 20px;\n border: 0;\n line-height: 1;\n }\n </style>\n <slot></slot>\n <button type="button" class="close-button" aria-label="Close dialog" data-close-dialog>&#9587;</button>\n';
73-
if (window.ShadyCSS) window.ShadyCSS.prepareTemplate(tmpl, 'details-dialog');
7471

7572
var DetailsDialogElement = function (_CustomElement2) {
7673
_inherits(DetailsDialogElement, _CustomElement2);
@@ -80,12 +77,8 @@
8077

8178
var _this = _possibleConstructorReturn(this, (DetailsDialogElement.__proto__ || Object.getPrototypeOf(DetailsDialogElement)).call(this));
8279

83-
if (window.ShadyCSS) window.ShadyCSS.styleElement(_this);
84-
_this.attachShadow({ mode: 'open' });
85-
_this.shadowRoot.appendChild(document.importNode(tmpl.content, true));
86-
80+
_this._createCloseButton();
8781
_this.details = _this.parentElement;
88-
_this.closeButton = _this.shadowRoot.querySelector('.close-button');
8982
_this.setAttribute('role', 'dialog');
9083

9184
var keyDownHelpers = _this._keyDownHelpers.bind(_this);
@@ -95,19 +88,55 @@
9588
if (this.details.open) {
9689
this._autofocus();
9790
this.details.addEventListener('keydown', keyDownHelpers);
98-
this.shadowRoot.addEventListener('click', captureDismissal);
91+
this.addEventListener('click', captureDismissal);
9992
} else {
93+
var _iteratorNormalCompletion = true;
94+
var _didIteratorError = false;
95+
var _iteratorError = undefined;
96+
97+
try {
98+
for (var _iterator = this.querySelectorAll('form')[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
99+
var form = _step.value;
100+
101+
form.reset();
102+
}
103+
} catch (err) {
104+
_didIteratorError = true;
105+
_iteratorError = err;
106+
} finally {
107+
try {
108+
if (!_iteratorNormalCompletion && _iterator.return) {
109+
_iterator.return();
110+
}
111+
} finally {
112+
if (_didIteratorError) {
113+
throw _iteratorError;
114+
}
115+
}
116+
}
117+
100118
var summary = this.details.querySelector('summary');
101119
summary.focus();
102120

103121
this.details.removeEventListener('keydown', keyDownHelpers);
104-
this.shadowRoot.removeEventListener('click', captureDismissal);
122+
this.removeEventListener('click', captureDismissal);
105123
}
106124
}.bind(_this), { capture: true });
107125
return _this;
108126
}
109127

110128
_createClass(DetailsDialogElement, [{
129+
key: '_createCloseButton',
130+
value: function _createCloseButton() {
131+
this.closeButton = document.createElement('button');
132+
this.closeButton.innerHTML = '&#9587;';
133+
this.closeButton.classList.add('dd-close-button');
134+
this.closeButton.setAttribute('type', 'button');
135+
this.closeButton.setAttribute('aria-label', 'Close dialog');
136+
this.closeButton.setAttribute('data-close-dialog', true);
137+
this.appendChild(this.closeButton);
138+
}
139+
}, {
111140
key: '_autofocus',
112141
value: function _autofocus() {
113142
var autofocus = this.querySelector('[autofocus]');

index.html

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,13 @@
7070
Title
7171
</div>
7272
<div class="Box-body overflow-auto">
73-
Lorem ipsum dolor sit amet, <a href="#url">consectetur adipisicing elit</a>, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
73+
<form>
74+
<label>
75+
This input gets reset on close.
76+
<input type="text" autofocus>
77+
</label>
78+
</form>
79+
Lorem ipsum dolor sit amet, <a href="#url">consectetur adipisicing elit</a>, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.<form>
7480
</div>
7581
<div class="Box-footer">
7682
<button type="button" class="btn btn-block" autofocus data-close-dialog>Close</button>

index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ class DetailsDialogElement extends HTMLElement {
1616
this.details.addEventListener('keydown', keyDownHelpers)
1717
this.addEventListener('click', captureDismissal)
1818
} else {
19+
for (const form of this.querySelectorAll('form')) {
20+
form.reset()
21+
}
22+
1923
const summary = this.details.querySelector('summary')
2024
summary.focus()
2125

0 commit comments

Comments
 (0)