icon-cloud.tsx 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. "use client";
  2. import { useTheme } from "next-themes";
  3. import { useEffect, useMemo, useState } from "react";
  4. import {
  5. Cloud,
  6. fetchSimpleIcons,
  7. ICloud,
  8. renderSimpleIcon,
  9. SimpleIcon,
  10. } from "react-icon-cloud";
  11. export const cloudProps: Omit<ICloud, "children"> = {
  12. containerProps: {
  13. style: {
  14. display: "flex",
  15. justifyContent: "center",
  16. alignItems: "center",
  17. width: "100%",
  18. },
  19. },
  20. options: {
  21. reverse: true,
  22. depth: 1,
  23. wheelZoom: false,
  24. imageScale: 2,
  25. activeCursor: "default",
  26. tooltip: "native",
  27. initial: [0.1, -0.1],
  28. clickToFront: 500,
  29. tooltipDelay: 0,
  30. outlineColour: "#0000",
  31. maxSpeed: 0.04,
  32. minSpeed: 0.02,
  33. },
  34. };
  35. export const renderCustomIcon = (icon: SimpleIcon, theme: string) => {
  36. const bgHex = theme === "light" ? "#f3f2ef" : "#080510";
  37. const fallbackHex = theme === "light" ? "#6e6e73" : "#ffffff";
  38. const minContrastRatio = theme === "dark" ? 2 : 1.2;
  39. return renderSimpleIcon({
  40. icon,
  41. bgHex,
  42. fallbackHex,
  43. minContrastRatio,
  44. size: 42,
  45. aProps: {
  46. href: undefined,
  47. target: undefined,
  48. rel: undefined,
  49. onClick: (e: any) => e.preventDefault(),
  50. },
  51. });
  52. };
  53. export type DynamicCloudProps = {
  54. iconSlugs: string[];
  55. };
  56. type IconData = Awaited<ReturnType<typeof fetchSimpleIcons>>;
  57. const IconCloud = ({ iconSlugs }: DynamicCloudProps) => {
  58. const [data, setData] = useState<IconData | null>(null);
  59. const { theme } = useTheme();
  60. useEffect(() => {
  61. fetchSimpleIcons({ slugs: iconSlugs }).then(setData);
  62. }, [iconSlugs]);
  63. const renderedIcons = useMemo(() => {
  64. if (!data) return null;
  65. return Object.values(data.simpleIcons).map((icon) =>
  66. renderCustomIcon(icon, theme || "light"),
  67. );
  68. }, [data, theme]);
  69. if (!data) {
  70. return null;
  71. }
  72. return (
  73. // @ts-ignore
  74. <Cloud {...cloudProps}>{renderedIcons}</Cloud>
  75. );
  76. };
  77. export default IconCloud;