Style¶
PythonNative styles are plain Python dicts; an element's style prop
may be a single dict or a list of dicts (later entries win on key
collision). StyleSheet is a small helper
for declaring named styles and composing them.
StyleSheet, style resolution, and theming support.
Provides:
- A
StyleSheethelper for creating and composing reusable style dictionaries. - A
resolve_styleutility for flattening thestyleprop accepted by every component factory. - A pair of light and dark default themes plus a
ThemeContextfor distributing a theme dict across a subtree.
Style values are plain Python dicts so they are trivial to compose, diff, and store. Properties unrecognized by the platform handler are ignored.
Example
Classes:
| Name | Description |
|---|---|
StyleSheet |
Utility for creating, composing, and flattening style dictionaries. |
Functions:
| Name | Description |
|---|---|
resolve_style |
Flatten a |
Attributes:
| Name | Type | Description |
|---|---|---|
ThemeContext |
Context
|
Default theme context populated with |
ThemeContext
module-attribute
¶
ThemeContext: Context = create_context(DEFAULT_LIGHT_THEME)
Default theme context populated with DEFAULT_LIGHT_THEME.
Wrap a subtree in
Provider(ThemeContext, my_theme, ...) to
override the theme for that subtree, then read it inside descendants
via use_context(ThemeContext).
StyleSheet
¶
Utility for creating, composing, and flattening style dictionaries.
All methods are stateless and return fresh dicts, so the values can be reused safely across components.
Methods:
| Name | Description |
|---|---|
create |
Create a set of named styles from keyword arguments. |
compose |
Merge multiple style dicts. |
flatten |
Flatten a style value or list of styles into a single dict. |
create
staticmethod
¶
Create a set of named styles from keyword arguments.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**named_styles
|
_StyleDict
|
Each keyword argument is a style name mapping to a dict of property values. |
{}
|
Returns:
| Type | Description |
|---|---|
Dict[str, _StyleDict]
|
A dict mapping each name to a copy of the supplied dict, so |
Dict[str, _StyleDict]
|
the caller can mutate the result without affecting the |
Dict[str, _StyleDict]
|
originals. |
compose
staticmethod
¶
Merge multiple style dicts.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*styles
|
_StyleDict
|
Style dicts to merge. Later dicts override keys from earlier ones. |
()
|
Returns:
| Type | Description |
|---|---|
_StyleDict
|
A new dict containing the merged result. Falsy entries |
_StyleDict
|
(e.g., |
flatten
staticmethod
¶
flatten(styles: Any) -> _StyleDict
Flatten a style value or list of styles into a single dict.
Equivalent to
resolve_style but exposed
on StyleSheet for parity with React Native's API.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
styles
|
Any
|
A single dict, a list of dicts, or |
required |
Returns:
| Type | Description |
|---|---|
_StyleDict
|
A flat dict combining the inputs. |
resolve_style
¶
Flatten a style prop into a single dict.
Accepts None, a single dict, or a list of dicts (later entries
override earlier ones, mirroring React Native's array-style
pattern). Used by every built-in element factory in
pythonnative.components.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
style
|
StyleValue
|
The raw value of the component's |
required |
Returns:
| Type | Description |
|---|---|
_StyleDict
|
A flat dict suitable for the native handler. Always a fresh |
_StyleDict
|
dict, never the input. |
Next steps¶
- See worked examples in Styling.
- See the full per-component property catalogue in Component properties.