useWidget
HOOK

HOOKS & API / CONTEXT

useWidget

Read the live runtime context of the current widget instance: active size, canvas width and height, declarative size matcher match, theme, resolved user settings, preview mode, locales/ translation helpers, and instance-bound fetch.

Signature#

SIGNATURE
const {
  size, width, height, match,
  theme, settings, isPreview,
  t, locale, locales, defaultLocale, isZh,
  reduceMotion, fetch,
} = useWidget();

Returned Fields#

sizestringRead-only
Active size name: "small", "medium", "large", or "custom" (matches deskpack.json; never use "middle").
widthnumberRead-only
Live canvas width in logical macOS points (updates as the user resizes a custom widget).
heightnumberRead-only
Live canvas height in logical macOS points.
matchfunctionHelper
Declaratively branch JSX or scalar values by active size: match({ small?, medium?, large?, custom?, default? }). Wrap JSX branches in () => <...> for lazy evaluation so non-matching sizes are never constructed; scalar values (font sizes, padding, counts) can be passed directly. Returns non-null T when default or all four sizes are supplied, otherwise T | null.
themeWidgetThemeRead-only
Active material, dark mode, accent color, and system preferences; see Styling & Theme.
settingsobjectRead-only
Resolved values for deskpack.json options (user edits merged with defaults). Use useSetting to write back.
isPreviewbooleanRead-only
true in the Widget Library, import preview, and thumbnails. Timers and networking pause — render realistic sample data.
localestringRead-only
Active BCP-47 locale tag from macOS (or the preview language selected in Widget Studio, e.g. "en", "zh-Hans").
localesobjectOptional
Loaded translation dictionaries from locales/<tag>.json, keyed by BCP-47 locale tag.
defaultLocalestringOptional
Fallback locale tag declared in deskpack.json (defaults to "en").
isZhbooleanRead-only
true when rendering in Chinese.
tfunctionHelper
Unified translator: resolves keys from locales/<tag>.json with dot-nested paths and {var} named interpolation (e.g. t("status.done"), t("todayCount", { count })) along the BCP-47 fallback chain; tiny single-file widgets without locales/ can also pass inline t("中文", "English"), t("中文", { en, ja }), or positional t("今日 {0} 项", "{0} today", count). See Localization.
widgetIdstringRead-only
Unique ID of this placed desktop instance.
packageIdstringRead-only
Package ID declared in deskpack.json.
isProbooleanRead-only
true when Deskset Pro is unlocked.
isCommunityPackbooleanOptional
true when running as a community .deskpack.
apiVersionnumberOptional
Host SDK API level (currently 1) for feature detection.
reduceMotionbooleanOptional
true when macOS Reduce Motion is enabled.
fetchfunctionOptional
Instance-bound fetch(url, init) helper that preserves widget identity across sequential await calls.

Size Branching with match(...)#

Every Deskset widget is a plain React function component. Declare supported sizes and optional custom dimensions in deskpack.json (sizes / sizeDimensions), keep shared hooks, state, and outer card chrome once at the top of your component, and use match(...) to branch layouts or style values cleanly:

TSX
import React, { HStack, Icon, Spacer, Text, VStack, useWidget } from "deskset-react";

export default function StatusCard() {
  const { width, settings, theme, t, isPreview, reduceMotion, match } = useWidget();
  const count = isPreview ? 12 : Number(settings.count || 0);

  // Pass scalar values directly (TypeScript narrows to `number` when `default` is supplied)
  const valueSize = match({ small: 24, medium: 30, large: 38, default: 30 });
  const pad = match({ small: 12, default: 16 });

  return (
    <VStack
      maxWidth={true}
      maxHeight={true}
      padding={pad}
      background={theme.isPure ? "clear" : "theme.card"}
      cornerRadius={theme.isPure ? 0 : theme.corner}
      clip={!theme.isPure}
      animation={reduceMotion ? "none" : "spring"}
    >
      {/* Wrap JSX branches in () => so only the active size runs */}
      {match({
        small: () => (
          <VStack maxWidth={true} maxHeight={true} alignment="leading" spacing={6}>
            <Icon name="checkmark.seal.fill" size={16} color="theme.accent" />
            <Spacer />
            <Text fontSize={valueSize} fontWeight="bold" monospacedDigits>
              {String(count)}
            </Text>
            <Text fontSize={12} color="theme.secondary" lineLimit={1}>
              {t("completed")}
            </Text>
          </VStack>
        ),
        medium: () => (
          <HStack maxWidth={true} maxHeight={true} spacing={12} alignment="center">
            <Icon name="checkmark.seal.fill" size={28} color="theme.accent" />
            <VStack alignment="leading" spacing={4}>
              <Text fontSize={13} fontWeight="semibold" color="theme.secondary">
                {t("completed")}
              </Text>
              <Text fontSize={valueSize} fontWeight="bold" monospacedDigits>
                {t("todayCount", { count })}
              </Text>
            </VStack>
            <Spacer />
          </HStack>
        ),
        default: () => (
          <VStack maxWidth={true} maxHeight={true} alignment="leading" spacing={8}>
            <HStack spacing={6} alignment="center">
              <Icon name="checkmark.seal.fill" size={18} color="theme.accent" />
              <Text fontSize={14} fontWeight="semibold" color="theme.secondary">
                {t("completed")}
              </Text>
              <Spacer />
              <Text fontSize={11} color="theme.tertiary" monospacedDigits>
                {`${Math.round(width)}pt`}
              </Text>
            </HStack>
            <Spacer />
            <Text fontSize={valueSize} fontWeight="bold" monospacedDigits>
              {t("todayCount", { count })}
            </Text>
          </VStack>
        ),
      })}
    </VStack>
  );
}

Notes#

  • size and match keys are unified to "small", "medium", "large", and "custom" everywhere; writing size === "middle" or match({ middle: ... }) is flagged as an error in Widget Studio.
  • Always wrap JSX branches in () => <...> so inactive sizes skip list iteration and expression evaluation. Keep all React Hooks at the top level of your component — never call Hooks inside a match branch callback.
  • When supporting custom sizes, branch on width and height so layouts stay crisp at any aspect ratio.