GithubHelp home page GithubHelp logo

qmhc / event-modifier Goto Github PK

View Code? Open in Web Editor NEW
2.0 2.0 0.0 75 KB

A lib for quickly add event modifiers.

License: MIT License

JavaScript 22.81% Shell 2.75% TypeScript 74.44%
component html javascript react toolkit typescript vue

event-modifier's Introduction

Event Modifier

Help you quickly modify event via chain apis

npm version

Usage

Modifiers:

// Also a shortcut alias 'em'
import { eventModifier } from 'event-modifier'

function listener() {
  // do something
}

eventModifier()
  .stop()
  .prevent()
  .keys('Enter', 'Space')
  .apply(listener)

Create custom guards:

import { em, createGuard } from 'event-modifier'

createGuard('stopOthers', e => e.stopImmediatePropagation())

// for inferring type
declare module 'event-modifier' {
  interface CustomModifier {
    stopOthers: () => this
  }
}

// use
em().stopOthers()

Inspiration

<template>
  <div
    tabindex="0"
    @click="handle"
    @keydown.tab="handle"
    @keydown.enter.stop="handle"
    @keydown.space.stop.prevent="handle"
  ></div>
</template>

<script setup>
function handle() {
  // do something
}
</script>

Pretty good.

function handle() {
  // do something
}

function keyHandle(event: KeyboardEvent) {
  const key = event.code || event.key

  switch (key) {
    case 'Tab': return handle()
    case 'Enter':
      event.stopPropagation()
      return handle()
    case 'Space':
      event.stopPropagation()
      event.preventDefault()
      return handle()
  }
}

function render() {
  return (
    <div tabindex={'0'} onClick={handle} onKeydown={keyHandle}></div>
  )
}

Lots of template code, Sad.

Route

Maybe decorator?

License

All in MIT license.

event-modifier's People

Contributors

qmhc avatar

Stargazers

 avatar  avatar

Watchers

 avatar  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.