-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add download button in DataCardInfo.svelte (#1168)
- Loading branch information
1 parent
9736484
commit 310179a
Showing
9 changed files
with
347 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@undp-data/svelte-undp-design": patch | ||
--- | ||
|
||
added Download component in svelte-undp-design |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
packages/svelte-undp-design/src/lib/Download/Download.stories.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import type { Meta, StoryObj } from '@storybook/svelte'; | ||
|
||
import Download from './Download.svelte'; | ||
|
||
// More on how to set up stories at: https://storybook.js.org/docs/7.0/svelte/writing-stories/introduction | ||
const meta = { | ||
title: 'Example/Download', | ||
component: Download, | ||
tags: ['docsPage'], | ||
argTypes: { | ||
url: { | ||
type: 'string', | ||
description: 'URL for doanloaded file' | ||
}, | ||
title: { | ||
type: 'string', | ||
description: 'Title for file', | ||
defaultValue: '' | ||
} | ||
} | ||
} satisfies Meta<Download>; | ||
|
||
export default meta; | ||
type Story = StoryObj<typeof meta>; | ||
|
||
// More on writing stories with args: https://storybook.js.org/docs/7.0/svelte/writing-stories/args | ||
export const Primary: Story = { | ||
args: { | ||
url: 'assets/undp-logo-blue.svg', | ||
title: 'undp-logo-blue.svg' | ||
} | ||
}; |
89 changes: 89 additions & 0 deletions
89
packages/svelte-undp-design/src/lib/Download/Download.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
<script lang="ts"> | ||
import { filesize } from 'filesize'; | ||
export let url: string; | ||
export let title = ''; | ||
let extension = ''; | ||
const downloadFile = () => { | ||
const element = document.createElement('a'); | ||
element.href = url; | ||
element.download = url; | ||
element.click(); | ||
element.remove(); | ||
}; | ||
const handleKeyDown = (event: KeyboardEvent) => { | ||
if (event.key === 'Enter') { | ||
downloadFile(); | ||
} | ||
}; | ||
const fileUrl = new URL(url); | ||
const filePath = fileUrl.pathname.split('/'); | ||
const fileName = filePath[filePath.length - 1]; | ||
if (!title) { | ||
title = fileName; | ||
} | ||
const fileExtensions = fileName.split('.'); | ||
extension = ''; | ||
if (fileExtensions.length > 1) { | ||
extension = fileExtensions[fileExtensions.length - 1].toLocaleUpperCase(); | ||
} | ||
let fileFormat = extension; | ||
const getFileSize = () => { | ||
return new Promise<void>((resolve, reject) => { | ||
const fileUrl = new URL(url.replace('pmtiles://', '')); | ||
const filePath = fileUrl.pathname.split('/'); | ||
let bytes = 'N/A'; | ||
fetch(fileUrl.toString()).then((res) => { | ||
if (res.ok) { | ||
const contentLength = res.headers.get('content-length'); | ||
if (contentLength) { | ||
bytes = filesize(Number(contentLength), { round: 1 }) as string; | ||
} | ||
} | ||
fileFormat = `${fileFormat} (${bytes})`; | ||
resolve(); | ||
}); | ||
}); | ||
}; | ||
$: if (url) { | ||
getFileSize(); | ||
} | ||
</script> | ||
|
||
<div class="download-card"> | ||
<!-- svelte-ignore a11y-missing-attribute --> | ||
<a role="button" on:click={downloadFile} on:keydown={handleKeyDown}> | ||
<div class="description"> | ||
{#if title} | ||
<p class="title">{title}</p> | ||
{/if} | ||
<p class="format"> | ||
{fileFormat} | ||
</p> | ||
<span class="download"> | ||
Download | ||
<span class="download-animated"><i /></span> | ||
</span> | ||
</div> | ||
</a> | ||
</div> | ||
|
||
<style lang="scss"> | ||
@use '../css/base-minimal.min.css'; | ||
@use '../css/download-card.min.css'; | ||
.download-card { | ||
cursor: pointer; | ||
.description { | ||
width: fit-content; | ||
max-width: 300px; | ||
} | ||
} | ||
</style> |
179 changes: 179 additions & 0 deletions
179
packages/svelte-undp-design/src/lib/css/download-card.min.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,179 @@ | ||
div.download-card .download, | ||
div.download-card .format { | ||
font-size: 0.875rem; | ||
line-height: 1.4; | ||
} | ||
@media (min-width: 48em) { | ||
div.download-card .download, | ||
div.download-card .format { | ||
font-size: 1rem; | ||
} | ||
} | ||
div.download-card .title { | ||
font-size: 1rem; | ||
} | ||
@media (min-width: 48em) { | ||
div.download-card .title { | ||
font-size: 1.25rem; | ||
} | ||
} | ||
:lang(my) div.download-card .download, | ||
:lang(my) div.download-card .format, | ||
div.download-card :lang(my) .download, | ||
div.download-card :lang(my) .format { | ||
font-size: 0.75rem; | ||
line-height: 1.7; | ||
} | ||
@media (min-width: 48em) { | ||
:lang(my) div.download-card .download, | ||
:lang(my) div.download-card .format, | ||
div.download-card :lang(my) .download, | ||
div.download-card :lang(my) .format { | ||
font-size: 0.875rem; | ||
} | ||
} | ||
:lang(my) div.download-card .title, | ||
div.download-card :lang(my) .title { | ||
font-size: 0.875rem; | ||
} | ||
@media (min-width: 48em) { | ||
:lang(my) div.download-card .title, | ||
div.download-card :lang(my) .title { | ||
font-size: 1rem; | ||
} | ||
} | ||
div.download-card { | ||
display: inline-block; | ||
} | ||
@media (min-width: 48em) { | ||
div.download-card { | ||
min-width: 330px; | ||
} | ||
} | ||
div.download-card > a { | ||
background: none; | ||
} | ||
div.download-card:hover div.publication-thumbnail__image.yellow:after, | ||
div.download-card:hover div.publication-thumbnail__image:after { | ||
background: linear-gradient(27.66deg, #ffeb00, transparent 70.49%); | ||
opacity: 0.75; | ||
} | ||
div.download-card:hover div.publication-thumbnail__image.red:after { | ||
background: linear-gradient(27.66deg, #ee402d, transparent 70.49%); | ||
opacity: 0.75; | ||
} | ||
div.download-card:hover div.publication-thumbnail__image.green:after { | ||
background: linear-gradient(27.66deg, #6de354, transparent 70.49%); | ||
opacity: 0.75; | ||
} | ||
div.download-card:hover div.publication-thumbnail__image.blue:after { | ||
background: linear-gradient(27.66deg, #60d4f2, transparent 70.49%); | ||
opacity: 0.75; | ||
} | ||
div.download-card:hover div.card-thumbnail__image.yellow:before, | ||
div.download-card:hover div.card-thumbnail__image:before { | ||
background: linear-gradient(67.76deg, #ffeb00, transparent 61.11%); | ||
opacity: 0.75; | ||
} | ||
div.download-card:hover div.card-thumbnail__image.red:before { | ||
background: linear-gradient(67.76deg, #ee402d, transparent 61.11%); | ||
opacity: 0.75; | ||
} | ||
div.download-card:hover div.card-thumbnail__image.green:before { | ||
background: linear-gradient(67.76deg, #6de354, transparent 61.11%); | ||
opacity: 0.75; | ||
} | ||
div.download-card:hover div.card-thumbnail__image.blue:before { | ||
background: linear-gradient(67.76deg, #60d4f2, transparent 61.11%); | ||
opacity: 0.75; | ||
} | ||
div.download-card:hover .download-animated:after { | ||
-webkit-transform: rotate(-45deg) translate(7px, -7px); | ||
-moz-transform: rotate(-45deg) translate(7px, -7px); | ||
-ms-transform: rotate(-45deg) translate(7px, -7px); | ||
-o-transform: rotate(-45deg) translate(7px, -7px); | ||
transition: rotate(-45deg) translate(7px, -7px); | ||
} | ||
div.download-card:hover .download-animated:before { | ||
-webkit-transform: translateY(-10px); | ||
-moz-transform: translateY(-10px); | ||
-ms-transform: translateY(-10px); | ||
-o-transform: translateY(-10px); | ||
transition: translate(0, -10px); | ||
} | ||
div.download-card:hover .external-link-animated:after { | ||
-webkit-transform: translate(5px, -5px); | ||
-moz-transform: translate(5px, -5px); | ||
-ms-transform: translate(5px, -5px); | ||
-o-transform: translate(5px, -5px); | ||
transition: translate(5px, -5px); | ||
} | ||
div.download-card:hover .external-link-animated:before { | ||
-webkit-transform: rotate(-45deg) translate(7px); | ||
-moz-transform: rotate(-45deg) translate(7px); | ||
-ms-transform: rotate(-45deg) translate(7px); | ||
-o-transform: rotate(-45deg) translate(7px); | ||
transition: rotate(-45deg) translate(7px, 0); | ||
} | ||
div.download-card .description { | ||
background: #fafafa; | ||
padding: 1.5rem; | ||
} | ||
div.download-card .title { | ||
margin-bottom: 0.25rem; | ||
} | ||
div.download-card .format { | ||
color: #55606e; | ||
margin-bottom: 1rem; | ||
} | ||
div.download-card .download { | ||
align-items: center; | ||
background-image: none; | ||
display: flex; | ||
font-weight: 700; | ||
letter-spacing: 0.03em; | ||
text-transform: uppercase; | ||
} | ||
div.download-card .download .download-animated { | ||
margin-left: 0.75rem; | ||
} | ||
div.download-card .download .external-link-animated { | ||
margin-bottom: 0.5rem; | ||
margin-left: 0.75rem; | ||
} | ||
[dir='rtl'] div.download-card .download-animated, | ||
[dir='rtl'] div.download-card .external-link-animated { | ||
margin-left: 0; | ||
margin-right: 0.75rem; | ||
} | ||
[dir='rtl'] div.download-card:hover div.publication-thumbnail__image.yellow:after, | ||
[dir='rtl'] div.download-card:hover div.publication-thumbnail__image:after { | ||
background: linear-gradient(318deg, #ffeb00, transparent 70.49%); | ||
} | ||
[dir='rtl'] div.download-card:hover div.publication-thumbnail__image.red:after { | ||
background: linear-gradient(318deg, #ee402d, transparent 70.49%); | ||
} | ||
[dir='rtl'] div.download-card:hover div.publication-thumbnail__image.green:after { | ||
background: linear-gradient(318deg, #6de354, transparent 70.49%); | ||
} | ||
[dir='rtl'] div.download-card:hover div.publication-thumbnail__image.blue:after { | ||
background: linear-gradient(318deg, #60d4f2, transparent 70.49%); | ||
} | ||
[dir='rtl'] div.download-card:hover div.card-thumbnail__image.yellow:before, | ||
[dir='rtl'] div.download-card:hover div.card-thumbnail__image:before { | ||
background: linear-gradient(297deg, #ffeb00, transparent 61.11%); | ||
} | ||
[dir='rtl'] div.download-card:hover div.card-thumbnail__image.red:before { | ||
background: linear-gradient(297deg, #ee402d, transparent 61.11%); | ||
} | ||
[dir='rtl'] div.download-card:hover div.card-thumbnail__image.green:before { | ||
background: linear-gradient(297deg, #6de354, transparent 61.11%); | ||
} | ||
[dir='rtl'] div.download-card:hover div.card-thumbnail__image.blue:before { | ||
background: linear-gradient(297deg, #60d4f2, transparent 61.11%); | ||
} | ||
[dir='rtl'] div.download-card div.download-card__download .download-animated, | ||
[dir='rtl'] div.download-card div.download-card__download:after { | ||
margin-left: 0; | ||
margin-right: 0.75rem; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.