npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

position-sass

v0.3.0

Published

Mixins for setting an elements position and offsets

Downloads

13

Readme

Position

Position is set of mixins for handling position and offsets.

This project takes Hugo Giraudel's mixin and adds some nice features including new keywords and support for custom units.

Docs

You can view the docs online here or locally in Chrome by running:

$ grunt docs

There is also a Grunt task to build the docs:

$ grunt sassdoc

Tests

Tests are available from the excellent Bootcamp and can be run using:

$ grunt test

API

You can play with the latest version in a Sassmeister gist here.

The main mixin takes the following form:

  • position is one of absolute, fixed, relative or static.
  • offsets is a list of values defining the offsets
@include position(position, offsets);

There are four mixins to remove the need to set the postition value. They all take offsets as a single argument, other than static which has no arguments as offsets will have no effect.

@include absolute(offsets);
@include fixed(offsets);
@include relative(offsets);
@include static;

The value for offsets is a list of keywords, some of which accept values which must follow those keywords in the list. Any combination of the folowing values are supported, with values set in the order they are declared, meaning in the event of two values effecting the same offset, the value set by the last value will take precidence.

Standard Offsets

You can use top, bottom, left and right either with or without a value:

.Example-without-value {
  @include absolute(top left);
}

.Example-with-value {
  @include absolute(top 1rem left 2rem);
}

Renders:

.Example-without-value {
  position: absolute;
  top: 0;
  left 0;
}

.Example-with-value {
  position: absolute;
  top: 1rem;
  left 2rem;
}

Offset keywords

Offset keywords allow you to set offsets to a given value. *Note: all is aliased to offset:

.Example-with-offset {
  @include absolute(offset 2rem);
}

.Example-with-all {
  @include absolute(offset 2rem);
}

.Example-with-offset-h {
  @include absolute(offset-h 2rem);
}

.Example-with-offset-v {
  @include absolute(offset-v 2rem);
}

Renders:

.Example-with-offset {
  position: absolute;
  top: 2rem;
  bottom: 2rem;
  left: 2rem;
  right: 2rem;
}

.Example-with-all {
  position: absolute;
  top: 2rem;
  bottom: 2rem;
  left: 2rem;
  right: 2rem;
}

.Example-with-offset-h {
  position: absolute;
  left: 2rem;
  right: 2rem;
}

.Example-with-offset-v {
  position: absolute;
  top: 2rem;
  bottom: 2rem;
}

Fill keywords

Fill keywords set the offsets to zero. They cannot be used with values and are equivalent to using the offset keywords with a value of zero.

.Example-with-fill {
  @include absolute(fill);
}

.Example-with-fill-h {
  @include absolute(fill-h);
}

.Example-with-fill-v {
  @include absolute(fill-v);
}

Renders:

.Example-with-fill {
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
}

.Example-with-fill-h {
  position: absolute;
  left: 0;
  right: 0;
}

.Example-with-fill-v {
  position: absolute;
  top: 0;
  bottom: 0;
}

Using custom values

Where things get interesting is the ability to define a function called position-parse-value-filter. If this function is defined it is called when a value isn't recognised (it is an unknown, unitless value). This means you can process this value yourself.

For example, most projects are full hardcoded position-property declarations which quickly become inconsistant and ad-hoc. Why not enforce consistancy and improve readability on your projectby using a set of custom units:


// Define a map of units
$custom-units-map: (
  hairline: 1px,
  single: 10px,
  double: 20px,
  triple: 30px,
  quadruple: 40px,
  half: 5px,
  third: 3.33333333px,
  quarter: 2,5px
);

// Override
@function position-parse-value-filter($key, $orientation){
  @if map-has-key($custom-units-map, $key) {
    @return map-get($custom-units-map, $key);
  } @else {
    // If it isn't recognised throw an error
    // Unfortunately Sass doesn't allow us to call super.
    @return position-throw-error($position-invalid-value-error, "Invalid value #{$value}");
  }
}

// Then you can use

.Example {
  @include fixed(fill-h single top triple bottom third);
}

*Note this API has changed since version 0.2.18. In version 0.2.18 you overrode the 'pos-parse-value-filter' function, but now you are no longer overriding the default handling. Apart from being less opaque, this means that you can define this function before or after you import box.

There is a lot more that you can do with this simple functionality. For example you could use unitless values in the $custom-units-map and multiply them with a vertical rhythm unit, or use breakpoint context to tweak these units across breakpoints, so that a declaration of single can mean different values at different breakpoints. More examples coming soon.

Dependencies & Compatability

It has no dependencies on other Sass libs and should work with Sass 3.3 and up, though it's currently only tested in 3.4.