Drawing with React
Actions that draw themselves and handle their own presses.
Choose this only for what layers cannot draw: users cannot restyle it.
An action that draws itself has a component. It runs inside the deck's React tree, so the
@sdeck/app hooks work, and it handles its own input:
import { useNow, usePress } from "@sdeck/plugin";
const stopwatch = plugin.action({
id: "since",
name: "Seconds since press",
component: ({ settings, setSettings }) => {
const now = useNow(1000);
usePress(() => setSettings({ since: Date.now() }));
const seconds = Math.round((now.getTime() - Number(settings.since || now)) / 1000);
return <div style={{ display: "flex", fontSize: 32 }}>{seconds}</div>;
},
settings: { since: setting.number("Since", { default: 0 }) },
});Hooks for components: usePress (with onLongPress and onDoublePress), useHold,
useKeyPressed, useKeyRepeat, useSwipe, useNow, useInterval, useNavigation, usePage,
and from the host useHostState("system.cpu"), useExpression, useTemplate, useVariable,
useImage("icon:lucide/sun") and useRunAction. temperature.use() reads a live value of your
own, and useShared a Shared source. Layout is flexbox: give elements
display: "flex". Sensor strips show no text, so draw icons and bars there.
Doing something while held
useHold runs one thing when its key or sensor is pressed and another when it is let go:
const talk = plugin.action({
id: "talk",
name: "Push to talk",
slots: ["key", "sensor"],
component: () => {
const { held, error } = useHold(
() => microphone.open(),
() => microphone.close(),
);
const background = error ? "#7f1d1d" : held ? "#16a34a" : "#1e293b";
return <div style={{ display: "flex", width: "100%", height: "100%", background }} />;
},
});The second always follows the first, and waits for it: also when the first failed, the page changed
meanwhile or the deck was unplugged. A headless action needs none of this. Users can set any button
to Hold, which runs the action when pressed and its undo when let go.