Add icons to menu

feat/cmd-k
Kamran Ahmed 2 years ago
parent c4f3c3529d
commit 80dac2f7f7
  1. 58
      src/components/CommandMenu/CommandMenu.tsx
  2. 9
      src/icons/best-practices.svg
  3. 3
      src/icons/guide.svg
  4. 3
      src/icons/home.svg
  5. 1
      src/icons/roadmap.svg
  6. 3
      src/icons/user.svg
  7. 3
      src/icons/video.svg

@ -1,22 +1,48 @@
import { useEffect, useRef, useState } from 'preact/hooks'; import { useEffect, useRef, useState } from 'preact/hooks';
import BestPracticesIcon from '../../icons/best-practices.svg';
import HomeIcon from '../../icons/home.svg';
import UserIcon from '../../icons/user.svg';
import RoadmapIcon from '../../icons/roadmap.svg';
import GuideIcon from '../../icons/guide.svg';
import VideoIcon from '../../icons/video.svg';
import { httpGet } from '../../lib/http'; import { httpGet } from '../../lib/http';
import { useKeydown } from '../../hooks/use-keydown'; import { useKeydown } from '../../hooks/use-keydown';
import { isLoggedIn } from '../../lib/jwt';
type PageType = { type PageType = {
url: string; url: string;
title: string; title: string;
group: string; group: string;
icon?: string;
isProtected?: boolean;
}; };
const defaultPages: PageType[] = [ const defaultPages: PageType[] = [
{ url: '/', title: 'Home', group: 'Pages' }, { url: '/', title: 'Home', group: 'Pages', icon: HomeIcon },
{ url: '/settings/update-profile', title: 'Account', group: 'Pages' }, {
{ url: '/roadmaps', title: 'Roadmaps', group: 'Pages' }, url: '/settings/update-profile',
{ url: '/best-practices', title: 'Best Practices', group: 'Pages' }, title: 'Account',
{ url: '/guides', title: 'Guides', group: 'Pages' }, group: 'Pages',
{ url: '/videos', title: 'Videos', group: 'Pages' }, icon: UserIcon,
isProtected: true,
},
{ url: '/roadmaps', title: 'Roadmaps', group: 'Pages', icon: RoadmapIcon },
{
url: '/best-practices',
title: 'Best Practices',
group: 'Pages',
icon: BestPracticesIcon,
},
{ url: '/guides', title: 'Guides', group: 'Pages', icon: GuideIcon },
{ url: '/videos', title: 'Videos', group: 'Pages', icon: VideoIcon },
]; ];
function shouldShowPage(page: PageType) {
const isUser = isLoggedIn();
return !page.isProtected || isUser;
}
export function CommandMenu() { export function CommandMenu() {
const inputRef = useRef<HTMLInputElement>(null); const inputRef = useRef<HTMLInputElement>(null);
const [isActive, setIsActive] = useState(false); const [isActive, setIsActive] = useState(false);
@ -41,20 +67,19 @@ export function CommandMenu() {
if (allPages.length > 0) { if (allPages.length > 0) {
return allPages; return allPages;
} }
const { error, response } = await httpGet<PageType[]>(`/pages.json`); const { error, response } = await httpGet<PageType[]>(`/pages.json`);
if (!response) { if (!response) {
return defaultPages; return defaultPages.filter(shouldShowPage);
} }
setAllPages([...defaultPages, ...response]); setAllPages([...defaultPages, ...response].filter(shouldShowPage));
return response; return response;
} }
useEffect(() => { useEffect(() => {
if (!searchedText) { if (!searchedText) {
setSearchResults(defaultPages); setSearchResults(defaultPages.filter(shouldShowPage));
return; return;
} }
@ -87,7 +112,7 @@ export function CommandMenu() {
type="text" type="text"
value={searchedText} value={searchedText}
className="w-full rounded-t-md border-b p-4 text-sm focus:bg-gray-50 focus:outline-0" className="w-full rounded-t-md border-b p-4 text-sm focus:bg-gray-50 focus:outline-0"
placeholder="Search anywhere .." placeholder="Search roadmaps, guides or pages .."
autocomplete="off" autocomplete="off"
onInput={(e) => { onInput={(e) => {
const value = (e.target as HTMLInputElement).value.trim(); const value = (e.target as HTMLInputElement).value.trim();
@ -133,17 +158,20 @@ export function CommandMenu() {
<div class="border-b border-gray-100"></div> <div class="border-b border-gray-100"></div>
)} )}
<a <a
class={`block w-full rounded p-2 text-sm ${ class={`flex w-full items-center rounded p-2 text-sm ${
counter === activeCounter ? 'bg-gray-100' : '' counter === activeCounter ? 'bg-gray-100' : ''
}`} }`}
onMouseOver={() => setActiveCounter(counter)} onMouseOver={() => setActiveCounter(counter)}
href={page.url} href={page.url}
> >
{searchedText && ( {!page.icon && (
<span class="mr-2 text-gray-400">{page.group}</span> <span class="mr-2 text-gray-400">{page.group}</span>
)} )}
{!searchedText && ( {page.icon && (
<span class="mr-2 text-gray-400">ICON</span> <img
src={page.icon}
class="mr-2 h-4 w-4"
/>
)} )}
{page.title} {page.title}
</a> </a>

@ -0,0 +1,9 @@
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor"
stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round">
<line x1="10" x2="21" y1="6" y2="6"></line>
<line x1="10" x2="21" y1="12" y2="12"></line>
<line x1="10" x2="21" y1="18" y2="18"></line>
<polyline points="3 6 4 7 6 5"></polyline>
<polyline points="3 12 4 13 6 11"></polyline>
<polyline points="3 18 4 19 6 17"></polyline>
</svg>

After

Width:  |  Height:  |  Size: 491 B

@ -0,0 +1,3 @@
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-6 h-6">
<path stroke-linecap="round" stroke-linejoin="round" d="M9 6.75V15m6-6v8.25m.503 3.498l4.875-2.437c.381-.19.622-.58.622-1.006V4.82c0-.836-.88-1.38-1.628-1.006l-3.869 1.934c-.317.159-.69.159-1.006 0L9.503 3.252a1.125 1.125 0 00-1.006 0L3.622 5.689C3.24 5.88 3 6.27 3 6.695V19.18c0 .836.88 1.38 1.628 1.006l3.869-1.934c.317-.159.69-.159 1.006 0l4.994 2.497c.317.158.69.158 1.006 0z" />
</svg>

After

Width:  |  Height:  |  Size: 525 B

@ -0,0 +1,3 @@
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-6 h-6">
<path stroke-linecap="round" stroke-linejoin="round" d="M2.25 12l8.954-8.955c.44-.439 1.152-.439 1.591 0L21.75 12M4.5 9.75v10.125c0 .621.504 1.125 1.125 1.125H9.75v-4.875c0-.621.504-1.125 1.125-1.125h2.25c.621 0 1.125.504 1.125 1.125V21h4.125c.621 0 1.125-.504 1.125-1.125V9.75M8.25 21h8.25" />
</svg>

After

Width:  |  Height:  |  Size: 436 B

@ -0,0 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-milestone"><path d="M18 6H5a2 2 0 0 0-2 2v3a2 2 0 0 0 2 2h13l4-3.5L18 6Z"></path><path d="M12 13v8"></path><path d="M12 3v3"></path></svg>

After

Width:  |  Height:  |  Size: 343 B

@ -0,0 +1,3 @@
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-6 h-6">
<path stroke-linecap="round" stroke-linejoin="round" d="M15.75 6a3.75 3.75 0 11-7.5 0 3.75 3.75 0 017.5 0zM4.501 20.118a7.5 7.5 0 0114.998 0A17.933 17.933 0 0112 21.75c-2.676 0-5.216-.584-7.499-1.632z" />
</svg>

After

Width:  |  Height:  |  Size: 346 B

@ -0,0 +1,3 @@
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-6 h-6">
<path stroke-linecap="round" d="M15.75 10.5l4.72-4.72a.75.75 0 011.28.53v11.38a.75.75 0 01-1.28.53l-4.72-4.72M4.5 18.75h9a2.25 2.25 0 002.25-2.25v-9a2.25 2.25 0 00-2.25-2.25h-9A2.25 2.25 0 002.25 7.5v9a2.25 2.25 0 002.25 2.25z" />
</svg>

After

Width:  |  Height:  |  Size: 372 B

Loading…
Cancel
Save