Giter Site home page Giter Site logo

lookback / meteor-bodyclass Goto Github PK

View Code? Open in Web Editor NEW
12.0 15.0 2.0 24 KB

Easily scope CSS by automatically adding the current template and layout names as classes on the body element.

Home Page: https://atmospherejs.com/lookback/body-class

License: MIT License

JavaScript 46.98% CoffeeScript 48.61% HTML 4.41%

meteor-bodyclass's Introduction

Lookback Body Classes for Meteor

Circle CI

This package automatically adds classes on the body element (default) for the names of the current template and layout used. It also reactively updates a class name returned from a function.

Having classes for templates and layouts are great (if not vital) for scoping with CSS and similar.

Install

lookback:body-class is available on Atmosphere:

meteor add lookback:body-class

Usage

The package exports the BodyClass namespace on the client.

The add method adds a class to the body:

// Adds a string, string array or result from a function to the body element.

BodyClass.add('some-class')
// => body.some-class
BodyClass.add(['some-class', 'other-class'])
// => body.some-class.other-class
Session.setDefault('state', 'foo');

BodyClass.add(function() {
  return Session.get('state');
});
// => body.foo

Session.set('state', 'bar');
// => body.bar

Iron Router

The run method adds the names of the current template and/or layout to the body, and cleanup removes them. Goes well with Iron Router's onBeforeAction and onStop hooks:

Router.route('home', {
  path: '/home'
});

Router.route('foo', {
  path: '/foo',
  layoutTemplate: 'DefaultTemplate'
});

Router.onBeforeAction(function() {
  BodyClass.run();
});

Router.onStop(function() {
  BodyClass.cleanup();
});

Router.go('home');
// => body.home

Router.go('foo');
// => body.defaulttemplate.foo

This behavior is available as a nifty one-line-init Iron Router plugin:

Router.plugin('bodyClasses');

Flow Router

Triggers are automatically added for you, with:

FlowRouter.triggers.enter([() => BodyClass.run()]);
FlowRouter.triggers.exit([() => BodyClass.cleanup()]);

For Flow Router, we use the route name and potentially the layout name for a template. Like this:

FlowRouter.route({
  name: 'something',
  layout: 'MyLayout'
});

As of Flow Router 2.8.0, route groups also can specify options, and thus you can use the layout property there as well.

FlowRouter.group({
  name: 'webapp',
  layout: 'WebAppLayout'
});

Note that template and layout names are added in lower case.

Configure

It's possible to configure the package with the following options:

BodyClass.config({
  element: 'body' // What element to manipulate, defaults to 'body'
  classes: []     // What classes that *always* should be added
});

Tests

This package is tested with the sanjo:jasmine package, bundled in a separate Meteor app in the test-app dir. To run tests:

cd test-app
meteor --test

Version history

  • 0.4.1
    • If current route has no layout, get group's, then try parent's, then default layout
  • 0.4.0
    • Add support for Flow Router.
    • Needs Meteor 1.2.0.2.
  • 0.1.0 - Initial publish.

Contributions

Contributions are welcome. Please open issues and/or file Pull Requests.

License

MIT.


Made by Lookback.

meteor-bodyclass's People

Contributors

francisbyrne avatar johanbrook avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

meteor-bodyclass's Issues

BodyClass.run() in Router.onBeforeAction causes hooks to run twice

This is global hooks and current route's hooks. It's happening because BodyClass.run() is a reactive data source, and it's changing. I would prefer for performance for hooks to run once, and it seems to continue to work when the hooks don't run again, when I do:

Router.onBeforeAction ->
  Tracker.nonreactive ->
    BodyClass.run()

I'd recommend either not changing the dep or adding a Tracker.nonreactive to your example in the README.

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.