abstract class
Components::Component
- Components::Component
- Reference
- Object
Overview
Base class for all components Components are reusable, composable units built from HTML elements
Direct Known Subclasses
Defined in:
components/base/component.crConstructors
Macro Summary
-
component_css(css_string)
Macro for registering component-level CSS.
Instance Method Summary
-
#<<(child : Component | Elements::HTMLElement | String | Elements::RawHTML) : self
Add a child to the component
-
#[](name : String) : String | Nil
Get an attribute value
-
#[]=(name : String, value : String) : String
Set an attribute value
-
#add_children(*children : Component | Elements::HTMLElement | String | Elements::RawHTML) : self
Add multiple children
-
#attributes : Hash(String, String)
Component attributes (props)
-
#build(&block : self -> Nil) : self
Build content using a block
-
#children : Array(Component | Elements::HTMLElement | String | Elements::RawHTML)
Component children
-
#component_id : String
Unique identifier for this component instance
-
#render : SafeHTML
THE OUTPUT CONTRACT (docs/SAFE_HTML_V1.md).
-
#render_content : String
Abstract method to be implemented by subclasses
- #render_safe_content : SafeHTML
-
#to_raw_html : Elements::RawHTML
Render the component as raw HTML (for adding to elements)
-
#to_s(io : IO) : Nil
Convert to string (alias for render)
Constructor Detail
Macro Detail
Macro for registering component-level CSS. CSS is stored as a class variable and registered with the global ComponentCSSRegistry when the class is first loaded.
Usage: class ButtonComponent < Components::StatelessComponent component_css <<-CSS .btn { display: inline-flex; align-items: center; } .btn-primary { background-color: var(--color-blue-500); } CSS end
Instance Method Detail
Add a child to the component
Add multiple children
Component children
THE OUTPUT CONTRACT (docs/SAFE_HTML_V1.md). Every Component renders
to SafeHTML, not String — this is the type-level boundary that
makes "a component that hand-built unsafe markup and forgot to escape
a sink" a thing the compiler catches at the response-sink type, rather
than a thing a human has to notice at review time.
This delegates to #render_safe_content, which subclasses may override
directly (the preferred, migrated path — see DataTableComponent for
the exemplar). The default implementation bridges the legacy
render_content : String path through the loud, greppable
SafeHTML.unsafe escape hatch, so every existing hand-built component
keeps compiling and running unchanged — but its output is now
explicitly, auditably marked "not yet vouched for by the safe DSL."
grep -rn 'reason: "legacy component' src/ finds every component
still on this bridge; that grep result is the migration progress list.
Override this in a migrated component to build SafeHTML directly via
the Elements/tag DSL (escaped by construction) instead of hand-built
String.build. See docs/SAFE_HTML_V1.md "migration guide".