| 1 |
The hook fn doesn't set any styles on either of the prop fns (`getRootProps()`/`getInputProps()`). |
| 2 |
|
| 3 |
### Using inline styles |
| 4 |
|
| 5 |
```jsx harmony |
| 6 |
import React, {useMemo} from 'react'; |
| 7 |
import {useDropzone} from 'react-dropzone'; |
| 8 |
|
| 9 |
const baseStyle = { |
| 10 |
flex: 1, |
| 11 |
display: 'flex', |
| 12 |
flexDirection: 'column', |
| 13 |
alignItems: 'center', |
| 14 |
padding: '20px', |
| 15 |
borderWidth: 2, |
| 16 |
borderRadius: 2, |
| 17 |
borderColor: '#eeeeee', |
| 18 |
borderStyle: 'dashed', |
| 19 |
backgroundColor: '#fafafa', |
| 20 |
color: '#bdbdbd', |
| 21 |
outline: 'none', |
| 22 |
transition: 'border .24s ease-in-out' |
| 23 |
}; |
| 24 |
|
| 25 |
const focusedStyle = { |
| 26 |
borderColor: '#2196f3' |
| 27 |
}; |
| 28 |
|
| 29 |
const acceptStyle = { |
| 30 |
borderColor: '#00e676' |
| 31 |
}; |
| 32 |
|
| 33 |
const rejectStyle = { |
| 34 |
borderColor: '#ff1744' |
| 35 |
}; |
| 36 |
|
| 37 |
function StyledDropzone(props) { |
| 38 |
const { |
| 39 |
getRootProps, |
| 40 |
getInputProps, |
| 41 |
isFocused, |
| 42 |
isDragAccept, |
| 43 |
isDragReject |
| 44 |
} = useDropzone({accept: {'image/*': []}}); |
| 45 |
|
| 46 |
const style = useMemo(() => ({ |
| 47 |
...baseStyle, |
| 48 |
...(isFocused ? focusedStyle : {}), |
| 49 |
...(isDragAccept ? acceptStyle : {}), |
| 50 |
...(isDragReject ? rejectStyle : {}) |
| 51 |
}), [ |
| 52 |
isFocused, |
| 53 |
isDragAccept, |
| 54 |
isDragReject |
| 55 |
]); |
| 56 |
|
| 57 |
return ( |
| 58 |
<div className="container"> |
| 59 |
<div {...getRootProps({style})}> |
| 60 |
<input {...getInputProps()} /> |
| 61 |
<p>Drag 'n' drop some files here, or click to select files</p> |
| 62 |
</div> |
| 63 |
</div> |
| 64 |
); |
| 65 |
} |
| 66 |
|
| 67 |
<StyledDropzone /> |
| 68 |
``` |
| 69 |
|
| 70 |
### Using styled-components |
| 71 |
|
| 72 |
```jsx harmony |
| 73 |
import React from 'react'; |
| 74 |
import {useDropzone} from 'react-dropzone'; |
| 75 |
import styled from 'styled-components'; |
| 76 |
|
| 77 |
const getColor = (props) => { |
| 78 |
if (props.isDragAccept) { |
| 79 |
return '#00e676'; |
| 80 |
} |
| 81 |
if (props.isDragReject) { |
| 82 |
return '#ff1744'; |
| 83 |
} |
| 84 |
if (props.isFocused) { |
| 85 |
return '#2196f3'; |
| 86 |
} |
| 87 |
return '#eeeeee'; |
| 88 |
} |
| 89 |
|
| 90 |
const Container = styled.div` |
| 91 |
flex: 1; |
| 92 |
display: flex; |
| 93 |
flex-direction: column; |
| 94 |
align-items: center; |
| 95 |
padding: 20px; |
| 96 |
border-width: 2px; |
| 97 |
border-radius: 2px; |
| 98 |
border-color: ${props => getColor(props)}; |
| 99 |
border-style: dashed; |
| 100 |
background-color: #fafafa; |
| 101 |
color: #bdbdbd; |
| 102 |
outline: none; |
| 103 |
transition: border .24s ease-in-out; |
| 104 |
`; |
| 105 |
|
| 106 |
function StyledDropzone(props) { |
| 107 |
const { |
| 108 |
getRootProps, |
| 109 |
getInputProps, |
| 110 |
isFocused, |
| 111 |
isDragAccept, |
| 112 |
isDragReject |
| 113 |
} = useDropzone({accept: {'image/*': []}}); |
| 114 |
|
| 115 |
return ( |
| 116 |
<div className="container"> |
| 117 |
<Container {...getRootProps({isFocused, isDragAccept, isDragReject})}> |
| 118 |
<input {...getInputProps()} /> |
| 119 |
<p>Drag 'n' drop some files here, or click to select files</p> |
| 120 |
</Container> |
| 121 |
</div> |
| 122 |
); |
| 123 |
} |
| 124 |
|
| 125 |
<StyledDropzone /> |
| 126 |
``` |