From 8495349955a5395e4c742ad8815b6cec38620809 Mon Sep 17 00:00:00 2001 From: "Lucian I. Last" Date: Fri, 24 May 2024 10:35:49 +0200 Subject: [PATCH 1/2] Fix download button render --- package.json | 1 - src/components/DownloadPDF.tsx | 42 ++++++++++++++++++++++------------ src/scss/_app.scss | 8 +++---- yarn.lock | 5 ---- 4 files changed, 32 insertions(+), 24 deletions(-) diff --git a/package.json b/package.json index f179c7d..4654aec 100644 --- a/package.json +++ b/package.json @@ -13,7 +13,6 @@ }, "dependencies": { "@react-pdf/renderer": "^3.4.2", - "@uidotdev/usehooks": "^2.4.1", "@vitejs/plugin-react-swc": "^3.6.0", "date-fns": "^3.6.0", "file-saver": "^2.0.5", diff --git a/src/components/DownloadPDF.tsx b/src/components/DownloadPDF.tsx index 5f3dbd0..85b9acc 100644 --- a/src/components/DownloadPDF.tsx +++ b/src/components/DownloadPDF.tsx @@ -1,7 +1,6 @@ -import React, { FC } from 'react' +import { ChangeEvent, FC, useEffect, useState } from 'react' import { PDFDownloadLink } from '@react-pdf/renderer' import { Invoice, TInvoice } from '../data/types' -import { useDebounce } from '@uidotdev/usehooks' import InvoicePage from './InvoicePage' import FileSaver from 'file-saver' @@ -11,9 +10,20 @@ interface Props { } const Download: FC = ({ data, setData }) => { - const debounced = useDebounce(data, 500) + const [showDoc, setShowDoc] = useState(true) + const [t, setT] = useState(null) - function handleInput(e: React.ChangeEvent) { + useEffect(() => { + setShowDoc(false) + if (t) clearTimeout(t) + setT( + setTimeout(() => { + setShowDoc(true) + }, 500) as any, + ) + }, [data]) + + function handleInput(e: ChangeEvent) { if (!e.target.files?.length) return const file = e.target.files[0] @@ -37,7 +47,7 @@ const Download: FC = ({ data, setData }) => { } function handleSaveTemplate() { - const blob = new Blob([JSON.stringify(debounced)], { + const blob = new Blob([JSON.stringify(data)], { type: 'text/plain;charset=utf-8', }) FileSaver(blob, title + '.template') @@ -45,15 +55,19 @@ const Download: FC = ({ data, setData }) => { const title = data.invoiceTitle ? data.invoiceTitle.toLowerCase() : 'invoice' return ( -
- } - fileName={`${title}.pdf`} - aria-label="Save PDF" - title="Save PDF" - className="download-pdf__pdf" - > +
+ {showDoc ? ( + } + fileName={`${title}.pdf`} + aria-label="Save PDF" + title="Save PDF" + className="download-pdf__pdf" + > + ) : ( +
) } diff --git a/src/images/template_download.svg b/src/images/template_download.svg index f747796..1a19017 100644 --- a/src/images/template_download.svg +++ b/src/images/template_download.svg @@ -5,12 +5,12 @@ viewBox="0 0 58 58" style="enable-background:new 0 0 58 58;" xml:space="preserve"> - + - + - + diff --git a/src/scss/_app.scss b/src/scss/_app.scss index 2b15544..5a82b8c 100644 --- a/src/scss/_app.scss +++ b/src/scss/_app.scss @@ -1,6 +1,16 @@ .app { - margin: 30px auto 50px auto; + margin: 30px auto 50px; width: 700px; + + &__title { + display: inline-block; + width: 100%; + text-align: center; + @media only screen and (max-width: 920px) { + margin-left: 4px; + text-align: start; + } + } } .input, @@ -43,47 +53,75 @@ } .download-pdf { - position: fixed; - top: 100px; - margin-left: -110px; - width: 40px; - height: 40px; + overflow: hidden; + position: absolute; + top: 2px; + left: -82px; transition: opacity 0.2s ease-in-out; + display: grid; + justify-items: center; + align-items: center; + gap: 4px 0; + grid-template-columns: 60px; + text-align: center; + + @media only screen and (max-width: 920px) { + top: -82px; + right: 4px; + left: auto; + grid-template-rows: 40px 20px; + grid-auto-flow: column; + gap: 0 10px; + } button { border: none; } > .loading { opacity: 0.3; - cursor: wait; + cursor: wait !important; } - a, - label, - button { + .download-pdf__pdf, + .download-pdf__template_download, + .download-pdf__template_upload { display: block; background-repeat: no-repeat; - top: 0; - left: 0; - width: 100%; - height: 100%; + background-position: center; + width: 30px; + height: 30px; + cursor: pointer; + border-radius: 5px; + outline: #fff solid 2px; + &:hover { + outline-offset: 2px; + ~ p { + opacity: 1; + } + } + + @media only screen and (min-width: 921px) { + margin-top: 12px; + } + } + button { + background-color: transparent; } .download-pdf__pdf { - background: url('../images/download.svg'); + background-image: url('../images/download.svg'); } .download-pdf__template_download { - width: 30px; - height: 30px; - background: url('../images/template_download.svg'); + background-image: url('../images/template_download.svg'); } .download-pdf__template_upload { - width: 30px; - height: 30px; - background: url('../images/template_upload.svg'); + background-image: url('../images/template_upload.svg'); } input[type='file'] { visibility: hidden; } + p { + margin: 0; + } } .image {