UNPKG

react-dropzone

Version:
113 lines (95 loc) 2.29 kB
The hook fn doesn't set any styles on either of the prop fns (`getRootProps()`/`getInputProps()`). ### Using inline styles ```jsx harmony import React, {useMemo} from 'react'; import {useDropzone} from 'react-dropzone'; const baseStyle = { width: 200, height: 200, borderWidth: 2, borderColor: '#666', borderStyle: 'dashed', borderRadius: 5 }; const activeStyle = { borderStyle: 'solid', borderColor: '#6c6', backgroundColor: '#eee' }; const acceptStyle = { borderStyle: 'solid', borderColor: '#00e676' }; const rejectStyle = { borderStyle: 'solid', borderColor: '#ff1744' }; function StyledDropzone(props) { const { getRootProps, getInputProps, isDragActive, isDragAccept, isDragReject } = useDropzone({accept: 'image/*'}); const style = useMemo(() => ({ ...baseStyle, ...(isDragActive ? activeStyle : {}), ...(isDragAccept ? acceptStyle : {}), ...(isDragReject ? rejectStyle : {}) }), [ isDragActive, isDragReject ]); return ( <div {...getRootProps({style})}> <input {...getInputProps()} /> <p>Drag 'n' drop some files here, or click to select files</p> </div> ); } <StyledDropzone /> ``` ### Using styled-components ```jsx harmony import React from 'react'; import {useDropzone} from 'react-dropzone'; import styled from 'styled-components'; const getColor = (props) => { if (props.isDragAccept) { return '#00e676'; } if (props.isDragReject) { return '#ff1744'; } if (props.isDragActive) { return '#6c6'; } return '#666'; } const Container = styled.div` width: 200px; height: 200px; border-width: 2px; border-radius: 5px; border-color: ${props => getColor(props)}; border-style: ${props => props.isDragActive ? 'solid' : 'dashed'}; background-color: ${props => props.isDragActive ? '#eee' : ''}; `; function StyledDropzone(props) { const { getRootProps, getInputProps, isDragActive, isDragAccept, isDragReject } = useDropzone({accept: 'image/*'}); return ( <Container {...getRootProps({isDragActive, isDragAccept, isDragReject})}> <input {...getInputProps()} /> <p>Drag 'n' drop some files here, or click to select files</p> </Container> ); } <StyledDropzone /> ```