-
Notifications
You must be signed in to change notification settings - Fork 2
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
1 parent
29b15d6
commit 8540ef7
Showing
14 changed files
with
10,463 additions
and
335 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
node_modules | ||
dist | ||
coverage | ||
coverage | ||
.docz |
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,30 @@ | ||
import React from 'react'; | ||
import useKey from '../../src/useKeyCapture'; | ||
import './styles.css'; | ||
|
||
const displayKeys = ['Enter', 'Escape', 'Tab', 'ArrowUp']; | ||
|
||
const GlobalComponent = () => { | ||
const { keyData } = useKey(); | ||
|
||
const getIsActive = key => (keyData.key === key ? 'active' : ''); | ||
|
||
const getCombinedActiveClass = () => | ||
keyData.key === 'A' && keyData.isWithShift ? 'active' : ''; | ||
|
||
return ( | ||
<> | ||
<div className="message"> | ||
Press below mentioned keys to see the hook in action | ||
</div> | ||
<div className="container"> | ||
{displayKeys.map(key => { | ||
return <div className={`box ${getIsActive(key)}`}>{key}</div>; | ||
})} | ||
<div className={`box ${getCombinedActiveClass()}`}>Shift + A</div> | ||
</div> | ||
</> | ||
); | ||
}; | ||
|
||
export default GlobalComponent; |
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,28 @@ | ||
import React from 'react'; | ||
import useKey from '../../src/useKeyCapture'; | ||
import './styles.css'; | ||
|
||
const displayKeys = ['Q', 'W', 'E', 'R', 'T', 'Y', 'Backspace']; | ||
|
||
const TargetEventComponent = () => { | ||
const { keyData, getTargetProps } = useKey(); | ||
|
||
const getIsActive = key => (keyData.key === key ? 'active' : ''); | ||
|
||
return ( | ||
<div className="container-target"> | ||
<div className="message"> | ||
Type QWERTY in the input element below. If the focus outside the target, | ||
we won't see any change. | ||
</div> | ||
<input {...getTargetProps()} /> | ||
<div className="container"> | ||
{displayKeys.map(key => { | ||
return <div className={`box ${getIsActive(key)}`}>{key}</div>; | ||
})} | ||
</div> | ||
</div> | ||
); | ||
}; | ||
|
||
export default TargetEventComponent; |
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,62 @@ | ||
.box { | ||
height: 100px; | ||
width: 100px; | ||
margin: 4px; | ||
display: flex; | ||
align-items: center; | ||
justify-content: center; | ||
border: 4px solid #b8e994; | ||
border-radius: 4px; | ||
transition: 0.3s all ease-in-out; | ||
background: #ffffff; | ||
} | ||
|
||
.container { | ||
display: flex; | ||
align-items: center; | ||
justify-content: center; | ||
min-height: 200px; | ||
border: 1px solid #ccc; | ||
border-radius: 8px; | ||
background: #5f3ccf; | ||
} | ||
|
||
.container-target .container { | ||
border: none; | ||
} | ||
.container-target .message { | ||
color: #fff; | ||
margin: 48px; | ||
} | ||
|
||
.container-target input { | ||
width: 500px; | ||
height: 50px; | ||
background: #977af7; | ||
border: none; | ||
outline: none; | ||
font-size: 48px; | ||
line-height: 50px; | ||
color: #fff; | ||
text-align: center; | ||
padding: 8px; | ||
border-radius: 8px; | ||
} | ||
|
||
.container-target { | ||
background: #5f3ccf; | ||
display: flex; | ||
flex-flow: column; | ||
justify-content: center; | ||
align-items: center; | ||
border-radius: 8px; | ||
} | ||
|
||
.box.active { | ||
background: #78e08f; | ||
color: #ffffff; | ||
} | ||
|
||
.message { | ||
text-align: center; | ||
} |
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,44 @@ | ||
--- | ||
name: Docs | ||
route: / | ||
order: 1 | ||
--- | ||
|
||
# use-key-capture | ||
|
||
use-key-capture is a custom hook which will let us not to worry about the key-press event. Just plugin in use-key-capture hook to the **target** we want to listen for key press event or by default it can listen for key-press event **globally**. | ||
|
||
## Installation | ||
|
||
**npm** | ||
|
||
```bash | ||
npm i use-key-capture | ||
``` | ||
|
||
**yarn** | ||
|
||
```bash | ||
yarn add use-key-capture | ||
``` | ||
|
||
## How it is used | ||
|
||
```jsx | ||
import useKey from 'use-key-capture'; | ||
... | ||
const { keyData, resetKeyData, getTargetProps } = useKey(); | ||
``` | ||
|
||
## Properties | ||
|
||
| Property name | Description | | ||
| ------------- | ------------------------------ | | ||
| keyData | gives pressed key details | | ||
| resetKeyData | resets the pressed key details | | ||
|
||
## Target Prop getter | ||
|
||
| Property name | Description | | ||
| -------------- | ------------------------------------------ | | ||
| getTargetProps | gives the required props to target element | |
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,91 @@ | ||
--- | ||
name: Demo | ||
route: /demo | ||
order: 2 | ||
--- | ||
|
||
import { Playground } from 'docz'; | ||
import GlobalComponent from '../Components/GlobalEventComponent.js'; | ||
import TargetEventComponent from '../Components/TargetEventComponent.js'; | ||
|
||
## Target Listener | ||
|
||
Listen only for the key-press event for a particular target. | ||
|
||
It is provided by `getTargetProps` props getter. If `getTargetProps` props getter is not provided, then it will listen for key-press event globally. | ||
|
||
```jsx | ||
<input {...getTargetProps()} /> | ||
``` | ||
|
||
<TargetEventComponent /> | ||
|
||
```jsx | ||
import React from 'react'; | ||
import useKey from 'use-key-capture'; | ||
import './styles.css'; | ||
|
||
const displayKeys = ['Q', 'W', 'E', 'R', 'T', 'Y', 'Backspace']; | ||
|
||
const TargetEventComponent = () => { | ||
const { keyData, getTargetProps } = useKey(); | ||
|
||
const getIsActive = key => (keyData.key === key ? 'active' : ''); | ||
|
||
return ( | ||
<div className="container-target"> | ||
<div className="message"> | ||
Type QWERTY in the input element below. If the focus outside the target, | ||
we won't see any change. | ||
</div> | ||
<input {...getTargetProps()} /> | ||
<div className="container"> | ||
{displayKeys.map(key => { | ||
return <div className={`box ${getIsActive(key)}`}>{key}</div>; | ||
})} | ||
</div> | ||
</div> | ||
); | ||
}; | ||
export default TargetEventComponent; | ||
``` | ||
## Global Listener | ||
If no target is specified, it will listen for global event. | ||
<GlobalComponent /> | ||
```jsx | ||
import React from 'react'; | ||
import useKey from 'use-key-capture'; | ||
import './styles.css'; | ||
const displayKeys = ['Enter', 'Escape', 'Tab', 'ArrowUp']; | ||
const GlobalComponent = () => { | ||
const { keyData } = useKey(); | ||
const getIsActive = key => (keyData.key === key ? 'active' : ''); | ||
const getCombinedActiveClass = () => | ||
keyData.key === 'A' && keyData.isWithShift ? 'active' : ''; | ||
return ( | ||
<> | ||
<div className="message"> | ||
Press below mentioned keys to see the hook in action | ||
</div> | ||
<div className="container"> | ||
{displayKeys.map(key => { | ||
return <div className={`box ${getIsActive(key)}`}>{key}</div>; | ||
})} | ||
<div className={`box ${getCombinedActiveClass()}`}>Shift + A</div> | ||
</div> | ||
</> | ||
); | ||
}; | ||
export default GlobalComponent; | ||
``` |
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
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
Oops, something went wrong.