-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extend API to allow event listener options
- Loading branch information
1 parent
feb66b0
commit ba76146
Showing
4 changed files
with
59 additions
and
51 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,41 @@ | ||
import { text, withKnobs } from '@storybook/addon-knobs'; | ||
import { withKnobs } from '@storybook/addon-knobs'; | ||
import { storiesOf } from '@storybook/react'; | ||
import * as React from 'react'; | ||
import useHotkeys from '../index'; | ||
import usePrevious from './utils/hooks/usePrevious'; | ||
|
||
const Basic = ({ hotkey }: { hotkey: string }) => { | ||
const [keyPressed, setKeyPressed] = React.useState(false); | ||
const prevHotkey = usePrevious(hotkey); | ||
const Basic: React.FC<{ hotkey: string }> = ({ hotkey }) => { | ||
useHotkeys(hotkey, () => { | ||
alert(`${hotkey} pressed!`); | ||
}); | ||
|
||
return <div>Press {hotkey}</div>; | ||
}; | ||
|
||
React.useEffect(() => { | ||
if (hotkey !== prevHotkey) { | ||
setKeyPressed(false); | ||
} | ||
}, [hotkey]); | ||
const EventCapture = () => { | ||
useHotkeys( | ||
'Escape', | ||
() => { | ||
alert('Escape pressed: capturing phase'); | ||
}, | ||
true | ||
); | ||
|
||
useHotkeys(hotkey, event => { | ||
event.preventDefault(); | ||
setKeyPressed(true); | ||
return <Basic hotkey={'Escape'} />; | ||
}; | ||
|
||
const EscapeHatch = () => { | ||
useHotkeys('*', (event: KeyboardEvent) => { | ||
alert(`${event.key} pressed!`); | ||
}); | ||
|
||
return ( | ||
<div> | ||
<span>Hotkey selected:</span> <strong>{hotkey}</strong> | ||
<br /> | ||
<span>Hotkey pressed: {keyPressed ? 'TRUE' : 'FALSE'}</span> | ||
</div> | ||
); | ||
return <p>Press any key</p>; | ||
}; | ||
|
||
storiesOf('useHotkeys', module) | ||
.addDecorator(withKnobs) | ||
.add('Core API', () => <Basic hotkey={text('Hotkeys', 'Escape')} />) | ||
.add('Escape hatch', () => <Basic hotkey={'*'} />); | ||
.add('Basic', () => <Basic hotkey={'Escape'} />) | ||
.add('Modifier combination', () => <Basic hotkey={'Meta+Shift+z'} />) | ||
.add('Key sequences', () => <Basic hotkey={'j o b'} />) | ||
.add('Space in sequence', () => <Basic hotkey={'w " " d'} />) | ||
.add('Event listener options', () => <EventCapture />) | ||
.add('Escape hatch', () => <EscapeHatch />); |
This file was deleted.
Oops, something went wrong.
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