Bootstrap Command Palette — Free HTML CSS JS Snippet

Bootstrap Command Palette · Navigation · Plain HTML, CSS & JS · Live preview

What's included

Features

A genuine Ctrl+K / Cmd+K global keyboard shortcut, not just a clickable button
activeIndex is re-clamped on every keystroke so it can never point past a shortened filtered list
Full arrow-key navigation (Up/Down) plus Enter-to-run, with mouse click running the identical function
Opening always resets the query and highlight, so no stale search state leaks between sessions
Input focus is wired through Bootstrap's real shown.bs.modal event, after the open transition finishes

About this UI Snippet

Bootstrap Command Palette — HTML, CSS & JavaScript

Screenshot of the Bootstrap Command Palette snippet rendered live

Filtering and keyboard navigation share one activeIndex and one visible array, both recomputed together inside render() — every keystroke re-filters COMMANDS into visible, then clamps activeIndex with Math.min(activeIndex, visible.length - 1) so a highlighted row can never point past the end of a list that just got shorter mid-search. Skipping that clamp is the single most common bug in a homemade command palette: filter down to two results while the fourth item was highlighted, and the highlight silently vanishes or throws trying to read an item that no longer exists.

The palette opens through two different triggers — clicking the button, or a global keydown listener checking (e.ctrlKey || e.metaKey) && e.key === 'k' — and both funnel through the same open() function, which resets the query and activeIndex before showing the modal, so a stale search from a previous session never lingers into the next one. Bootstrap's own shown.bs.modal event is what focuses the input, deliberately after the modal's own opening transition completes rather than immediately on open, since focusing an element still mid-transition can be unreliable across browsers.

Build with AI

Build, Understand, Optimize, and Extend It With AI

Hand this snippet to an AI coding assistant like Claude and ask it to group commands into labeled sections (Navigation, Actions, Recent) the way many real command palettes do, or to add fuzzy matching so a query like "godb" still matches "Go to Dashboard".

Prompt to recreate it

Copy this into your AI assistant of choice to build the effect from scratch, or as a jumping-off point for your own variant:

text
Build a Bootstrap 5.3 command palette (Ctrl+K style), using the real Bootstrap CDN framework (bootstrap.min.css and bootstrap.bundle.min.js), not custom CSS made to resemble it.

Requirements:
- A global keydown listener that opens a real Bootstrap modal when Ctrl+K or Cmd+K is pressed, in addition to a visible trigger button.
- Inside the modal, a search input filters a list of at least 6 sample commands live on every keystroke (case-insensitive substring match on each command's label).
- Support ArrowUp/ArrowDown to move a highlighted active index through the currently filtered list, and Enter to run the highlighted command. The active index must be re-clamped after every filter so it can never point past the end of a shortened list.
- Clicking a command in the list runs it the same way pressing Enter on it would.
- Reset the query and active index every time the palette opens, and focus the input once the modal's own open transition has finished (via Bootstrap's shown.bs.modal event).

Want to tighten it up first? Run this prompt through the AI Prompt Studio to score it across 8 quality dimensions, catch anti-patterns, and tune the wording for Claude, ChatGPT, or Gemini before you paste it in.

Source Code

<div class="container py-5 d-flex justify-content-center">
  <div class="card bscmd-card">
    <div class="card-body p-4 text-center">
      <p class="small text-muted mb-2">Press Ctrl+K (or Cmd+K on Mac), or click below.</p>
      <button type="button" class="btn btn-outline-secondary" id="bscmdOpen">
        Search commands <kbd class="ms-1">Ctrl</kbd><kbd>K</kbd>
      </button>
      <p class="small mt-3 mb-0" id="bscmdStatus">&nbsp;</p>
    </div>
  </div>
</div>

<div class="modal fade" id="bscmdModal" tabindex="-1" aria-hidden="true">
  <div class="modal-dialog modal-dialog-centered">
    <div class="modal-content bscmd-modal-content">
      <div class="p-2 border-bottom">
        <input type="text" class="form-control border-0 bscmd-input" id="bscmdInput" placeholder="Type a command...">
      </div>
      <ul class="list-unstyled mb-0 bscmd-list" id="bscmdList"></ul>
    </div>
  </div>
</div>

Step by step

How to Use

  1. 1
    Press Ctrl+K (or Cmd+K on Mac)The palette opens as a real Bootstrap modal with the input already focused.
  2. 2
    Type part of a command, like "dash"The list filters live to just "Go to Dashboard".
  3. 3
    Press the down arrow a few timesThe highlighted row moves down the filtered list and never overshoots the last item.
  4. 4
    Press EnterThe highlighted command runs, the palette closes, and a status message confirms which one.
  5. 5
    Reopen and click a command directly insteadClicking any row runs it exactly the same way pressing Enter on it would.

Real-world uses

Common Use Cases

DEV
Developer tools and internal admin panels
The classic "power user" pattern — pairs with bootstrap-keyboard-shortcut-help-modal to document every available shortcut in one place.
SaaS dashboards with many destinations and actions
A faster way to reach a deep settings page or trigger an action than navigating through several menus.
Learning keyboard-first UI patterns
A complete, realistic example of the filter-plus-clamped-index technique every list-style keyboard navigator needs.

Got questions?

Frequently Asked Questions

Resetting to 0 on every keystroke would keep yanking the highlight back to the top while a user is trying to arrow down toward a specific item — clamping only intervenes when the current index has actually become invalid, preserving the user's position otherwise.

Ctrl+K is free in most browsers on most platforms (Firefox uses it for the search bar in older versions, which is the one real exception), which is why command palettes conventionally offer Cmd+K as the equivalent on Mac — e.preventDefault() stops the default browser behavior once the modal opens.

Yes. Track query and activeIndex in component state, derive the filtered visible list in the render function, and attach the global keydown listener in a mount lifecycle hook (useEffect, onMounted) with cleanup on unmount.

Replace the status message inside runCommand() with a real router push, a function call, or a dispatched action — everything else (filtering, keyboard nav, modal lifecycle) is unrelated to what a command actually does when it runs.