|
111 | 111 | return !!(node.isContentEditable) |
112 | 112 | } |
113 | 113 |
|
| 114 | + function isSlashKey(event) { |
| 115 | + return event.key === '/' || event.code === 'Slash' |
| 116 | + } |
| 117 | + |
114 | 118 | function renderPageResults(root, items, query) { |
115 | 119 | if (!query) { |
116 | 120 | root.innerHTML = '<p class="design-paragraph">Type to search docs, pricing, and the rest of the site.</p>' |
|
182 | 186 | var lastFocus = null |
183 | 187 |
|
184 | 188 | function isOpen() { |
185 | | - return !overlay.hidden |
| 189 | + return overlay.classList.contains('is-open') |
186 | 190 | } |
187 | 191 |
|
188 | 192 | function runPage(query) { |
|
211 | 215 | nodes[activeIndex].scrollIntoView({ block: 'nearest' }) |
212 | 216 | } |
213 | 217 |
|
| 218 | + function focusModalInput() { |
| 219 | + try { |
| 220 | + modalInput.focus() |
| 221 | + if (typeof modalInput.select === 'function') modalInput.select() |
| 222 | + } catch (_) {} |
| 223 | + } |
| 224 | + |
214 | 225 | function open() { |
215 | 226 | lastFocus = document.activeElement |
216 | 227 | overlay.hidden = false |
| 228 | + overlay.classList.add('is-open') |
| 229 | + overlay.setAttribute('aria-hidden', 'false') |
217 | 230 | document.body.classList.add('overflow-hidden') |
218 | | - modalInput.focus() |
219 | | - modalInput.select() |
| 231 | + focusModalInput() |
| 232 | + window.requestAnimationFrame(focusModalInput) |
220 | 233 | if (index && modalInput.value) runModal(modalInput.value) |
221 | 234 | } |
222 | 235 |
|
223 | 236 | function close() { |
224 | 237 | if (!isOpen()) return |
| 238 | + overlay.classList.remove('is-open') |
225 | 239 | overlay.hidden = true |
| 240 | + overlay.setAttribute('aria-hidden', 'true') |
226 | 241 | document.body.classList.remove('overflow-hidden') |
227 | | - if (lastFocus && typeof lastFocus.focus === 'function') { |
| 242 | + if (lastFocus && typeof lastFocus.focus === 'function' && lastFocus !== modalInput) { |
228 | 243 | try { lastFocus.focus() } catch (_) {} |
229 | 244 | } |
230 | 245 | } |
|
240 | 255 | if (pageInput) { |
241 | 256 | if (initial) pageInput.value = initial |
242 | 257 | runPage(pageInput.value) |
| 258 | + pageInput.focus() |
243 | 259 | } else if (initial) { |
244 | 260 | modalInput.value = initial |
245 | 261 | } |
|
296 | 312 | if (event.target === overlay) close() |
297 | 313 | }) |
298 | 314 |
|
299 | | - window.addEventListener('keydown', function (event) { |
| 315 | + document.addEventListener('keydown', function (event) { |
300 | 316 | if (event.key === 'Escape' && isOpen()) { |
301 | 317 | event.preventDefault() |
302 | 318 | close() |
303 | 319 | return |
304 | 320 | } |
305 | 321 |
|
306 | | - if (event.key === '/' && !event.metaKey && !event.ctrlKey && !event.altKey && !isOpen()) { |
| 322 | + if (isSlashKey(event) && !event.metaKey && !event.ctrlKey && !event.altKey && !isOpen()) { |
307 | 323 | if (isTypingTarget(event.target)) return |
308 | 324 | event.preventDefault() |
309 | 325 | open() |
|
319 | 335 | event.preventDefault() |
320 | 336 | setActive(activeIndex - 1) |
321 | 337 | } |
| 338 | + }, true) |
| 339 | + |
| 340 | + modalInput.addEventListener('keypress', function (event) { |
| 341 | + if (!isSlashKey(event) || modalInput.value) return |
| 342 | + event.preventDefault() |
322 | 343 | }) |
323 | 344 | }) |
324 | 345 | })() |
0 commit comments