Visibility Utils

Introduction

NPM version Dependency Status

A collection of dynamically generated css visibility related utility classes.

Class names follow the Emmet abbreviation syntax, with colons (':') replaced by two dashes (--) to follow BEM naming conventions. E.g., the visibility: hidden utility class name is .u-v--h and the backface-visibility: visible one is .u-bfv--v. In the same vein, the opacity: 0.5 class will be .u-o--0\.5 (It is automatically escaped).

Available classes

  • .u-v--h (visibility: hidden)
  • .u-v--v (visibility: visible)
  • .u-v--inher (visibility: inherit)
  • .u-c--init (visibility: initial)
  • .u-v--un (visibility: unset)

  • .u-bfv--h (backface-visibility: hidden)

  • .u-bfv--v (backface-visibility: visible)

  • .u-o--<level> (opacity: )

Additionally, JigSass Visibility provides the following stateful helpers:

  • .u-is-hidden: Hides elements visually and from screen readers.
  • .u-is-visually-hidden: Hides elements visually but leaves them accesible for screen readers.
  • .u-is-visually-hidden.u-is-focusable: Reveal focusable visually hidden elements on focus (with keyboard navigation or js).

Installation

Using npm:

npm i -S jigsass-utils-visibility

Usage

Import JigSass Utils Visibility into your main scss file near its very end, together with all other utilities (utilities should always be the last to be imported).

@import 'path/to/jigsass-utils-visibility/scss/index';

Like all other JigSass Utils, JigSass Visibility does not automatically generate any CSS when imported. You would need to explicitly indicate that each individual visibility class should actually be generated in each component or object it is used in (clarification: This will include style declarations inside .foo and .bar):

// _c.foo.scss
.foo {
  @include jigsass-util(u-v, $modifier: h); // <-- visibility: hidden

  ...
}
// _c.bar.scss
.bar {
  @include jigsass-util(u-bfv, $modifier: h);  // <-- backface-visibility: hidden
  @include jigsass-util(
    u-bfv,
    $modifier: visible,
    $from: large
  ); // <-- backface-visibility: visible from large bp an on.

  ...
}
// _c.baz.scss
.baz {
  @include jigsass-util(u-is-hidden, $from: medium); // <-- display: none from medium bp an on.

  ...
}

Doing so helps us a great deal with portability, as no matter where we import component or object partials, the correct utility classes will be generated. Think of it as a poor man's dependency management.

Developer communication is also assisted by including "dependencies" wherever they are required, as anyone going through a partial, can easily understand how it should be marked up with just a glance.

As far as bloat goes, just don't worry about it - the actual styles will only be generated once, at the location in the cascade where the Jigsass Visibility partial was imported into the main file.

JigSass Visibility classes are responsive-enabled, using JigSass MQ and the breakpoints defined in the $jigsass-breakpoints variable.

Based on the breakpoint arguments passed to jigsass-util when including a visibility class, responsive modifiers are generated according to the following logic:

.u-v--<modifier>[-[-from-<breakpoint-name>][-until-<breakpoint-name>][-misc-<breakpoint-name>]]

So, assuming the medium, large and landscape breakpoints are defined in $jigsass-breakpoints as 600px, 1024px and (orientation: landscape) respectively,

@include jigsass-util(u-v, $modifier: h);

will generate the .u-v--h class, which is not limited to any media-query.

@include jigsass-util(u-v, $modifier: h, $until: medium);

will generate the .u-v--h--until-medium class, which will be in effect at (max-width: 37.49em) and will override styles in the default class until that point.

@include jigsass-util(u-v, $modifier: h, $from: large, $misc: landscape);

will generate the .u-v--h--from-large-when-landscape class, which will go into effect at (min-width: 64em) and (orientation: landscape) and will override styles in the default class under these conditions.

License: MIT

u-v--v (visible)

A util to modify an element's visibility property to visible.

Include with:

@include jigsass-util(u-v, $modifier: visible[, $from, $until, $misc]);

Example

<div class='fpo u-v--v'>
  This div's visibility is set to: <em>visible</em>.
</div>

u-v--h (hidden)

A util to modify an element's visibility property to hidden.

Include with:

@include jigsass-util(u-v, $modifier: hidden[, $from, $until, $misc]);

Example

<div class='fpo u-v--h'>
  This div's visibility is set to: <em>hidden</em>.
</div>

u-bfv--v (visible)

A util to modify an element's backface-visibility property to visible.

Include with:

@include jigsass-util(u-bfv, $modifier: visible[, $from, $until, $misc]);

Example

<div class='u-bfv--v'>
  A div with backface-visibility set to visible.
</div>

u-bfv--h (hidden)

A util to modify an element's backface-visibility property to hidden.

Include with:

@include jigsass-util(u-bfv, $modifier: hidden[, $from, $until, $misc]);

Example

<div class='u-bfv--h'>
  A div with backface-visibility set to hidden.
</div>

u-o--0

A util to modify an element's opacity property to 0.

Include with:

@include jigsass-util(u-o, $modifier: 0[, $from, $until, $misc]);

Example

<div class='fpo u-o--0'>
  A div with opacity set to 0.
</div>

u-o--0.5

A util to modify an element's opacity property to 0.5.

Include with:

@include jigsass-util(u-o, $modifier: 0.5[, $from, $until, $misc]);

Example

<div class='fpo u-o--0.5'>
  A div with opacity set to 0.5.
</div>

u-o--1

A util to modify an element's opacity property to 1.

Include with:

@include jigsass-util(u-o, $modifier: 1[, $from, $until, $misc]);

Example

<div class='fpo u-o--1'>
  A div with opacity set to 1.
</div>

u-is-hidden

A util to hide elements visually and from screen readers.

Include with:

@include jigsass-util(u-is-hidden [, $from, $until, $misc]);

Example

<div class='fpo u-is-hidden>
  This div is hidden both visually and from screen readers.
</div>

u-is-visually-hidden

A util to hide elements visually but leaves them accesible for screen readers.

Optionally reveal focusable visually hidden elements on focus (with keyboard navigation or js), by attaching the u-is-focusable class to them.

Include with:

@include jigsass-util(u-is-visually-hidden [, $from, $until, $misc]);

Example

<p>Tab-cycle inside the demo iFrame to reveal the focusable visually hidden element</p>
<div tabindex='0' class='fpo [ u-is-visually-hidden u-is-focusable ]'>
  This div is hidden visually but is available to screen readers.
  It is revealed when focusd
</div>