Git Product home page Git Product logo

snabbdom-typestyle's Introduction

Snabbdom TypeStyle

npm CircleCI Code Climate Code Climate license

Maintainable, scalable, and elegant styling with Snabbdom + TypeStyle

  • All the power and benefits of TypeStyle
  • Internal handling of classname mapping
  • Server-side rendering support

Installation

npm install snabbdom-typestyle

Usage

Simply pass css to your Snabbdom virtual node!

  import { Style } from 'snabbdom-typestyle';

  function view() {

      const buttonStyle: Style = {
          color: 'blue'
      };

      return (
          <button css={ buttonStyle }>
              My Button
          </button>
      );
  }

The CssModule is essentially a wrapper around TypeStyle style and can be passed either a single NestedCssProperties or an array of NestedCssProperties (or Style, which is an alias provided by snabbdom-typestyle).

Make sure to pass the CssModule before the ClassModule when initializing Snabbdom.

  import { init } from 'snabbdom';
  import CssModule from 'snabbdom-typestyle';
  import ClassModule from 'snabbdom/modules/class';

  const modules = [
    CssModule,
    ClassModule
  ];

  const patch = init(modules);

Or, if you are using Cycle.js pass modules in the options of makeDOMdriver.

run(main, { DOM: makeDOMDriver('#root', { modules }) });

For examples, take a look at this fork of the Cycle.js Todo-MVC implementation which uses snabbdom-typestyle.

Server-side Rendering

To use snabbdom-typestyle in a server-side rendered environment, initialize Snabbdom with the serverSideCssModule.

import { serverSideCssModule, collectStyles } from 'snabbdom-typestyle';
import modulesForHTML from 'snabbdom-to-html/modules';
import { h } from 'snabbdom';

const modules = [
  serverSideCssModule,
  modulesForHTML.class
];

const patch = init(modules);

When you are rendering your html, you can grab the styles via collectStyles(node: VNode): String.

h('style#styles', collectStyles(vtree));

Then, on the client-side, pass a selector for the style element rendered by the server to makeClientSideCssModule(styleElementSelector: string | undefined).

Doing this avoids duplication of the style element when the application is hydrated.

import { makeClientSideCssModule } from 'snabbdom-typestyle';
import ClassModule from 'snabbdom/modules/class';

const modules = [
  makeClientSideCssModule('#styles'),
  ClassModule
];

Take a look at the Cycle.js example here

License

MIT

snabbdom-typestyle's People

Contributors

sliptype avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

snabbdom-typestyle's Issues

Add example TodoMVC

Summary

It would be nice to have a version of TodoMVC that uses snabbdom-typestyle

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.