mariposa 1.0.1

  • Readme
  • Changelog
  • Example
  • Installing
  • 33

mariposa #

Stupid-simple, low-level Web application library built with Incremental DOM and html_builder. Somewhat comparable to React.

Mariposa offers no state management; this is by design. Handling state should be handled via dart:async, Redux, or some other mechanism. Calling for a re-render is not memory-intensive, thanks to Incremental DOM.

Thanks to libraries like zen and html_builder/elements.dart, it is possible to cleanly build HTML AST's with a Dart DSL that looks exactly like an HTML AST.

Usage #

The most simple example:

import 'dart:html' hide Node;
import 'package:html_builder/elements.dart';
import 'package:html_builder/html_builder.dart';
import 'package:mariposa/dom.dart' as mariposa_dom;

Node greeting({String message}) => b(c: [text(message)]);

void main() {
    () => greeting(message: 'Hello!'),

This is a super-small example of an application without state. See web/main.dart for an example of an application with state. Complex applications should consider dart:async, Redux, Flux, or some similar state management architecture to create manageable applications.

Widgets #

Note: Not the same as Flutter widgets.

In real-world applications, oftentimes one will need to interact with the state of the DOM, or whatever tree is being rendered.

For this, create a Widget. Not only can widgets be rendered like normal nodes, but upon rendering, their afterRender method is called. Right before destruction, beforeDestroy is invoked.

Both methods are passed an AbstractElement, which prevents a platform-agnostic way to handle events, perform query selectors, and other things. Abstract elements always provide a handle to the nativeElement.

This makes it possible to manage the state of a specific node in the tree, among other things.

import 'dart:io';
import 'package:html_builder/html_builder.dart';
import 'package:html_builder/elements.dart';
import 'package:mariposa/dom.dart' as mariposa;

Passing down state and context #

Passing state down a stateless tree can become ugly very quickly. However, Mariposa provides a class called ContextAwareWidget, which can interact with a RenderContext, a scoped state handled internally by Mariposa.

The Context class has provisions for dependency injection, so you don't need any hacks to have fully independent, context-aware widgets:

class MyWidget extends ContextAwareWidget {
  Node contextAwareRender(RenderContext ctx) {
    // Return something...

Server-side Rendering #

It's easy; in fact, server-side rendering is Mariposa's bread-and-butter. DOM support was added long after string rendering:

import 'dart:io';
import 'package:html_builder/html_builder.dart';
import 'package:html_builder/elements.dart';
import 'package:mariposa/string.dart' as mariposa;

main() async {
  var server = await HttpServer.bind(InternetAddress.loopbackIPv4, 3000);
  print('Listening at http://${server.address.address}:${server.port}');

  await for (var request in server) {
    var html = mariposa.render(myApp());
      ..headers.contentType = ContentType.HTML

Node myApp() {
  return html(c: [
    head(c: [
        name: 'viewport',
        content: 'width=device-width, initial-scale=1',
      title(c: [
        text('Hello, Mariposa!'),
    body(c: [
      h1(c: [
        text('Hello, Mariposa!'),
      i(c: [
        text('Server-side rendering is easy!'),

1.0.1 #

  • Add RenderContext and ContextAwareWidget.

1.0.0+1 #

  • Dart 2 fixes


import 'dart:html' hide Node;
import 'package:html_builder/elements.dart';
import 'package:html_builder/html_builder.dart';
import 'package:mariposa/dom.dart' as mariposa_dom;

Node greeting({String message}) => b(c: [text(message)]);

void main() {
    () => greeting(message: 'Hello!'),

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:

  mariposa: ^1.0.1

2. Install it

You can install packages from the command line:

with pub:

$ pub get

with Flutter:

$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:

import 'package:mariposa/mariposa.dart';
Describes how popular the package is relative to other packages. [more]
Code health derived from static analysis. [more]
Reflects how tidy and up-to-date the package is. [more]
Weighted score of the above. [more]
Learn more about scoring.

We analyzed this package on Jan 25, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.0
  • pana: 0.13.4

Health suggestions

Fix lib/src/mariposa_dom.dart. (-9.99 points)

Analysis of lib/src/mariposa_dom.dart reported 21 hints, including:

line 33 col 54: Unnecessary new keyword.

line 69 col 36: DO use curly braces for all flow control structures.

line 84 col 15: Unnecessary new keyword.

line 103 col 7: DO use curly braces for all flow control structures.

line 105 col 7: DO use curly braces for all flow control structures.

Fix lib/src/mariposa_string.dart. (-6.31 points)

Analysis of lib/src/mariposa_string.dart reported 13 hints, including:

line 10 col 28: Unnecessary new keyword.

line 12 col 24: Unnecessary new keyword.

line 14 col 53: Unnecessary new keyword.

line 23 col 5: DO use curly braces for all flow control structures.

line 38 col 13: Unnecessary new keyword.

Fix lib/src/render_context.dart. (-1.49 points)

Analysis of lib/src/render_context.dart reported 3 hints:

line 13 col 34: Unnecessary new keyword.

line 34 col 11: Unnecessary new keyword.

line 40 col 11: Unnecessary new keyword.

Fix lib/src/incremental_dom.dart. (-1 points)

Analysis of lib/src/incremental_dom.dart reported 2 hints:

line 35 col 12: Avoid return types on setters.

line 38 col 12: Avoid return types on setters.

Fix lib/src/abstract_element.dart. (-0.50 points)

Analysis of lib/src/abstract_element.dart reported 1 hint:

line 25 col 3: Avoid return types on setters.

Maintenance suggestions

Package is getting outdated. (-58.36 points)

The package was last published 82 weeks ago.

The package description is too short. (-20 points)

Add more detail to the description field of pubspec.yaml. Use 60 to 180 characters to describe the package, what it does, and its target use case.


Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0-dev <3.0.0
html_builder ^1.0.0 1.0.5
js ^0.6.1 0.6.1+1
zen ^1.0.0 1.0.0
Transitive dependencies
args 1.5.2
charcode 1.1.2
collection 1.14.12
csslib 0.14.6+1 0.16.1
logging 0.11.4
meta 1.1.8
path 1.6.4
source_span 1.6.0
term_glyph 1.1.0
Dev dependencies
build_runner >=0.8.0 <=0.9.0
build_web_compilers ^0.4.0