@matsun/reactiscapslockactive
Version:
A lightweight and easy to use React component that tracks whether or not Caps lock is active
2 lines (1 loc) • 2.34 kB
JavaScript
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t(require("react")):"function"==typeof define&&define.amd?define(["react"],t):e.ReactIsCapsLockActive=t(e.React)}(this,function(r){"use strict";function i(e,t){for(var n=0;n<t.length;n++){var o=t[n];o.enumerable=o.enumerable||!1,o.configurable=!0,"value"in o&&(o.writable=!0),Object.defineProperty(e,o.key,o)}}function s(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function u(e){return(u=Object.setPrototypeOf?Object.getPrototypeOf:function(e){return e.__proto__||Object.getPrototypeOf(e)})(e)}function a(e,t){return(a=Object.setPrototypeOf||function(e,t){return e.__proto__=t,e})(e,t)}function f(e){if(void 0===e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return e}r=r&&r.hasOwnProperty("default")?r.default:r;var p="keydown",d="keyup";return function(e){function c(){var e,t,n,o;!function(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}(this,c);for(var r=arguments.length,i=new Array(r),a=0;a<r;a++)i[a]=arguments[a];return n=this,o=(e=u(c)).call.apply(e,[this].concat(i)),s(f(f(t=!o||"object"!=typeof o&&"function"!=typeof o?f(n):o)),"state",{isCapsLockActive:!1}),s(f(f(t)),"wasCapsLockActivated",function(e){e.getModifierState&&e.getModifierState("CapsLock")&&!1===t.state.isCapsLockActive&&t.setState({isCapsLockActive:!0})}),s(f(f(t)),"wasCapsLockDeactivated",function(e){e.getModifierState&&!e.getModifierState("CapsLock")&&!0===t.state.isCapsLockActive&&t.setState({isCapsLockActive:!1})}),t}var t,n,o;return function(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function");e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,writable:!0,configurable:!0}}),t&&a(e,t)}(c,r.Component),t=c,(n=[{key:"componentDidMount",value:function(){document.addEventListener(p,this.wasCapsLockActivated),document.addEventListener(d,this.wasCapsLockDeactivated)}},{key:"componentWillUnmount",value:function(){document.removeEventListener(p,this.wasCapsLockActivated),document.removeEventListener(d,this.wasCapsLockDeactivated)}},{key:"render",value:function(){return this.props.children(this.state.isCapsLockActive)}}])&&i(t.prototype,n),o&&i(t,o),c}()});