CommandPalette is a filterable grouped context menu: a searchable popup with categorized options, triggered by right-click on a target element. Based on stak-grouplist by newk5.
Basic usage with right-click trigger and grouped items.
Using the select AJAX event to handle item selection on the server side.
Trigger the CommandPalette via a keyboard shortcut using p:hotkey (Ctrl+Shift+K). The palette also remains available via right-click on the target element.
<style>
.palette-target {
border: 2px solid var(--p-content-border-color);
border-radius: 3px;
padding: 40px;
text-align: center;
cursor: context-menu;
background: light-dark(var(--p-surface-50), var(--p-surface-950));
font-size: 14px;
color: var(--p-text-color);
user-select: none;
}
</style>
<div class="card">
<h5>Basic usage</h5>
<p>Basic usage with right-click trigger and grouped items.</p>
<h:form id="formBasicUsage">
<h:panelGroup id="commandPaletteTarget" layout="block" styleClass="palette-target">
Right-click here to open the CommandPalette
</h:panelGroup>
<pe:commandPalette id="palette" for="commandPaletteTarget"
label="Actions" filterPlaceholder="Search actions..." width="260" height="350">
<pe:commandPaletteItem group="File" value="new" label="New File"/>
<pe:commandPaletteItem group="File" value="open" label="Open File"/>
<pe:commandPaletteItem group="File" value="save" label="Save"/>
<pe:commandPaletteItem group="File" value="saveAs" label="Save As..."/>
<pe:commandPaletteItem group="Edit" value="undo" label="Undo"/>
<pe:commandPaletteItem group="Edit" value="redo" label="Redo"/>
<pe:commandPaletteItem group="Edit" value="cut" label="Cut"/>
<pe:commandPaletteItem group="Edit" value="copy" label="Copy"/>
<pe:commandPaletteItem group="Edit" value="paste" label="Paste"/>
<pe:commandPaletteItem group="View" value="zoomIn" label="Zoom In"/>
<pe:commandPaletteItem group="View" value="zoomOut" label="Zoom Out"/>
<pe:commandPaletteItem group="View" value="fullscreen" label="Fullscreen"/>
</pe:commandPalette>
</h:form>
</div>
<div class="card">
<h5>Selection Event</h5>
<p>Using the <i>select</i> AJAX event to handle item selection on the server side.</p>
<h:form id="formSelectionEvent">
<p:messages id="messages" showSummary="true" showDetail="true"/>
<h:panelGroup id="paletteTarget" layout="block" styleClass="palette-target">
Right-click here to select a Node action
</h:panelGroup>
<pe:commandPalette id="palette2" for="paletteTarget"
label="Nodes" filterPlaceholder="Search nodes..." width="260">
<pe:commandPaletteItem group="Node1" value="1" label="Create Node1 Action1"
itemTitle="Creates a new Node1 action"/>
<pe:commandPaletteItem group="Node1" value="2" label="Create Node1 Action2"
itemTitle="Creates another Node1 action"/>
<pe:commandPaletteItem group="Node2" value="3" label="Create Node2 Action1"
itemTitle="Creates a new Node2 action"/>
<pe:commandPaletteItem group="Node2" value="4" label="Create Node2 Action2"
itemTitle="Creates another Node2 action"/>
<p:ajax event="select" listener="#{commandPaletteController.onItemSelect}"
update="messages"/>
</pe:commandPalette>
</h:form>
</div>
<div class="card">
<h5>Hotkey Trigger</h5>
<p>Trigger the CommandPalette via a keyboard shortcut using <code>p:hotkey</code> (<kbd>Ctrl+Shift+K</kbd>). The palette also remains available via right-click on the target element.</p>
<h:form id="formHotkey">
<p:messages id="messages" showSummary="true" showDetail="true"/>
<h:panelGroup id="hotkeyTarget" layout="block" styleClass="palette-target">
Right-click or press <kbd>Ctrl+Shift+K</kbd> to open the CommandPalette
</h:panelGroup>
<p:hotkey bind="ctrl+shift+k" handler="PF('hotkeyPaletteWidget').showCentered();"/>
<pe:commandPalette id="hotkeyPalette" for="hotkeyTarget"
label="Actions" filterPlaceholder="Search actions..." width="260" height="350"
widgetVar="hotkeyPaletteWidget">
<pe:commandPaletteItem group="File" value="new" label="New File"/>
<pe:commandPaletteItem group="File" value="open" label="Open File"/>
<pe:commandPaletteItem group="File" value="save" label="Save"/>
<pe:commandPaletteItem group="File" value="saveAs" label="Save As..."/>
<pe:commandPaletteItem group="Edit" value="undo" label="Undo"/>
<pe:commandPaletteItem group="Edit" value="redo" label="Redo"/>
<pe:commandPaletteItem group="Edit" value="cut" label="Cut"/>
<pe:commandPaletteItem group="Edit" value="copy" label="Copy"/>
<pe:commandPaletteItem group="Edit" value="paste" label="Paste"/>
<pe:commandPaletteItem group="View" value="zoomIn" label="Zoom In"/>
<pe:commandPaletteItem group="View" value="zoomOut" label="Zoom Out"/>
<pe:commandPaletteItem group="View" value="fullscreen" label="Fullscreen"/>
<p:ajax event="select" listener="#{commandPaletteController.onItemSelect}"
update="messages"/>
</pe:commandPalette>
</h:form>
</div>
package org.primefaces.extensions.showcase.controller;
import java.io.Serial;
import java.io.Serializable;
import jakarta.faces.application.FacesMessage;
import jakarta.faces.context.FacesContext;
import jakarta.faces.view.ViewScoped;
import jakarta.inject.Named;
import org.primefaces.event.SelectEvent;
import org.primefaces.extensions.component.commandpalette.CommandPaletteSelection;
/**
* Controller for CommandPalette showcase.
*
* @since 16.0.0
*/
@Named
@ViewScoped
public class CommandPaletteController implements Serializable {
@Serial
private static final long serialVersionUID = 1L;
public void onItemSelect(final SelectEvent<CommandPaletteSelection> event) {
final CommandPaletteSelection selection = event.getObject();
final FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_INFO,
"Item Selected",
"Group: " + selection.getGroup()
+ ", Value: " + selection.getValue()
+ ", Label: " + selection.getLabel());
FacesContext.getCurrentInstance().addMessage(null, msg);
}
}