1.0.1 • Published 4 years ago

css-mqpacker-starter v1.0.1

Weekly downloads
3
License
ISC
Repository
github
Last release
4 years ago

css-mqpacker-starter

Mixin starter with scss for css-mqpacker

You must use the package with scss and postcss. You will find the webpack rule for compiling scss files with css-mqpacker below or use webpack starter.

Installation Breakpoints Init order Mixins Source mixin code Possible order error Webpack rule for compiling

Installation

Install the package

npm i css-mqpacker-starter

Import in main scss file

@import "~css-mqpacker-starter";
@include initMediaPosition();

Now use it :)

@include sm {
  body {
    font-size: 15px;
  }
}

Breakpoints

The package provide default breakpoints:

BreakpointPixels
\$xl1200px
\$lg992px
\$md768px
\$sm576px
\$xs420px
\$xxs360px

Mixins

There are media queries mixins for each breakpoint. Desktop First and Mobile First.

@include xl {
}
@include xl-up {
}
downup
xlxl-up
lglg-up
mdmd-up
smsm-up
xsxs-up
xxsxxs-up

Init order:

Put the mixin before custom scss code. It will create placeholder styles for body tag. It's important for css-mqpacker; There can be wrong order media queries without the mixin.

@include initMediaPosition();

Possible error see below

Source mixin code:

@mixin xl {
  @media only screen and (max-width: $xl - 1) {
    @content;
  }
}

@mixin xl-up {
  @media only screen and (min-width: $xl) {
    @content;
  }
}

Possible order error

In the case:

.card {
  // styles
  @include sm {
    // styles 2
  }
}

.container {
  max-width: 1200px;
  @include md {
    max-width: 500px;
  }
  @include sm {
    max-width: none;
  }
}

Output after css-mqpacker will be:

.card {
  // styles
}
.container {
  max-width: 1200px;
}

@media only screen and (min-width: $sm) {
  .card {
    // styles 2
  }
  .container {
    max-width: none;
  }
}
@media only screen and (min-width: $md) {
  .container {
    max-width: 500px;
  }
}

On screen \$sm and below style .container{ max-width: none; } will not be applied. Use @include initMediaPosition() to prevent this error.

Webpack rule for compiling

Here is example: Webpack starter

// rules:
[
  {
    test: /\.scss$/,
    use: [
      isDev ? "style-loader" : MiniCssExtractPlugin.loader,
      {
        loader: "css-loader",
        options: { sourceMap: true },
      },
      {
        loader: "postcss-loader",
        options: {
          sourceMap: true,
          config: { path: "postcss.config.js" },
        },
      },
      {
        loader: "sass-loader",
        options: { sourceMap: true },
      },
    ],
  },
];