Command palette
Search, shortcuts and server actions in one box. Press mod+k on any page of this site.
Try it now.
Press
Ctrl
+
K
(
Cmd
+
K
on a Mac) or use the button below. This site's palette has every docs page, shortcuts
(
g h
,
g c
), a server search of components, server actions with arguments and a confirmation.
Open the palette
Page action counter: 0
One module per app
use SutraUI.Commands
and return what this person may do now from
actions/1
.
It is read per socket on mount and on every
handle_params
.
defmodule MyAppWeb.Commands do
use SutraUI.Commands
alias SutraUI.Commands.Action
@impl true
def actions(_socket) do
[
Action.new(id: "go.home", title: "Go to the home page", section: "Shortcuts",
shortcut: "g h", navigate: "/"),
Action.new(id: "theme.toggle", title: "Toggle dark mode",
js: Phoenix.LiveView.JS.dispatch("phx:toggle-theme")),
Action.new(
id: "demo.toast",
title: "Send a test toast",
args: [
%{name: :kind, label: "Which kind?", type: :choice,
choices: [{"info", "Info"}, {"error", "Error"}]},
%{name: :message, label: "Message", type: :text, required: false}
],
run: fn socket, args ->
SutraUI.Toast.send(String.to_existing_atom(args.kind), args[:message] || "Hello")
{:ok, socket}
end
),
Action.new(id: "demo.reset", title: "Reset the demo", tone: :danger,
confirm: "Reset every demo?", confirm_label: "Reset",
run: fn socket, _args -> {:ok, socket} end)
]
end
@impl true
def sources, do: [components: "Components"]
@impl true
def search(:components, query, _socket) do
for item <- MyApp.Catalog.search(query) do
Action.new(id: "component:#{item.slug}", title: item.name, navigate: item.path,
children: [Action.new(id: "component:#{item.slug}:copy",
title: "Copy the link", copy: item.path)])
end
end
end
# one page's own actions; they leave with the page
SutraUI.Commands.put_page_actions(socket, [Action.new(...)])
What an action does
An action does exactly one of these when chosen.
navigate
live_redirect to a path. Every docs page.
patch
live_patch to a path of the same LiveView.
href
Browser navigation (http, https or mailto).
event
Pushes an event to the page's LiveView. The counter on this page.
js
Runs a JS command. Toggle dark mode.
copy
Copies a text. Copy the dependency line.
run
A server function, with optional args and confirm. Send a test toast.
Try these in the palette
- Navigate: type "tool" and Enter to open Tooltip; type "dialg" (a typo in a five-letter word) to see it still find Dialog.
- Children: "Change color theme" opens a page of themes; Backspace on an empty box goes back one step.
- Args and a server action: "Send a test toast" asks for a kind, then a message, then runs on the server.
- Server search source: "Pick a component to look up" searches the Components source on the server. A result's own actions open with the right arrow.
- Confirm and danger: "Reset the demo" asks first, with a Reset button and Cancel.
- Disabled with a reason: "Publish the docs" says why it cannot run.
-
Page action:
"Add one to the counter on this page" exists only here, and leaves when you navigate away. Its shortcut is
g i. - Shortcuts: press ? in the palette's page (not while typing) to list them. Recent actions are kept in this browser.
Keys
- Arrow up and down move; Enter runs; Escape goes back one step, then closes.
- Search runs in the browser for actions (words in order, starts of words first, one typo in a word of five letters or more, accents and case ignored) and in Elixir for server sources; an older answer never covers a newer one.
-
Server actions are looked up again by id;
allowis checked again and arguments are validated before they run.