UNPKG

react-konami-code

Version:

Trigger an easter egg by pressing a sequence of keys. Available as a component or a custom hook. Supports timeout and input debounce/reset.

1 lines 3.85 kB
(()=>{"use strict";var t={n:e=>{var n=e&&e.__esModule?()=>e.default:()=>e;return t.d(n,{a:n}),n},d:(e,n)=>{for(var o in n)t.o(n,o)&&!t.o(e,o)&&Object.defineProperty(e,o,{enumerable:!0,get:n[o]})},o:(t,e)=>Object.prototype.hasOwnProperty.call(t,e),r:t=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})}},e={};t.r(e),t.d(e,{default:()=>f,useKonami:()=>h});const n=require("react");var o=t.n(n);const r=require("prop-types");var i=t.n(r);const s=function(){function t(t,e){this.t=e,this.fn=t,this.timerIntervalID=setInterval(this.fn,this.t)}return t.prototype.stop=function(){return this.timerIntervalID&&(clearInterval(this.timerIntervalID),this.timerIntervalID=null),this},t.prototype.start=function(){return this.timerIntervalID||(this.stop(),this.timerIntervalID=setInterval(this.fn,this.t)),this},t.prototype.reset=function(t){return this.t=t,this.stop().start()},t}(),u=function(t,e){return!(!Array.isArray(t)||!Array.isArray(e))&&t.length===e.length&&t.every((function(t,n){return t===e[n]}))};var a,p=[38,38,40,40,37,39,37,39,66,65],l=(a=function(t,e){return a=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var n in e)Object.prototype.hasOwnProperty.call(e,n)&&(t[n]=e[n])},a(t,e)},function(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Class extends value "+String(e)+" is not a constructor or null");function n(){this.constructor=t}a(t,e),t.prototype=null===e?Object.create(e):(n.prototype=e.prototype,new n)}),c={action:i().func,className:i().string,code:i().arrayOf(i().number),disabled:i().bool,onTimeout:i().func,resetDelay:i().number,timeout:i().number},d=function(t){function e(e){var n=t.call(this,e)||this;return n.state={done:!1,input:[]},n.timeoutID=null,n.onKeyUp=n.onKeyUp.bind(n),n.resetInput=n.resetInput.bind(n),n}return l(e,t),e.prototype.componentDidMount=function(){var t=this,e=this.props.resetDelay;document.addEventListener("keyup",this.onKeyUp);var n=Number(e);0!==n&&(this._timer=new s((function(){return t.resetInput()}),n))},e.prototype.shouldComponentUpdate=function(t,e){return this.props.className!==t.className||this.props.disabled!==t.disabled||this.state.done!==e.done},e.prototype.componentWillUnmount=function(){var t=this.props.resetDelay;this.timeoutID&&clearTimeout(this.timeoutID),0!==t&&this._timer.stop(),document.removeEventListener("keyup",this.onKeyUp)},e.prototype.onKeyUp=function(t){var e=this,n=this.state,o=n.done,r=n.input,i=this.props,s=i.action,a=i.code,p=i.disabled,l=i.onTimeout,c=i.resetDelay,d=i.timeout,h=Number(c);p||(0!==h&&this._timer.reset(h),r.push(t.keyCode),a&&r.splice(-a.length-1,r.length-a.length),this.setState({input:r},(function(){u(e.state.input,a)&&!o&&(0!==h&&e._timer.stop(),e.setState({done:!0},(function(){"function"==typeof s&&s()})),d&&(e.timeoutID=setTimeout((function(){e.setState({done:!1}),"function"==typeof l&&l()}),Number(d))))})))},e.prototype.resetInput=function(){this.setState({input:[]})},e.prototype.render=function(){var t=this.props,e=t.children,n=t.className,r=t.disabled,i=this.state.done;return o().createElement("div",{className:"konami ".concat(null!=n?n:""),style:{display:!i||r?"none":"block"}},e)},e}(o().Component);d.defaultProps={className:"",code:p,disabled:!1,resetDelay:1e3,children:null,action:null,onTimeout:null,timeout:0},d.propTypes=c;const h=function(t,e){var o=(void 0===e?{}:e).code,r=void 0===o?p:o,i=(0,n.useState)([]),s=i[0],u=i[1],a=(0,n.useCallback)((function(e){var n=s;n.push(e.keyCode),n.splice(-r.length-1,s.length-r.length),u(n),n.join("").includes(r.join(""))&&t()}),[s,u,r,t]);(0,n.useEffect)((function(){return document.addEventListener("keyup",a),function(){document.removeEventListener("keyup",a)}}),[a])},f=d;module.exports=e})();