GithubHelp home page GithubHelp logo

liorp / react-konami-code Goto Github PK

View Code? Open in Web Editor NEW

This project forked from vmarchesin/react-konami-code

0.0 0.0 0.0 1.71 MB

Trigger an easter egg by pressing a sequence of keys.

License: MIT License

JavaScript 19.75% TypeScript 80.25%

react-konami-code's Introduction

react-konami-code GitHub stars

Trigger an easter egg by pressing a sequence of keys. Also available as a custom hook.

npm npm gzip size Travis Maintenance Known Vulnerabilities

Install

NPM

npm i react-konami-code -S

Example

Clone this repo and run npm start in the example folder.

Usage

CommonJS Module (Webpack or Browserify)

import React from 'react';
import Konami from 'react-konami-code';

export default class App extends React.Component {
  easterEgg = () => {
    alert('Hey, you typed the Konami Code!');
  }

  render = () => (
    <Konami action={this.easterEgg}>
      {"Hey, I'm an Easter Egg! Look at me!"}
    </Konami>
  )
}

TypeScript

import * as React from 'react';
import Konami from 'react-konami-code';

export default class App extends React.Component {
  public render = () => (
    <Konami action={this.easterEgg}>
      {"Hey, I'm an Easter Egg! Look at me!"}
    </Konami>
  )

  private easterEgg = () => {
    alert('Hey, you typed the Konami Code!');
  }
}

Custom Hook

Refer to the Using the custom Hook section.

Component

Children

The content to be displayed should be passed as children inside the Konami component, and it will be wrapped inside a div. You can however not pass any children, and then just use the action callback to fire your easter egg.

You can pass children and action at the same time to display some content and fire a secondary action.

Props

action function

Default: null

The callback action that should fire when the code is input.

className string

Default: ""

CSS classes can be applied to the div wrapping your secret content. By default the div will always have the konami className.

<Konami className="myclass">
  {"Hey, I'm an Easter Egg!"}
</Konami>

will result in:

<div className="konami myclass">
  Hey, I'm an Easter Egg!
</div>

code Array<number>

Default: [38,38,40,40,37,39,37,39,66,65]

An array with the sequence of keyCodes necessary to trigger the action. The default code is the Konami Code: ↑ ↑ ↓ ↓ ← → ← → B A

You can find the keyCodes for each character here.

disabled boolean

Default: false

If the trigger should be disabled or not. This is dynamic and you can enable/disable at will. The action callback will only trigger when disabled == false.

onTimeout function

The callback to fire when the timeout is finished, if any.

resetDelay number

Default: 1000

The delay interval on which you need to start the input again. If you set it to 0 it will never reset the user input. Value should be in ms.

timeout number

Default: null

The timeout to hide the easter egg. When the timeout is finished it will set display: none to the wrapping div and will fire onTimeout. By default it runs forever. Value should be in ms.

Using the custom Hook

If you want to call an action without rendering children or handling timeouts it's recommended to use the useKonami hook.

import React from 'react';
import { useKonami } from 'react-konami-code';
const easterEgg = () => {
  alert('Hey, you typed the Konami Code!');
}
export default () => {
  useKonami(easterEgg);
  return <div />;
};

API

useKonami(action, [options])

action function

Required

The callback action that should fire when the code is input.

options object

  • code

    Default: [38,38,40,40,37,39,37,39,66,65]

    An array with the sequence of keyCodes necessary to trigger the action. Refer to main code section for the keyCodes.

License

MIT

Contact

You can reach me on my Github or send an email to [email protected].

react-konami-code's People

Contributors

benzleung avatar eliamartani avatar friemt avatar mechamobau avatar thecampagnards avatar vitorluizc avatar vmarchesin avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.