Giter Club home page Giter Club logo

effector-hotkey's Introduction

Effector-hotkey

Hotkeys with Effector made easy

  • Easy-to-use, no need to implement by yourself
  • Supports both Windows/MacOS style hotkeys
  • Doesn't break if using with SSR

Installation

npm i effector-hotkey

Usage

import { hotkey } from 'effector-hotkey';

const copyPressed = hotkey({ key: 'Ctrl+C' });

sample({
  clock: copyPressed,
  source: $formData,
  target: saveFx,
});

Customization

Specifying event type

import { hotkey } from 'effector-hotkey';

const spaceDown = hotkey({ key: 'Space', type: 'keydown' });
const spaceUp = hotkey({ key: 'Space', type: 'keyup' });
const spacePress = hotkey({ key: 'Space', type: 'keypress' });

Shortcut

import { hotkey } from 'effector-hotkey';

const copyPressed = hotkey('Ctrl+C');
const spaceDown = hotkey('Space', 'keydown');

filter prop

import { hotkey } from 'effector-hotkey';
import { createStore } from 'effector';

const $isConfirmModalOpened = createStore(true);

hotkey({
  key: 'Y',
  filter: $isConfirmModalOpened,
  target: removeFx,
});

hotkey({
  key: 'N',
  filter: $isConfirmModalOpened,
  target: closeModal,
});

target prop

If you want to just trigger something instead of listening to event, you can use target prop:

import { sample } from 'effector';
import { hotkey } from 'effector-hotkey';

hotkey({
  key: 'Ctrl+C',
  target: copyTextFx,
});
// <=>
sample({
  clock: hotkey('Ctrl+C'),
  target: copyTextFx,
});

Extra

keyup, keydown, keypress events

You can use internal wrappers for native events as well

import { keyup, keydown, keypress } from 'effector-hotkey';

keyup.watch(console.log); // KeyboardEvent

$isShiftDown, $isCtrlDown, $isAltDown

You can also use pre-made stores to track if Shift/Ctrl/Alt buttons are held

Simple use-case: display hotkeys in UI while holding Ctrl

import { useStore } from 'effector-react';
import { hotkey, $isCtrlDown } from 'effector-hotkey';

const SubmitButton = () => {
  const isCtrlDown = useStore($isCtrlDown);

  return (
    <Button onClick={savePressed}>{isCtrlDown ? 'Ctrl+S' : 'Save'}</Button>
  );
};

const savePressed = createEvent<MouseEvent>();

sample({
  clock: [savePressed, hotkey('Ctrl+S')],
  target: saveFx,
});

effector-hotkey's People

Contributors

drevoed avatar kelin2025 avatar

Watchers

 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.