5.2.0 • Published 11 months ago

postcss-sort-media-queries v5.2.0

Weekly downloads
39,672
License
MIT
Repository
github
Last release
11 months ago

PostCSS Sort Media Queries

npm Node.js CI license npm

🌏 EnglishO'zbek

PostCSS plugin for sorting and combining CSS media queries with mobile first / desktop first methodologies.

From 5.0.0 plugin support Media Feature Types: “range”

Table of Contents

Online demo

And here is the online demo

Examples

Mobile first sorting

before

@media screen and (max-width: 640px) {
  .head { color: #cfcfcf }
}
@media screen and (max-width: 768px) {
  .footer { color: #cfcfcf }
}
@media screen and (max-width: 640px) {
  .main { color: #cfcfcf }
}
@media screen and (min-width: 1280px) {
  .mobile-first { color: #cfcfcf }
}
@media screen and (width > 640px) {
  .mobile-first { color: #cfcfcf }
}
@media screen and (max-width: 640px) {
  .footer { color: #cfcfcf }
}

after

@media screen and (width > 640px) {
  .mobile-first { color: #cfcfcf }
}
@media screen and (min-width: 1280px) {
  .mobile-first { color: #cfcfcf }
}
@media screen and (max-width: 768px) {
  .footer { color: #cfcfcf }
}
@media screen and (max-width: 640px) {
  /* combined */
  .head { color: #cfcfcf }
  .main { color: #cfcfcf }
  .footer { color: #cfcfcf }
}

Desktop first sorting

before

@media screen and (width < 640px) {
  .header { color: #cdcdcd }
}
@media screen and (min-width: 760px) {
  .desktop-first { color: #cdcdcd }
}
@media screen and (width < 640px) {
  .main { color: #cdcdcd }
}
@media screen and (min-width: 1280px) {
  .desktop-first { color: #cdcdcd }
}
@media screen and (max-width: 760px) {
  .footer { color: #cdcdcd }
}
@media screen and (max-width: 640px) {
  .footer { color: #cdcdcd }
}

after

@media screen and (max-width: 760px) {
  .footer { color: #cdcdcd }
}
@media screen and (width < 640px) {
  /* combined */
  .header { color: #cdcdcd }
  .main { color: #cdcdcd }
  .footer { color: #cdcdcd }
}
@media screen and (min-width: 760px) {
  .desktop-first { color: #cdcdcd }
}
@media screen and (min-width: 1280px) {
  .desktop-first { color: #cdcdcd }
}

Install

First thing's, install the module:

npm install postcss postcss-sort-media-queries --save-dev

Usage

Check you project for existed PostCSS config: postcss.config.js in the project root, "postcss" section in package.json or postcss in bundle config.

If you already use PostCSS, add the plugin to plugins list:

module.exports = {
  plugins: [
+   require('postcss-sort-media-queries')({
+     sort: 'mobile-first', // default value
+   }),
    require('autoprefixer')
  ]
}

or with custom sort function

module.exports = {
  plugins: [
+   require('postcss-sort-media-queries')({
+     sort: function(a, b) {
+        // custom sorting function
+     }
+   }),
    require('autoprefixer')
  ]
}

If you do not use PostCSS, add it according to official docs and set this plugin in settings.

Options

Sorting works based on dutchenkoOleg/sort-css-media-queries function.

sort

This option support string or function values.

  • {string} 'mobile-first' - (default) mobile first sorting
  • {string} 'desktop-first' - desktop first sorting
  • {function} your own sorting function

'mobile-first'

postcss([
  sortMediaQueries({
    sort: 'mobile-first' // default
  })
]).process(css);

'desktop-first'

postcss([
  sortMediaQueries({
    sort: 'desktop-first'
  })
]).process(css);

Custom sort function

postcss([
  sortMediaQueries({
    function(a, b) {
      return a.localeCompare(b);
    }
  })
]).process(css);

In this example, all your media queries will sort by A-Z order.

This sorting function is directly passed to Array#sort() method of an array of all your media queries.

Sort configuration

By this configuration you can control sorting behaviour.

postcss([
  sortMediaQueries({
    configuration: {
      unitlessMqAlwaysFirst: true, // or false
    }
  })
]).process(css);

Or alternatively create a sort-css-mq.config.json file in the root of your project. Or add property sortCssMQ: {} in your package.json.

Only Top Level

Sort only top level media queries to prevent eject nested media queries from parent node

postcss([
  sortMediaQueries({
    onlyTopLevel: true,
  })
]).process(css);

Changelog

See Releases history

License

MIT

Other PostCSS plugins

  • postcss-momentum-scrolling - plugin for adding momentum style scrolling behavior (-webkit-overflow-scrolling:touch) for elements with overflow (scroll, auto) on iOS

Thanks

5.2.0

11 months ago

5.1.0

12 months ago

4.4.1

12 months ago

5.0.1

12 months ago

5.0.0

12 months ago

4.3.0

2 years ago

4.2.1

2 years ago

4.0.0

3 years ago

3.12.13

3 years ago

4.1.0

3 years ago

3.11.12

3 years ago

3.10.11

3 years ago

3.9.10

3 years ago

3.8.9

3 years ago

3.7.7

3 years ago

3.6.5

3 years ago

3.4.3

3 years ago

3.3.3

3 years ago

3.2.3

3 years ago

3.1.1

3 years ago

3.0.0

3 years ago

2.4.17

3 years ago

2.3.17

3 years ago

2.3.15

3 years ago

2.2.14

3 years ago

2.2.13

3 years ago

2.2.12

3 years ago

2.1.11

3 years ago

2.1.10

4 years ago

2.1.6

4 years ago

2.1.5

4 years ago

2.1.9

4 years ago

2.0.3

4 years ago

2.0.2

4 years ago

1.7.26

4 years ago

1.7.25

4 years ago

1.7.24

4 years ago

1.6.24

4 years ago

1.5.24

4 years ago

1.4.24

4 years ago

1.4.23

4 years ago

1.2.23

4 years ago

1.3.22

4 years ago

1.31.21

4 years ago

1.1.20

4 years ago

1.1.18

4 years ago

1.1.16

4 years ago

1.0.15

4 years ago

1.0.13

4 years ago

1.0.9

4 years ago

1.0.7

4 years ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago