improved iconPicker component + other improvements

This commit is contained in:
daniel31x13 2024-08-19 18:14:09 -04:00
parent 2893d3caf2
commit dc388ebba5
15 changed files with 222 additions and 72 deletions

16
components/Icon.tsx Normal file
View File

@ -0,0 +1,16 @@
import React, { forwardRef } from "react";
import * as Icons from "@phosphor-icons/react";
type Props = {
icon: string;
} & Icons.IconProps;
const Icon = forwardRef<SVGSVGElement, Props>(({ icon, ...rest }) => {
const IconComponent: any = Icons[icon as keyof typeof Icons];
if (!IconComponent) {
return <></>;
} else return <IconComponent {...rest} />;
});
export default Icon;

View File

@ -2,6 +2,9 @@ import { icons } from "@/lib/client/icons";
import React, { useMemo, useState } from "react"; import React, { useMemo, useState } from "react";
import Fuse from "fuse.js"; import Fuse from "fuse.js";
import TextInput from "./TextInput"; import TextInput from "./TextInput";
import Popover from "./Popover";
import { HexColorPicker } from "react-colorful";
import Icon from "./Icon";
const fuse = new Fuse(icons, { const fuse = new Fuse(icons, {
keys: [{ name: "name", weight: 4 }, "tags", "categories"], keys: [{ name: "name", weight: 4 }, "tags", "categories"],
@ -9,9 +12,29 @@ const fuse = new Fuse(icons, {
useExtendedSearch: true, useExtendedSearch: true,
}); });
type Props = {}; type Props = {
onClose: Function;
alignment?: "left" | "right" | "bottom" | "top";
color: string;
setColor: Function;
iconName: string;
setIconName: Function;
weight: "light" | "regular" | "bold" | "fill" | "duotone";
setWeight: Function;
className?: string;
};
const IconPicker = (props: Props) => { const IconPicker = ({
onClose,
alignment,
color,
setColor,
iconName,
setIconName,
weight,
setWeight,
className,
}: Props) => {
const [query, setQuery] = useState(""); const [query, setQuery] = useState("");
const filteredQueryResultsSelector = useMemo(() => { const filteredQueryResultsSelector = useMemo(() => {
@ -22,24 +45,67 @@ const IconPicker = (props: Props) => {
}, [query]); }, [query]);
return ( return (
<div className="w-fit"> <Popover
onClose={onClose}
className={
className +
" bg-base-200 border border-neutral-content p-3 h-72 w-72 overflow-y-auto rounded-lg"
}
>
<div>
<div className="mb-3 flex gap-3">
<div className="w-full flex flex-col justify-between">
<Icon
icon={iconName}
size={80}
weight={weight as any}
color={color}
className="mx-auto"
/>
<select
className="border border-neutral-content bg-base-100 focus:outline-none focus:border-primary duration-100 w-full rounded-[0.375rem] h-7"
value={weight}
onChange={(e) => setWeight(e.target.value)}
>
<option value="regular">Regular</option>
<option value="thin">Thin</option>
<option value="light">Light</option>
<option value="bold">Bold</option>
<option value="fill">Fill</option>
<option value="duotone">Duotone</option>
</select>
</div>
<HexColorPicker color={color} onChange={(e) => setColor(e)} />
</div>
<TextInput <TextInput
className="p-2 rounded w-full mb-5" className="p-2 rounded w-full mb-3 h-7 text-sm"
placeholder="Search icons" placeholder="Search icons"
value={query} value={query}
onChange={(e) => setQuery(e.target.value)} onChange={(e) => setQuery(e.target.value)}
/> />
<div className="grid grid-cols-6 gap-5 w-fit">
<div className="grid grid-cols-5 gap-1 w-full">
{filteredQueryResultsSelector.map((icon) => { {filteredQueryResultsSelector.map((icon) => {
const IconComponent = icon.Icon; const IconComponent = icon.Icon;
return ( return (
<div key={icon.name} onClick={() => console.log(icon.name)}> <div
<IconComponent size={32} weight="fill" /> key={icon.pascal_name}
onClick={() => setIconName(icon.pascal_name)}
className={`cursor-pointer btn p-1 box-border ${
icon.pascal_name === iconName
? "outline outline-1 outline-primary"
: ""
}`}
>
<IconComponent size={32} weight={weight} color={color} />
</div> </div>
); );
})} })}
</div> </div>
</div> </div>
</Popover>
); );
}; };

View File

@ -105,8 +105,7 @@ export default function LinkActions({
alignToTop ? "" : "translate-y-10" alignToTop ? "" : "translate-y-10"
}`} }`}
> >
{permissions === true || {(permissions === true || permissions?.canUpdate) && (
(permissions?.canUpdate && (
<li> <li>
<div <div
role="button" role="button"
@ -122,7 +121,7 @@ export default function LinkActions({
: t("pin_to_dashboard")} : t("pin_to_dashboard")}
</div> </div>
</li> </li>
))} )}
<li> <li>
<div <div
role="button" role="button"
@ -173,8 +172,9 @@ export default function LinkActions({
tabIndex={0} tabIndex={0}
onClick={async (e) => { onClick={async (e) => {
(document?.activeElement as HTMLElement)?.blur(); (document?.activeElement as HTMLElement)?.blur();
console.log(e.shiftKey);
e.shiftKey e.shiftKey
? async () => { ? (async () => {
const load = toast.loading(t("deleting")); const load = toast.loading(t("deleting"));
await deleteLink.mutateAsync(link.id as number, { await deleteLink.mutateAsync(link.id as number, {
@ -188,7 +188,7 @@ export default function LinkActions({
} }
}, },
}); });
} })()
: setDeleteLinkModal(true); : setDeleteLinkModal(true);
}} }}
className="whitespace-nowrap" className="whitespace-nowrap"

View File

@ -6,6 +6,8 @@ import Modal from "../Modal";
import { useTranslation } from "next-i18next"; import { useTranslation } from "next-i18next";
import { useUpdateCollection } from "@/hooks/store/collections"; import { useUpdateCollection } from "@/hooks/store/collections";
import toast from "react-hot-toast"; import toast from "react-hot-toast";
import IconPicker from "../IconPicker";
import Icon from "../Icon";
type Props = { type Props = {
onClose: Function; onClose: Function;
@ -20,6 +22,7 @@ export default function EditCollectionModal({
const [collection, setCollection] = const [collection, setCollection] =
useState<CollectionIncludingMembersAndLinkCount>(activeCollection); useState<CollectionIncludingMembersAndLinkCount>(activeCollection);
const [iconPicker, setIconPicker] = useState(false);
const [submitLoader, setSubmitLoader] = useState(false); const [submitLoader, setSubmitLoader] = useState(false);
const updateCollection = useUpdateCollection(); const updateCollection = useUpdateCollection();
@ -71,17 +74,32 @@ export default function EditCollectionModal({
<div> <div>
<p className="w-full mb-2">{t("color")}</p> <p className="w-full mb-2">{t("color")}</p>
<div className="color-picker flex justify-between items-center"> <div className="color-picker flex justify-between items-center">
<HexColorPicker <div className="flex flex-col gap-2 items-center w-32 relative">
color={collection.color} <Icon
onChange={(color) => icon={collection.icon as string}
size={80}
weight={collection.iconWeight as any}
color={collection.color as string}
onClick={() => setIconPicker(true)}
/>
{iconPicker && (
<IconPicker
onClose={() => setIconPicker(false)}
className="top-20"
color={collection.color as string}
setColor={(color: string) =>
setCollection({ ...collection, color }) setCollection({ ...collection, color })
} }
weight={collection.iconWeight as any}
setWeight={(iconWeight: string) =>
setCollection({ ...collection, iconWeight })
}
iconName={collection.icon as string}
setIconName={(icon: string) =>
setCollection({ ...collection, icon })
}
/> />
<div className="flex flex-col gap-2 items-center w-32"> )}
<i
className="bi-folder-fill text-5xl"
style={{ color: collection.color }}
></i>
<div <div
className="btn btn-ghost btn-xs" className="btn btn-ghost btn-xs"
onClick={() => onClick={() =>

View File

@ -120,8 +120,7 @@ export default function LinkDetailModal({ onClose, onEdit, link }: Props) {
<div className="sm:m-auto p-10 w-full max-w-xl"> <div className="sm:m-auto p-10 w-full max-w-xl">
<LinkDetails link={link} /> <LinkDetails link={link} />
{permissions === true || {(permissions === true || permissions?.canUpdate) && (
(permissions?.canUpdate && (
<> <>
<br /> <br />
<br /> <br />
@ -138,7 +137,7 @@ export default function LinkDetailModal({ onClose, onEdit, link }: Props) {
</div> </div>
</div> </div>
</> </>
))} )}
</div> </div>
</Drawer> </Drawer>
); );

View File

@ -88,7 +88,7 @@ export default function NewCollectionModal({ onClose, parent }: Props) {
<p className="w-full mb-2">{t("color")}</p> <p className="w-full mb-2">{t("color")}</p>
<div className="color-picker flex justify-between items-center"> <div className="color-picker flex justify-between items-center">
<HexColorPicker <HexColorPicker
color={collection.color} color={collection.color as string}
onChange={(color) => onChange={(color) =>
setCollection({ ...collection, color }) setCollection({ ...collection, color })
} }
@ -96,7 +96,7 @@ export default function NewCollectionModal({ onClose, parent }: Props) {
<div className="flex flex-col gap-2 items-center w-32"> <div className="flex flex-col gap-2 items-center w-32">
<i <i
className={"bi-folder-fill text-5xl"} className={"bi-folder-fill text-5xl"}
style={{ color: collection.color }} style={{ color: collection.color as string }}
></i> ></i>
<div <div
className="btn btn-ghost btn-xs" className="btn btn-ghost btn-xs"

View File

@ -31,6 +31,9 @@ export default function NewLinkModal({ onClose }: Props) {
readable: "", readable: "",
monolith: "", monolith: "",
textContent: "", textContent: "",
icon: "",
iconWeight: "",
color: "",
collection: { collection: {
name: "", name: "",
ownerId: data?.user.id as number, ownerId: data?.user.id as number,

View File

@ -35,6 +35,9 @@ export default function UploadFileModal({ onClose }: Props) {
readable: "", readable: "",
monolith: "", monolith: "",
textContent: "", textContent: "",
icon: "",
iconWeight: "",
color: "",
collection: { collection: {
name: "", name: "",
ownerId: data?.user.id as number, ownerId: data?.user.id as number,

21
components/Popover.tsx Normal file
View File

@ -0,0 +1,21 @@
import React from "react";
import ClickAwayHandler from "./ClickAwayHandler";
type Props = {
children: React.ReactNode;
onClose: Function;
className?: string;
};
const Popover = ({ children, className, onClose }: Props) => {
return (
<ClickAwayHandler
onClickOutside={() => onClose()}
className={`absolute z-50 ${className || ""}`}
>
{children}
</ClickAwayHandler>
);
};
export default Popover;

View File

@ -205,7 +205,7 @@ export default function ReadableView({ link }: Props) {
> >
<i <i
className="bi-folder-fill drop-shadow text-2xl" className="bi-folder-fill drop-shadow text-2xl"
style={{ color: link?.collection.color }} style={{ color: link?.collection.color as string }}
></i> ></i>
<p <p
title={link?.collection.name} title={link?.collection.name}

View File

@ -112,7 +112,7 @@ export default function Index() {
<div className="flex items-center gap-2"> <div className="flex items-center gap-2">
<i <i
className="bi-folder-fill text-3xl drop-shadow" className="bi-folder-fill text-3xl drop-shadow"
style={{ color: activeCollection?.color }} style={{ color: activeCollection?.color as string }}
></i> ></i>
<p className="sm:text-3xl text-2xl capitalize w-full py-1 break-words hyphens-auto font-thin"> <p className="sm:text-3xl text-2xl capitalize w-full py-1 break-words hyphens-auto font-thin">

View File

@ -0,0 +1,8 @@
-- AlterTable
ALTER TABLE "Collection" ADD COLUMN "icon" TEXT,
ADD COLUMN "iconWeight" TEXT;
-- AlterTable
ALTER TABLE "Link" ADD COLUMN "color" TEXT,
ADD COLUMN "icon" TEXT,
ADD COLUMN "iconWeight" TEXT;

View File

@ -0,0 +1,3 @@
-- AlterTable
ALTER TABLE "Collection" ALTER COLUMN "color" DROP NOT NULL,
ALTER COLUMN "color" DROP DEFAULT;

View File

@ -94,7 +94,9 @@ model Collection {
id Int @id @default(autoincrement()) id Int @id @default(autoincrement())
name String name String
description String @default("") description String @default("")
color String @default("#0ea5e9") icon String?
iconWeight String?
color String?
parentId Int? parentId Int?
parent Collection? @relation("SubCollections", fields: [parentId], references: [id]) parent Collection? @relation("SubCollections", fields: [parentId], references: [id])
subCollections Collection[] @relation("SubCollections") subCollections Collection[] @relation("SubCollections")
@ -133,6 +135,9 @@ model Link {
collection Collection @relation(fields: [collectionId], references: [id]) collection Collection @relation(fields: [collectionId], references: [id])
collectionId Int collectionId Int
tags Tag[] tags Tag[]
icon String?
iconWeight String?
color String?
url String? url String?
textContent String? textContent String?
preview String? preview String?

View File

@ -36,6 +36,14 @@
scrollbar-width: none; scrollbar-width: none;
} }
.hide-color-picker {
opacity: 0;
display: block;
width: 32px;
height: 32px;
border: none;
}
.hyphens { .hyphens {
hyphens: auto; hyphens: auto;
} }