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, typed Style, style resolution, and theming.
PythonNative ships a single, fully-typed Style
TypedDict that enumerates every supported style property and constrains
enum-shaped values via typing.Literal. The TypedDict
gives editors and type-checkers (mypy, pyright) full autocomplete and
validation: a typo such as flex_direction="collumn" is now a static
error, not a silent runtime no-op.
Style values remain plain dicts at runtime so they are trivial to compose, diff, and store. Properties unrecognized by a platform handler are still ignored, so third-party handlers may extend the palette without modifying core types.
The runtime helpers (resolve_style,
StyleSheet) accept either a Style
TypedDict, a regular Dict[str, Any] (for forward-compat or
unrestricted use), or a list of either kind of dict, and always
return a fresh, flat dict.
Example
Classes:
| Name | Description |
|---|---|
EdgeInsets |
Per-edge spacing values used for |
ShadowOffset |
Shadow displacement in points. |
TransformRotate |
Rotation transform in degrees (numeric) or with explicit unit suffix. |
TransformScale |
Uniform scale transform. |
TransformScaleX |
Horizontal-only scale transform. |
TransformScaleY |
Vertical-only scale transform. |
TransformTranslate |
Translation transform along one or both axes. |
Style |
Statically-typed style dictionary. |
StyleSheet |
Utility for creating, composing, and flattening style dictionaries. |
Functions:
| Name | Description |
|---|---|
style |
Construct a |
resolve_style |
Flatten a |
Attributes:
| Name | Type | Description |
|---|---|---|
Color |
Color value: |
|
Dimension |
A length value. Numbers are points/dp; strings ending in |
|
EdgeValue |
Padding/margin value: a uniform number, a |
|
TransformEntry |
A single transform operation. |
|
TransformSpec |
|
|
StyleProp |
Public type for the |
|
ThemeContext |
Context
|
Default theme context populated with |
Color
module-attribute
¶
Color = str
Color value: "#RRGGBB", "#AARRGGBB", or any string a platform
handler recognizes (e.g., "red"). Stored verbatim and parsed by the
handler at apply-time, so palettes from third-party libraries pass through
unchanged.
Dimension
module-attribute
¶
A length value. Numbers are points/dp; strings ending in "%" are
parent-relative percentages.
EdgeValue
module-attribute
¶
EdgeValue = Union[Dimension, EdgeInsets]
Padding/margin value: a uniform number, a "%" string, or an
EdgeInsets dict.
TransformEntry
module-attribute
¶
TransformEntry = Union[TransformRotate, TransformScale, TransformScaleX, TransformScaleY, TransformTranslate, Dict[str, Any]]
A single transform operation.
TransformSpec
module-attribute
¶
TransformSpec = Union[TransformEntry, List[TransformEntry]]
transform style value: a single operation or an ordered list.
StyleProp
module-attribute
¶
Public type for the style parameter on every component factory.
Accepts a Style TypedDict (recommended), a
plain Dict[str, Any] (forward-compat / unrestricted), a list of
either with None entries skipped, or None.
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).
EdgeInsets
¶
Bases: TypedDict
Per-edge spacing values used for padding and margin.
Mirrors React Native's EdgeInsets shape with PythonNative's
convenience aliases. Any subset of keys may be supplied.
TransformRotate
¶
Style
¶
Bases: TypedDict
Statically-typed style dictionary.
Lists every style property recognized by the built-in components
plus their layout engine, with enum-shaped values constrained via
Literal. Style is a total=False TypedDict
so any subset of keys is valid at construction time.
Custom native components may accept additional, unlisted keys — they are ignored by the built-in handlers but flow through the reconciler unmodified, so third-party handlers can read them.
Example
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. |
absolute_fill |
Return a style that absolutely fills the parent. |
create
staticmethod
¶
Create a set of named styles from keyword arguments.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**named_styles
|
Style
|
Each keyword argument is a style name
mapping to a |
{}
|
Returns:
| Type | Description |
|---|---|
Dict[str, Style]
|
A dict mapping each name to a copy of the supplied style, |
Dict[str, Style]
|
so the caller can mutate the result without affecting the |
Dict[str, Style]
|
originals. |
compose
staticmethod
¶
Merge multiple style dicts.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*styles
|
StyleProp
|
Style dicts to merge. Later dicts override keys
from earlier ones. Falsy entries ( |
()
|
Returns:
| Type | Description |
|---|---|
Style
|
A new |
flatten
staticmethod
¶
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 and typed
as returning a Style.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
styles
|
StyleProp
|
A single dict, a list of dicts, or |
required |
Returns:
| Type | Description |
|---|---|
Style
|
A flat |
style
¶
Construct a Style from keyword arguments.
Equivalent to Style(...) but works with any Python version
(TypedDict.__init__ is fragile prior to 3.11) and reads more
naturally inside expressions:
Unknown keys are accepted at runtime to keep the door open for
third-party styling extensions; static type checkers will still
reject them when this helper's return type is annotated as
Style.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**properties
|
Any
|
Style key/value pairs. |
{}
|
Returns:
| Type | Description |
|---|---|
Style
|
A fresh |
resolve_style
¶
Flatten a style prop into a single dict.
Accepts None, a single dict (Style or untyped), 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 |
|---|---|---|---|
value
|
StyleProp
|
The raw value of the component's |
required |
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
A flat dict suitable for the native handler. Always a fresh |
Dict[str, Any]
|
dict, never the input. |
Next steps¶
- See worked examples in Styling.
- See the full per-component property catalogue in Component properties.