Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: add download button to download icon as svg #899

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
51 changes: 43 additions & 8 deletions packages/preview-astro/src/components/icondetailmodal.tsx
@@ -1,5 +1,5 @@
import React, { useEffect, useRef, useState } from "react";
import { FaRegClipboard } from "react-icons/fa6";
import { FaDownload, FaRegClipboard } from "react-icons/fa6";
import copy from "copy-to-clipboard";
import toast from "cogo-toast";

Expand Down Expand Up @@ -64,6 +64,33 @@ export function IconDetailModal(
const importCode = `import { ${props.iconName} } from "react-icons/${props.iconSet}";`;
const useCode = `<${props.iconName} />`;

const downloadSvg = () => {
const iconElement = document.getElementsByClassName("icon")[0];
if (iconElement === null) {
toast.error("Icon could not be retrieved.", {
position: "bottom-center",
});
return;
}
const icon = (iconElement?.firstChild as Element).outerHTML;

const svgBlob = new Blob(
['<?xml version="1.0" standalone="no"?>\r\n', icon],
{ type: "image/svg+xml;charset=utf-8" },
);

const downloadLink = Object.assign(document.createElement("a"), {
href: URL.createObjectURL(svgBlob),
download: `${props.iconName}.svg`,
});

document.body.appendChild(downloadLink);
downloadLink.click();
document.body.removeChild(downloadLink);

toast.success("Download started", { position: "bottom-center" });
};

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const Component = props.component as React.ComponentType<any>;
const [selectedColor, setSelectedColor] = React.useState<number>(0);
Expand Down Expand Up @@ -127,6 +154,12 @@ export function IconDetailModal(
</button>
</li>
))}
<li>
<button onClick={downloadSvg}>
<FaDownload />
<span className="text">Download SVG</span>
</button>
</li>
</ul>
</div>
</Modal>
Expand All @@ -152,23 +185,22 @@ const useModalAnimation = ({ onClose, isOpen }: useModalAnimationProps) => {
handleInitOpenAnimation();
}, [isOpen]);


useEffect(() => {
const modalElement = modalRef.current;

const handleTransitionEnd = () => {
if (!animationIsOpen) {
onClose?.();
}
};

if (modalElement) {
modalElement.addEventListener('transitionend', handleTransitionEnd);
modalElement.addEventListener("transitionend", handleTransitionEnd);
}

return () => {
if (modalElement) {
modalElement.removeEventListener('transitionend', handleTransitionEnd);
modalElement.removeEventListener("transitionend", handleTransitionEnd);
}
};
}, [animationIsOpen, onClose]);
Expand All @@ -180,7 +212,7 @@ const useModalAnimation = ({ onClose, isOpen }: useModalAnimationProps) => {
return {
animationIsOpen,
handleCloseModal,
modalRef
modalRef,
};
};

Expand Down Expand Up @@ -208,7 +240,10 @@ function Modal({
className={`overlay ${animationIsOpen ? "" : "appearing"}`}
onClick={() => handleCloseModal()}
></div>
<div className={`modal-body ${animationIsOpen ? "open" : "close"}`} ref={modalRef}>
<div
className={`modal-body ${animationIsOpen ? "open" : "close"}`}
ref={modalRef}
>
<div className="header">
<h3 className="title">{title}</h3>
<button
Expand Down
10 changes: 10 additions & 0 deletions packages/preview-astro/src/styles/_components.scss
Expand Up @@ -163,6 +163,7 @@ a {

.icon {
min-height: 64px;
position: relative;
display: flex;
align-items: center;
justify-content: center;
Expand All @@ -173,6 +174,15 @@ a {
0 1px 2px 0 rgba(0, 0, 0, 0.06);
border: 2px solid transparent;
font-size: 1.6em;

.download {
height: 16px;
color: rgba(0, 0, 0, 0.5);
position: absolute;
bottom: 4px;
right: 4px;
cursor: pointer;
}
}

&:hover {
Expand Down