shaffex

Button is a user interface component that triggers an action when tapped by the user. It can display text, an icon, or both, and it’s highly customizable. In SwiftUI, a Button is a user interface control that performs an action when tapped by the user. It can contain text, an image, or both, and you can customize its appearance and behavior.

Parameters:

action is the action executed when the button is tapped. For more details see Actions

role (optional) is used to semantically indicate the purpose of a button, such as whether it performs a destructive action or is a cancel button. This semantic information can be used by the system to adjust the button’s appearance accordingly and to provide appropriate accessibility features.

Note: If you don’t specify any role, default value is none

Examples


Simple Button

<body>
    <button>I am a button</button>
</body>

Screenshot


Button with custom view

<body>
    <vstack>
        <button>
            <vstack padding="" background="red" clipShape="capsule" foregroundColor="white">
                <image systemName="plus" font="largeTitle"/>
                <text>My custom button</text>
            </vstack>
        </button>
        <button padding="" background="red" clipShape="circle">
            <vstack foregroundColor="white">
                <image systemName="plus" font="largeTitle"/>
            </vstack>
        </button>
    </vstack>
</body>

Screenshot


Button with role

<body>
    <vstack>
        <button>Default Button</button>
        <button role="destructive">Destructive Button</button>
        <button role="cancel">Cancel Button</button>
        <text>Note: cancel role is in bold in alerts etc</text>
    </vstack>
</body>

Screenshot


Alert with cancel and destructive buttons

<body>
    <emptyview alert="isPresented:myAlert" onAppear="presentAlert:isPresented:myAlert">
	</emptyview>
    <alert id="myAlert" alertTitle="Warning" alertMessage="Do you want to delete all files?">
        <button role="destructive">Destructive</button>
        <button role="cancel">Cancel</button>
    </alert>
</body>

Screenshot


Button with buttonStyle

<body>
    <vstack>
        <button>Default Button</button>
        <button buttonStyle="plain">plain Button</button>
        <button buttonStyle="bordered">bordered Button</button>
        <button buttonStyle="borderless">borderless Button</button>
        <button buttonStyle="borderedProminent">borderedProminent Button</button>
    </vstack>
</body>

Screenshot


Confirmation dialog with positive, destructive and cancel buttons

<body>
    <vstack confirmationDialog="isPresented:myConfirmationDialog" onAppear="presentConfirmationDialog:isPresented:myConfirmationDialog"/>
    <alert id="myConfirmationDialog" alertTitle="Confirmation Dialog" alertMessage="Can you see positive, destructive and cancel buttons?">
        <button role="">Positive</button>
        <button role="destructive">Destructive</button>
        <button role="cancel">Cancel</button>
    </alert>
</body>

Screenshot