Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

babel-plugin-jsx-event-modifiers

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

babel-plugin-jsx-event-modifiers

JSX Syntactic Sugar Plugin for Event Modifiers

  • 1.0.2
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
76K
increased by1.92%
Maintainers
1
Weekly downloads
 
Created
Source

Event Modifiers for JSX

This babel plugin adds some syntactic sugar to JSX.

Usage:

npm i babel-plugin-jsx-event-modifiers -D

or

yarn add babel-plugin-jsx-event-modifiers -D

Then add jsx-event-modifiers to your .babelrc file under plugins

example .babelrc (for vue):

{
  "presets": ["es2015"],
  "plugins": ["jsx-event-modifiers", "transform-vue-jsx"]
}

example .babelrc (for react):

{
  "presets": [
    "es2015",
    "react"
  ],
  "plugins": ["jsx-event-modifiers"]
}
Event Modifiers

Example:

export default {
  render () {
    return (
      <input
        onKeyUp:up={this.methodForPressingUp}
        onKeyUp:down={this.methodForPressingDown}
      />
    )
  }
}

will be transpiled into:

export default {
  render () {
    return (
      <input
        onKeyUp={event => {
          if (event.charCode === 38)
            this.methodForPressingUp(event);

          if (event.charCode === 40)
            this.methodForPressingDown(event);
        }} />
    );
  }
}
API:
ModifierDescription
:stopexecutes event.stopPropagation()
:preventexecutes event.preventDefault()
:k{keyCode}checks for the keyCode
:{keyAlias}checks for the keyCode that is assigned to this keyAlias
Stop

event.stopPropagation() is called before the expression

Example:

export default {
  render () {
    return (
      <div>
        <a href="/" onClick:stop />
        <a href="/" onClick:stop={this.method} />
      </div>
    )
  }
}

is transpiled to:

export default {
  render () {
    return (
      <div>
        <a href="/" onClick={event => {
          event.stopPropagation();
        }} />
        <a href="/" onClick={event => {
          event.stopPropagation();
          this.method(event);
        }} />
      </div>
    );
  }
}
Prevent

event.preventDefault() is called before the expression

Example:

export default {
  render () {
    return (
      <div>
        <a href="/" onClick:prevent />
        <a href="/" onClick:prevent={this.method} />
      </div>
    )
  }
}

is transpiled to:

export default {
  render () {
    return (
      <div>
        <a href="/" onClick={event => {
          event.preventDefault();
        }} />
        <a href="/" onClick={event => {
          event.preventDefault();
          this.method(event);
        }} />
      </div>
    );
  }
}
KeyCode

event.charCode is compared to the keyCode

Example:

export default {
  render () {
    return <input onKeyUp:k13={this.method} />
  }
}

is transpiled to:

export default {
  render () {
    return (
      <input onKeyUp={event => {
        if (event.charCode === 13)
          this.method(event);
      }} />
    );
  }
}
KeyAlias

There is a predefined list of aliases for keycodes:

const aliases = {
  esc: 27,
  tab: 9,
  enter: 13,
  space: 32,
  up: 38,
  left: 37,
  right: 39,
  down: 40,
  'delete': [8, 46]
}

Example:

export default {
  render () {
    return <input onKeyUp:enter={this.method} />
  }
}

is transpiled to:

export default {
  render () {
    return (
      <input onKeyUp={event => {
        if (event.charCode === 13)
          this.method(event);
      }} />
    );
  }
}
You can combine them:

Example:

export default {
  render () {
    return <input
      onKeyUp:enter={this.method}
      onKeyUp:k60={this.otherMethod} />
  }
}

is transpiled to:

export default {
  render () {
    return (
      <input
        onKeyUp={event => {
          if (event.charCode === 13)
            this.method(event);

          if (event.charCode === 60)
            this.otherMethod(event);
        }} />
    );
  }
}

FAQs

Package last updated on 02 May 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc