From 8705a9622046f6ed7d6557966e40fa5c070d6ec2 Mon Sep 17 00:00:00 2001 From: Aaryan Khandelwal <65252264+aaryan610@users.noreply.github.com> Date: Tue, 18 Jun 2024 14:48:23 +0530 Subject: [PATCH] dev: create a global component for emoji/icon logo (#4851) --- packages/ui/src/emoji/helpers.ts | 11 ++++ packages/ui/src/emoji/index.ts | 1 + packages/ui/src/emoji/logo.tsx | 102 +++++++++++++++++++++++++++++++ 3 files changed, 114 insertions(+) create mode 100644 packages/ui/src/emoji/helpers.ts create mode 100644 packages/ui/src/emoji/logo.tsx diff --git a/packages/ui/src/emoji/helpers.ts b/packages/ui/src/emoji/helpers.ts new file mode 100644 index 000000000..faa8ebdd9 --- /dev/null +++ b/packages/ui/src/emoji/helpers.ts @@ -0,0 +1,11 @@ +export const emojiCodeToUnicode = (emoji: string) => { + if (!emoji) return ""; + + // convert emoji code to unicode + const uniCodeEmoji = emoji + .split("-") + .map((emoji) => parseInt(emoji, 10).toString(16)) + .join("-"); + + return uniCodeEmoji; +}; diff --git a/packages/ui/src/emoji/index.ts b/packages/ui/src/emoji/index.ts index 128b80292..c881d8897 100644 --- a/packages/ui/src/emoji/index.ts +++ b/packages/ui/src/emoji/index.ts @@ -2,3 +2,4 @@ export * from "./emoji-icon-picker-new"; export * from "./emoji-icon-picker"; export * from "./emoji-icon-helper"; export * from "./icons"; +export * from "./logo"; diff --git a/packages/ui/src/emoji/logo.tsx b/packages/ui/src/emoji/logo.tsx new file mode 100644 index 000000000..11fca64c4 --- /dev/null +++ b/packages/ui/src/emoji/logo.tsx @@ -0,0 +1,102 @@ +"use client"; + +import React, { FC } from "react"; +import { Emoji } from "emoji-picker-react"; +import useFontFaceObserver from "use-font-face-observer"; +// icons +import { LUCIDE_ICONS_LIST } from "./icons"; +// helpers +import { emojiCodeToUnicode } from "./helpers"; + +type TLogoProps = { + in_use: "emoji" | "icon"; + emoji?: { + value?: string; + url?: string; + }; + icon?: { + name?: string; + color?: string; + }; +}; + +type Props = { + logo: TLogoProps; + size?: number; + type?: "lucide" | "material"; +}; + +export const Logo: FC = (props) => { + const { logo, size = 16, type = "material" } = props; + + // destructuring the logo object + const { in_use, emoji, icon } = logo; + + // derived values + const value = in_use === "emoji" ? emoji?.value : icon?.name; + const color = icon?.color; + const lucideIcon = LUCIDE_ICONS_LIST.find((item) => item.name === value); + + const isMaterialSymbolsFontLoaded = useFontFaceObserver([ + { + family: `Material Symbols Rounded`, + style: `normal`, + weight: `normal`, + stretch: `condensed`, + }, + ]); + // if no value, return empty fragment + if (!value) return <>; + + if (!isMaterialSymbolsFontLoaded) { + return ( + + ); + } + + // emoji + if (in_use === "emoji") { + return ; + } + + // icon + if (in_use === "icon") { + return ( + <> + {type === "lucide" ? ( + <> + {lucideIcon && ( + + )} + + ) : ( + + {value} + + )} + + ); + } + + // if no value, return empty fragment + return <>; +};