diff --git a/site/src/components/CustomizeIconContext.tsx b/site/src/components/CustomizeIconContext.tsx index 00de9c9e9..dbebb31ea 100644 --- a/site/src/components/CustomizeIconContext.tsx +++ b/site/src/components/CustomizeIconContext.tsx @@ -1,4 +1,6 @@ -import { createContext, useState } from 'react'; +import { createContext, useRef, useState, MutableRefObject, useContext, useMemo } from 'react'; + +type IconsRef = Record; interface ICustomIconStyle { color: string; @@ -8,13 +10,14 @@ interface ICustomIconStyle { size: number; setSize: (n: number) => void; resetStyle: () => void; + iconsRef: MutableRefObject; } const DEFAULT_STYLE = { color: 'currentColor', strokeWidth: 2, size: 24, -} +}; export const IconStyleContext = createContext({ color: 'currentColor', @@ -23,25 +26,43 @@ export const IconStyleContext = createContext({ setStroke: (n: number) => null, size: 24, setSize: (n: number) => null, - resetStyle: () => null + resetStyle: () => null, + iconsRef: { current: {} }, }); -export function CustomizeIconContext({ children }) { +export function CustomizeIconContext({ children }): JSX.Element { + const iconsRef = useRef({}); const [color, setColor] = useState(DEFAULT_STYLE.color); const [stroke, setStroke] = useState(DEFAULT_STYLE.strokeWidth); const [size, setSize] = useState(DEFAULT_STYLE.size); - function resetStyle(){ + function resetStyle() { setColor(DEFAULT_STYLE.color); setStroke(DEFAULT_STYLE.strokeWidth); setSize(DEFAULT_STYLE.size); } - return ( - - {children} - + const value = useMemo( + () => ({ + color, + setColor, + strokeWidth: stroke, + setStroke, + size, + setSize, + resetStyle, + iconsRef, + }), + [color, setColor, stroke, setStroke, size, setSize, resetStyle, iconsRef], ); + + return {children}; +} + +export function useCustomizeIconContext(): ICustomIconStyle { + const context = useContext(IconStyleContext); + if (context === undefined) { + throw new Error('useCustomizeIconContext must be used within a IconStyleContextProvider'); + } + return context; } diff --git a/site/src/components/Header.tsx b/site/src/components/Header.tsx index 5ac74f349..8b5f35f3a 100644 --- a/site/src/components/Header.tsx +++ b/site/src/components/Header.tsx @@ -13,20 +13,49 @@ import AngularLogo from '../../public/framework-logos/angular.svg'; import FlutterLogo from '../../public/framework-logos/flutter.svg'; import SvelteLogo from '../../public/framework-logos/svelte.svg'; import LaravelLogo from '../../public/framework-logos/laravel.svg'; +import { useState } from 'react'; +import { useCustomizeIconContext } from './CustomizeIconContext'; +import { IconEntity } from '../types'; -function generateZip(icons) { +type IconContent = [icon: string, src:string] + +async function generateZip(icons: IconContent[]) { const zip = new JSZip(); - Object.values(icons).forEach(icon => - // @ts-ignore - zip.file(`${icon.name}.svg`, icon.src), + + const addingZipPromises = icons.map(([name, src]) => + zip.file(`${name}.svg`, src), ); + + await Promise.all(addingZipPromises) + return zip.generateAsync({ type: 'blob' }); } -const Header = ({ data }) => { +interface HeaderProps { + data: IconEntity[]; +} + +const Header = ({ data }: HeaderProps) => { + const [zippingIcons, setZippingIcons] = useState(false); + const { iconsRef } = useCustomizeIconContext(); + const downloadAllIcons = async () => { - const zip = await generateZip(data); + console.log(iconsRef); + setZippingIcons(true); + + let iconEntries: IconContent[] = Object.entries(iconsRef.current).map(([name, svgEl]) => [ + name, + svgEl.outerHTML, + ]); + + // Fallback + if (iconEntries.length === 0) { + iconEntries = data.map(icon => [icon.name, icon.src]); + } + + const zip = await generateZip(iconEntries); download(zip, 'lucide.zip'); + setZippingIcons(false); }; const repositoryUrl = 'https://github.com/lucide-icons/lucide'; @@ -76,7 +105,7 @@ const Header = ({ data }) => { name: 'lucide-laravel', Logo: LaravelLogo, href: 'https://github.com/mallardduck/blade-lucide-icons', - } + }, ]; return ( @@ -124,7 +153,13 @@ const Header = ({ data }) => { - diff --git a/site/src/components/IconListItem.tsx b/site/src/components/IconListItem.tsx index b7ab134c0..cf5c32747 100644 --- a/site/src/components/IconListItem.tsx +++ b/site/src/components/IconListItem.tsx @@ -1,8 +1,8 @@ -import { Box, Button, ButtonProps, Flex, Text, useToast } from '@chakra-ui/react'; +import { Button, ButtonProps, Flex, Text, useToast } from '@chakra-ui/react'; import download from 'downloadjs'; import copy from 'copy-to-clipboard'; -import { memo, useContext } from 'react'; -import { IconStyleContext } from './CustomizeIconContext'; +import { memo } from 'react'; +import { useCustomizeIconContext } from './CustomizeIconContext'; import { IconWrapper } from './IconWrapper'; interface IconListItemProps { @@ -13,9 +13,9 @@ interface IconListItemProps { onClick?: ButtonProps['onClick']; } -const IconListItem = ({ name, content, src, onClick }: IconListItemProps) => { +const IconListItem = ({ name, content, onClick, src: svg }: IconListItemProps) => { const toast = useToast(); - const { color, size, strokeWidth } = useContext(IconStyleContext); + const { color, size, strokeWidth, iconsRef } = useCustomizeIconContext(); return (