5.0.0 • Published 3 years ago

postcss-stack v5.0.0

Weekly downloads
90
License
MIT
Repository
github
Last release
3 years ago

PostCSS Stack

A better way to manage z-indexes

NPM Version Build Status Dependency Status

Define the stack of components and PostCSS Stack will resolve the z-indexes for you instead of playing a game of z-index: 99999.

/* input */
.modal {
  z-index: stack('modal');
}

.tool-tip {
  z-index: stack('tool-tip');
}

.element-beneath {
  z-index: stack('beneath');
}
/* output */
.modal {
  z-index: 2;
}

.tool-tip {
  z-index: 1;
}

.element-beneath {
  z-index: -1;
}

Install

# npm
npm install --save-dev postcss postcss-stack

# or yarn
yarn add --dev postcss postcss-stack

Usage

Add it to your PostCSS work-flow, whatever way you choose to.

// Using a postcss.config.js
const stack = require('postcss-stack');

module.exports = {
  plugins: [
    stack({
      list: [
        { 'beneath': -1 },
        'application',
        'tool-tip',
        'modal'
      ]
    })
  ]
};

Then call the stack function with relevant item name in your css.

/* input */
.application {
  z-index: stack('application');
}
/* output */
.application {
  z-index: 0;
}

See tests for more examples.

Options

optiontypedefaultdescription
listarray or function[]Array of items in the stack or function returning array of items
incrementnumber1The increment value

list

The list of items that defines the stack. An item can either be explicitly defined by a name key and z-index value or by a name string that will have it's z-index auto-generated.

stack({
  list: [
    { 'explicit': -10 }, // stack('explicit') => z-index: -10
    'auto', // stack('auto') => z-index: 0
    'generated', // stack('generated') => z-index: 1
  ]
});

By default it's ordered by the order of the array, but you can use Array.prototype.reverse() to better visualize how the stack is set.

stack({
  list: [
    { 'beneath': -1 }, // z-index: -1
    'application', // z-index: 0
    'tool-tip',  // z-index: 1
    'modal'  // z-index: 2
  ]
});

// Reversed
stack({
  list: [
    'modal' // z-index: 2
    'tool-tip', // z-index: 1
    'application', // z-index: 0
    { 'beneath': -1 }, // z-index: -1
  ].reverse();
});

increment

Allows a increment value to be set. For example if increment: 100, the output of our example would be:

.modal {
  z-index: 200;
}

.tool-tip {
  z-index: 100;
}

.element-beneath {
  z-index: -1;
}

Stacking Context

Remember that stacking context is relative to the parent, so if you have a parent element that is z-index: 2 and a child of z-index: 1, the child will not be below the parent but instead relative to any other stacking context of that parent.

For more information on stacking context, MDN has a good overview.

License

5.0.0

3 years ago

4.0.1

3 years ago

4.0.0

4 years ago

3.0.1

5 years ago

3.0.0

5 years ago

2.2.0

5 years ago

2.1.0

5 years ago

2.0.0

5 years ago

1.5.0

5 years ago

1.4.0

6 years ago

1.3.0

6 years ago

1.2.0

6 years ago

1.1.1

6 years ago

1.1.0

6 years ago

1.0.0

6 years ago