duck.html.components.styleยถ
Style HTML Component.
This module defines a reusable Style component for embedding CSS styles within an HTML document.
Module Contentsยถ
Classesยถ
Style HTML Component. |
APIยถ
- class duck.html.components.style.Style(element: Optional[str] = None, properties: Optional[Dict[str, str]] = None, props: Optional[Dict[str, str]] = None, style: Optional[Dict[str, str]] = None, inner_html: Optional[Union[str, str, float]] = None, children: Optional[List[duck.html.components.HtmlComponent]] = None, **kwargs)[source]ยถ
Bases:
duck.html.components.InnerComponentStyle HTML Component.
The
Stylecomponent allows developers to define and embed custom CSS styles directly within an HTML page. It can be used to dynamically style elements without needing an external stylesheet.Features:
Supports inline CSS.
Can be dynamically added to any component.
Enables styling customization for other components.
Example Usage:
style = Style( inner_html=''' .custom-popup { background-color: rgba(0, 0, 0, 0.8); color: white; padding: 10px; border-radius: 5px; } ''' ) component.add_child(style)This will generate the following HTML output:
<style> .custom-popup { background-color: rgba(0, 0, 0, 0.8); color: white; padding: 10px; border-radius: 5px; } </style>Notes:
Automatic Nonce Addition: When
ENABLE_HEADERS_SECURITY_POLICY=Trueandcsp_nonce_flagis set inCSP_TRUSTED_SOURCES, thenonceproperty is automatically added.Request Resolution Required: For the
nonceto be set automatically, the request must be resolved. This is achieved by callingget_request_or_raiseon the component root (or the current component if the root isNone).The
inner_htmlparameter must contain valid CSS code.This component is intended for inline styles and does not support linking to external CSS files.
Styles defined within this component will apply globally unless scoped using class or ID selectors.
Initialization
Initialize an HTML component.
- Parameters:
element โ The HTML element tag name (e.g., textarea, input, button). Can be None, but make sure element is returned by get_element method.
accept_inner_html โ Whether the HTML component accepts an inner body (e.g., inner-body-here).
inner_html โ Inner html to add to the HTML component. Defaults to None.
properties โ Dictionary for properties to initialize the component with.
props โ Just same as properties argument (added for simplicity).
style โ Dictionary for style to initialize the component with.
**kwargs โ Extra keyword arguments
- Raises:
HtmlComponentError โ If โelementโ is not a string or โinner_htmlโ is set but โaccept_inner_htmlโ is False.
- property propertiesยถ