Skip to Content
sdeck docs

Actions

Things a button does or shows, with a look users can restyle.

An action is either headless (it runs, and sdeck draws its look) or draws itself with React and handles its own presses.

A headless action has run and usually a look. It can run from a key, a trigger, a workflow or a hotkey, and users can restyle its look in the composer:

const toggleLight = plugin.action({
  id: "toggle",
  name: "Toggle the light",
  icon: "lightbulb", // a Lucide icon name, shown in the action list
  slots: ["key", "sensor"], // where it can go (default: keys, and dials)
  settings: {
    room: setting.text("Room", { default: "Office" }),
  },
  run: async ({ settings, signal }) => {
    const toggle = () =>
      fetch(`http://lights.local/${settings.room}/toggle`, { method: "POST", signal });
    await toggle();
    // `undo` makes it reversible (toggle buttons and the Undo step use it).
    return { label: `Toggle ${settings.room}`, undo: toggle };
  },
  look: (s) => iconLook({ icon: "lucide/lightbulb", color: "#fbbf24", caption: s.room }),
});

run receives the settings (with {{templates}} already filled in), the plugin's context, what started it (source), a signal that aborts when the run is canceled, and deck to navigate or change brightness. Throw to fail: the key flashes red and the error is in Activity.

Dials

An action for keys also goes on dials, and is drawn the same way, in the middle of the strip above the dial. Pressing the dial runs it. turn says what turning does: the action runs once for each step, with these settings instead of the button's.

const volume = plugin.action({
  id: "volume",
  name: "Volume",
  settings: {
    operation: setting.select("Operation", { mute: "Mute", up: "Louder", down: "Quieter" }),
    step: setting.number("Step (%)", { default: 5 }),
  },
  turn: { left: { operation: "down" }, right: { operation: "up" } },
  run: ({ settings }) => change(settings.operation, settings.step),
});

Where a turn depends on the button's settings, make turn a function: it gets them, and which way the dial turned. Return undefined for a turn that does nothing.

turn: (settings, side) => ({ operation: "add", value: side === "left" ? -settings.value : settings.value }),

Users can have turning do something else. Versions of sdeck without dials ignore turn, and versions before the function ignore a function.

The knobs of remote devices are dials too: everything here works on them. Their display is the same shape as a Stream Deck +'s strip for one dial, so what is drawn for one looks the same on the other.

To draw for the room there is, 200 × 100, add "dial" to slots: look then gets "dial" as its second argument. An action that draws itself goes on dials only if it lists them, and handles turning with useDialTurn. That needs "apiVersion": 6.

Actions that only show something

A label or a graph does nothing when pressed. Mark it display: true: it then needs no run, and is not offered where an action runs without a button (steps of a workflow, gestures, hotkeys).

plugin.action({ id: "cpu", name: "CPU load", display: true, look: () => cpuLook });

Looks that change on their own

A clock or a timer changes without anyone pressing it. Give the action useValues: a hook that runs while its button is shown. What it returns is self.<name> in the look, so sdeck draws it and users can restyle all of it in the Look tab:

const since = plugin.action({
  id: "since",
  name: "Time since press",
  settings: { at: setting.number("Since", { default: 0 }) },
  // What users find when they pick a value, under the action's name.
  values: { seconds: { name: "Seconds since", type: "number" } },
  useValues: ({ settings, setSettings }) => {
    const now = useNow(1000);
    usePress(() => setSettings({ at: Date.now() }));
    return { seconds: settings.at ? Math.round((now.getTime() - settings.at) / 1000) : 0 };
  },
  look: () => iconLook({ icon: "lucide/timer", caption: "{{duration(self.seconds)}}" }),
});

It gets what a component gets, and can use the same hooks. Without run or display it handles the button's presses itself, as here; with one of them, users get the Behavior tab too. Every button already has self.on, held, index, label, running, flash and error, so use other names. useValues needs "apiVersion": 4 in package.json.

formatDate, formatDuration and formatNumber from @sdeck/plugin write values the way expressions do.

Every button of yours

context.buttons.list() gives every button that has one of your actions, on all pages and profiles, shown or not, with its settings. context.buttons.watch(listener) tells you when they change. A component and useValues get the id of theirs as button, so state kept by that id outlives a page switch and is the same on every deck. Publish it as a live value and users can read it anywhere; that is how Basics offers its timers. This needs "apiVersion": 4 too.

On this page