Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@
npm-debug.log*
yarn-debug.log*
yarn-error.log*
package-lock.json
67 changes: 57 additions & 10 deletions src/pages/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { TbBrandTwitter, TbShare, TbDownload, TbCopy } from "react-icons/tb";
import React, { useRef, useState, useEffect } from "react";
import { TbBrandTwitter, TbShare, TbDownload, TbCopy, TbChevronDown } from "react-icons/tb";
import React, { useRef, useState, useEffect, useCallback } from "react";
import {
download,
downloadSVG,
fetchData,
downloadJSON,
cleanUsername,
Expand All @@ -19,6 +20,8 @@ const App = () => {
const [theme, setTheme] = useState("standard");
const [data, setData] = useState(null);
const [error, setError] = useState(null);
const [showDownloadMenu, setShowDownloadMenu] = useState(false);
const downloadMenuRef = useRef();

useEffect(() => {
if (!data) {
Expand All @@ -27,6 +30,17 @@ const App = () => {
draw();
}, [data, theme]);

// Close dropdown when clicking outside
useEffect(() => {
const handler = (e) => {
if (downloadMenuRef.current && !downloadMenuRef.current.contains(e.target)) {
setShowDownloadMenu(false);
}
};
document.addEventListener("mousedown", handler);
return () => document.removeEventListener("mousedown", handler);
}, []);

const handleSubmit = (e) => {
e.preventDefault();

Expand Down Expand Up @@ -62,6 +76,18 @@ const App = () => {
copyToClipboard(canvasRef.current);
};

const onDownloadSVG = (e) => {
e.preventDefault();
if (data != null) {
downloadSVG(
data,
username,
theme,
"Made by @sallar & friends - github-contributions.vercel.app"
);
}
};

const onDownloadJson = (e) => {
e.preventDefault();
if (data != null) {
Expand Down Expand Up @@ -139,14 +165,35 @@ const App = () => {
<TbCopy size={18} />
Copy
</button>
<button
className="App-download-button"
onClick={onDownload}
type="button"
>
<TbDownload size={18} />
Download
</button>
<div className="App-download-dropdown" ref={downloadMenuRef}>
<button
className="App-download-button"
onClick={() => setShowDownloadMenu((v) => !v)}
type="button"
>
<TbDownload size={18} />
Download
<TbChevronDown size={14} style={{ marginLeft: 2 }} />
</button>
{showDownloadMenu && (
<div className="App-download-menu">
<button
className="App-download-menu-item"
onClick={(e) => { onDownload(e); setShowDownloadMenu(false); }}
type="button"
>
<TbDownload size={15} /> PNG
</button>
<button
className="App-download-menu-item"
onClick={(e) => { onDownloadSVG(e); setShowDownloadMenu(false); }}
type="button"
>
<TbDownload size={15} /> SVG
</button>
</div>
)}
</div>
{global.navigator && "share" in navigator && (
<button
className="App-download-button"
Expand Down
42 changes: 42 additions & 0 deletions src/styles/App.css
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,48 @@ form button {
gap: 7px;
}

.App-download-dropdown {
position: relative;
display: inline-flex;
}

.App-download-menu {
position: absolute;
top: calc(100% + 6px);
left: 50%;
transform: translateX(-50%);
background-color: var(--bg);
border: 1px solid var(--input-border);
border-radius: 0.4rem;
box-shadow: 0 4px 16px rgba(0, 0, 0, 0.12);
display: flex;
flex-direction: column;
min-width: 110px;
z-index: 100;
overflow: hidden;
}

.App-download-menu-item {
height: 2.6rem;
line-height: 2.6rem;
padding: 0 1rem;
background: transparent;
border: none;
border-radius: 0;
color: var(--text);
font-size: 0.85em;
cursor: pointer;
display: flex;
align-items: center;
gap: 7px;
transition: background 0.15s ease;
}

.App-download-menu-item:hover {
background-color: var(--button-bg);
color: var(--button-text);
}

.App-themes label {
display: flex;
justify-content: flex-start;
Expand Down
Loading