toHtml method

String toHtml(
  1. String targetId
)

Generates the final

Implementation

String toHtml(String targetId) {
  final handlers = <String, String>{};

  if (onClick != null) {
    handlers['click'] = onClick!.toJs();
  } else if (customJs != null) {
    handlers['click'] = customJs!;
  }

  if (onLoad != null) {
    handlers['load'] = onLoad!.toJs();
  }

  final eventScripts = handlers.entries.map((entry) {
    return "document.querySelector('#$targetId')?.addEventListener('${entry.key}', () => { ${entry.value.trim()} });";
  }).join('\n');

  return '''
<script>
document.addEventListener('DOMContentLoaded', () => {
$eventScripts
});
</script>
''';
}