h function

Element h(
  1. String tag, {
  2. String? id,
  3. String? className,
  4. Map<String, dynamic>? attributes,
  5. Map<String, Function>? events,
  6. List? children,
  7. bool selfClosing = false,
})

Creates a generic virtual DOM element.

tag is the HTML tag name (e.g., 'div', 'custom-element').

  • id: The element's ID.
  • className: The CSS class(es).
  • attributes: Additional HTML attributes.
  • events: Event listeners (e.g., {'click': (e) {}}).
  • children: The child nodes (can be Node, String, or list of them).
  • selfClosing: Whether this tag is self-closing (void element).

Usually, you should use the specific tag helpers (like div, span) instead of calling this directly.

Implementation

Element h(
  String tag, {
  String? id,
  String? className,
  Map<String, dynamic>? attributes,
  Map<String, Function>? events,
  List<dynamic>?
  children, // Kept for internal flexibility, but helpers will enforce structure
  bool selfClosing = false,
}) {
  final attrs = <String, dynamic>{
    if (id != null) 'id': id,
    if (className != null) 'class': className,
    ...?attributes,
  };

  return Element(
    tag,
    attributes: attrs,
    events: events ?? const {},
    children: _normalizeChildren(children),
    selfClosing: selfClosing,
  );
}