2.2.8 • Published 21 days ago

@saas-ui/hotkeys v2.2.8

Weekly downloads
-
License
MIT
Repository
github
Last release
21 days ago

@saas-ui/hotkeys

Easy manageable keyboard shortcuts for Chakra UI

Todo

This package is still in development and might not work 100% as expected. If you find any issues, please open an issues here on Github.

  • Add tests
  • Test compatibility with all Chakra UI components, like Menu's

How it works

Create a config object containing all the keyboard shortcuts in your app. The keys in the config can be used as shortcuts with the included hook throughout your app. Comes with an HotkeysList component to list all available key combinations for your users.

Other features

  • Supports shifted keys like ?, =, @.
  • ⌥ ⇧ ⌃ ⌘ shorthands are supported.
  • Won't trigger inside inputs / content editable elements.
  • Hooks also work without a global config object.
  • The HotkeysList can also be used to list other options, like markdown support.

Installation

$ yarn add @saas-ui/hotkeys

#or

$ npm i @saas-ui/hotkeys --save

Usage

1. Setup your hotkeys config (optional)

// app.tsx
import { HotkeysProvider, HotkeysListOptions } from '@saas-ui/hotkeys'

const hotkeys: HotkeysListOptions = {
  general: {
    title: 'General',
    hotkeys: {
      help: { label: 'Help', command: '?' },
      search: { label: 'Search', command: '⌘ K' },
    },
  },
  markdown: {
    title: 'Markdown',
    hotkeys: {
      bold: { label: 'Bold', command: '**text**' },
    },
  },
}

export default const App = () => {
  return (
    ...
      <HotkeysProvider hotkeys={hotkeys}>
        {children}
      </HotkeysProvider>
    ...
  )
}

2. Setup your hooks

import {
  useHotkeysShortcut,
  useHotkeysContext,
  HotkeysLiist,
  HostkeysSearch,
  HotkeysListItems,
} from '@saas-ui/hotkeys'

export const MyComponent = () => {
  const help = useHotkeysShortcut('general.help', () => {
    onOpen()
  })

  useHotkeysShortcut('general.search', () => {
    searchRef.current?.focus()
  })

  return <>Press {help} for help.</>
}

3. Using the HotkeysList component

import {
  useHotkeysShortcut,
  useHotkeysContext,
  HotkeysLiist,
  HostkeysSearch,
  HotkeysListItems,
} from '@saas-ui/hotkeys'

export const HotkeysListModal = () => {
  const searchRef = useRef<HTMLInputElement | null>(null)
  const { isOpen, onOpen, onClose } = useDisclosure()

  const help = useHotkeysShortcut('general.help', () => {
    onOpen()
  })

  const { hotkeys } = useHotkeysContext()

  return (
    <Box>
      <Text>
        Press <strong>{help}</strong> for help
      </Text>

      <Modal isOpen={isOpen} onClose={onClose} initialFocusRef={searchRef}>
        <ModalOverlay />
        <ModalContent>
          <ModalCloseButton />
          <ModalHeader>Keyboard shortcuts</ModalHeader>

          <ModalBody>
            <HotkeysList hotkeys={hotkeys}>
              <HotkeysSearch ref={searchRef} />
              <HotkeysListItems />
            </HotkeysList>
          </ModalBody>
        </ModalContent>
      </Modal>
    </Box>
  )
}

4 Standalone usage

If you're not using Chakra UI or don't want to use the provider, you can use the useHotkeys hook standalone.

import { useHotkeys } from '@saas-ui/hotkeys'

const Component = () => {
  useHotkeys('/', () => {
    // do something
  })

  return null
}

License

MIT - Appulse Software

2.2.8

21 days ago

2.2.7

27 days ago

2.2.5

1 month ago

2.2.6

1 month ago

2.2.4

2 months ago

2.2.3

3 months ago

2.2.2

3 months ago

2.2.1

3 months ago

2.2.0

3 months ago

2.1.15

4 months ago

2.1.14

4 months ago

2.1.13

4 months ago

2.1.12

5 months ago

2.1.11

5 months ago

2.1.10

5 months ago

2.0.0-rc.30

11 months ago

2.0.3

9 months ago

2.0.2

10 months ago

2.0.1

10 months ago

2.0.0

10 months ago

2.1.9

5 months ago

2.1.2

8 months ago

2.1.1

8 months ago

2.1.4

7 months ago

2.1.3

7 months ago

2.1.6

6 months ago

2.1.5

7 months ago

2.1.8

6 months ago

2.1.7

6 months ago

2.1.0

9 months ago

2.0.0-next.15

12 months ago

2.0.0-next.16

12 months ago

2.0.0-next.17

12 months ago

2.0.0-next.18

12 months ago

2.0.0-next.19

12 months ago

2.0.0-next.11

1 year ago

1.4.2

1 year ago

2.0.0-next.12

12 months ago

1.4.1

1 year ago

2.0.0-next.13

12 months ago

2.0.0-next.14

12 months ago

2.0.0-rc.28

11 months ago

2.0.0-rc.29

11 months ago

2.0.0-rc.26

11 months ago

2.0.0-rc.27

11 months ago

2.0.0-rc.24

12 months ago

2.0.0-rc.25

11 months ago

2.0.0-rc.22

12 months ago

2.0.0-rc.23

12 months ago

2.0.0-rc.20

12 months ago

2.0.0-rc.21

12 months ago

2.0.0-next.8

1 year ago

2.0.0-next.9

1 year ago

2.0.0-next.6

1 year ago

2.0.0-next.7

1 year ago

2.0.0-next.5

1 year ago

2.0.0-next.10

1 year ago

2.0.0-next.2

1 year ago

2.0.0-next.3

1 year ago

2.0.0-next.0

1 year ago

2.0.0-next.1

1 year ago

1.4.0

1 year ago

1.2.0

2 years ago

1.3.0

1 year ago

1.1.2

2 years ago

1.1.1

2 years ago

1.1.0

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago

1.0.0-rc.5

2 years ago

1.0.0-rc.3

2 years ago

1.0.0-rc.4

2 years ago

0.7.0-next.1

2 years ago

0.7.0-next.2

2 years ago

0.7.0-next.3

2 years ago

0.7.0-next.4

2 years ago

0.7.0-next.0

2 years ago

0.6.3

2 years ago

0.6.2

2 years ago

1.0.0-rc.1

2 years ago

1.0.0-rc.2

2 years ago

1.0.0-rc.0

2 years ago

0.6.1

2 years ago

0.6.0

2 years ago

0.5.0-next.0

2 years ago

0.5.0

2 years ago

0.3.0

2 years ago

0.2.1

2 years ago

0.2.0

2 years ago

0.2.6

2 years ago

0.2.3

2 years ago

0.4.0

2 years ago

0.3.1

2 years ago

0.2.2

2 years ago

0.2.5

2 years ago

0.2.4

2 years ago

0.1.0

2 years ago