OneScript
UI

Overview

General-purpose modifiers for OneScript views.

Every view accepts a modifier prop. Import Modifier from onescript/views and build it by chaining calls, as in SwiftUI. Modifiers apply in call order.

This section covers modifiers that work without a specific parent layout. Layout-specific modifiers are documented with their corresponding layout.

Numeric dimensions in this section use points (pt).

import {
  Color, Font, Modifier, Shape, Text,
} from "onescript/views"

<Text
  modifier={Modifier
    .font(Font.body)
    .padding(12)
    .background(Color.systemGray6)
    .clipShape(Shape.rect(8))}
>
  Content
</Text>

Order

Each modifier applies to the result of the calls before it, so the same calls in a different order produce a different result:

import { Color, Modifier, Shape, Text, VStack } from "onescript/views"

<VStack spacing={12}>
  {/* The background includes the padding. */}
  <Text modifier={Modifier
    .padding(12)
    .background(Color.systemGray6)}>
    Padding inside
  </Text>
  {/* The padding is outside the background. */}
  <Text modifier={Modifier
    .background(Color.systemGray6)
    .padding(12)}>
    Padding outside
  </Text>
  {/* Clip after the background to round it. */}
  <Text modifier={Modifier
    .padding(12)
    .background(Color.systemGray6)
    .clipShape(Shape.rect(8))}>
    Rounded
  </Text>
</VStack>

Combining modifiers

Modifier is immutable: every call returns a new chain, and Modifier itself is the empty chain. Use append(other) to add a modifier received from a parent, or to add one conditionally. It is named append rather than then because objects with a then method are treated as promises by await.

import { Color, Font, Modifier, Shape, Text } from "onescript/views"

function Badge(props: { label: string, selected: boolean, modifier?: Modifier }) {
  return (
    <Text
      modifier={Modifier
        .font(Font.caption)
        .padding({ horizontal: 8, vertical: 4 })
        .append(props.selected ? Modifier.background(Color.blue) : Modifier)
        .clipShape(Shape.capsule)
        .append(props.modifier ?? Modifier)}
    >
      {props.label}
    </Text>
  )
}

modifier only accepts a Modifier from onescript/views; any other value throws a TypeError.

Layout and sizing

ModifierPurpose
paddingAdd space around content.
frameApply fixed or flexible dimensions.

Appearance and clipping

ModifierPurpose
backgroundDraw a shape style behind the current result.
clipShapeClip with a rectangle, circle, or capsule.
clippedClip drawing to the frame boundary.

Text and environment

ModifierPurpose
fontSet an inheritable font.
fontWeightSet an inheritable font weight.
foregroundStyleSet an inheritable foreground shape style.
italicApply italic styling.
lineLimitLimit how many lines text can use.
disabledDisable controls in the view hierarchy.
controlSizeSet the preferred size of controls.
buttonStyleSet the native style for descendant Buttons.

Lists and controls

ModifierPurpose
toggleStyleSet the native style for descendant Toggles.
pickerStyleSet the native style for descendant Pickers.
labelStyleShow the title, icon, or both of descendant Labels.
progressViewStyleSet the native style for descendant ProgressViews.
datePickerStyleSet the native style for descendant DatePickers.
labelsHiddenHide the labels of controls.
tagMark the value a Picker option or List row represents.
listStyleSet the native style for Lists.
formStyleSet the native style for Forms.

Scaling

ModifierPurpose
scaledToFitScale a view to fit its available space while preserving its aspect ratio.
scaledToFillScale a view to fill its available space while preserving its aspect ratio.
ModifierPurpose
toolbarPlace items in the navigation bar or bottom bar.
navigationTitleSet the page title.
navigationBarTitleDisplayModeSet how the title is displayed.

Presentation

ModifierPurpose
sheetSlide a card up from the bottom.
fullScreenCoverCover the whole screen.
popoverShow a bubble next to a view.
alertShow an alert.
confirmationDialogShow a set of actions to choose from.

How is this guide?

Last updated on

On this page