|
| 1 | +--- |
| 2 | +import { Sprite } from 'astro-icon'; |
| 3 | +
|
| 4 | +export interface Props { |
| 5 | + id: string; |
| 6 | + placeholder: string; |
| 7 | + selector: string; |
| 8 | +} |
| 9 | +
|
| 10 | +const { id, placeholder, selector = '#integrations > *' } = Astro.props as Props; |
| 11 | +
|
| 12 | +const inlineStyle = ` |
| 13 | + ${selector}[hidden] { |
| 14 | + display: none; |
| 15 | + } |
| 16 | +
|
| 17 | +` |
| 18 | +--- |
| 19 | + |
| 20 | +<section> |
| 21 | + <div> |
| 22 | + <input type="text" {id} {placeholder} aria-label="Search"> |
| 23 | + <button aria-hidden="true"> |
| 24 | + <Sprite pack="heroicons-outline" name="search" size={24} /> |
| 25 | + </button> |
| 26 | + </div> |
| 27 | +</section> |
| 28 | + |
| 29 | +<style set:html={inlineStyle}></style> |
| 30 | + |
| 31 | +<script src="https://polyfill.io/v3/polyfill.min.js?features=URLSearchParams" /> |
| 32 | +<script type="module" define:vars={{selector, id}}> |
| 33 | + /* https://gomakethings.com/how-to-create-a-search-page-for-a-static-website-with-vanilla-js/ */ |
| 34 | + const stopWords = ['a', 'an', 'and', 'are', 'aren\'t', 'as', 'by', 'can', 'cannot', 'can\'t', 'could', 'couldn\'t', 'how', 'is', 'isn\'t', 'it', 'its', 'it\'s', 'that', 'the', 'their', 'there', 'they', 'they\'re', 'them', 'to', 'too', 'us', 'very', 'was', 'we', 'well', 'were', 'what', 'whatever', 'when', 'whenever', 'where', 'with', 'would', 'yet', 'you', 'your', 'yours', 'yourself', 'yourselves', 'the']; |
| 35 | + |
| 36 | + const input = document.getElementById(id); |
| 37 | + const button = document.querySelector(`#${id} + button`); |
| 38 | + const items = document.querySelectorAll(selector); |
| 39 | + |
| 40 | + const FPS_30 = 1000 / 30; |
| 41 | + |
| 42 | + function debounce(fn, timeout = 150) { |
| 43 | + let timer; |
| 44 | + return (...args) => { |
| 45 | + clearTimeout(timer); |
| 46 | + timer = setTimeout(() => fn.apply(this, args), timeout); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + const updateOpen = () => { |
| 51 | + requestAnimationFrame(() => { |
| 52 | + (!!input.value || document.activeElement === input) |
| 53 | + ? input.parentElement.setAttribute('open', '') |
| 54 | + : input.parentElement.removeAttribute('open'); |
| 55 | + }); |
| 56 | + } |
| 57 | + |
| 58 | + const update = () => { |
| 59 | + updateOpen(); |
| 60 | + |
| 61 | + const regexMap = input.value.toLowerCase() |
| 62 | + .split(' ') |
| 63 | + .filter(word => word.length && !stopWords.includes(word)) |
| 64 | + .map(word => new RegExp(word, 'i')); |
| 65 | + |
| 66 | + for (const item of items) { |
| 67 | + const isMatch = !regexMap.length || regexMap.some(regex => !!item.textContent.match(regex)); |
| 68 | + isMatch ? item.removeAttribute('hidden') : item.setAttribute('hidden', ''); |
| 69 | + } |
| 70 | + |
| 71 | + updateURL() |
| 72 | + } |
| 73 | + |
| 74 | + const updateURL = () => { |
| 75 | + const q = input.value?.trim() |
| 76 | + |
| 77 | + if (q) { |
| 78 | + const params = new URLSearchParams() |
| 79 | + params.set('q', q) |
| 80 | + window.history.replaceState(null, null, `?${params.toString()}`) |
| 81 | + } else { |
| 82 | + const [href] = window.location.href.split('?') |
| 83 | + window.history.replaceState(null, null, href) |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + const hydrateQuery = () => { |
| 88 | + const params = new URLSearchParams(location.search) |
| 89 | + input.value = params.get('q') || '' |
| 90 | + } |
| 91 | + |
| 92 | + input.addEventListener('input', debounce(update, FPS_30), true); |
| 93 | + button.addEventListener('click', input.focus, true); |
| 94 | + input.addEventListener('focus', updateOpen, true); |
| 95 | + input.addEventListener('blur', updateOpen, true); |
| 96 | + |
| 97 | + hydrateQuery() |
| 98 | + update(); |
| 99 | +</script> |
| 100 | + |
| 101 | +<style> |
| 102 | + section { |
| 103 | + display: flex; |
| 104 | + justify-content: flex-end; |
| 105 | + font-size: 16px; |
| 106 | + width: calc(25ch + 2.5rem); |
| 107 | + } |
| 108 | + |
| 109 | + div { |
| 110 | + display: flex; |
| 111 | + border-radius: 9999px; |
| 112 | + padding: 0.25rem; |
| 113 | + background: var(--color-white); |
| 114 | + } |
| 115 | + |
| 116 | + input { |
| 117 | + line-height: 2.5rem; |
| 118 | + width: 25ch; |
| 119 | + max-width: 25ch; |
| 120 | + padding-left: 0.75rem; |
| 121 | + background: transparent; |
| 122 | + font-size: 16px; |
| 123 | + outline: none; |
| 124 | + } |
| 125 | + |
| 126 | + input::placeholder { |
| 127 | + color: var(--color-midnight); |
| 128 | + opacity: 0.75; |
| 129 | + } |
| 130 | + |
| 131 | + button { |
| 132 | + display: flex; |
| 133 | + align-items: center; |
| 134 | + justify-content: center; |
| 135 | + width: 2.5rem; |
| 136 | + height: 2.5rem; |
| 137 | + border-radius: 9999px; |
| 138 | + background: var(--color-dusk); |
| 139 | + color: var(--color-white); |
| 140 | + } |
| 141 | + |
| 142 | + @media (max-width: 768px) { |
| 143 | + div, section { |
| 144 | + width: 100%; |
| 145 | + } |
| 146 | + |
| 147 | + input { |
| 148 | + flex: 1; |
| 149 | + max-width: unset; |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + @media(hover: hover) { |
| 154 | + div { |
| 155 | + background: transparent; |
| 156 | + } |
| 157 | + |
| 158 | + input { |
| 159 | + max-width: 0; |
| 160 | + padding-left: 0; |
| 161 | + } |
| 162 | + |
| 163 | + input, button { |
| 164 | + transition: 250ms ease; |
| 165 | + } |
| 166 | + |
| 167 | + input:focus, |
| 168 | + input:not(:placeholder-shown) { |
| 169 | + max-width: initial; |
| 170 | + padding: 0 .5rem; |
| 171 | + padding-left: 0.75rem; |
| 172 | + } |
| 173 | + |
| 174 | + button { |
| 175 | + background: transparent; |
| 176 | + color: var(--color-midnight); |
| 177 | + } |
| 178 | + |
| 179 | + div:hover, div[open] { |
| 180 | + background: var(--color-white); |
| 181 | + } |
| 182 | + |
| 183 | + div:hover > input { |
| 184 | + max-width: 25ch; |
| 185 | + padding: 0 .5rem; |
| 186 | + padding-left: 0.75rem; |
| 187 | + } |
| 188 | + |
| 189 | + div:hover > button, |
| 190 | + input:focus + button, |
| 191 | + input:not(:placeholder-shown) + button { |
| 192 | + background: var(--color-dusk); |
| 193 | + color: var(--color-white); |
| 194 | + } |
| 195 | + } |
| 196 | +</style> |
0 commit comments