render method

Element render()

Creates an HTML Element represention of Component.

Implementation

Element render() {
  // Create the element with the tag name.
  element ??= document.createElement(tag) as HTMLElement;

  if (element == null) {
    throw Exception("Could not create element with tag name: $tag");
  }

  if (id != null) {
    element?.id = id!;
  }

  if (className != null) {
    element?.className = className!;
  }

  if (children != null) {
    for (final child in children!) {
      element!.append(child.render());
    }
  }

  if (style != null) {
    final styleString = style!.entries
        .map((entry) => "${entry.key}: ${entry.value};")
        .join(" ");

    element!.setAttribute('style', styleString);
  }

  _renderAttributes(element!);

  _registerEventListeners(element!);

  onStackChange.add(("add", this));
  stack.add(this);

  return element!;
}