1
0
mirror of https://github.com/onkelbeh/cheatsheets.git synced 2026-08-15 21:44:51 +02:00
Files
cheatsheets/_parcel/helpers/search.js
German Capuano 8a9907db52 fix accessibility and search form (#1501)
Co-authored-by: German Capuano <e@gcapu.com>
Co-authored-by: Rico Sta. Cruz <rstacruz@users.noreply.github.com>
2020-07-12 23:34:53 +10:00

44 lines
850 B
JavaScript

import { splitwords } from './permutate'
import qsa from 'dom101/query-selector-all'
/**
* Show everything.
*
* @example
* Search.showAll()
*/
export function showAll() {
qsa('[data-search-index]').forEach((el) => {
el.removeAttribute('hidden')
el.style.removeProperty('display')
})
}
/**
* Search for a given keyword.
*
* @example
* Search.show('hello')
*/
export function show(val) {
const keywords = splitwords(val)
if (!keywords.length) return showAll()
const selectors = keywords
.map((k) => `[data-search-index~=${JSON.stringify(k)}]`)
.join('')
qsa('[data-search-index]').forEach((el) => {
el.setAttribute('hidden', true)
el.style.setProperty('display', "none")
})
qsa(selectors).forEach((el) => {
el.removeAttribute('hidden')
el.style.removeProperty('display')
})
}