mariposa 1.1.0-alpha.3

mariposa #

Chat on Gitter Pub Build status License

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

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;

void main() {
  mariposa_dom.render(
    Greeting(message: 'Hello!'),
    querySelector('#app'),
  );
}

class Greeting extends Widget {
  final String message;

  Greeting({@required this.message});

  @override
  Node render() {
    return Text.b(message);
  }
}

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 (using package:angel_container ), so you don't need any hacks to have fully independent, context-aware widgets.

The usage of package:angel_container also means that you can use the same dependency injection found in version 2 of the Angel framework.

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

StatefulWidget #

Flutter users will immediately recognize this pattern, which is built on top of ContextAwareWidget:

import 'dart:async';
import 'package:html_builder/elements.dart';
import 'package:html_builder/html_builder.dart';
import 'package:mariposa/mariposa.dart';

class TodoList extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return new _TodoListState();
  }
}

class _TodoListState extends State<TodoList> {
  String message = 'Getting todos...';

  @override
  void initState() {
    super.initState();
    new Future.delayed(const Duration(seconds: 2)).then((_) {
      setState(() {
        message = 'Got todos!';
      });
    });
  }

  @override
  Node render() {
    return Div(
      children: [
        Heading.h1(
          child: TextNode('Hi!'),
        ),
        Text.italicized('Hi!'),
      ],
    );
  }
}

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/mariposa.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(),
        createRenderer: () =>
            new StringRenderer(whitespace: '', pretty: false));
    request.response
      ..headers.set('content-type', 'text/html')
      ..write(html)
      ..close();
  }
}

class MyApp extends Widget {
  @override
  Node render() {
    return Html(
      lang: 'en',
      children: [
        Head(children: [
          Meta(
            name: 'viewport',
            content: 'width=device-width, initial-scale=1',
          ),
          Title(
            child: TextNode('Hello, Mariposa!'),
          ),
        ]),
        Body(
          children: [
            Heading.h1(
              child: TextNode('Hello, Mariposa!'),
            ),
            Text.italicized('Server-side rendering is easy!'),
          ],
        ),
      ],
    );
  }
}

1.1.0-alpha.3 #

  • Added auto-generated Style class.
  • Added HtmlWidget base class, which is the basis of widgets like Div, Button, etc.
  • Corrected StatefulWidget infrastructure.
  • Add a unique stamp ID to each created node - this will be useful for re-hydrating SSR output.
  • Rendering now takes a Node, rather than a Node Function().

1.1.0-alpha.2 #

  • Fixed a bug null return nodes crashed DOM applications.

1.1.0-alpha.1 #

  • Fixed a bug where Lists were being joined with a comma; now they are joined by a single space.

1.1.0-alpha #

  • Added StatefulWidget and State.
  • package:mariposa/mariposa.dart now exports package:html_builder.
  • Use package:angel_container in RenderContext.
  • Depend on package:meta.

1.0.1 #

  • Add RenderContext and ContextAwareWidget.

1.0.0+1 #

  • Dart 2 fixes

example/main.dart

import 'dart:html' show querySelector;
import 'package:html_builder/elements.dart';
import 'package:html_builder/html_builder.dart';
import 'package:mariposa/dom.dart' as mariposa_dom;
import 'package:mariposa/mariposa.dart';
import 'package:meta/meta.dart';

void main() {
  mariposa_dom.render(
    Greeting(message: 'Hello!'),
    querySelector('#app'),
  );
}

class Greeting extends Widget {
  final String message;

  Greeting({@required this.message});

  @override
  Node render() {
    return Text.bold(message);
  }
}

Use this package as a library

1. Depend on it

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


dependencies:
  mariposa: ^1.1.0-alpha.3

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';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
0
Health:
Code health derived from static analysis. [more]
75
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
54
Overall:
Weighted score of the above. [more]
33
Learn more about scoring.

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

  • Dart: 2.7.1
  • pana: 0.13.4

Health suggestions

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

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

line 21 col 25: Use = to separate a named parameter from its default value.

line 38 col 13: Unnecessary new keyword.

line 114 col 9: DO use curly braces for all flow control structures.

line 133 col 17: Unnecessary new keyword.

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

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

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

line 13 col 24: Use = to separate a named parameter from its default value.

line 14 col 28: Unnecessary new keyword.

line 16 col 24: Unnecessary new keyword.

line 18 col 7: Unnecessary new keyword.

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

Fix lib/src/elements/text.dart. (-6.31 points)

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

line 14 col 14: Unnecessary new keyword.

line 22 col 12: Unnecessary new keyword.

line 36 col 12: Unnecessary new keyword.

line 50 col 12: Unnecessary new keyword.

line 64 col 12: Unnecessary new keyword.

Fix additional 8 files with analysis or formatting issues. (-7.49 points)

Additional issues in the following files:

  • lib/src/render_context_impl.dart (3 hints)
  • lib/src/elements/html_element.dart (2 hints)
  • lib/src/elements/interactive.dart (2 hints)
  • lib/src/incremental_dom.dart (2 hints)
  • lib/src/render_context.dart (2 hints)
  • lib/src/stateful_widget.dart (2 hints)
  • lib/src/abstract_element.dart (1 hint)
  • lib/src/elements/style.dart (1 hint)

Maintenance suggestions

Package is getting outdated. (-41.37 points)

The package was last published 73 weeks ago.

Package is pre-release. (-5 points)

Pre-release versions should be used with caution; their API can change in breaking ways.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0-dev <3.0.0
angel_container ^1.0.0-alpha 1.1.0
html_builder ^1.0.0 1.0.5
js ^0.6.1 0.6.1+1
meta ^1.0.0 1.1.8
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
matcher 0.12.6
quiver 2.1.2+1
source_span 1.6.0
stack_trace 1.9.3
term_glyph 1.1.0
Dev dependencies
build_runner ^0.10.0
build_web_compilers ^0.4.0
code_builder ^3.0.0
dart_style ^1.0.0
html ^0.13.0
mariposa_test ^1.0.0
path ^1.0.0 1.6.4
recase ^2.0.0