SPB Git

Repository Public MIT

doc-glass-text-field.md 3.5 KB

# GlassTextField

Identifier glass-text-field
Module SPGlassForms
Kind content-component — a system TextField on an opaque themed surface with glassFieldOutline
Platforms iOS 26, iPadOS 26, macOS 26
Example Examples/Gallery/Forms/GlassTextFieldExample.swift

# Minimal usage

swift
@State private var name = ""

GlassTextField("Name", text: $name)

# Variants

Variant Use
prompt: Placeholder shown while the field is empty.
helper: Explanation under the field, replaced by the error when there is one.
validation: Rules checked in order: .required(), .minLength(_:), .maxLength(_:), .custom(message:_:).
validateOn: .afterEditing (default: once the field was left or submitted), .always, .never.
errorMessage: An error decided by the host (server answer). It replaces the validation message.
input: .standard, .email, .username, .url, .number, .name, or a custom GlassTextInputConfiguration.

# Parameters

Parameter Type Default Meaning
label LocalizedStringKey — Label above the field; also its accessibility label.
text Binding<String> — The value.
prompt LocalizedStringKey? nil Placeholder.
helper LocalizedStringKey? nil Helper text.
validation GlassFieldValidation .none Rules of the field.
validateOn GlassValidationTrigger .afterEditing When the message starts to show.
errorMessage String? nil External error.
input GlassTextInputConfiguration .standard Autocorrection, capitalisation, keyboard, content type.
focus FocusState<Bool>.Binding? nil Focus owned by the host.

GlassTextInputConfiguration holds autocorrectionDisabled, capitalization, keyboard and contentKind. Capitalisation and keyboard apply on iOS and iPadOS only.

# States

Idle, focused (outline in the theme's focusRing colour), error (outline in destructive, icon and message under the field), disabled (.disabled(_:), dimmed). A required field shows an asterisk next to its label and announces "Required".

# Advanced example

swift
@State private var email = ""
@State private var serverError: String?
@FocusState private var emailFocused: Bool

GlassTextField(
    "Email", text: $email, prompt: "name@example.com",
    validation: GlassFieldValidation(
        .required(),
        .custom(message: String(localized: "Enter a valid address")) { $0.contains("@") }
    ),
    errorMessage: serverError,
    input: .email,
    focus: $emailFocused
)
.submitLabel(.next)
.onSubmit { register() }

The rules are pure and can be checked before submitting: validation.isValid(email).

# Accessibility

  • The visible label is the accessibility label of the field; "Required" is appended for required fields.
  • An error is an icon plus a sentence, read as "Error: …"; it is never colour alone.
  • Label, value and messages follow Dynamic Type and wrap.
  • Default messages are localised (English, French); lengths count user-perceived characters.

# Limits

  • Single line; use GlassTextArea for multiline text.
  • Validation is synchronous. Asynchronous checks (availability of a name) go through errorMessage.
  • Length and custom rules accept an empty value; add .required() to refuse it.
  • On macOS, capitalisation and keyboard settings have no effect, and only some content types exist.
  • Focus changes and the keyboard were not exercised on a running device here.
Files main
Menu

Theme