icon-cloud.tsx 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 [isClient, setIsClient] = useState(false);
  59. const [data, setData] = useState<IconData | null>(null);
  60. const { theme } = useTheme();
  61. useEffect(() => {
  62. setIsClient(true);
  63. }, []);
  64. useEffect(() => {
  65. if (isClient) {
  66. fetchSimpleIcons({ slugs: iconSlugs }).then(setData);
  67. }
  68. }, [iconSlugs, isClient]);
  69. const renderedIcons = useMemo(() => {
  70. if (!data) return null;
  71. return Object.values(data.simpleIcons).map((icon) =>
  72. renderCustomIcon(icon, theme || "light"),
  73. );
  74. }, [data, theme]);
  75. if (!isClient) {
  76. return null; // Do not render anything server-side
  77. }
  78. return (
  79. // @ts-ignore
  80. <Cloud {...cloudProps}>{renderedIcons}</Cloud>
  81. );
  82. };
  83. export default IconCloud;