Migrate empty and item docs to Basecoat APIs

This commit is contained in:
Ronan Berder
2026-06-03 18:59:21 +08:00
parent 317c490e0c
commit 7e7d509e90
164 changed files with 15779 additions and 2940 deletions
+156 -109
View File
@@ -1,8 +1,10 @@
---
layout: layouts/page.njk
title: Command
description: Fast, composable command menu for quick navigation and actions.
description: Command menu for search and quick actions.
toc:
- label: About
id: about
- label: Usage
id: usage
children:
@@ -22,61 +24,76 @@ toc:
- label: Examples
id: examples
children:
- label: Dialog
id: example-dialog
- label: Basic
id: example-basic
- label: Shortcuts
id: example-shortcuts
- label: Groups
id: example-groups
- label: Scrollable
id: example-scrollable
- label: RTL
id: example-rtl
- label: API Reference
id: api-reference
---
{% from "macros/code_preview.njk" import code_preview %}
{% from "macros/code_block.njk" import code_block %}
{% from "command.njk" import command, command_dialog %}
{% macro command_items(suggestions_id="suggestions", settings_id="settings") %}
{% macro command_items(suggestions_id="suggestions", settings_id="settings", shortcuts=false) %}
<div role="group" aria-labelledby="{{ suggestions_id }}">
<span role="heading" id="{{ suggestions_id }}">Suggestions</span>
<div role="menuitem" onclick="console.log('Open calendar')">
<div role="menuitem" data-filter="Calendar" data-keywords="date event schedule">
{% lucide "calendar" %}
<span>Calendar</span>
{% if shortcuts %}<span data-shortcut>⌘K</span>{% endif %}
</div>
<div role="menuitem" onclick="console.log('Search emoji')">
<div role="menuitem" data-filter="Search Emoji" data-keywords="emoji smile reaction">
{% lucide "smile" %}
<span>Search Emoji</span>
{% if shortcuts %}<span data-shortcut>⌘E</span>{% endif %}
</div>
<div role="menuitem" aria-disabled="true">
{% lucide "calculator" %}
<span>Calculator</span>
</div>
</div>
<hr role="separator" />
<hr role="separator">
<div role="group" aria-labelledby="{{ settings_id }}">
<span role="heading" id="{{ settings_id }}">Settings</span>
<div role="menuitem" onclick="console.log('Open profile')" data-filter="Profile">
<div role="menuitem" data-filter="Profile" data-keywords="user account">
{% lucide "user" %}
<span>Profile</span>
<kbd class="ml-auto text-muted-foreground bg-transparent tracking-widest">⌘P</kbd>
{% if shortcuts %}<span data-shortcut>⌘P</span>{% endif %}
</div>
<div role="menuitem" onclick="console.log('Open billing')" data-filter="Billing">
<div role="menuitem" data-filter="Billing" data-keywords="invoice payment">
{% lucide "credit-card" %}
<span>Billing</span>
<kbd class="ml-auto text-muted-foreground bg-transparent tracking-widest">⌘B</kbd>
{% if shortcuts %}<span data-shortcut>⌘B</span>{% endif %}
</div>
<div role="menuitem" onclick="console.log('Open settings')" data-filter="Settings">
<div role="menuitem" data-filter="Settings" data-keywords="config preferences">
{% lucide "settings" %}
<span>Settings</span>
<kbd class="ml-auto text-muted-foreground bg-transparent tracking-widest">⌘S</kbd>
{% if shortcuts %}<span data-shortcut>⌘S</span>{% endif %}
</div>
</div>
{% endmacro %}
{% set code_standalone %}
{% call command(
id="demo-command-standalone",
main_attrs={ "class": "rounded-lg border shadow-md" }
) %}
{% call command(id="demo-command-standalone") %}
{{ command_items("suggestions", "settings") }}
{% endcall %}
{% endset %}
{{ code_preview("command-standalone", code_standalone | prettyHtml, class=" w-full max-w-[450px]") }}
{{ code_preview("command-standalone", code_standalone | prettyHtml, class="w-full max-w-sm") }}
<h2 id="about"><a href="#about">About</a></h2>
<section class="prose">
<p>shadcn/ui's Command component is built on <code>cmdk</code>. Basecoat implements the same command-menu pattern with vanilla HTML, CSS, and a small JavaScript controller for filtering, active item navigation, and dialog closing.</p>
</section>
<h2 id="usage"><a href="#usage">Usage</a></h2>
@@ -97,7 +114,7 @@ toc:
Components with JavaScript
{% lucide "arrow-right" %}
</a>
<a class="badge-outline" href="/installation/#install-cli">
<a class="badge-outline" href="/cli">
Use the CLI
{% lucide "arrow-right" %}
</a>
@@ -109,68 +126,53 @@ toc:
<h4 id="usage-html-js-2"><a href="#usage-html-js-2">Step 2: Add your command HTML</a></h4>
<section class="prose">
<p>You can use the command menu standalone:</p>
</section>
{{ code_block(code_standalone | prettyHtml, "html") }}
<section class="prose">
<p>Wrap the command menu in a <code>&lt;dialog class="command-dialog"&gt;</code> to display in a dialog:</p>
<p>Use <code>&lt;dialog class="command-dialog"&gt;</code> for the command palette variant.</p>
</section>
{% set code_dialog %}
<dialog class="command-dialog" aria-label="Command menu">
{% set code_dialog_usage %}<button class="btn-outline" onclick="document.getElementById('command-palette').showModal()">Open</button>
<dialog id="command-palette" class="command-dialog" aria-label="Command menu">
<div class="command">
<!-- Regular command content: header, menu, etc. -->
<!-- Command content -->
</div>
</dialog>
{% endset %}
{{ code_block(code_dialog | prettyHtml, "html") }}
</dialog>{% endset %}
{{ code_block(code_dialog_usage | prettyHtml, "html") }}
<h4 id="usage-html-js-3"><a href="#usage-html-js-3">HTML structure</a></h4>
<section class="prose">
<p>The command menu can be used standalone or inside a native dialog. The structure is:</p>
<dl>
<dt><code class="highlight language-html">&lt;dialog class="command-dialog"&gt;</code> <span class="badge-secondary">Optional</span></dt>
<dd>For dialog variant, wraps a <code>&lt;div class="command"&gt;</code> to display as a modal. Uses native <code>&lt;dialog&gt;</code> behavior and requires <code>aria-label</code> or <code>aria-labelledby</code> for accessibility.
<dd>Dialog variant. Wraps one <code>&lt;div class="command"&gt;</code>. Add <code>aria-label</code> or <code>aria-labelledby</code> for an accessible name.
<dl>
<dt><code class="highlight language-html">&lt;div class="command"&gt;</code></dt>
<dd>Wraps around the entire command component. Can be used standalone or inside a dialog.
<dd>Root command menu. Can also be used standalone without the dialog wrapper.
<dl>
<dt><code class="highlight language-html">&lt;header&gt;</code></dt>
<dd>Contains the search input to filter command options.
<dd>Search input wrapper. The macro includes a search icon and input.
<dl>
<dt><code class="highlight language-html">&lt;input type="text"&gt;</code></dt>
<dd>The search input. Should have:
<ul>
<li><code>role="combobox"</code>: indicates the input controls a menu with filtering</li>
<li><code>aria-expanded="true"</code>: the menu is always expanded</li>
<li><code>aria-controls="{ MENU_ID }"</code>: points to the menu's id</li>
</ul>
</dd>
<dt><code class="highlight language-html">&lt;input type="text" role="combobox"&gt;</code></dt>
<dd>Filter input. Use <code>aria-expanded="true"</code> and <code>aria-controls="{ MENU_ID }"</code> to point to the command list.</dd>
</dl>
</dd>
<dt><code class="highlight language-html">&lt;div role="menu"&gt;</code></dt>
<dd>The menu containing the command items. Should have <code>id="{ MENU_ID }"</code> refered to by the input's <code>aria-controls</code> attribute.
<dt><code class="highlight language-html">&lt;div role="menu" id="{ MENU_ID }"&gt;</code></dt>
<dd>Command list. Set <code>data-empty</code> to customize the empty state.
<dl>
<dt><code class="highlight language-html">&lt;div role="group" aria-labelledby="{ HEADING_ID }"&gt;</code> <span class="badge-secondary">Optional</span></dt>
<dd>Groups related command items. Hidden automatically when every item in the group is filtered out.</dd>
<dt><code class="highlight language-html">&lt;span role="heading" id="{ HEADING_ID }"&gt;</code> <span class="badge-secondary">Optional</span></dt>
<dd>Group heading. Use an <code>id</code> when the parent group uses <code>aria-labelledby</code>.</dd>
<dt><code class="highlight language-html">&lt;div role="menuitem"&gt;</code> or <code class="highlight language-html">&lt;a role="menuitem"&gt;</code></dt>
<dd>Command item that triggers an action when clicked. Use <code>&lt;a&gt;</code> for navigation links, or <code>&lt;div&gt;</code> for actions. In a command-dialog, clicking a menuitem automatically closes the dialog unless the <code>data-keep-command-open</code> attribute is present. Optional attributes:
<ul>
<li><code>href</code>: If present on an <code>&lt;a&gt;</code>, the item navigates to that URL</li>
<li><code>data-filter</code>: custom text used for filtering (if different from displayed content, e.g., <code>data-filter="calendar event"</code>)</li>
<li><code>data-keywords</code>: additional keywords for enhanced filtering (e.g., <code>data-keywords="calendar event date"</code>)</li>
<li><code>data-force</code>: forces the item to always display, regardless of filter (e.g., <code>data-force</code>)</li>
<li><code>aria-disabled="true"</code>: marks item as disabled (excluded from keyboard navigation and filtering)</li>
</ul>
</dd>
<dd>Selectable command item. Use <code>&lt;a&gt;</code> for navigation and <code>&lt;div&gt;</code> for actions. Supports <code>data-filter</code>, <code>data-keywords</code>, <code>data-force</code>, <code>data-keep-command-open</code>, and <code>aria-disabled="true"</code>.</dd>
<dt><code class="highlight language-html">&lt;span data-shortcut&gt;</code> <span class="badge-secondary">Optional</span></dt>
<dd>Shortcut hint aligned to the inline end of the item.</dd>
<dt><code class="highlight language-html">&lt;span data-indicator&gt;</code> <span class="badge-secondary">Optional</span></dt>
<dd>Item indicator. It becomes visible with <code>data-checked="true"</code> or <code>aria-selected="true"</code>.</dd>
<dt><code class="highlight language-html">&lt;hr role="separator"&gt;</code> <span class="badge-secondary">Optional</span></dt>
<dd>Separator between groups/items. Hidden when filtering is active.</dd>
<dt><code class="highlight language-html">&lt;div role="group"&gt;</code> <span class="badge-secondary">Optional</span></dt>
<dd>Group of items, can have <code>aria-labelledby</code> attribute to link to a heading.</dd>
<dt><code class="highlight language-html">&lt;span role="heading"&gt;</code></dt>
<dd>Group heading, must have an <code>id</code> attribute if you use the <code>aria-labelledby</code> attribute on the group.</dd>
<dd>Separator between groups/items. Hidden while filtering.</dd>
</dl>
</dd>
</dl>
@@ -185,7 +187,7 @@ toc:
<section class="prose">
<dl>
<dt><code>basecoat:initialized</code></dt>
<dd>Once the component is fully initialized, it dispatches a custom (non-bubbling) <code>basecoat:initialized</code> event on itself.</dd>
<dd>Once the component is initialized, it dispatches a custom non-bubbling <code>basecoat:initialized</code> event on itself.</dd>
</dl>
</section>
@@ -196,7 +198,7 @@ toc:
</div>
<div class="flex flex-wrap gap-2 my-6">
<a class="badge-outline" href="/installation/#install-macros" target="_blank">
<a class="badge-outline" href="/cli#macros" target="_blank">
Use Nunjucks or Jinja macros
{% lucide "arrow-right" %}
</a>
@@ -211,7 +213,7 @@ toc:
</div>
{% set raw_code %}{% raw %}{% call command() %}
<div role="menuitem">
<div role="menuitem" data-keywords="date event">
<svg>...</svg>
<span>Calendar</span>
</div>
@@ -222,67 +224,112 @@ toc:
{% endcall %}{% endraw %}{% endset %}
{{ code_block(raw_code, "jinja") }}
<section class="prose">
<p>You can also use the <code>items</code> parameter to pass an array of items:</p>
</section>
{% set icon_calendar %}{% lucide "calendar" %}{% endset %}
{% set icon_settings %}{% lucide "settings" %}{% endset %}
{% set raw_code_items %}{% raw %}{% set icon_calendar %}{% lucide "calendar" %}{% endset %}
{% set icon_settings %}{% lucide "settings" %}{% endset %}
{% set items = [
{ type: "group", label: "Suggestions", items: [
{ label: "Calendar", icon: icon_calendar, keywords: "date event" },
{ label: "Search Emoji", icon: icon_settings },
{ label: "Calculator", disabled: true }
]},
{ type: "separator" },
{ type: "group", label: "Settings", items: [
{ label: "Profile", url: "/profile", icon: icon_settings, keywords: "user account" },
{ label: "Billing", url: "/billing", icon: icon_settings },
{ label: "Settings", url: "/settings", icon: icon_settings }
]}
] %}
{% call command(items=items) %}{% endcall %}{% endraw %}{% endset %}
{{ code_block(raw_code_items, "jinja") }}
<h2 id="examples"><a href="#examples">Examples</a></h2>
<h3 id="example-dialog"><a href="#example-dialog">Dialog</a></h3>
<h3 id="example-basic"><a href="#example-basic">Basic</a></h3>
<section class="prose">
<p>You can use some simple JavaScript to open the command menu with a keyboard shortcut (in this case <kbd class="kbd">⌘J</kbd>).</p>
<p>A simple command menu in a dialog. The button also shows the keyboard shortcut used in this example.</p>
</section>
{% set code_dialog %}
<button
type="button"
class="btn-outline"
onclick="document.getElementById('demo-command-dialog').showModal()"
>
{% set code_basic %}
<button type="button" class="btn-outline" onclick="document.getElementById('command-basic').showModal()">
Open command menu
<kbd class="kbd">⌘J</kbd>
</button>
{% call command_dialog(id="demo-command-dialog") %}
{{ command_items("cmd-suggestions", "cmd-settings") }}
{% call command_dialog(id="command-basic") %}
{{ command_items("command-basic-suggestions", "command-basic-settings") }}
{% endcall %}
<script>
document.addEventListener('keydown', (e) => {
if ((e.metaKey || e.ctrlKey) && e.key === 'j') {
e.preventDefault();
const dialog = document.getElementById('demo-command-dialog');
if (dialog.open) {
dialog.close();
} else {
dialog.showModal();
dialog.querySelector('header input')?.focus();
}
document.addEventListener('keydown', (event) => {
if ((event.metaKey || event.ctrlKey) && event.key === 'j') {
event.preventDefault();
const dialog = document.getElementById('command-basic');
dialog.open ? dialog.close() : dialog.showModal();
dialog.querySelector('header input')?.focus();
}
});
</script>
{% endset %}
{{ code_preview("command-basic", code_basic | prettyHtml) }}
{{ code_preview("command-dialog", code_dialog | prettyHtml) }}
<h3 id="example-shortcuts"><a href="#example-shortcuts">Shortcuts</a></h3>
{% set code_shortcuts %}
{% call command(id="command-shortcuts") %}
{{ command_items("command-shortcuts-suggestions", "command-shortcuts-settings", true) }}
{% endcall %}
{% endset %}
{{ code_preview("command-shortcuts", code_shortcuts | prettyHtml, class="w-full max-w-sm") }}
<h3 id="example-groups"><a href="#example-groups">Groups</a></h3>
{% set code_groups %}
{% call command(id="command-groups") %}
{{ command_items("command-groups-suggestions", "command-groups-settings") }}
{% endcall %}
{% endset %}
{{ code_preview("command-groups", code_groups | prettyHtml, class="w-full max-w-sm") }}
<h3 id="example-scrollable"><a href="#example-scrollable">Scrollable</a></h3>
{% set code_scrollable %}
{% call command(id="command-scrollable") %}
<div role="group" aria-labelledby="command-scrollable-heading">
<span role="heading" id="command-scrollable-heading">Pages</span>
{% for i in range(0, 18) %}
<div role="menuitem" data-filter="Page {{ i + 1 }}">
{% lucide "file" %}
<span>Page {{ i + 1 }}</span>
</div>
{% endfor %}
</div>
{% endcall %}
{% endset %}
{{ code_preview("command-scrollable", code_scrollable | prettyHtml, class="w-full max-w-sm") }}
<h3 id="example-rtl"><a href="#example-rtl">RTL</a></h3>
<section class="prose">
<p>Command supports document direction. Set <code>dir="rtl"</code> on the command root or a parent element.</p>
</section>
{% set code_rtl %}
<div dir="rtl">
{% call command(id="command-rtl", placeholder="اكتب أمراً أو ابحث...") %}
<div role="group" aria-labelledby="command-rtl-suggestions">
<span role="heading" id="command-rtl-suggestions">اقتراحات</span>
<div role="menuitem" data-filter="Calendar">
{% lucide "calendar" %}
<span>التقويم</span>
<span data-shortcut>⌘K</span>
</div>
<div role="menuitem" data-filter="Settings">
{% lucide "settings" %}
<span>الإعدادات</span>
<span data-shortcut>⌘S</span>
</div>
</div>
{% endcall %}
</div>
{% endset %}
{{ code_preview("command-rtl", code_rtl | prettyHtml, class="w-full max-w-sm") }}
<h2 id="api-reference"><a href="#api-reference">API Reference</a></h2>
<section class="prose">
<dl>
<dt><code class="highlight language-html">.command</code></dt>
<dd>Root command menu.</dd>
<dt><code class="highlight language-html">.command-dialog</code></dt>
<dd>Native dialog variant. Closes automatically when an item is clicked unless that item has <code>data-keep-command-open</code>.</dd>
<dt><code class="highlight language-html">data-filter</code></dt>
<dd>Overrides item text used by the filter.</dd>
<dt><code class="highlight language-html">data-keywords</code></dt>
<dd>Additional whitespace- or comma-separated search terms.</dd>
<dt><code class="highlight language-html">data-force</code></dt>
<dd>Always keeps an item visible while filtering.</dd>
<dt><code class="highlight language-html">data-empty</code></dt>
<dd>Custom empty-state text on the menu element.</dd>
</dl>
</section>