WAI-ARIA compliant React command palette like the one in Atom and Sublime

WAI-ARIA compliant React command palette like the one in Atom and Sublime

An accessible browser compatible javascript command palette.

React Command Palette

WAI-ARIA compliant React command palette like the one in Atom and Sublime

Codefresh build status Codeship Status for asabaylus/react-command-palette codecov Maintainability Test Coverage

Screenshot

Live Playground

For examples of the command palette in action, go to the

Storybook

OR

To run that demo on your own computer:

  • Clone this repository
  • npm install
  • npm run storybook
  • Visit http://localhost:6006/

Usage

Install it in your project

$ npm i --save react-command-palette

Import into your react app and pass commands

import CommandPalette from 'react-command-palette';

const commands = [{
    name: "Foo",
    command() {}
  },{
    name: "Bar",
    command() {}
  }
  ... 
 ];
 
 ReactDOM.render(
  <CommandPalette commands={commands} />, 
  document.getElementById('app'))

Props

  • open a boolean, when set to true it forces the command palette to be displayed. Defaults to "false".

  • closeOnSelect a boolean, when set to true the command palette will close immediateley when the user makes a selection. Defaults to "false".

  • hotKeys a string that contains a keyboard shortcut for opening/closing the palette. Defaults to "cmd+shift+p". Uses mousetrap key combos

  • options options controls how fuzzy search is configured. Note: use at your own risk, this is likley to change in the future. The search options are derived from these fuzzysort options. However the command palette options prop must have the following values included to function correctly:

  key: "name", // must be "name"
  keys: ["name"], // must include "name"

  // other options may be freely configured
  threshold: -Infinity, 
  limit: 7,
  allowTypo: true, 
  scoreFn: null 
  • commands appears in the command palette. For each command in the array the object must have a name and a command. The name is a user friendly string that will be display to the user. The command is a function that will be executed when the user clicks or presses the enter key. Commands may also include custom properties where "this" will be bound to the command, for example:
  {
    id: 1,
    color: 'pink',
    name: "Foo",
    command() {
      document.location.href = `somepage.html?id=${this.id}&color=${this.color}`;
    }
  },
  ...
  • maxDisplayed a number between 1 and 500 that determines the maxium number of commands that will be rendered on screen. Defaults to 7

  • spinner a string or a React.ComponentType that is displayed when the user selects an item. If a custom spinner is not set then the default spinner will be used. If a custom component or string is provided then it will automatically be wrapped inside a div with a role="status" attribute. If a component is provided then it will be be wrapped in a div that also contains a sibling node with a div contain "Loading..." visible only to screen readers.

  • trigger a string or a React.ComponentType the opens the command palette when clicked. If a custom trigger is not set then by default a button will be used. If a custom component or string is provided then it will automatically be wrapped inside an accessible div that will allow it be keyboard accessible, clickable and focusable for assistive technologies.

    Example with a component:

    // jsx trigger prop
    <CommandPalette commands={data} trigger={<b>Click Me!</b>}>
    
    // html generated trigger
    <div role="button" tabindex="0"><b>Click Me!</b></div>
    

    Example with a string:

    // jsx trigger prop
    <CommandPalette commands={data} trigger="Click Me!">
    
    // html generated trigger
    <div role="button" tabindex="0">Click Me!</div>
    

    When the trigger is clicked it will open the command palette, no custom handlers or events are required.

Developer Setup

# install dependencies
$ npm install

# run lint
$ npm run lint

# beautify code
$ npm run prettier

# visual regression tests
$ npm run chromatic

# run unit tests
$ npm test

# start the dev environment
$ npm start

# update the docs
$ npm run docs

Building with Docker

Build and tag the Docker image:

$ docker build -t  react-command-palette .

Then, spin up the container once the build is done:

$ docker run -it \
  -v ${PWD}:/usr/src/app \
  -v /usr/src/app/node_modules \
  -p 6006:6006 \
  npm i && npm run dev

You only need to run "npm i" the when the container is first created. The devDependencies need to be installed to compile and test the build during development. On subsequent builds run:

$ docker run -it \
  -v ${PWD}:/usr/src/app \
  -v /usr/src/app/node_modules \
  -p 6006:6006 \
  npm run dev

Open your browser to http://localhost:6006/ and you should see the app. Try making a change to the command-palette component within your code editor. You should see the app hot-reload. Kill the server once done.

Package for production with Docker:

CodeFresh.io will autmatically run this build to prepare the package for publication to npm whenever a pull request is merged to master.

Sponsors

Visual Regression Tests by ChromaticQA

Github Repository