-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
132 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
:root { | ||
color-scheme: light dark; | ||
|
||
--color-text: #000; | ||
--color-bg: #fff; | ||
--color-border: #ccc; | ||
--color-border-focus: #0d99ff; | ||
} | ||
|
||
@media (prefers-color-scheme: dark) { | ||
:root { | ||
--color-text: #eeee; | ||
--color-bg: #111; | ||
--color-border: #333; | ||
--color-border-focus: #0c8ce9; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,12 @@ | ||
--- | ||
import icons from '../../../dist/icons.json'; | ||
import '../index.css'; | ||
import { IconGrid } from '../components/icon-grid'; | ||
--- | ||
|
||
<html> | ||
<body> | ||
<body class="text-text bg-bg"> | ||
<main> | ||
<ul class="grid grid-cols-[repeat(auto-fill,minmax(4rem,1fr))]"> | ||
{ | ||
Object.entries(icons).map(([name, svg]) => { | ||
return ( | ||
<li class="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" | ||
set:html={svg} | ||
/> | ||
</li> | ||
); | ||
}) | ||
} | ||
</ul> | ||
<IconGrid client:load /> | ||
</main> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters