npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

hotkey-react

v1.0.0

Published

基于 react 实现可随意组合快捷键

Downloads

1

Readme

在 React 项目中优雅使用快捷键

提供了类组件模式和函数式组件模式用法 本插件可以解决,在 react 项目中,对于基础组件,业务组件,form 表单等,对快捷键有负责特殊需求的一个解决方案,它能很简便的应用到你 react 项目之中,同时为应对更加复杂的交互场景,提供出 快捷键控制器,可以在任何地方获取到并且可以将快捷键权限交付到任何一个想生效的位置

核心概念

scope 快捷键作用域,可以控制一批快捷键是否生效,全局的 scope 是 all hotkeyManager 快捷键管理器

下面是实例代码

// 根节点
import {
  createHotkeyScope,
  useHotkeysManager,
  HotkeysProvider,
  HotkeysTarget,
  withHotKeys
} from 'react-hotkey';

import { HotkeyFunDemo } from './HotkeyFunDemo';
import { HotkeyClassDemo } from './HotkeyClassDemo';
export const HotkeyDemo = () => {
  return (
    <div>
      <HotkeysProvider>
        <HotkeyFunDemo></HotkeyFunDemo>
        <HotkeyClassDemo></HotkeyClassDemo>
      </HotkeysProvider>
    </div>
  );
};
// 类组件
import {
  createHotkeyScope,
  useHotkeysManager,
  HotkeysProvider,
  HotkeysTarget,
  withHotKeys
} from 'react-hotkey';

import type { HotkeyConfig } from 'react-hotkey';
export class HotkeyClassDemoContainer extends React.PureComponent {
  scopeName: string = createHotkeyScope('HotkeyClassDemo');
  hotkeyConfig: HotkeyConfig[] = [
    {
      keyName: 'a',
      onKeyDown: () => {
        console.log('on key down');
      }
    }
  ];
  render() {
    const hotkeyManage = (this.props as any).hotkeyManager as HotkeyManager;
    console.log('hotkeyManage', hotkeyManage.currentScope);
    return (
      <HotkeysTarget hotkeys={this.hotkeyConfig} scopeName={this.scopeName}>
        <div style={{ width: 200, height: 200, border: '1px solid' }}>
          点击我触发 类组件的快捷键
        </div>
      </HotkeysTarget>
    );
  }
}

export const HotkeyClassDemo = withHotKeys(HotkeyClassDemoContainer);
// 函数式组件
import {
  createHotkeyScope,
  useHotkeysManager,
  HotkeysProvider,
  HotkeysTarget,
  withHotKeys,
  useHotkeysManager
} from 'react-hotkey';

import React, { memo, useMemo } from "react";

export const HotkeyFunDemo = memo(() => {
  const scopeName = useMemo(() => {
    return createHotkeyScope();
  }, []);
  const hotkeyConfig = useMemo(() => {
    return [
      {
        keyName: 'a',
        onKeyDown: () => {
          console.log('on key down');
        }
      }
    ] as HotkeyConfig[];
  }, []);
  
  const { hotkeyManager } = useHotkeysManager();
  console.log('hotkeyManager', hotkeyManager.currentScope);
  return (
    <HotkeysTarget hotkeys={hotkeyConfig} scopeName={scopeName}>
      <div style={{ width: 200, height: 200, border: '1px solid' }}>
        点击我触发 函数组件的快捷键
      </div>
    </HotkeysTarget>
  );
});

使用者需要关注如下几个对象

  1. HotkeysProvider 需要再最外层包裹上,不需要传参数

  2. HotkeysTarget 在需要快捷键的组件上包裹上,参数如下

    1. hotkeys?: HotkeyConfig[]; 快捷键相关配置
    2. scopeName?: string; 当前组件的作用域, 如果是 'all' 则快捷键一直生效
    3. autoActive?: boolean; 组件加载时自动生效当前的快捷组键
    4. className?: string; 快捷键这层 class
    5. style?: React.CSSProperties; 快捷键这层 style
    6. enableHotkey?: boolean; 是否开启快捷键
    7. containerRef?: React.MutableRefObject; containerRef 指定 容器生效位置
  3. HotkeyConfig 配置

     interface HotkeyConfig{
       keyName: string;
       onKeyDown: (keyName: string, scopeName: string, event: KeyboardEvent, handler: HotkeysEvent) => void;
     }

    keyName 可以参考如下配置,可以使用单键,或者组合键

    let CTRL_KEY = isMacOS() ? 'command' : 'control';
    
    export const HOTKEY_MAPPING = {
     left: 'left',
    right: 'right',
       up: 'up',
     down: 'down',
      esc: 'esc',
    space: 'space',
    enter: 'enter',
      tab: 'tab',
    selectCell: 'up,down,left,right',
    undo: `${CTRL_KEY}+z`,
    redo: `${CTRL_KEY}+shift+z`,
    } as const;
    
  4. hotkeyManager 快捷键控制器

    当遇到场景比较复杂的快捷键时,只用组件就无法精确控制快捷键的焦点在哪里生效