-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
6 changed files
with
165 additions
and
13 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
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
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
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,50 @@ | ||
import * as React from 'react'; | ||
import {storiesOf} from '@storybook/react'; | ||
import {action} from '@storybook/addon-actions'; | ||
import {useDropArea} from '..'; | ||
import ShowDocs from '../util/ShowDocs'; | ||
|
||
const Demo = () => { | ||
const [bond, state] = useDropArea({ | ||
onFiles: action('onFiles'), | ||
onUri: action('onUri'), | ||
onText: action('onText'), | ||
}); | ||
|
||
const style: React.CSSProperties = { | ||
width: 300, | ||
height: 200, | ||
margin: '50px auto', | ||
border: '1px solid #000', | ||
textAlign: 'center', | ||
lineHeight: '200px', | ||
...(state.over | ||
? { | ||
border: '1px solid green', | ||
outline: '3px solid yellow', | ||
background: '#f8f8f8', | ||
} | ||
: {}), | ||
}; | ||
|
||
return ( | ||
<div> | ||
<div {...bond} style={style}>Drop here</div> | ||
<div style={{maxWidth: 300, margin: '0 auto'}}> | ||
<ul style={{margin: 0, padding: '10px 18px'}}> | ||
<li>See logs in <code>Actions</code> tab.</li> | ||
<li>Drag in and drop files.</li> | ||
<li><code>Cmd + V</code> paste text here.</li> | ||
<li>Drag in images from other tabs.</li> | ||
<li>Drag in link from navigation bar.</li> | ||
<li>Below is state returned by the hook:</li> | ||
</ul> | ||
<pre>{JSON.stringify(state, null, 4)}</pre> | ||
</div> | ||
</div> | ||
); | ||
}; | ||
|
||
storiesOf('UI|useDropArea', module) | ||
.add('Docs', () => <ShowDocs md={require('../../docs/useDrop.md')} />) | ||
.add('Default', () => <Demo />); |
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
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 |
---|---|---|
@@ -1,14 +1,85 @@ | ||
import * as React from 'react'; | ||
|
||
const useDropArea = (el: React.ReactElement<any>) => { | ||
if (process.env.NODE_ENV !== 'production') { | ||
if (!React.isValidElement(el)) { | ||
throw new TypeError( | ||
'useDropArea first argument must be a valid ' + | ||
'React element, such as <div/>.' | ||
); | ||
} | ||
import {useMemo, useState} from 'react'; | ||
import useRefMounted from './useRefMounted'; | ||
|
||
export interface DropAreaState { | ||
over: boolean; | ||
} | ||
|
||
export interface DropAreaBond { | ||
onDragOver: React.DragEventHandler; | ||
onDragEnter: React.DragEventHandler; | ||
onDragLeave: React.DragEventHandler; | ||
onDrop: React.DragEventHandler; | ||
onPaste: React.ClipboardEventHandler; | ||
} | ||
|
||
export interface DropAreaOptions { | ||
onFiles?: (files: File[], event?) => void; | ||
onText?: (text: string, event?) => void; | ||
onUri?: (url: string, event?) => void; | ||
} | ||
|
||
const noop = () => {}; | ||
const defaultState: DropAreaState = { | ||
over: false, | ||
}; | ||
|
||
const createProcess = (options: DropAreaOptions, mounted: React.RefObject<boolean>) => ( | ||
dataTransfer: DataTransfer, | ||
event, | ||
) => { | ||
const uri = dataTransfer.getData('text/uri-list'); | ||
|
||
if (uri) { | ||
(options.onUri || noop)(uri, event); | ||
return; | ||
} | ||
|
||
if (dataTransfer.files && dataTransfer.files.length) { | ||
(options.onFiles || noop)(Array.from(dataTransfer.files), event); | ||
return; | ||
} | ||
|
||
if (dataTransfer.items && dataTransfer.items.length) { | ||
dataTransfer.items[0].getAsString((text) => { | ||
if (mounted.current) { | ||
(options.onText || noop)(text, event); | ||
} | ||
}); | ||
} | ||
}; | ||
|
||
const createBond = (process, setOver): DropAreaBond => ({ | ||
onDragOver: (event) => { | ||
event.preventDefault(); | ||
}, | ||
onDragEnter: (event) => { | ||
event.preventDefault(); | ||
setOver(true); | ||
}, | ||
onDragLeave: () => { | ||
setOver(false); | ||
}, | ||
onDrop: (event) => { | ||
event.preventDefault(); | ||
event.persist(); | ||
setOver(false); | ||
process(event.dataTransfer, event); | ||
}, | ||
onPaste: (event) => { | ||
event.persist(); | ||
process(event.clipboardData, event); | ||
}, | ||
}); | ||
|
||
const useDropArea = (options: DropAreaOptions = {}): [DropAreaBond, DropAreaState] => { | ||
const {onFiles, onText, onUri} = options; | ||
const mounted = useRefMounted(); | ||
const [over, setOver] = useState<boolean>(false); | ||
const process = useMemo(() => createProcess(options, mounted), [onFiles, onText, onUri]); | ||
const bond: DropAreaBond = useMemo(() => createBond(process, setOver), [process, setOver]); | ||
|
||
return [bond, {over}]; | ||
}; | ||
|
||
export default useDropArea; |