VStack

The VStack component in the Scripting app is a layout view that arranges its child views vertically. It provides flexible options for aligning its subviews and controlling the spacing between them.


VStack Component

Type Declaration

1declare const VStack: FunctionComponent<VStackProps>

Description

The VStack component arranges its child views in a vertical line, making it ideal for creating vertically stacked layouts. You can customize the alignment of subviews and the spacing between them to suit your design needs.


Properties

alignment (Optional)

  • Type: HorizontalAlignment
  • Default: "center"
  • Description: Determines the horizontal alignment of the subviews within the stack. The alignment specifies how views are positioned relative to each other horizontally when placed vertically in the VStack.
  • Accepted Values:
    • "leading": Aligns views to the left.
    • "center": Centers views horizontally.
    • "trailing": Aligns views to the right.

Example

1<VStack alignment="leading">
2  <Text>Leading Aligned</Text>
3  <Text>Another Item</Text>
4</VStack>

spacing (Optional)

  • Type: number | undefined
  • Default: Automatically calculated based on the child views if not specified.
  • Description: Sets the distance in pixels between adjacent subviews. Use undefined to let the stack automatically determine the optimal spacing.

Example

1<VStack spacing={10}>
2  <Text>Item 1</Text>
3  <Text>Item 2</Text>
4</VStack>

children (Optional)

  • Type:
    1(VirtualNode | undefined | null | (VirtualNode | undefined | null)[])[] | VirtualNode | undefined
  • Description: The child elements to display within the stack. You can pass individual elements, arrays of elements, or undefined/null values. Nullish values are ignored, allowing for dynamic layouts.

Example

1<VStack>
2  <Text>First Item</Text>
3  <Image systemName="star" />
4</VStack>

HorizontalAlignment Type

Horizontal alignment guides control how views are positioned relative to each other when placed vertically in a VStack.

Type Declaration

1type HorizontalAlignment = 'leading' | 'center' | 'trailing'

Alignment Options

  • leading: Aligns all subviews to the left edge of the stack.
  • center: Centers all subviews horizontally.
  • trailing: Aligns all subviews to the right edge of the stack.

Visual Guide

Below is an illustration of the three alignment options:

Horizontal Alignment


Usage Example

1<VStack alignment="leading" spacing={10}>
2  <Image systemName="globe" />
3  <Text>Leading Aligned Item</Text>
4  <Text>Another Item</Text>
5</VStack>

Explanation

  1. alignment="leading": Aligns all subviews to the left.
  2. spacing={10}: Adds 10 pixels of space between each subview.
  3. Contains two child views:
    • An Image view displaying a system icon.
    • Two Text views displaying labeled items.

Best Practices

  1. Use alignment to control horizontal positioning when stacking text and icons for better visual consistency.
  2. Leverage spacing to create breathable and aesthetically pleasing layouts.
  3. Pass dynamic or conditional children without worrying about null or undefined values.

This documentation ensures you can confidently use the VStack component to create clean, vertically stacked layouts in your Scripting app projects.