2025-10-30 10:03:34 +07:00
{#
2025-10-30 19:09:21 +08:00
Renders a standalone command menu component with search and keyboard navigation.
2025-10-30 10:03:34 +07:00
@param id {string} [optional] - Unique identifier for the command component.
@param items {array} [optional] - Array of items to render (alternative to using caller).
@param placeholder {string} [optional] [default="Type a command or search..."] - Placeholder text for the search input.
@param empty_text {string} [optional] [default="No results found."] - Text displayed when no results match the search.
@param main_attrs {object} [optional] - Additional HTML attributes for the main container div.
@param input_attrs {object} [optional] - Additional HTML attributes for the search input.
2025-11-02 11:56:40 +08:00
@param menu_attrs {object} [optional] - Additional HTML attributes for the menu div.
2025-10-30 10:03:34 +07:00
#}
{% macro command (
id = None ,
items = None ,
placeholder = "Type a command or search..." ,
empty_text = "No results found." ,
main_attrs ={},
2025-10-30 19:09:21 +08:00
input_attrs ={},
2025-11-02 11:56:40 +08:00
menu_attrs ={}
2025-10-30 19:09:21 +08:00
) %}
{% set id = id or ( "command-" ~ range ( 100000 , 999999 ) | random | string ) %}
<div
id=" {{ id }} "
class="command {{ main_attrs.class }} "
aria-label="Command menu"
{% for key , value in main_attrs.items () %}
{% if key ! = 'class' %}{{ key }} =" {{ value }} " {% endif %}
{% endfor %}
>
<header>
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-search-icon lucide-search"><circle cx="11" cy="11" r="8"/><path d="m21 21-4.3-4.3"/></svg>
<input
type="text"
id=" {{ id }} -input"
placeholder=" {{ placeholder }} "
autocomplete="off"
autocorrect="off"
spellcheck="false"
aria-autocomplete="list"
role="combobox"
aria-expanded="true"
2025-11-02 11:56:40 +08:00
aria-controls=" {{ id }} -menu"
2025-10-30 19:09:21 +08:00
{% for key , value in input_attrs.items () %}
{{ key }} =" {{ value }} "
{% endfor %}
>
</header>
<div
2025-11-02 11:56:40 +08:00
role="menu"
id=" {{ id }} -menu"
2025-10-30 19:09:21 +08:00
aria-orientation="vertical"
data-empty=" {{ empty_text }} "
2025-11-02 11:56:40 +08:00
class="scrollbar {{ menu_attrs.class }} "
{% for key , value in menu_attrs.items () %}
2025-10-30 19:09:21 +08:00
{% if key ! = 'class' %}{{ key }} =" {{ value }} " {% endif %}
{% endfor %}
>
{% if items and items | length > 0 %}
{{ render_command_items ( items , id ~ "-items" if id else "items" ) }}
{% else %}
{{ caller () if caller }}
{% endif %}
</div>
</div>
{% endmacro %}
{#
Renders a command dialog (modal command palette).
@param id {string} [optional] - Unique identifier for the command dialog.
@param items {array} [optional] - Array of items to render (alternative to using caller).
@param placeholder {string} [optional] [default="Type a command or search..."] - Placeholder text for the search input.
@param empty_text {string} [optional] [default="No results found."] - Text displayed when no results match the search.
@param dialog_attrs {object} [optional] - Additional HTML attributes for the dialog element.
@param input_attrs {object} [optional] - Additional HTML attributes for the search input.
2025-11-02 11:56:40 +08:00
@param menu_attrs {object} [optional] - Additional HTML attributes for the menu div.
2025-10-30 19:09:21 +08:00
@param open {boolean} [optional] [default=False] - Whether the command dialog should be open initially.
#}
{% macro command_dialog (
id = None ,
items = None ,
placeholder = "Type a command or search..." ,
empty_text = "No results found." ,
2025-10-30 10:03:34 +07:00
dialog_attrs ={},
input_attrs ={},
2025-11-02 11:56:40 +08:00
menu_attrs ={},
2025-10-30 10:03:34 +07:00
open = False
) %}
2025-10-30 19:09:21 +08:00
{% set id = id or ( "command-dialog-" ~ range ( 100000 , 999999 ) | random | string ) %}
2025-10-30 10:03:34 +07:00
<dialog
id=" {{ id }} "
2025-10-30 19:09:21 +08:00
class="dialog command-dialog {{ dialog_attrs.class }} "
2025-10-30 10:03:34 +07:00
aria-label="Command menu"
{% if open %} open {% endif %}
onclick="if (event.target === this) this.close()"
{% for key , value in dialog_attrs.items () %}
{% if key ! = 'class' %}{{ key }} =" {{ value }} " {% endif %}
{% endfor %}
>
2025-11-02 11:56:40 +08:00
<div class="command">
<header>
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-search-icon lucide-search"><circle cx="11" cy="11" r="8"/><path d="m21 21-4.3-4.3"/></svg>
<input
type="text"
id=" {{ id }} -input"
placeholder=" {{ placeholder }} "
autocomplete="off"
autocorrect="off"
spellcheck="false"
aria-autocomplete="list"
role="combobox"
aria-expanded="true"
aria-controls=" {{ id }} -menu"
{% for key , value in input_attrs.items () %}
{{ key }} =" {{ value }} "
2025-10-30 10:03:34 +07:00
{% endfor %}
>
2025-11-02 11:56:40 +08:00
</header>
<div
role="menu"
id=" {{ id }} -menu"
aria-orientation="vertical"
data-empty=" {{ empty_text }} "
class="scrollbar {{ menu_attrs.class }} "
{% for key , value in menu_attrs.items () %}
{% if key ! = 'class' %}{{ key }} =" {{ value }} " {% endif %}
{% endfor %}
>
{% if items and items | length > 0 %}
{{ render_command_items ( items , id ~ "-items" if id else "items" ) }}
{% else %}
{{ caller () if caller }}
{% endif %}
2025-10-30 19:09:21 +08:00
</div>
<button type="button" aria-label="Close dialog" onclick="this.closest('dialog').close()">
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-x-icon lucide-x"><path d="M18 6 6 18"/><path d="m6 6 12 12"/></svg>
</button>
2025-11-02 11:56:40 +08:00
</div>
2025-10-30 10:03:34 +07:00
</dialog>
{% endmacro %}
{#
Renders a list of items for the command component.
2025-11-02 11:56:40 +08:00
@param items {array} - Array of items to render. Each item can be:
2025-12-10 14:49:32 +08:00
- { type: "item", label: "Text or HTML", icon: "HTML", url: "href", keywords: "search terms", disabled: true, attrs: {} }
2025-11-02 11:56:40 +08:00
- { type: "group", label: "Group Title", items: [...] }
- { type: "separator" }
2025-12-10 14:49:32 +08:00
Note: Use attrs to add data-label for alternative search text if needed. If url is provided, renders as <a role="menuitem"> instead of <div role="menuitem">. Label supports HTML (marked as safe).
2025-10-30 10:03:34 +07:00
@param parent_id_prefix {string} [optional] - The prefix for the item id.
#}
{% macro render_command_items ( items , parent_id_prefix = "items" ) %}
{% for item in items %}
{% set item_id = parent_id_prefix ~ "-" ~ loop .index %}
{% if item.type == "group" %}
{% set group_label_id = item.id if item.id else "group-label-" + item_id %}
<div
role="group"
aria-labelledby=" {{ group_label_id }} "
{% if item.attrs %}
{% for key , value in item.attrs.items () %}
{{ key }} =" {{ value }} "
{% endfor %}
{% endif %}
>
2025-12-10 14:49:32 +08:00
<span role="heading" id=" {{ group_label_id }} "> {{ item.label | safe }} </span>
2025-12-21 14:20:09 +08:00
{{ render_command_items ( item [ 'items' ], item_id ) if item [ 'items' ] }}
2025-10-30 10:03:34 +07:00
</div>
{% elif item.type == "separator" %}
<hr role="separator" />
{% elif item.type == "item" or not item.type %}
2025-12-10 14:49:32 +08:00
{% if item.url %}
<a
id=" {{ item_id }} "
href=" {{ item.url }} "
role="menuitem"
{% if item.keywords %} data-keywords=" {{ item.keywords }} " {% endif %}
{% if item.disabled %} aria-disabled="true" {% endif %}
{% if item.attrs %}
{% for key , value in item.attrs.items () %}
{% if key ! = "href" %}{{ key }} =" {{ value }} " {% endif %}
{% endfor %}
{% endif %}
>
{% if item.icon %}{{ item.icon | safe }}{% endif %}
2025-12-18 08:57:48 +08:00
{{ item.label | safe }}
2025-12-10 14:49:32 +08:00
</a>
{% else %}
<div
id=" {{ item_id }} "
role="menuitem"
{% if item.keywords %} data-keywords=" {{ item.keywords }} " {% endif %}
{% if item.disabled %} aria-disabled="true" {% endif %}
{% if item.attrs %}
{% for key , value in item.attrs.items () %}
{{ key }} =" {{ value }} "
{% endfor %}
{% endif %}
>
{% if item.icon %}{{ item.icon | safe }}{% endif %}
2025-12-18 08:57:48 +08:00
{{ item.label | safe }}
2025-12-10 14:49:32 +08:00
</div>
{% endif %}
2025-10-30 10:03:34 +07:00
{% endif %}
{% endfor %}
{% endmacro %}