Giter Site home page Giter Site logo

buschtoens / ember-css-modules-active-route Goto Github PK

View Code? Open in Web Editor NEW
9.0 3.0 0.0 307 KB

`:app-root` & `:document-root` selectors to apply styles to the root element, when a route is active.

License: Other

JavaScript 30.76% TypeScript 57.26% HTML 10.19% CSS 1.78%
ember emberjs ember-addon ember-css-modules ember-routing

ember-css-modules-active-route's Introduction

ember-css-modules-active-route

Build Status npm version Download Total Ember Observer Score code style: prettier dependencies devDependencies

:app-root and :document-root selectors to apply styles to the root element, when a route is active.

Installation

ember install ember-css-modules-active-route ember-css-modules

This is a plugin for ember-css-modules, so you need to have it installed as well.

Usage

Example

Router.map(function() {
  this.route('foo', function() {
    this.route('bar');
  });
  this.route('qux');
});
/* app/foo/styles.css */

:app-root {
  background: green;
}
/* app/foo/index/styles.css */

:app-root {
  background: red;
}

When the user enters the foo route, the :app-root pseudo-selector will be applied to the app's rootElement (<body>). The background of the page will be red, as foo.index overrides foo.

When the user navigates to foo.bar, the background will turn green, as the user has left the foo.index route and the override no longer takes effect.

When the user navigates to qux, the background will become transparent again, as no route styles are active any more.

Combining Selectors

You can also combine the :app-root & :document-root selectors with other regular selectors. For instance, instead of just using :document-root, which targets the :root element (<html>), you can target child elements instead:

:document-root :global(.some-cookie-banner) {
  display: none;
}

In this example, <div class="some-cookie-banner"> is inserted by the backend and would be hidden, while the user is on a certain route.

Specificity

CSS Specificity can be a tricky thing. This addon tries to make everything work out of the box. For every level of nesting, the selector specificity will be increased automatically. This way, overrides in child routes actually override declarations in parent routes, without the source order being relevant.

You can also manually set the specificity, like so:

:app-root(3) {
  background: red;
}

:app-root(2) {
  background: green;
}

The background will be red, as 3 is a higher specificity than 2.

How does it work?

The :app-root / :app-root(n) and :document-root / :document-root(n) selectors are replaced with "magic" class name selectors by lib/route-plugin.js. These selectors are repeated n times to raise them to the necessary specificity level.

n can either be specified explicitly as :app-root(n), or when used as :app-root will be derived from the depth of route nesting by counting the / in the file path of the respective styles.css. This ensures that rules in nested child routes override rules from their parent routes.

:app-root {
  background: yellow;
}
:app-root(3) {
  background: green;
}
:app-document(1) body {
  background: blue;
}

/* becomes */

/* Class name may be repeated more often depending on level of nesting. */
.css-modules-active-route-app.css-modules-active-route-app {
  background: yellow;
}

/* With an explicit `n` provided, the class name is repeated that many times. */
.css-modules-active-route-app.css-modules-active-route-app.css-modules-active-route-app {
  background: green;
}

/* Combining with other selectors is possible. */
.css-modules-active-route-document body {
  background: blue;
}

When the a transition is started or finished the CSSModulesActiveRouteService resolves the styles for the current route hierarchy via the Ember container using the route name. This implies, that you cannot render custom / non-default templates for routes, which is deprecated anyway.

ember-css-modules-active-route's People

Contributors

buschtoens avatar dependabot-preview[bot] avatar ember-tomster avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

ember-css-modules-active-route's Issues

FOUC during route transitions with loading sub-state

If a transition cannot be finished synchronously, because one of the route hools returns a Promise, users may encounter a FOUC.

I need to investigate how we can prevent this.

This probably also means, that applying styles for the loading sub-state route currently does not work, as it is not an explicit transition target.

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.