-
-
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
5 changed files
with
102 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# `useStartTyping` | ||
|
||
React sensor hook that fires a callback when user start typing. Can be use | ||
to focus default input field on the page. | ||
|
||
## Usage | ||
|
||
```jsx | ||
import useStartTyping from 'react-use/lib/useStartTyping'; | ||
|
||
const Demo = () => { | ||
useStartTyping(() => alert('Started typing...')); | ||
|
||
return null; | ||
}; | ||
``` |
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,41 @@ | ||
import {storiesOf} from '@storybook/react'; | ||
import * as React from 'react'; | ||
import {useStartTyping} from '..'; | ||
import ShowDocs from './util/ShowDocs'; | ||
|
||
const Demo = () => { | ||
const input = React.useRef(null); | ||
useStartTyping(() => { | ||
if (input.current) { | ||
input.current.focus(); | ||
} | ||
}); | ||
|
||
return ( | ||
<div> | ||
<p>Start typing, and below field will get focused.</p> | ||
<input ref={input} /> | ||
|
||
<br /> | ||
<hr /> | ||
|
||
<p>Try focusing below elements and see what happens.</p> | ||
<button>When button is focused, it will lose it.</button> | ||
<br /> | ||
<br /> | ||
<input /> | ||
<br /> | ||
<br /> | ||
<textarea>Editable textarea</textarea> | ||
<br /> | ||
<br /> | ||
<div contentEditable>Editable DIV</div> | ||
</div> | ||
); | ||
}; | ||
|
||
storiesOf('Sensors|useStartTyping', module) | ||
// .add('Docs', () => <ShowDocs md={require('../../docs/useStartTyping.md')} />) | ||
.add('Demo', () => | ||
<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 |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import {useLayoutEffect} from 'react'; | ||
|
||
const isFocusedElementEditable = () => { | ||
const {activeElement, body} = document; | ||
|
||
// If not element has focus, we assume it is not editable, too. | ||
if (activeElement === body) return false; | ||
|
||
// Assume <input> and <textarea> elements are editable. | ||
switch (activeElement.tagName) { | ||
case 'INPUT': | ||
case 'TEXTAREA': | ||
return true; | ||
} | ||
|
||
// Check if any other focused element id editable. | ||
return activeElement.hasAttribute('contenteditable'); | ||
}; | ||
|
||
const isTypedCharGood = ({keyCode}: KeyboardEvent) => { | ||
// 0...9 | ||
if ((keyCode >= 48) && (keyCode <= 57)) return true; | ||
// a...z | ||
if ((keyCode >= 65) && (keyCode <= 90)) return true; | ||
// All other keys. | ||
return false; | ||
}; | ||
|
||
const useStartTyping = (onStartTyping: (event: KeyboardEvent) => void) => { | ||
useLayoutEffect(() => { | ||
const keydown = (event) => { | ||
!isFocusedElementEditable() && isTypedCharGood(event) && onStartTyping(event); | ||
}; | ||
|
||
document.addEventListener('keydown', keydown); | ||
return () => { | ||
document.removeEventListener('keydown', keydown); | ||
}; | ||
}, []); | ||
}; | ||
|
||
export default useStartTyping; |