forked from rstacruz/cheatsheets
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearch.js
More file actions
40 lines (32 loc) · 737 Bytes
/
search.js
File metadata and controls
40 lines (32 loc) · 737 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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('aria-hidden')
})
}
/**
* 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('aria-hidden', true)
})
qsa(selectors).forEach(el => {
el.removeAttribute('aria-hidden')
})
}