For complex component logic, you can use a Python front matter block (delimited by ---) at the start of the component file. This block allows you to define types, defaults, and custom logic using the props object (an instance of slippers.props.Props).
1. Type Checking with props.types
Define a dictionary mapping prop names to Python types. If a passed prop does not match the type, a PropError is raised. The typing module is automatically available in the front matter.
2. Default Values with props.defaults
Define a dictionary of default values. If a prop is passed as None or omitted, it will fall back to these values.
3. Prop Mapping and Logic
You can manipulate the props object directly using props['name'] = value to create new variables or modify existing ones. Since the front matter is standard Python, you can also perform imports.
---
props.types = {
'required_string': str,
'optional_number': Optional[int],
}
props.defaults = {
'default_number': 10,
}
props['new_number'] = props['default_number'] * 2
---
{{ required_string }}
{{ new_number }}
---
props.types = {
'required_string': str,
'optional_number': Optional[int],
'default_number': int,
}
props.defaults = {
'default_number': 10,
}
props['new_number'] = props['default_number'] * 2
---
Required string: {{ required_string }}
Optional number: {{ optional_number }}
Default number: {{ default_number }}
New number: {{ new_number }}