Frontend 
gems

Welcome!

I've gathered some tips and tricks regarding front-end development during the creation of many projects and i've put them all here in case somebody will benefit fom them. This article contains general web-dev tips, Angular 2+ snippets but not ES6ish javascript (since the internet is covered with these articles).



General Frontend tips


Controlling opacity

On your css when you apply colors to different elements with Hex values (e.g. #FFFFFF), you can add 2 more aditional characters in the end, to specify the opacity of the color.

Here's an example

.foo {
    color: #FFFFFF50
}

In this example, the foo div gets a white color with 50% opacity.




Quick and dirty margins or spacings

Instead of putting space between elements with margin like below.

<div>
    <p style="margin-left: 10px;">foo</p>
</div>

You can give space between elements's textNodes with the &nbsp; keyword like this

<div>
    <p>&nbsp;&nbsp;foo</p>
</div>

CAREFUL 👉 &nbsp; can be only applied to textNodes of elements and not actual elements, textNodes are considered the non HTML children of the corresponding element.




Copying large objects from Google Chrome Devtools 🕸

When you console.log an object and want to copy it to the clipboard do these steps.

  1. Right click on the object 👉 save as temporary variable
  2. Run this command on the console: copy(temp1)



Angular tips


Prettier ngClass

Some people use this type of ngClass 👇

<div [ngClass]="active ? 'activated' : '' "></div>

You can do this instead

<div [ngClass]="{'activated': active}"></div>

Why?

  • Why not?
  • You can have multiple comparisons
  • You get rid of ternary operators

First change on ngOnChanges

When using ngOnChanges, you should know that there is a firstChange property (https://angular.io/api/core/SimpleChange) that tells you whether the change was before or after ngOnInit. This results in less logic on the constructor and more logic on ngOnInit which is a recomended way to develop applications in Angular.

Conclusion

I'm planning to make this list bigger and bigger over time, so any suggestion or tip would be appreciated and posted along with the corresponding credit to the author. Feel free to leave a comment if this helped you or you liked it!

Affiliate Links

Fastmail

I've been using Fastmail for over a year now, and it's been a game-changer for managing multiple email accounts. With their platform, you can seamlessly handle different email providers in one place. New users get 10% off their subscription!

Code: 7aa3c189

Tangem wallet

If you're looking for a secure and easy-to-use hardware wallet for your crypto assets, I recommend Tangem. Use my referral link to get a 10% discount on your first purchase!

Code: ZQ6MMC

1Password

1Password is the best password manager I've used. It's secure, easy to use, and has saved me countless hours.

Using these referral links helps support my work. I only recommend products I use and trust. Thank you for your support!