Button

The Button component in the Scripting app allows you to create interactive elements with customizable actions, labels, styles, and roles. Buttons can trigger actions, execute intents, and display various visual styles based on the configuration. This documentation provides a detailed guide on how to use the Button API, including its properties, roles, styles, and examples.


Button

Description

You create a button by providing an action or an intent and a label. The label can be a simple text, an icon, or a complex view. Buttons are essential for creating interactive interfaces, such as submitting forms or navigating between pages.

Properties

PropertyTypeDescription
titlestringThe text label displayed on the button.
systemImagestring (optional)The name of a system icon to display alongside the button's title.
childrenVirtualNode or VirtualNode[]Custom view(s) to be used as the button label instead of title.
role'destructive' | 'cancel' | 'close' | 'confirm' (optional)Describes the purpose of the button. destructive highlights the button as performing a potentially dangerous action, while cancel implies dismissal.
intentAppIntent<any>An intent to execute when the button is triggered. Available for Widget or LiveActivity. See the Interactive Widget and LiveActivity.
action() => voidA function to execute when the user triggers the button.

ButtonStyle

Defines the visual appearance of the button.

ValueDescription
automaticThe default style based on the button's context.
borderedA standard bordered style.
borderedProminentA prominent bordered style that stands out.
borderlessA style without any border.
plainA plain style with minimal decoration, though it may indicate pressed, focused, or enabled states visually.

ButtonBorderShape

Specifies the shape of the button's border when using bordered or borderedProminent styles.

ValueDescription
automaticDefers to the system to determine the appropriate shape.
capsuleA capsule-shaped border.
circleA circular border.
roundedRectangleA rectangle with rounded corners.
buttonBorderDefers to the environment to determine the resolved border shape.
{ roundedRectangleRadius: number }A rounded rectangle with a specific corner radius.

ControlSize

Defines the size of the button and other controls.

ValueDescription
miniThe smallest control size.
smallA compact control size.
regularThe standard control size.
largeA large control size.
extraLargeThe largest control size, typically for high emphasis or accessibility purposes.

CommonViewProps

These properties can be applied to customize the appearance and behavior of buttons within a view.

PropertyTypeDescription
controlSizeControlSizeSets the size for controls within this view.
buttonStyleButtonStyleApplies custom interaction behavior and appearance to buttons.
buttonBorderShapeButtonBorderShapeSpecifies the shape of the border for bordered and borderedProminent button styles.

Example Usage

Basic Button with Action

<Button title="Sign in" action={handleSignIn} />

Button with System Image

<Button title="Delete" systemImage="trash" role="destructive" action={handleDelete} />

Button with Custom Label

<Button>
  <Text>Custom Label</Text>
</Button>

Button Executing an AppIntent

<Button
  title="Start Workout"
  intent={MyStartWorkoutIntent({ duration: 30 })}
  buttonStyle="borderedProminent"
/>

Styling Buttons

<Group
  buttonStyle="bordered"
  buttonBorderShape={{ roundedRectangleRadius: 8 }}
  controlSize="large"
>
  <Button title="Save" action={handleSave} />
</Group>

Notes

  • Use role to indicate buttons with specific purposes, such as canceling or destructive actions.
  • Combine buttonStyle and buttonBorderShape for consistent theming across views.
  • The intent property integrates buttons with Widget and LiveActivity for seamless interactions.

For further details on AppIntent, refer to the Interactive Widget and LiveActivity documentation.