|
1 | 1 | import React from 'react' |
2 | | -import styled from 'styled-components' |
| 2 | +import styled, { css } from 'styled-components' |
3 | 3 | import Modal from 'react-modal' |
4 | | -import { loadTagDrawingSets } from '../../lib/hashtags' |
| 4 | +import { loadTagDrawingSets, saveTagDrawingSetsString } from '../../lib/hashtags' |
5 | 5 |
|
6 | 6 | export const ExportTagsModal: React.FC<{ isOpen: boolean; closeModal: () => void }> = ({ |
7 | 7 | isOpen, |
8 | 8 | closeModal, |
9 | 9 | }) => { |
10 | 10 | const tagDrawingSets = loadTagDrawingSets() |
11 | | - const exportString = JSON.stringify(tagDrawingSets, null, 2) |
| 11 | + const initialExportString = JSON.stringify(tagDrawingSets, null, 2) |
| 12 | + |
| 13 | + // exportString state powers selectAll |
| 14 | + const [exportString, setExportString] = React.useState(initialExportString) |
| 15 | + const [showCopySuccess, setShowCopySuccess] = React.useState(false) |
12 | 16 |
|
13 | 17 | function selectAll() { |
14 | 18 | navigator.clipboard.writeText(exportString) |
| 19 | + setShowCopySuccess(true) |
| 20 | + setTimeout(() => setShowCopySuccess(false), 2000) |
| 21 | + } |
| 22 | + |
| 23 | + // Edits in textarea are written to localStorage |
| 24 | + function onChange(e: React.ChangeEvent<HTMLTextAreaElement>) { |
| 25 | + setExportString(e.currentTarget.value) |
| 26 | + saveTagDrawingSetsString(e.currentTarget.value) |
15 | 27 | } |
16 | 28 |
|
17 | 29 | return ( |
18 | | - <Modal |
19 | | - isOpen={isOpen} |
20 | | - onRequestClose={closeModal} |
21 | | - > |
22 | | - <div onKeyDown={e => e.stopPropagation()}> |
23 | | - <CloseButton onClick={closeModal}>close</CloseButton> |
| 30 | + <Modal isOpen={isOpen} onRequestClose={closeModal}> |
| 31 | + <Wrap onKeyDown={e => e.stopPropagation()}> |
24 | 32 | <Title>Export Hashtags</Title> |
25 | | - <SelectAllButton onClick={selectAll}>select all</SelectAllButton> |
26 | | - <Code>{exportString}</Code> |
27 | | - </div> |
| 33 | + <ButtonBar> |
| 34 | + <SelectAllButton onClick={selectAll} showSuccess={showCopySuccess}>select all</SelectAllButton> |
| 35 | + <CloseButton onClick={closeModal}>close</CloseButton> |
| 36 | + </ButtonBar> |
| 37 | + <ExportTextarea value={exportString} onChange={onChange}>{exportString}</ExportTextarea> |
| 38 | + </Wrap> |
28 | 39 | </Modal> |
29 | 40 | ) |
30 | 41 | } |
31 | 42 |
|
32 | | -const CloseButton = styled.button.attrs({ classNames: 'Explorer__AddTagModal__CloseButton' })` |
33 | | - font-size: 24px; |
34 | | - position: absolute; |
35 | | - right: 24px; |
36 | | - top: 24px; |
| 43 | +const Wrap = styled.div.attrs({ classNames: 'Explorer__ExportTagsModal__Wrap' })` |
| 44 | + display: flex; |
| 45 | + flex-direction: column; |
| 46 | + height: 100%; |
| 47 | +` |
| 48 | + |
| 49 | +const Title = styled.h1.attrs({ classNames: 'Explorer__ExportTagsModal__Title' })` |
| 50 | + text-align: center; |
37 | 51 | ` |
38 | 52 |
|
39 | | -const SelectAllButton = styled.button.attrs({ classNames: 'Explorer__AddTagModal__SelectAllButton' })` |
| 53 | +const ButtonBar = styled.div.attrs({ classNames: 'Explorer__ExportTagsModal__ButtonBar' })` |
| 54 | + display: flex; |
| 55 | + justify-content: space-between; |
| 56 | +` |
| 57 | + |
| 58 | +const SelectAllButton = styled.button.attrs({ classNames: 'Explorer__ExportTagsModal__SelectAllButton' })<{ |
| 59 | + showSuccess: boolean |
| 60 | +}>` |
| 61 | + align-self: flex-start; |
40 | 62 | font-size: 24px; |
| 63 | +
|
| 64 | + ${o => o.showSuccess && css` |
| 65 | + position: relative; |
| 66 | +
|
| 67 | + &::after { |
| 68 | + background: rgb(200, 255, 230); |
| 69 | + border: 1px solid rgba(8, 128, 44, 1); |
| 70 | + border-radius: 5px; |
| 71 | + box-shadow: inset 0 0 5px rgba(15, 225, 80, 1); |
| 72 | + color: rgba(0, 64, 0, 1); |
| 73 | + content: "copied to clipboard ✓"; |
| 74 | + display: block; |
| 75 | + left: calc(100% + 16px); |
| 76 | + padding: 1px 0 2px; |
| 77 | + position: absolute; |
| 78 | + top: -2px; |
| 79 | + width: 250px; |
| 80 | + z-index: 1; |
| 81 | + } |
| 82 | + `} |
41 | 83 | ` |
42 | 84 |
|
43 | | -const Title = styled.h1.attrs({ classNames: 'Explorer__AddTagModal__Title' })` |
44 | | - text-align: center; |
| 85 | +const CloseButton = styled.button.attrs({ classNames: 'Explorer__ExportTagsModal__CloseButton' })` |
| 86 | + font-size: 24px; |
45 | 87 | ` |
46 | 88 |
|
47 | | -const Code = styled.code.attrs({ classNames: 'Explorer__AddTagModal__Code' })` |
48 | | - color: #999; |
49 | | - display: block; |
50 | | - margin: 24px 0 0; |
51 | | - white-space: pre; |
| 89 | +const ExportTextarea = styled.textarea.attrs({ classNames: 'Explorer__ExportTagsModal__ExportTextarea' })` |
| 90 | + height: 100%; |
| 91 | + margin: 16px -20px -20px; |
| 92 | + padding: 16px; |
52 | 93 | ` |
0 commit comments