-
-
Notifications
You must be signed in to change notification settings - Fork 425
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
91 additions
and
15 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,73 @@ | ||
import React from 'react'; | ||
import styled from 'styled-components'; | ||
|
||
const FullWidth = styled.div` | ||
width: 100%; | ||
height: 100%; | ||
`; | ||
const ChildWrapper = styled(FullWidth)` | ||
opacity: ${({ dragging }) => (dragging ? 0.1 : 1)}; | ||
`; | ||
const Area = styled(FullWidth)` | ||
position: relative; | ||
`; | ||
|
||
const DragHelp = styled(FullWidth)` | ||
position: absolute; | ||
text-align: center; | ||
color: white; | ||
font-size: 18px; | ||
border: 4px dashed white; | ||
`; | ||
|
||
const prevent = e => { | ||
e.preventDefault(); | ||
e.stopPropagation(); | ||
}; | ||
|
||
export default class DropArea extends React.Component { | ||
state = { dragging: false }; | ||
|
||
onDrop = e => { | ||
this.onEnd(); | ||
prevent(e); | ||
const { files } = e.dataTransfer; | ||
const file = files[0]; | ||
if (!file || file.type !== "image/svg+xml") { | ||
return; | ||
} | ||
const fileReader = new FileReader(); | ||
fileReader.onload = () => { | ||
const text = fileReader.result; | ||
this.props.onChange(text); | ||
}; | ||
fileReader.readAsText(file); | ||
}; | ||
onStart = () => { | ||
console.log('start'); | ||
this.setState({ dragging: true }); | ||
}; | ||
onEnd = () => { | ||
console.log('end'); | ||
this.setState({ dragging: false }); | ||
}; | ||
render() { | ||
const { dragging } = this.state; | ||
return ( | ||
<Area | ||
onDragEnterCapture={this.onStart} | ||
onDragLeaveCapture={this.onEnd} | ||
onDragOverCapture={prevent} | ||
onDrop={prevent} | ||
onDropCapture={this.onDrop} | ||
> | ||
{dragging && ( | ||
<DragHelp> | ||
<p>📄 Drop .svg file here.</p> | ||
</DragHelp> | ||
)} | ||
<ChildWrapper dragging={dragging}>{this.props.children}</ChildWrapper> | ||
</Area> | ||
); | ||
} | ||
} |
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
e6772d7
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully aliased the URL https://svgr-ejmqswkkuf.now.sh to the following alias.