Skip to main content
.environment(key: string, value: any)
key
string
required
The environment key to set. Standard keys include "colorScheme", "dynamicTypeSize", and other system-defined values.
value
any
required
The value to set for the given key. The type depends on the key being set.

Support

Usage

Set color scheme via environment

VStack([
    Text("Dark Mode Content"),
    Button("Action", () => doSomething())
])
    .environment("colorScheme", "dark")

Pass custom environment values

VStack([
    ChildComponent()
])
    .environment("accentColor", "purple")

Notes

Environment values propagate down the component tree to all descendants. Child components can read these values using the useEnvironment hook. This is the general-purpose mechanism behind more specific modifiers like colorScheme and dynamicTypeSize.

See Also