
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
babel-plugin-jsx-event-modifiers
Advanced tools
This babel plugin adds some syntactic sugar to JSX.
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"]
}
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);
}} />
);
}
}
Modifier | Description |
---|---|
:stop | executes event.stopPropagation() |
:prevent | executes event.preventDefault() |
:k{keyCode} | checks for the keyCode |
:{keyAlias} | checks for the keyCode that is assigned to this keyAlias |
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>
);
}
}
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>
);
}
}
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);
}} />
);
}
}
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);
}} />
);
}
}
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
JSX Syntactic Sugar Plugin for Event Modifiers
The npm package babel-plugin-jsx-event-modifiers receives a total of 59,793 weekly downloads. As such, babel-plugin-jsx-event-modifiers popularity was classified as popular.
We found that babel-plugin-jsx-event-modifiers demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.