|
68 | 68 | ;
|
69 | 69 | Object.setPrototypeOf(_CustomElement.prototype, HTMLElement.prototype);
|
70 | 70 | Object.setPrototypeOf(_CustomElement, HTMLElement);
|
| 71 | + function createCloseButton(el) { |
| 72 | + var closeButton = document.createElement('button'); |
| 73 | + closeButton.innerHTML = closeIcon(); |
| 74 | + closeButton.classList.add('dd-close-button'); |
| 75 | + closeButton.setAttribute('type', 'button'); |
| 76 | + closeButton.setAttribute('aria-label', 'Close dialog'); |
| 77 | + closeButton.setAttribute('data-close-dialog', true); |
| 78 | + el.appendChild(closeButton); |
| 79 | + return closeButton; |
| 80 | + } |
| 81 | + |
| 82 | + function autofocus(el) { |
| 83 | + var autofocus = el.querySelector('[autofocus]'); |
| 84 | + if (!autofocus) { |
| 85 | + autofocus = el; |
| 86 | + el.setAttribute('tabindex', '-1'); |
| 87 | + } |
| 88 | + autofocus.focus(); |
| 89 | + } |
| 90 | + |
| 91 | + function captureDismissal(event) { |
| 92 | + if (event.target.hasAttribute('data-close-dialog') || event.target.closest('[data-close-dialog]')) { |
| 93 | + event.target.closest('details').open = false; |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + function keyDownHelpers(event) { |
| 98 | + if (event.key === 'Escape') { |
| 99 | + event.currentTarget.open = false; |
| 100 | + } else if (event.key === 'Tab') { |
| 101 | + restrictTabBehavior(event); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + function restrictTabBehavior(event) { |
| 106 | + event.preventDefault(); |
| 107 | + |
| 108 | + var dialog = event.currentTarget; |
| 109 | + var elements = Array.from(dialog.querySelectorAll('a, input, button, textarea')).filter(function (element) { |
| 110 | + return !element.disabled && element.offsetWidth > 0 && element.offsetHeight > 0; |
| 111 | + }); |
| 112 | + |
| 113 | + var movement = event.shiftKey ? -1 : 1; |
| 114 | + var currentFocus = elements.filter(function (el) { |
| 115 | + return el.matches(':focus'); |
| 116 | + })[0]; |
| 117 | + var targetIndex = elements.length - 1; |
| 118 | + |
| 119 | + if (currentFocus) { |
| 120 | + var currentIndex = elements.indexOf(currentFocus); |
| 121 | + if (currentIndex !== -1) { |
| 122 | + var newIndex = currentIndex + movement; |
| 123 | + if (newIndex >= 0) targetIndex = newIndex % elements.length; |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + elements[targetIndex].focus(); |
| 128 | + } |
| 129 | + |
| 130 | + // Pulled from https://github.com/primer/octicons |
| 131 | + // We're only using one octicon so it doesn't make sense to include the whole module |
| 132 | + function closeIcon() { |
| 133 | + return '<svg version="1.1" width="12" height="16" viewBox="0 0 12 16" aria-hidden="true"><path d="M7.48 8l3.75 3.75-1.48 1.48L6 9.48l-3.75 3.75-1.48-1.48L4.52 8 .77 4.25l1.48-1.48L6 6.52l3.75-3.75 1.48 1.48z"/></svg>'; |
| 134 | + } |
71 | 135 |
|
72 | 136 | var DetailsDialogElement = function (_CustomElement2) {
|
73 | 137 | _inherits(DetailsDialogElement, _CustomElement2);
|
|
81 | 145 | _createClass(DetailsDialogElement, [{
|
82 | 146 | key: 'connectedCallback',
|
83 | 147 | value: function connectedCallback() {
|
84 |
| - this.createCloseButton(); |
| 148 | + this.closeButton = createCloseButton(this); |
85 | 149 | this.details = this.parentElement;
|
86 | 150 | this.setAttribute('role', 'dialog');
|
87 | 151 |
|
88 |
| - var keyDownHelpers = this.keyDownHelpers.bind(this); |
89 |
| - var captureDismissal = this.captureDismissal.bind(this); |
90 |
| - |
91 | 152 | this.details.addEventListener('toggle', function () {
|
92 | 153 | if (this.details.open) {
|
93 |
| - this.autofocus(); |
| 154 | + autofocus(this); |
94 | 155 | this.details.addEventListener('keydown', keyDownHelpers);
|
95 | 156 | this.addEventListener('click', captureDismissal);
|
96 | 157 | } else {
|
|
128 | 189 | }.bind(this), { capture: true });
|
129 | 190 | }
|
130 | 191 | }, {
|
131 |
| - key: 'createCloseButton', |
132 |
| - value: function createCloseButton() { |
133 |
| - this.closeButton = document.createElement('button'); |
134 |
| - this.closeButton.innerHTML = this.closeIcon(); |
135 |
| - this.closeButton.classList.add('dd-close-button'); |
136 |
| - this.closeButton.setAttribute('type', 'button'); |
137 |
| - this.closeButton.setAttribute('aria-label', 'Close dialog'); |
138 |
| - this.closeButton.setAttribute('data-close-dialog', true); |
139 |
| - this.appendChild(this.closeButton); |
140 |
| - } |
141 |
| - }, { |
142 |
| - key: 'autofocus', |
143 |
| - value: function autofocus() { |
144 |
| - var autofocus = this.querySelector('[autofocus]'); |
145 |
| - if (!autofocus) { |
146 |
| - autofocus = this; |
147 |
| - this.setAttribute('tabindex', '-1'); |
148 |
| - } |
149 |
| - autofocus.focus(); |
150 |
| - } |
151 |
| - }, { |
152 |
| - key: 'captureDismissal', |
153 |
| - value: function captureDismissal(event) { |
154 |
| - if (event.target.hasAttribute('data-close-dialog')) { |
155 |
| - this.details.open = false; |
156 |
| - } |
157 |
| - } |
158 |
| - }, { |
159 |
| - key: 'keyDownHelpers', |
160 |
| - value: function keyDownHelpers(event) { |
161 |
| - if (event.key === 'Escape') { |
162 |
| - event.currentTarget.open = false; |
163 |
| - } else if (event.key === 'Tab') { |
164 |
| - this.restrictTabBehavior(event); |
165 |
| - } |
166 |
| - } |
167 |
| - }, { |
168 |
| - key: 'restrictTabBehavior', |
169 |
| - value: function restrictTabBehavior(event) { |
170 |
| - event.preventDefault(); |
171 |
| - |
172 |
| - var modal = event.currentTarget; |
173 |
| - var elements = Array.from(modal.querySelectorAll('a, input, button, textarea')).filter(function (element) { |
174 |
| - return !element.disabled && element.offsetWidth > 0 && element.offsetHeight > 0; |
175 |
| - }); |
176 |
| - |
177 |
| - elements.unshift(this.closeButton); |
178 |
| - var movement = event.shiftKey ? -1 : 1; |
179 |
| - var currentFocus = elements.filter(function (el) { |
180 |
| - return el.matches(':focus'); |
181 |
| - })[0]; |
182 |
| - var targetIndex = elements.length - 1; |
183 |
| - |
184 |
| - if (currentFocus) { |
185 |
| - var currentIndex = elements.indexOf(currentFocus); |
186 |
| - if (currentIndex !== -1) { |
187 |
| - var newIndex = currentIndex + movement; |
188 |
| - if (newIndex >= 0) targetIndex = newIndex % elements.length; |
189 |
| - } |
190 |
| - } |
191 |
| - |
192 |
| - elements[targetIndex].focus(); |
193 |
| - } |
194 |
| - }, { |
195 |
| - key: 'closeIcon', |
196 |
| - value: function closeIcon() { |
197 |
| - return '<svg version="1.1" width="12" height="16" viewBox="0 0 12 16" aria-hidden="true"><path d="M7.48 8l3.75 3.75-1.48 1.48L6 9.48l-3.75 3.75-1.48-1.48L4.52 8 .77 4.25l1.48-1.48L6 6.52l3.75-3.75 1.48 1.48z"/></svg>'; |
| 192 | + key: 'toggle', |
| 193 | + value: function toggle(boolean) { |
| 194 | + boolean ? this.details.setAttribute('open', true) : this.details.removeAttribute('open'); |
198 | 195 | }
|
199 | 196 | }]);
|
200 | 197 |
|
|
0 commit comments