• Stars
    star
    4,746
  • Rank 8,906 (Top 0.2 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created about 3 years ago
  • Updated 9 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

fast, portable, and extensible cmd+k interface for your site

kbar

kbar is a simple plug-n-play React component to add a fast, portable, and extensible command + k (command palette) interface to your site.

demo

Background

Command + k interfaces are used to create a web experience where any type of action users would be able to do via clicking can be done through a command menu.

With macOS's Spotlight and Linear's command + k experience in mind, kbar aims to be a simple abstraction to add a fast and extensible command + k menu to your site.

Features

  • Built in animations and fully customizable components
  • Keyboard navigation support; e.g. control + n or control + p for the navigation wizards
  • Keyboard shortcuts support for registering keystrokes to specific actions; e.g. hit t for Twitter, hit ? to immediate bring up documentation search
  • Nested actions enable creation of rich navigation experiences; e.g. hit backspace to navigate to the previous action
  • Performance as a first class priority; tens of thousands of actions? No problem.
  • History management; easily add undo and redo to each action
  • Built in screen reader support
  • A simple data structure which enables anyone to easily build their own custom components

Usage

Have a fully functioning command menu for your site in minutes. First, install kbar.

npm install kbar

There is a single provider which you will wrap your app around; you do not have to wrap your entire app; however, there are no performance implications by doing so.

// app.tsx
import { KBarProvider } from "kbar";

function MyApp() {
  return (
    <KBarProvider>
      // ...
    </KBarProvider>
  );
}

Let's add a few default actions. Actions are the core of kbar – an action define what to execute when a user selects it.

  const actions = [
    {
      id: "blog",
      name: "Blog",
      shortcut: ["b"],
      keywords: "writing words",
      perform: () => (window.location.pathname = "blog"),
    },
    {
      id: "contact",
      name: "Contact",
      shortcut: ["c"],
      keywords: "email",
      perform: () => (window.location.pathname = "contact"),
    },
  ]

  return (
    <KBarProvider actions={actions}>
      // ...
    </KBarProvider>
  );
}

Next, we will pull in the provided UI components from kbar:

// app.tsx
import {
  KBarProvider,
  KBarPortal,
  KBarPositioner,
  KBarAnimator,
  KBarSearch,
  useMatches,
  NO_GROUP
} from "kbar";

// ...
  return (
    <KBarProvider actions={actions}>
      <KBarPortal> // Renders the content outside the root node
        <KBarPositioner> // Centers the content
          <KBarAnimator> // Handles the show/hide and height animations
            <KBarSearch /> // Search input
          </KBarAnimator>
        </KBarPositioner>
      </KBarPortal>
      <MyApp />
    </KBarProvider>;
  );
}

At this point hitting cmd+k (macOS) or ctrl+k (Linux/Windows) will animate in a search input and nothing more.

kbar provides a few utilities to render a performant list of search results.

  • useMatches at its core returns a flattened list of results and group name based on the current search query; i.e. ["Section name", Action, Action, "Another section name", Action, Action]
  • KBarResults renders a performant virtualized list of these results

Combine the two utilities to create a powerful search interface:

import {
  // ...
  KBarResults,
  useMatches,
  NO_GROUP,
} from "kbar";

// ...
// <KBarAnimator>
//   <KBarSearch />
<RenderResults />;
// ...

function RenderResults() {
  const { results } = useMatches();

  return (
    <KBarResults
      items={results}
      onRender={({ item, active }) =>
        typeof item === "string" ? (
          <div>{item}</div>
        ) : (
          <div
            style={{
              background: active ? "#eee" : "transparent",
            }}
          >
            {item.name}
          </div>
        )
      }
    />
  );
}

Hit cmd+k (macOS) or ctrl+k (Linux/Windows) and you should see a primitive command menu. kbar allows you to have full control over all aspects of your command menu – refer to the docs to get an understanding of further capabilities. Looking forward to see what you build.

Used by

Listed are some of the various usages of kbar in the wild – check them out! Create a PR to add your site below.

Contributing to kbar

Contributions are welcome!

New features

Please open a new issue so we can discuss prior to moving forward.

Bug fixes

Please open a new Pull Request for the given bug fix.

Nits and spelling mistakes

Please open a new issue for things like spelling mistakes and README tweaks – we will group the issues together and tackle them as a group. Please do not create a PR for it!

More Repositories

1

postel

tiny react library for building tooltips, flyovers, menus and more
TypeScript
80
star
2

clips

next.js site for storing and sharing interesting finds from the Internet paired with a macOS menubar app.
TypeScript
48
star
3

time-capsule

Write a letter to your future self - receive it in one year.
JavaScript
48
star
4

easel

a little canvas for any idea.
38
star
5

react-fullscreen-image

An accessible, fast, and snappy fullscreen React image viewer.
TypeScript
34
star
6

react-menubar

⚫️ Simple React, Typescript & Electron macOS menubar project setup
TypeScript
30
star
7

react-drag-elements

A small & efficient React Hook that allows users to drag items around and update lists of elements.
TypeScript
30
star
8

use-google-autocomplete

A simple React Hook API that returns Google Autocomplete results with session_token handling.
TypeScript
27
star
9

react-todo

A super accessible and easy to use todo list.
JavaScript
14
star
10

svg-to-coordinates

Return a JSON object of x, y coordinates from a single layered svg
JavaScript
12
star
11

tcc.im

Vanilla personal site 🍦
JavaScript
12
star
12

react-animated-menu

A React component that handles all logic for managing visual state for menus and lists.
TypeScript
12
star
13

tim

personal site.
TypeScript
8
star
14

blog

Personal blog using Gatsby.js
TypeScript
5
star
15

timcchang.com

Personal website & blog powered by Gatsby.js
TypeScript
5
star
16

checklist-chrome-extension

Minimal and interactive checklist living in your Chrome new tab.
JavaScript
5
star
17

tcc-config

Vim Script
4
star
18

scroll-animation-demo

Handle animations that depend on user interactions, specific element positions, & scrolling
JavaScript
4
star
19

globe

three.js fun
JavaScript
3
star
20

scratch-react

a react boilerplate that supports everything from hot reloading, file minifying, and styling
JavaScript
3
star
21

hot-vanilla-template

A simple, reusable hot reloading HTML/CSS/Javascript, project setup.
HTML
3
star
22

layouts

Recreating inspiring layouts and user interfaces
TypeScript
3
star
23

with-route-as-modal

JavaScript
2
star
24

custom-react-dropdown-demo

Custom date selector on desktop — defaults to native UI on mobile
TypeScript
1
star
25

gatsby-typescript-boilerplate

A Gatsby & Typescript boilerplate
TypeScript
1
star
26

timchang.now.sh

now.sh redirect for timcchang.com
1
star