Skip to content

Commit

Permalink
core setup
Browse files Browse the repository at this point in the history
  • Loading branch information
pranesh239 committed Jan 6, 2020
0 parents commit ee11879
Show file tree
Hide file tree
Showing 10 changed files with 3,897 additions and 0 deletions.
11 changes: 11 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"presets": [
[
"@babel/preset-env",
{
"targets": "> 0.25%, not dead"
}
],
"@babel/preset-react"
]
}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
14 changes: 14 additions & 0 deletions dist/useKeyCapture.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "use-key-capture",
"version": "0.0.1-beta.0",
"main": "dist/useKeyCapture.js",
"license": "MIT",
"scripts": {
"webpack": "rimraf dist && webpack"
},
"devDependencies": {
"@babel/core": "^7.7.7",
"@babel/preset-env": "^7.7.7",
"@babel/preset-react": "^7.7.4",
"babel-loader": "^8.0.6",
"rimraf": "^3.0.0",
"webpack": "^4.41.5",
"webpack-cli": "^3.3.10"
},
"dependencies": {
"react": "^16.12.0",
"react-dom": "^16.12.0"
},
"browserslist": "> 0.25%, not dead",
"author": "Pranesh T G",
"homepage": "https://github.com/pranesh239/use-key-capture#readme",
"bugs": {
"url": "https://github.com/pranesh239/use-key-capture/issues"
}
}
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default } from './useKeyCapture';
51 changes: 51 additions & 0 deletions src/useKeyCapture/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { useEffect, useReducer } from 'react';
import reducer from './useKeyCaptureReducer';

import {
useKeyInitialState,
getActionType,
useKeyActionTypes,
useEnhancedReducer
} from './useKeyCaptureUtils';

function useKeys() {
const [keyData, dispatch] = useReducer(
useEnhancedReducer(reducer),
useKeyInitialState
);

const dispatchWithActionDetails = event => {
dispatch({
payload: event,
type: getActionType(event)
});
};

/**
* It resets all state values to initial values
*/
const resetKeyDetails = () => {
dispatch({
type: useKeyActionTypes.RESET_CAPTURES
});
};

useEffect(() => {
// example for IS_TRUSTED
// setTimeout(() => {
// document.dispatchEvent(new KeyboardEvent('keydown'), { key: 'Escape' });
// }, 2000);
}, [keyData]);

useEffect(() => {
document.addEventListener('keydown', dispatchWithActionDetails);

return () => {
document.removeEventListener('keydown', dispatchWithActionDetails);
};
}, []);

return { keyData, resetKeyDetails };
}

export default useKeys;
42 changes: 42 additions & 0 deletions src/useKeyCapture/useKeyCaptureReducer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { useKeyInitialState, useKeyActionTypes } from './useKeyCaptureUtils';

export default function keyReducer(state, action) {
switch (action.type) {
case useKeyActionTypes.ESCAPE_KEY: {
return {
...useKeyInitialState,
isEscape: true
};
}

case useKeyActionTypes.ENTER_KEY: {
return {
...useKeyInitialState,
isEnter: true
};
}

case useKeyActionTypes.CAPS_ALPHABET: {
return {
...useKeyInitialState,
isCaps: true
};
}

case useKeyActionTypes.SMALL_ALPHABET: {
return {
...useKeyInitialState,
isSmall: true
};
}

case useKeyActionTypes.NUMBER: {
return {
...useKeyInitialState,
isNumber: true
};
}
default:
return { ...useKeyInitialState };
}
}
67 changes: 67 additions & 0 deletions src/useKeyCapture/useKeyCaptureUtils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { useCallback } from 'react';

export const useKeyInitialState = {
keyCode: null,
keyName: null,
isEscape: false,
isEnter: false,
isSpace: false,
isRightArrow: false,
isLeftArrow: false,
isUpArrow: false,
isDownArrow: false,
isTab: false,
isWithShift: false,
isWithCtrl: false,
isNumber: false,
isCaps: false,
isSmall: false,
isSpecialCharacter: false
};

export const useKeyActionTypes = {
ENTER_KEY: 'ENTER_KEY',
ESCAPE_KEY: 'ESCAPE_KEY',
RESET_CAPTURES: 'RESET_CAPTURES',
CAPS_ALPHABET: 'CAPS',
SMALL_ALPHABET: 'SMALL',
NUMBER: 'NUMBER'
};

const keyCodeMapper = {
13: useKeyActionTypes.ENTER_KEY,
27: useKeyActionTypes.ESCAPE_KEY
};

const isCapitalLetterPressed = key => /^[A-Z]$/.test(key);
const isSmallLetterPressed = key => /^[a-z]$/.test(key);

export const getActionType = eventDetails => {
if (!eventDetails) {
throw new Error('Event called with no details');
}

if (keyCodeMapper[eventDetails.keyCode])
return keyCodeMapper[eventDetails.keyCode];

if (isCapitalLetterPressed(eventDetails.key)) {
return useKeyActionTypes.CAPS_ALPHABET;
}

if (isSmallLetterPressed(eventDetails.key)) {
return useKeyActionTypes.SMALL_ALPHABET;
}

if (eventDetails.keyCode >= 48 && eventDetails.keyCode <= 57) {
return useKeyActionTypes.NUMBER;
}
return 'SOME_OTHER_KEY';
};

export const useEnhancedReducer = reducer =>
useCallback(
(state, action) => {
return reducer(state, action);
},
[reducer]
);
20 changes: 20 additions & 0 deletions webpack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const path = require('path');

module.exports = {
mode: 'production',
entry: path.resolve(__dirname, 'src/index.js'),
output: {
filename: 'useKeyCapture.js',
path: path.resolve(__dirname, 'dist')
},
module: {
rules: [
{
test: /\.js$/,
include: path.resolve(__dirname, 'src'),
exclude: /(node_modules|bower_components|build)/,
use: 'babel-loader'
}
]
}
};
Loading

0 comments on commit ee11879

Please sign in to comment.