/til

📝 Today I Learned

til

📝 Today I Learned

07/01/2018

CSS grid: fit-content

The fit-content property can be used in order to allow smaller reserved space than the column track.

How to use

grid-template-columns: fit-content(250px) 1fr;

codepen:

https://codepen.io/rachelandrew/pen/KZyaBj

Learned from:

Rachel Andrew

08/01/2018

Angular: trackBy

trackBy modifies the behavior so that it compares new data to old based on the return value of the supplied trackBy method. This allows Angular to reduce the amount of DOM update needed.

How to use

<!-- instructor-list.component.html.html -->
<ul>
  <li *ngFor="let instructor of instructorList: trackBy: trackByName" >
    <span>Instructor Name {{ instructor.name }}</span>
  </li>
</ul>
/* instructor-list.component.ts */
import { Component } from '@angular/core';

import { ListService } from '../list.service';

@Component({
  selector: 'app-instructor-list',
  templateUrl: './instructor-list.component.html'
})
export class InstructorListComponent {
  instructorList = {name: string}[];

  constructor(private ls: ListService){
    // In this example let's assume the list service provides the
    // instructors as a realtime list of filtered instructors.
    // New updates are sent at regular intervals regardless of content change.
    // As a result the object references change with each update.
    ls.getList()
      .subscribe(list => this.instructorList = list);
  } 
  
  trackByName(index, instructor) {
    return instructor.name;
  }
}

Learned from:

Angular Blog

09/01/2018

Vue: render single slot component

You can use render functions instead of templates for single slot components to avoid the extra wrapper element

How to use

Vue.component('element-query', {
  // use a render function to grab the slot directly!
  render() {
    return this.$scopedSlots.default({
      width: this.width,
      height: this.height,
    })
  },
  data() {
    return {
      width: null,
      height: null,
    }
  },
  mounted(){
    erd.listenTo(this.$el, (el) => {
      this.width = el.offsetWidth;
      this.height = el.offsetHeight;
    })
  },
})

Learned from:

Adam Wathan

10/01/2018

ES6: Array.from map function

You can provide a second argument to Array.from that is a map function.

How to use

Array.from(Array(20), (item, index) => index + 1);

Learned from:

Merrick Christensen

11/01/2018

Jasmine: mockDate

It is possible to mock the current date with the mockDate function of Jasmine.

How to use

// with native Date
const today = new Date('2015-10-19');
jasmine.clock().mockDate(today);

// with moment.js
const today = moment('2015-10-19').toDate();
jasmine.clock().mockDate(today);

Learned from:

jacwah

13/01/2018

DevTools: coverage

Press [ctrl] + [shift] + p or [cmd] + [shift] + p then type code "show coverage". You can then reload the page to see how much code is being unused on the page.

Learned from:

Sean Larkin

15/01/2018

ES6: unique array

It is possible to get unique values from an array with Set() and the rest operator.

How to use

const uniqueArray = (arr) => [...new Set(arr)];

Learned from:

Addy Osmani

Command Line: curly brackets

Use curly brackets in the command line to cut down on typing the same thing over and over

How tu use

$ touch component.{ts,css,html}

Learned from:

Wes Bos

20/01/2018

Angular: Pure pipe

With Pure pipe, we can optimize expression re-evaluation and get the caching that Angular provides.

How to use

@Pipe({
  name: 'purePipe',
  pure: true,
})
export class PurePipe {
  transform(input: string, param: string) {
    return input + param;
  }
}

Learned from:

Minko Gechev

22/01/2018

CSS: mix-blend-mode

The mix-blend-mode CSS property describes how an element's content should blend with the content of the element's direct parent and the element's background. For instance we can set a text to white and the mix-blend-mode property to difference. That would result in the text being always readable no matter the background.

How to use

.readable-text {
  color: white;
  mix-blend-mode: difference;
}

Learned from:

Timothy Achumba

23/01/2018

CSS: Aspect Ratio

It is possible to have a DOM element keep a fixed aspect ratio with a combination of CSS grid and SVG.

How to use

<style>
.aspectRatioSizer {
  display: grid;
}
.aspectRatioSizer > * {
  grid-area: 1 / 1 / 2 / 2;
}
</style>
<div class="aspectRatioSizer">
  <svg viewBox="0 0 7 2"></svg>
  <div>
    Content goes here
  </div>
</div>

codepen:

https://codepen.io/noamr/pen/mpamVN

Learned from:

Noam Rosenthal

HTML: preconnect

We can add preconnect hints for any hosts that you will be requesting assets from so the browser can then prep things by establishing all the necessary connections for those resources.

How to use

<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>

Learned from:

Jeremy Frank

30/01/2018

npm: use specific version of node for project

You can use a specific version of node so any time you do npm test or npm start, it will use this one, regardless of what your global node version is.

How to use

$ npm install node@7

Learned from:

Kat Marchán

06/02/2018

ES6: tagged template strings

You can use tagged template strings to get many editors to syntax-highlight the strings

How to use

const html = String.raw;
const wired = html`
  <div class="wow">
    html, yay
  </div>
`;

Learned from:

Alex Dytrych

22/02/2018

JS: Locale Curency

You can use toLocaleString to format currency

How to use

100.42.toLocaleString('fr-FR', { style: 'currency', currency: 'EUR' });
// "100,42 €"
100.42.toLocaleString('en-US', { style: 'currency', currency: 'USD' });
// "$100.42"

Learned from:

Wes Bos

27/02/208

git: changelog

You can use git log to generate a changelog since a given tag.

How to use

$ git log --oneline --no-merges [last tag]..

$ git log --oneline --no-merges v1.10.0..

Learned from:

Harry Roberts

05/03/2018

CSS: content-based sizing

You can make your layout fast and static by default by turning on content-based sizing on a per-element basis.

How to use

* {
  box-sizing: border-box;
  contain: strict;
}
[autosize] {
  contain: layout style paint;
}

Learned from:

Jason Miller

30/12/2018

JS: addEventListener() once

addEventListener() supports an once option.

How to use

target.addEventListener('click', () => console.log('once'), { once: true })

Learned from:

Surma

24/02/2019

SVG: rotate shape fill-box

To rotate a shape witout using the center of the SVG canvas as the origin of the rotation but its own center, you can add transform-box: fill-box to the shape you're rotating.

How to use

.triangle {
    animation-name: rotation;
    animation-duration: 1s;
    transform-origin: center;
    transform-box: fill-box;
}
@keyframes rotate {
    from { transform: rotate(90deg); }
    to { transform: rotate(0deg); }
}

Learned from:

Cassie Evans