NAME

Chandra::Element - DOM-like element construction for Chandra

SYNOPSIS

use Chandra::Element;

my $div = Chandra::Element->new({
    tag   => 'div',
    id    => 'app',
    class => 'container',
    style => { padding => '20px', background => '#fff' },
    children => [
        { tag => 'h1', data => 'Hello World' },
        {
            tag     => 'button',
            data    => 'Click Me',
            onclick => sub {
                my ($event, $app) = @_;
                print "Clicked!\n";
            },
        },
    ],
});

my $html = $div->render;

DESCRIPTION

Chandra::Element provides a Moonshine::Element-compatible API for building HTML element trees in Perl. Event handlers (onclick, onchange, etc.) are automatically compiled into JavaScript that communicates with Perl via the Chandra bridge.

METHODS

new(\%args)

Create a new element. Options:

tag - HTML tag name (default: 'div')
id - Element ID (auto-generated if not provided)
class - CSS class(es)
style - CSS styles as hashref or string
data - Text content
children - Arrayref of child elements (hashrefs or Element objects)
onclick, onchange, etc. - Event handler coderefs

add_child($child)

Add a child element. Accepts a hashref (auto-wrapped) or Element object.

children

my $children = $element->children;

Returns the arrayref of child elements.

render()

Render the element tree to an HTML string with event wiring.

get_element_by_id($id)

Find a descendant element by ID.

get_element_by_tag($tag)

Find the first descendant element with the given tag name.

get_elements_by_class($class)

Find all descendant elements with the given CSS class.

handlers()

Class method. Returns the global handler registry hashref.

get_handler($id)

Class method. Returns the handler coderef registered under $id.

clear_handlers()

Class method. Clears all registered handlers.

reset_ids()

Class method. Resets the auto-generated ID counter and clears all registered handlers. Useful in tests.

SEE ALSO

Chandra::App, Chandra::Bind, Chandra::Event