-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
icon-grid.tsx
103 lines (87 loc) · 2.76 KB
/
icon-grid.tsx
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import React from 'react';
import iconsJson from '../../../dist/icons.json';
import tagsJson from '../../../src/tags.json';
const icons = Object.entries(iconsJson).map(([name, svg]) => {
const tags: Array<string> = tagsJson[name] || [];
return { name, svg, tags };
});
export function IconGrid() {
const [query, setQuery] = React.useState(() => {
if (typeof window !== 'undefined') {
return new URLSearchParams(window.location.search).get('query') || '';
}
return '';
});
const deferredQuery = React.useDeferredValue(query);
// Sync the query to the URL
React.useEffect(() => {
if (typeof window !== 'undefined') {
const url = new URL(window.location.href);
if (query) {
url.searchParams.set('query', query);
} else {
url.searchParams.delete('query');
}
window.history.replaceState({}, '', url.toString());
}
}, [query]);
const results = React.useMemo(() => {
if (!deferredQuery) {
return icons;
}
return icons.filter(name => {
return (
name.name.toLowerCase().includes(deferredQuery.toLowerCase()) ||
name.tags.some(tag =>
tag.toLowerCase().includes(deferredQuery.toLowerCase()),
)
);
});
}, [deferredQuery]);
const searchRef = React.useRef<HTMLInputElement>(null);
React.useEffect(() => {
function handleKeydown(event: KeyboardEvent) {
if (!searchRef.current) return;
if (event.key === '/') {
event.preventDefault();
searchRef.current.focus();
}
}
console.log('adding event listener');
window.addEventListener('keydown', handleKeydown);
return () => {
window.removeEventListener('keydown', handleKeydown);
};
}, []);
return (
<div>
<input
ref={searchRef}
className="w-full bg-bg px-4 py-3 border-b border-border sticky top-0 focus:outline-none focus:ring-2 focus:ring-inset focus:ring-border-focus [&::-webkit-search-cancel-button]:appearance-none"
type="search"
placeholder={`Search ${icons.length} icons...`}
value={query}
onChange={event => setQuery(event.target.value)}
/>
<ul className="grid grid-cols-[repeat(auto-fill,minmax(4rem,1fr))]">
{results.map(icon => {
return (
<li className="w-full aspect-square grid place-items-center">
<svg
viewBox="0 0 24 24"
width="24"
height="24"
fill="none"
stroke="currentColor"
stroke-width="2"
stroke-linecap="round"
stroke-linejoin="round"
dangerouslySetInnerHTML={{ __html: icon.svg }}
/>
</li>
);
})}
</ul>
</div>
);
}