Today I Learned

8 posts by dariuszpieńczykowski

Spotify loves space on your hard disk

Check if it is a problem for you. But usually, Spotify cache takes about 10gb of space.

SpotifyCache

How to fix that.

1) Shut down you Spotify

2) Remove whole folder com.spotify.client (also from the bin) - ~Library/Caches/com.spotify.client

3) Locate the Spotify config folder (for me it was ~/Library/Application Support/Spotify)

4) Open prefs file with your favorite text editor.

5) Add another line and put a value in megabytes right after the equals sign. I chose 1024mb.

storage.size=1024

6) Relaunch Spotify.

Inspecting elements that disappear from DOM #2

If you don’t wanna play with inspecting “Break on” option. You can use browser debugger.

Only what you need to do is run the console. Chrome / Firefox - F12 or by hand.

Then, you can start focusing/ hovering/ anything that you wanna catch and then you need to press F8 (the same for Chrome and Firefox).

The console will break your js code and then you can easily inspect what you want. Easy, right? :)

Sass stylelint

Adding stylelint on the project (using ie RM) in a few steps:

First, you need to install a package: yarn add stylelint.

Second, you need to install a package with config for stylelint. For SASS it is yarn add stylelint-config-sass-guidelines

Third, you need to add to the project file .stylelintrc.json with some config, ie:

{
  "extends": "stylelint-config-sass-guidelines",
  "rules": {
    "block-opening-brace-space-before": null,
    "color-hex-case": "upper",
    "declaration-block-trailing-semicolon": "never",
    "max-nesting-depth": 5,
    "number-leading-zero": null,
    "scss/percent-placeholder-pattern": ".",
    "scss/selector-no-redundant-nesting-selector": "allow",
    "selector-class-pattern": ".",
    "selector-no-qualifying-type": null,
    "selector-pseudo-element-colon-notation": "single"
  }
}

PS. For Scss I created config here.

The last step :) You need to run this on your IDE (or run it in the terminal by hand). Structure stylelint in RM #1

About using & in nested selectors with BEM

In normal SCSS/SASS file code:

.calendar-container--theme-third {
  .calendar-reservation {
    &__checkout-wrapper:not(&--modifier):before {
      content: 'abc';
    }
  }
}

will be parsed to:

.calendar-container--theme-third .calendar-reservation__checkout-wrapper:not(.calendar-container--theme-third .calendar-reservation--modifier):before {
  content: 'abc';
}

so when U need to use ie. not with ampersand U will get the whole parent selector

:not(.calendar-container--theme-third .calendar-reservation--modifier)

instead of the last parent in & place

:not(.calendar-reservation--modifier)

For this situation, I created plugin/mixin: BEM-parent-selector

This mixin gives you an option to add selector only for the last parent.

.calendar-container--theme-second-2 {
  .calendar-reservation {
    @include BEM-parent-selector('&__checkout-wrapper:not(&--modifier):before') {
      content: 'abc';
    }
  }
}

will be parsed to:

.calendar-container--theme-second-2 .calendar-reservation__checkout-wrapper:not(.calendar-reservation--modifier):before {
   content: 'abc';
 }

How to Share Variables Between Javascript and Scss/Sass

When you need to share variables between javascript and scss/ sass you can export variables in your style file:

$calendar-min-width: 80px;
$animation-length: 250ms;
$animation-length-ms: $animation-length + 10ms;

:export {
  calendarMinWidth: $calendar-min-width;
  animationMillis: $animation-length-ms;
}

and after that you’re able to get this in javascript:

const variables = {
  calendarMinHeight: parseInt(styles.calendarMinHeight),
  animationMillis: parseInt(styles.animationMillis),
};

How to add autoprefixer in webpack

Firstly we need to add this to our project using yarn/npm.

So yarn add autoprefixer.

After a successful installation, we need to declare which browsers we wanna use for our autoprefixer.

To declare that, we need to add to our packages.json file a few lines:

“browserslist”: [
   “> 1%“,
   “last 2 versions”
 ],

here, we can set something else (https://github.com/browserslist/browserslist#queries)

After that, we need to configure the webpack config file (ie. webpack.config.js).

Firstly we require autoprefixer and we’re setting this as a variable (somewhere on the beginning of the file)

const autoprefixer = require('autoprefixer');

!important:

| We need to add postcss-loader loader between css-loader and sass-loader.

use: ['css-loader',
            {
              loader: 'postcss-loader',
              options: {
                plugins: () => [autoprefixer()]
              }
            },
            'sass-loader'],

if we have more loaders it could look like that:

  module: {
    rules: [
      {
        test: /\.(sass|scss)$/,
        loader: ExtractTextPlugin.extract({
          fallback: 'style-loader',
          use: ['css-loader',
            {
              loader: 'postcss-loader',
              options: {
                plugins: () => [autoprefixer()]
              }
            },
            'sass-loader'],
        }),
      },
      {
        test: /\.css$/,
        loader: ExtractTextPlugin.extract({
          fallback: 'style-loader',
          use: ['css-loader'],
        }),
      },
      {
        test: /\.js/,
        use: ['babel-loader?cacheDirectory'],
        exclude: /node_modules/,
      },
    ],
  },

Now, we need to restart the server and you can enjoy working autoprefixer :)

SASS / BEM - Not TIL but still some interesting magic

Case 1 - we don’t want to write parent classname again from deep nesting

.some-class
  $this: &

  &.--sub
    margin-top: 2.4rem
    #{$this}__title // so it is .some-class__title
      font-size: 1.7rem

equals:

.some-class.--sub
  margin-top: 2.4rem
.some-class.--sub .some-class__title
  font-size: 1.7rem

Case 2 - we want to have a (or any another tag) before parent class from deep nesting

.btn
  margin-top: 2.4rem
  @at-root a#{&} // so it is a.btn
    font-size: 1.7rem

equals:

.btn
  margin-top: 2.4rem
a.btn
  font-size: 1.7rem