<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Huy Tr.</title>
    <description>The latest articles on DEV Community by Huy Tr. (@huytd).</description>
    <link>https://dev.to/huytd</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F377%2F613943.png</url>
      <title>DEV Community: Huy Tr.</title>
      <link>https://dev.to/huytd</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/huytd"/>
    <language>en</language>
    <item>
      <title>TIL: JavaScript replace() command with callback</title>
      <dc:creator>Huy Tr.</dc:creator>
      <pubDate>Fri, 04 Jan 2019 05:13:07 +0000</pubDate>
      <link>https://dev.to/huytd/til-javascript-replace-command-with-callback-3l7h</link>
      <guid>https://dev.to/huytd/til-javascript-replace-command-with-callback-3l7h</guid>
      <description>&lt;p&gt;Of course, this is not new, it's already here centuries ago in the document &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace"&gt;https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace&lt;/a&gt;, but I never have to do any replacement complicated enough to use it, so I pay no attention to it until I read a pull request from a teammate today.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;replace()&lt;/code&gt; command in JavaScript has a callback that provided you with some more information such as the matched content, the index, and the original string. What you return in that callback will be replaced to the matched content.&lt;/p&gt;

&lt;p&gt;This allows you to have a more complicated replacement, for example: you want to replace just the &lt;strong&gt;second occurrence&lt;/strong&gt; of the letter &lt;code&gt;"a"&lt;/code&gt; in &lt;code&gt;"abcabc"&lt;/code&gt;to the letter &lt;code&gt;"$"&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;How would you write a regex for that? What if we change the requirement&lt;br&gt;
to any &lt;strong&gt;nth occurrence&lt;/strong&gt;? Even if you find a regex solution, is it&lt;br&gt;
elegant enough to not making any other developer vomit when they had to maintain your code?&lt;/p&gt;

&lt;p&gt;Using &lt;code&gt;replace()&lt;/code&gt; with a callback, we can just write:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"abcabc".replace(/a/g, (matched, index, original) =&amp;gt; {
  if (index !== 0) {
    return "$";
  } else {
    return matched;
  }
});
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;That's it, stop writing complicated regexes, start using &lt;code&gt;replace()&lt;/code&gt;&lt;br&gt;
with callbacks, it makes things easier. You and your teammates, all have a life to live, and sanity to save.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>todayilearned</category>
    </item>
    <item>
      <title>Emacs from scratch</title>
      <dc:creator>Huy Tr.</dc:creator>
      <pubDate>Sat, 30 Jun 2018 06:55:48 +0000</pubDate>
      <link>https://dev.to/huytd/emacs-from-scratch-1cg6</link>
      <guid>https://dev.to/huytd/emacs-from-scratch-1cg6</guid>
      <description>&lt;p&gt;Original posted &lt;a href="https://huytd.github.io/emacs-from-scratch.html" rel="noopener noreferrer"&gt;on my blog&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;Spacemacs is the first Emacs version I used, so I'm kind of attached to&lt;br&gt;
the &lt;code&gt;evil-mode&lt;/code&gt; and &lt;code&gt;SPC&lt;/code&gt; mnemonic key binding.&lt;/p&gt;

&lt;p&gt;But I always have a feeling that something is not right, Spacemacs hides&lt;br&gt;
a lot of things under the hood, and it loads a lot of stuff that I do&lt;br&gt;
not need during the startup. This would be a huge waste, and I need to&lt;br&gt;
have a fresh setup that I can control every single thing in my editor.&lt;/p&gt;

&lt;p&gt;So I created my own Emacs configuration, it's surprisingly easier than&lt;br&gt;
as I thought.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fhuytd.github.io%2Fimg%2Fcustom-emacs.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fhuytd.github.io%2Fimg%2Fcustom-emacs.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this post, I'll go through the steps I built a minimal&lt;br&gt;
Spacemacs-like version, with some basic key binding:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;General:
  SPC /     : ripgrep
  SPC TAB   : previous buffer
  SPC SPC   : Open M-x

Files:
  SPC p f   : find files

Buffers:
  SPC b b   : buffers list

Window:
  SPC w l   : move right
  SPC w h   : move left 
  SPC w j   : move down
  SPC w k   : move up
  SPC w /   : split right
  SPC w -   : split bottom
  SPC w x   : close window

Other:
  SPC a t   : open terminal in the current buffer
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h1&gt;
  
  
  Configuration file
&lt;/h1&gt;

&lt;p&gt;First, we need to create a &lt;code&gt;init.el&lt;/code&gt; file:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ mkdir -p ~/.emacs.d
$ touch ~/.emacs.d/init.el
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Now, Emacs will start with the ugly tools bar and status bar, just like&lt;br&gt;
any editor that I would uninstall right away. Let's change this.&lt;/p&gt;

&lt;h1&gt;
  
  
  Minimal UI
&lt;/h1&gt;

&lt;p&gt;Now, we need to disable all the ugly stuff and make Emacs just as clean&lt;br&gt;
and clear as Vim or Sublime, Atom,... or whatever modern editor today&lt;br&gt;
has.&lt;/p&gt;

&lt;p&gt;Put these to the beginning of your &lt;code&gt;init.el&lt;/code&gt;:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;;; Minimal UI
(scroll-bar-mode -1)
(tool-bar-mode   -1)
(tooltip-mode    -1)
(menu-bar-mode   -1)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Next, we'll add a package manager to start installing themes/packages.&lt;/p&gt;

&lt;h1&gt;
  
  
  Package Manager
&lt;/h1&gt;

&lt;p&gt;By default, Emacs is configured to use &lt;code&gt;ELPA&lt;/code&gt; package archive, we'll&lt;br&gt;
need to add more repository such as &lt;code&gt;GNU ELPA&lt;/code&gt;, &lt;code&gt;MELPA&lt;/code&gt;,...&lt;/p&gt;

&lt;p&gt;Put this to the beginning of your &lt;code&gt;init.el&lt;/code&gt;:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;;; Package configs
(require 'package)
(setq package-enable-at-startup nil)
(setq package-archives '(("org"   . "http://orgmode.org/elpa/")
                         ("gnu"   . "http://elpa.gnu.org/packages/")
                         ("melpa" . "https://melpa.org/packages/")))
(package-initialize)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Next, we'll use &lt;a href="https://github.com/jwiegley/use-package" rel="noopener noreferrer"&gt;use-package&lt;/a&gt;&lt;br&gt;
to configure our packages better, in case you don't know, this package&lt;br&gt;
provides a macro to allow you to easily install packages and isolate&lt;br&gt;
package configuration in a way that is both performance-oriented and&lt;br&gt;
tidy.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;;; Bootstrap `use-package`
(unless (package-installed-p 'use-package)
  (package-refresh-contents)
  (package-install 'use-package))
(require 'use-package)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h1&gt;
  
  
  Vim key binding with Evil Mode
&lt;/h1&gt;

&lt;p&gt;Now that you have package manager setted up, let's install our first&lt;br&gt;
package: &lt;code&gt;evil-mode&lt;/code&gt;, this package allows you to use Vim-like key&lt;br&gt;
binding in Emacs.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;;; Vim mode
(use-package evil
  :ensure t
  :config
  (evil-mode 1))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;That's it! Now restart your Emacs, you'll see the mode line displaying&lt;br&gt;
the current Vim mode, and you'll be able to navigate with &lt;code&gt;hjkl&lt;/code&gt;.&lt;/p&gt;

&lt;h1&gt;
  
  
  Installing Theme
&lt;/h1&gt;

&lt;p&gt;One of the best themes for Emacs I could found is &lt;code&gt;doom-themes&lt;/code&gt; package,&lt;br&gt;
that has a lot of cool themes. The following code will install this&lt;br&gt;
package and load its flagship theme &lt;code&gt;doom-one&lt;/code&gt;:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;;; Theme
(use-package doom-themes
  :ensure t
  :config
  (load-theme 'doom-one t))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h1&gt;
  
  
  Installing Helm
&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://github.com/emacs-helm/helm" rel="noopener noreferrer"&gt;Helm&lt;/a&gt; is a framework for&lt;br&gt;
incremental completions and narrowing selections in Emacs. Many people&lt;br&gt;
prefer &lt;code&gt;ivy&lt;/code&gt; because it's much lighter, for me, it's doesn't matter.&lt;br&gt;
I find &lt;code&gt;helm&lt;/code&gt; is easier to use and config.&lt;/p&gt;

&lt;p&gt;The following snippet will install &lt;code&gt;helm&lt;/code&gt; and configure &lt;em&gt;fuzzy&lt;br&gt;
matching&lt;/em&gt;:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;;; Helm
(use-package helm
  :ensure t
  :init
  (setq helm-mode-fuzzy-match t)
  (setq helm-completion-in-region-fuzzy-match t)
  (setq helm-candidate-number-list 50))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h1&gt;
  
  
  Installing Which Key
&lt;/h1&gt;

&lt;p&gt;In Spacemacs, when you press &lt;code&gt;SPC&lt;/code&gt; or any other key sequence, a small&lt;br&gt;
buffer will be popped up to show the list of features you can do next,&lt;br&gt;
installing &lt;code&gt;which-key&lt;/code&gt; will give you this.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;;; Which Key
(use-package which-key
  :ensure t
  :init
  (setq which-key-separator " ")
  (setq which-key-prefix-prefix "+")
  :config
  (which-key-mode))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h1&gt;
  
  
  Custom Key Binding
&lt;/h1&gt;

&lt;p&gt;Finally, you can start config your custom key binding with &lt;code&gt;general&lt;/code&gt;&lt;br&gt;
package, this is my config, the prefix is &lt;code&gt;SPC&lt;/code&gt; just like in Spacemacs:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;;; Custom keybinding
(use-package general
  :ensure t
  :config (general-define-key
  :states '(normal visual insert emacs)
  :prefix "SPC"
  :non-normal-prefix "M-SPC"
  ;; "/"   '(counsel-rg :which-key "ripgrep") ; You'll need counsel package for this
  "TAB" '(switch-to-prev-buffer :which-key "previous buffer")
  "SPC" '(helm-M-x :which-key "M-x")
  "pf"  '(helm-find-file :which-key "find files")
  ;; Buffers
  "bb"  '(helm-buffers-list :which-key "buffers list")
  ;; Window
  "wl"  '(windmove-right :which-key "move right")
  "wh"  '(windmove-left :which-key "move left")
  "wk"  '(windmove-up :which-key "move up")
  "wj"  '(windmove-down :which-key "move bottom")
  "w/"  '(split-window-right :which-key "split right")
  "w-"  '(split-window-below :which-key "split bottom")
  "wx"  '(delete-window :which-key "delete window")
  ;; Others
  "at"  '(ansi-term :which-key "open terminal")
))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h1&gt;
  
  
  Matching Titlebar color on MacOS
&lt;/h1&gt;

&lt;p&gt;If you're using Emacs on macOS, you can add this to have your titlebar&lt;br&gt;
color changed and matching your color theme:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;;; Fancy titlebar for MacOS
(add-to-list 'default-frame-alist '(ns-transparent-titlebar . t))
(add-to-list 'default-frame-alist '(ns-appearance . dark))
(setq ns-use-proxy-icon  nil)
(setq frame-title-format nil)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;




&lt;p&gt;From here, you can continue customizing Emacs as you need, for example,&lt;br&gt;
add &lt;a href="https://github.com/bbatsov/projectile" rel="noopener noreferrer"&gt;projectile&lt;/a&gt; package for&lt;br&gt;
project management, add more language supports, customize your mode&lt;br&gt;
line,...&lt;/p&gt;

&lt;p&gt;I hope you'll find this post helpful and will be able to build your own&lt;br&gt;
Emacs configuration. Also, you can check my &lt;a href="https://gist.github.com/huytd/6b785bdaeb595401d69adc7797e5c22c" rel="noopener noreferrer"&gt;customized configuration&lt;br&gt;
here&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>emacs</category>
    </item>
    <item>
      <title>Why do you use web-based Terminal emulators?</title>
      <dc:creator>Huy Tr.</dc:creator>
      <pubDate>Wed, 02 May 2018 23:35:20 +0000</pubDate>
      <link>https://dev.to/huytd/why-do-you-use-web-based-terminal-emulators-2em7</link>
      <guid>https://dev.to/huytd/why-do-you-use-web-based-terminal-emulators-2em7</guid>
      <description>&lt;p&gt;Just curious, not here to make a &lt;em&gt;flame war&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;I saw so many people using and recommending web-based terminal emulators such as Hyper.&lt;/p&gt;

&lt;p&gt;Personally, I actually gave it a try but totally disappointed when comparing to iTerm or Alacritty. I would not think about running tmux, vim or reading log files with &lt;code&gt;less&lt;/code&gt; in it.&lt;/p&gt;

&lt;p&gt;For those who still using it, I want to hear your opinions, is there any specific reason that you prefer Hyper over iTerm or anything else?&lt;/p&gt;

</description>
      <category>discuss</category>
      <category>terminal</category>
    </item>
    <item>
      <title>A note on Dynamic Programming</title>
      <dc:creator>Huy Tr.</dc:creator>
      <pubDate>Fri, 15 Dec 2017 02:57:53 +0000</pubDate>
      <link>https://dev.to/huytd/a-note-on-dynamic-programming-9gh</link>
      <guid>https://dev.to/huytd/a-note-on-dynamic-programming-9gh</guid>
      <description>&lt;p&gt;This is a quick note about Dynamic Programming from &lt;a href="http://www.algorist.com/"&gt;The Algorithm Design Manual&lt;/a&gt; book. I found it really helpful.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why dynamic programming is so hard?
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;ul&gt;
&lt;li&gt;Until you understand dynamic programming, it seems like magic.&lt;/li&gt;
&lt;li&gt;You must figure out the trick before you can use it.&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  The trick
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;ul&gt;
&lt;li&gt;Dynamic Programming is a technique for efficiently implementing a recursive algorithm by storing partial results.&lt;/li&gt;
&lt;li&gt;The trick is: seeing whether the naive recursive algorithm computes the same subproblems over and over again.&lt;/li&gt;
&lt;li&gt;If so, storing the answer for each subproblem in a table to lookup instead of recomputing.&lt;/li&gt;
&lt;li&gt;Start with a recursive algorithm or definition. Only once we have a correct recursive algorithm, do we worry about speeding it up by using a result matrix.&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  The advice on how to learn
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;ul&gt;
&lt;li&gt;Dynamic programming is best learned by carefully studying examples until things start to click&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;

</description>
      <category>algorithms</category>
    </item>
    <item>
      <title>Re-Implementing Facebook’s Reaction Animation</title>
      <dc:creator>Huy Tr.</dc:creator>
      <pubDate>Wed, 06 Dec 2017 02:02:58 +0000</pubDate>
      <link>https://dev.to/huytd/re-implementing-facebooks-reaction-animation-bee</link>
      <guid>https://dev.to/huytd/re-implementing-facebooks-reaction-animation-bee</guid>
      <description>&lt;p&gt;&lt;em&gt;Originally posted on &lt;a href="https://huytd.github.io/reimplementing-facebook-animation.html"&gt;my blog&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Disclaimer:&lt;/strong&gt; I’m not a Facebook engineer, and I don’t know how did they make it. This is just my own implementation. It may not have the best performance and I’m just tested on Chrome 😅&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--RIF-5tza--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/800/1%2AFcJ1dMlhikUNkUv7GSlv3A.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--RIF-5tza--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/800/1%2AFcJ1dMlhikUNkUv7GSlv3A.png" alt=""&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Facebook’s Reaction Popup — or whatever it called&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;If you are a Facebooker, chance you will use this popup every day.&lt;/p&gt;

&lt;p&gt;In this blog post, we will learn how to implement it. In HTML/CSS and a bit of JavaScript.&lt;/p&gt;
&lt;h1&gt;
  
  
  Breaking down the animation
&lt;/h1&gt;

&lt;p&gt;OK. Let’s break it down step by step, so we will see and understand the animation in detailed.&lt;/p&gt;

&lt;p&gt;There are 2 phases of the animation: &lt;strong&gt;Show up phase&lt;/strong&gt; and &lt;strong&gt;Mouse hover phase&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;For the sake of simplicity, this is the simplified version of a feed, on your News Feed. The green rectangle is where the Like button stays, you hover your mouse over it and a yellow popup (in reality, it‘s white) show up.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--gvPpHYtP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1000/1%2ANzur1ShozSVqzDh9nwgrBg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--gvPpHYtP--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1000/1%2ANzur1ShozSVqzDh9nwgrBg.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Then, the emoticons ascend, one by one. The red arrows here tells us the animation direction of the emoticons.&lt;/p&gt;

&lt;p&gt;If we look carefully into each emoticon, you will see it has the easing effect in its animation, like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--R3JbGlUM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/800/0%2A5Ky7VV0gTNcl0koU.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--R3JbGlUM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/800/0%2A5Ky7VV0gTNcl0koU.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;When all emoticons finish its animation, phase 1 (Show up) finished.&lt;/p&gt;

&lt;p&gt;Phase 2 (Mouse Hover) begins when the user hovers his mouse on an emoticon.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--IB_OWMRO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1000/1%2AuMheySPloQiUHzoipvqVBQ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--IB_OWMRO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/1000/1%2AuMheySPloQiUHzoipvqVBQ.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The selected emoticon will scale up a bit, and a black label appears on the top to tell us what is the name of this emoticon.&lt;/p&gt;

&lt;p&gt;Not so complex, huh?&lt;/p&gt;
&lt;h1&gt;
  
  
  Raw implementation
&lt;/h1&gt;

&lt;p&gt;Now, we start the implementation. In the beginning, we just implement the raw HTML structure, add some basic actions with CSS, no animation here in this part.&lt;/p&gt;

&lt;p&gt;It’s recommended to use Codepen.io for prototyping, so we have the live preview, SCSS compiling on the go,…&lt;/p&gt;

&lt;p&gt;But you can write code on your local machine, it’s up to you. All we need is: HTML, SCSS, and jQuery (or you can convert the jQuery code at the end of this post to vanilla JS or whatever you want)&lt;/p&gt;
&lt;h2&gt;
  
  
  Prototyping the news feed post
&lt;/h2&gt;

&lt;p&gt;So we want to create a news feed item, and add a Like button to it.&lt;/p&gt;

&lt;p&gt;HTML Code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div class="feed"&amp;gt; 
  &amp;lt;a class="like-btn"&amp;gt;&amp;lt;/a&amp;gt; 
&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;CSS (SCSS):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight scss"&gt;&lt;code&gt;&lt;span class="nt"&gt;html&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt;
&lt;span class="nt"&gt;body&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;padding&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;20px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;font-family&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;sans-serif&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nc"&gt;.feed&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;500px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;height&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;473px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;background-image&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="sx"&gt;url(&amp;lt;path-to-placeholder-image&amp;gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;position&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;relative&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nc"&gt;.like-btn&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nl"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;44px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="nl"&gt;height&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;25px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="nl"&gt;background&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mh"&gt;#D0D0D0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="nl"&gt;position&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;absolute&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="nl"&gt;bottom&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;13px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="nl"&gt;left&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;13px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="nl"&gt;cursor&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;pointer&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nd"&gt;:hover&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nl"&gt;background&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mh"&gt;#718C00&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can copy the URL of the image below and replace to &lt;code&gt;&amp;lt;path-to-placeholder-image&amp;gt;&lt;/code&gt;, or use the uploaded image &lt;a href="http://i.imgur.com/HckmGbj.png"&gt;on Imgur&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--rCjJBNlU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/800/1%2AxJwaLxGbCvBVBVcRZl_sjA.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--rCjJBNlU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/800/1%2AxJwaLxGbCvBVBVcRZl_sjA.png" alt=""&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Copy this image URL as a placeholder image&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Now you have your news feed prototype, try to hover over the Like button, see? It’s green! It’s cool now, isn’t it?&lt;/p&gt;
&lt;h2&gt;
  
  
  Display the reaction box
&lt;/h2&gt;

&lt;p&gt;The next step is adding the reaction box. This will be a &lt;strong&gt;312x55&lt;/strong&gt; pixels rectangle with rounded corners.&lt;/p&gt;

&lt;p&gt;Reaction box will only show up when the user hovers on the Like button.&lt;/p&gt;

&lt;p&gt;OK, let’s create a div, the class name is &lt;code&gt;reaction-box&lt;/code&gt;, put it inside the Like button.&lt;/p&gt;

&lt;p&gt;HTML Code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div class="feed"&amp;gt; 
  &amp;lt;a class="like-btn"&amp;gt; 
    &amp;lt;div class="reaction-box"&amp;gt;&amp;lt;/div&amp;gt; 
  &amp;lt;/a&amp;gt; 
&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Our reaction box will be hidden, so we set its display attribute to none. And in Like button’s &lt;code&gt;:hover&lt;/code&gt; event, we set it back to &lt;code&gt;block&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;CSS (SCSS):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight scss"&gt;&lt;code&gt;&lt;span class="nc"&gt;.like-btn&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
  &lt;span class="nc"&gt;...&lt;/span&gt; 

  &lt;span class="nc"&gt;.reaction-box&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
    &lt;span class="nl"&gt;position&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;absolute&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
    &lt;span class="nl"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;312px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nl"&gt;height&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;55px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
    &lt;span class="nl"&gt;background&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mh"&gt;#F0C674&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
    &lt;span class="nl"&gt;border-radius&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;28px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
    &lt;span class="nl"&gt;left&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;-25px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nl"&gt;bottom&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;25px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
    &lt;span class="nl"&gt;display&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;none&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
  &lt;span class="p"&gt;}&lt;/span&gt; 

  &lt;span class="k"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nd"&gt;:hover&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
    &lt;span class="nc"&gt;...&lt;/span&gt; 

    &lt;span class="nc"&gt;.reaction-box&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
      &lt;span class="nl"&gt;display&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;block&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
    &lt;span class="p"&gt;}&lt;/span&gt; 
  &lt;span class="p"&gt;}&lt;/span&gt; 
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is the result:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--XMdh_KyM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://cdn-images-1.medium.com/max/800/0%2A4oEgO5pJQP54ZznK.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--XMdh_KyM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://cdn-images-1.medium.com/max/800/0%2A4oEgO5pJQP54ZznK.gif" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Adding the emoticons
&lt;/h2&gt;

&lt;p&gt;Now we have the reaction box displayed, let’s add some emoticons so it will look like the real Facebook 🤗&lt;/p&gt;

&lt;p&gt;Because we’re just prototyping, so, just use the small circles to represent our emoticons.&lt;/p&gt;

&lt;p&gt;This is the HTML structure for an emoticon:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div class="reaction-icon"&amp;gt; 
  &amp;lt;label&amp;gt;Like&amp;lt;/label&amp;gt; 
&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We also need a label inside, so we can show the user what’s the name of this emoticon.&lt;/p&gt;

&lt;p&gt;Let’s add 6 reaction icons to represent the real Facebook’s reactions: &lt;em&gt;Like, Love, Haha, Wow, Sad&lt;/em&gt; and &lt;em&gt;Angry&lt;/em&gt;. Put them inside reaction box.&lt;/p&gt;

&lt;p&gt;Then we use CSS transform to scale up these emoticons on mouse hover event.&lt;/p&gt;

&lt;p&gt;HTML Code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;...
&amp;lt;div class="reaction-box"&amp;gt;
    &amp;lt;div class="reaction-icon"&amp;gt;
        &amp;lt;label&amp;gt;Like&amp;lt;/label&amp;gt;
    &amp;lt;/div&amp;gt;
    &amp;lt;div class="reaction-icon"&amp;gt;
        &amp;lt;label&amp;gt;Love&amp;lt;/label&amp;gt;
    &amp;lt;/div&amp;gt;
    &amp;lt;div class="reaction-icon"&amp;gt;
        &amp;lt;label&amp;gt;Haha&amp;lt;/label&amp;gt;
    &amp;lt;/div&amp;gt;
    &amp;lt;div class="reaction-icon"&amp;gt;
        &amp;lt;label&amp;gt;Wow&amp;lt;/label&amp;gt;
    &amp;lt;/div&amp;gt;
    &amp;lt;div class="reaction-icon"&amp;gt;
        &amp;lt;label&amp;gt;Sad&amp;lt;/label&amp;gt;
    &amp;lt;/div&amp;gt;
    &amp;lt;div class="reaction-icon"&amp;gt;
        &amp;lt;label&amp;gt;Angry&amp;lt;/label&amp;gt;
    &amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt; 
...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Insert this CSS inside reaction box:&lt;/p&gt;

&lt;p&gt;CSS (SCSS):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight scss"&gt;&lt;code&gt;&lt;span class="nc"&gt;.reaction-box&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nc"&gt;...&lt;/span&gt;
  &lt;span class="o"&gt;//&lt;/span&gt; &lt;span class="nt"&gt;insert&lt;/span&gt; &lt;span class="nt"&gt;here&lt;/span&gt;
  &lt;span class="nc"&gt;.reaction-icon&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;40px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;height&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;40px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;display&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;inline-block&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;background&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mh"&gt;#8959A8&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;border-radius&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;20px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;margin&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;8px&lt;/span&gt; &lt;span class="m"&gt;-1px&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt; &lt;span class="m"&gt;8px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nl"&gt;text-align&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;center&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="nt"&gt;label&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nl"&gt;padding&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;3px&lt;/span&gt; &lt;span class="m"&gt;5px&lt;/span&gt; &lt;span class="m"&gt;3px&lt;/span&gt; &lt;span class="m"&gt;5px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="nl"&gt;position&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;relative&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="nl"&gt;top&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;-24px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="nl"&gt;border-radius&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;10px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="nl"&gt;font-size&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;11px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="nl"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mh"&gt;#FFF&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="nl"&gt;background&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mh"&gt;#333&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And insert this inside &lt;code&gt;:hover&lt;/code&gt; event of the Like button.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight scss"&gt;&lt;code&gt;&lt;span class="k"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nd"&gt;:hover&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nc"&gt;...&lt;/span&gt; 

  &lt;span class="nc"&gt;.reaction-box&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;...&lt;/span&gt; 

    &lt;span class="nc"&gt;.reaction-icon&lt;/span&gt;&lt;span class="nd"&gt;:hover&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;   &lt;span class="c1"&gt;// changed here!&lt;/span&gt;
      &lt;span class="nl"&gt;transform&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;scale&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="mi"&gt;.4&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="nl"&gt;transform-origin&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;bottom&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We scale up the emoticon 1.4 times when the user hovers his mouse to Like button. We also set the &lt;code&gt;transform-origin&lt;/code&gt; attribute to bottom, so the origin of the scale-up effect will be on the bottom edge of the emoticon.&lt;/p&gt;

&lt;p&gt;And this is the result:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--krMGGkD9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://cdn-images-1.medium.com/max/800/0%2A3WebZQ_7XuIOB9TN.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--krMGGkD9--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://cdn-images-1.medium.com/max/800/0%2A3WebZQ_7XuIOB9TN.gif" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Did you see a bug?
&lt;/h2&gt;

&lt;p&gt;You may notice that the reaction box is too close to the Like button, it should not. How about moving it up a bit?&lt;/p&gt;

&lt;p&gt;Okay, since reaction box has an absolute position, let’s change its bottom attribute from &lt;code&gt;25px&lt;/code&gt; to &lt;code&gt;35px&lt;/code&gt; (10px upper)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight scss"&gt;&lt;code&gt;&lt;span class="nc"&gt;.reaction-box&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
  &lt;span class="err"&gt;... 
  &lt;/span&gt;&lt;span class="nl"&gt;left&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;-25px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nl"&gt;bottom&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;35px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
  &lt;span class="nc"&gt;...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Looks better now, right? But wait! WT*?? It’s broken! I can’t hover my mouse to the reaction box anymore!! What did you do??? Why you break my code???&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--xNzMmaxY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://cdn-images-1.medium.com/max/800/0%2AYlU4lgZ8s7kYk1zv.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--xNzMmaxY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://cdn-images-1.medium.com/max/800/0%2AYlU4lgZ8s7kYk1zv.gif" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;OK. Calm down. Let’s take a step back, and look at the code.&lt;/p&gt;

&lt;p&gt;The reason is that the distance between reaction box and Like button now increased to &lt;code&gt;10px&lt;/code&gt;, so, when you tried to move the cursor to reaction box, it will go out of Like button’s hover region, so it triggers the mouse out event. If you look at the CSS rules, you can see we display the reaction box based on Like button's hover event. On mouse out, reaction box will be back to its original state (&lt;code&gt;display: none&lt;/code&gt;)&lt;/p&gt;

&lt;p&gt;The solution is: Increase the hover region of Like button, so it can fill the distance to reaction box.&lt;/p&gt;

&lt;p&gt;There are many ways to do it, we will take the simplest way: Add the &lt;code&gt;::before&lt;/code&gt; pseudo-element to Like button, it will be a box with the size of &lt;code&gt;44x10&lt;/code&gt;, and it’s transparent, so user can’t see it but it will fill the distance to reaction box.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight scss"&gt;&lt;code&gt;&lt;span class="nc"&gt;.like-btn&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
  &lt;span class="err"&gt;... 

  &amp;amp;&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="n"&gt;before&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
    &lt;span class="nl"&gt;content&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"."&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
    &lt;span class="nl"&gt;opacity&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
    &lt;span class="nl"&gt;display&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;block&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
    &lt;span class="nl"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;44px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nl"&gt;height&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;10px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
    &lt;span class="nl"&gt;position&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;absolute&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
    &lt;span class="nl"&gt;top&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;-10px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nl"&gt;left&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now you can refresh the page. It should be fine now :)&lt;/p&gt;

&lt;h2&gt;
  
  
  Display emoticon’s label only on mouse hover
&lt;/h2&gt;

&lt;p&gt;The next thing to do is to hide all emoticon’s labels and display them when user hover on each emoticon.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight scss"&gt;&lt;code&gt;&lt;span class="nt"&gt;label&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
   &lt;span class="err"&gt;... 
   &lt;/span&gt;&lt;span class="nl"&gt;visibility&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;hidden&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then display them in &lt;code&gt;:hover&lt;/code&gt; event of reaction icon (emoticon).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.reaction-icon:hover { 
   ... 
   label { 
      visibility: visible; 
   } 
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is the result:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--vIp1tvaM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://cdn-images-1.medium.com/max/800/0%2AgANROpNDCZ71L6lE.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--vIp1tvaM--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://cdn-images-1.medium.com/max/800/0%2AgANROpNDCZ71L6lE.gif" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Alright, let’s take a break here. It’s a long post and you need some rest, so do I =]]&lt;/p&gt;

&lt;p&gt;You can revise the full code for this raw implementation phase here: &lt;a href="http://codepen.io/huytd/pen/ZOEoMe"&gt;Codepen — Facebook Reaction Animation — Part 1&lt;/a&gt;.&lt;/p&gt;

&lt;h1&gt;
  
  
  Adding Animation
&lt;/h1&gt;

&lt;p&gt;Now, let’s the pain begin. In this part, we will implement some animations to give the better feeling for users.&lt;/p&gt;

&lt;h2&gt;
  
  
  Emoticon zooming animation
&lt;/h2&gt;

&lt;p&gt;Let’s start with the simplest one. We will slowly zooming the emoticon by adding transition: all 0.3s; attribute to emoticons:&lt;/p&gt;

&lt;p&gt;CSS (SCSS):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight scss"&gt;&lt;code&gt;&lt;span class="nt"&gt;reaction-icon&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
   &lt;span class="err"&gt;... 
   // &lt;/span&gt;&lt;span class="na"&gt;Animation&lt;/span&gt;&lt;span class="err"&gt; 
   &lt;/span&gt;&lt;span class="nl"&gt;transition&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;all&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="mi"&gt;.3s&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By adding transition with &lt;code&gt;all&lt;/code&gt; and &lt;code&gt;0.3s&lt;/code&gt; parameters, we tell the browser that all of the emoticon’s attributes can be animated (slowly changing) in the duration of &lt;code&gt;0.3&lt;/code&gt; seconds.&lt;/p&gt;

&lt;p&gt;So this is our first animation:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--MAJcGnvs--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://cdn-images-1.medium.com/max/800/0%2A2ne_o-UDUnQ8eexr.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--MAJcGnvs--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://cdn-images-1.medium.com/max/800/0%2A2ne_o-UDUnQ8eexr.gif" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you have no idea about &lt;code&gt;transition&lt;/code&gt;, you may want to read this article &lt;a href="https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Transitions/Using_CSS_transitions"&gt;Using CSS transitions — MDN&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Emoticon’s show up animation
&lt;/h2&gt;

&lt;p&gt;Let’s take a look at the show-up animation again:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Ti35fvsl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/800/0%2AYC6_ONVx03CkCIzn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Ti35fvsl--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/800/0%2AYC6_ONVx03CkCIzn.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This animation can be represented in a graph:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--3LJBppbb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/800/0%2AXjJATseUYcp6n4nP.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--3LJBppbb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/800/0%2AXjJATseUYcp6n4nP.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you see, the y-axis represents emoticon’s y position over time.&lt;/p&gt;

&lt;p&gt;The function that represents the graph above is also the way we can control how the attributes change over time, they called: easing functions (or timing functions).&lt;/p&gt;

&lt;p&gt;In CSS transition, we can set the timing functions for an element by using transition-timing-function attribute.&lt;/p&gt;

&lt;p&gt;You should read more about timing function here &lt;a href="https://developer.mozilla.org/en-US/docs/Web/CSS/timing-function"&gt;Timing Function — MDN&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The timing function we gonna use here is &lt;code&gt;easeOutBack&lt;/code&gt;, it’s a bezier that can be defined in CSS by using &lt;code&gt;cubic-bezier()&lt;/code&gt; function.&lt;/p&gt;

&lt;p&gt;We will use the predefined &lt;code&gt;cubic-bezier()&lt;/code&gt; function for &lt;code&gt;easeOutBack&lt;/code&gt; from here &lt;a href="http://easings.net/#easeOutBack"&gt;Easing Function — easeOutBack&lt;/a&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;cubic&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="nx"&gt;bezier&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;0.175&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;0.885&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;0.32&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;1.275&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Add this function to &lt;code&gt;reaction-icon&lt;/code&gt;’s &lt;code&gt;transition&lt;/code&gt; attribute:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight scss"&gt;&lt;code&gt;&lt;span class="nc"&gt;.reaction-icon&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
   &lt;span class="err"&gt;... 
   // &lt;/span&gt;&lt;span class="na"&gt;Animation&lt;/span&gt;&lt;span class="err"&gt; 
   &lt;/span&gt;&lt;span class="nl"&gt;transition&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;all&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="mi"&gt;.3s&lt;/span&gt; &lt;span class="nf"&gt;cubic-bezier&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="mi"&gt;.175&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="mi"&gt;.885&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="mi"&gt;.32&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="mi"&gt;.275&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The emoticons will appear with the starting opacity is 0, scaling is 0 and the position is 100px away from the desired position — we call this is the first state.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight scss"&gt;&lt;code&gt;&lt;span class="nc"&gt;.reaction-icon&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
   &lt;span class="err"&gt;... 
   // &lt;/span&gt;&lt;span class="na"&gt;Animation&lt;/span&gt;&lt;span class="err"&gt; 
   &lt;/span&gt;&lt;span class="nl"&gt;opacity&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
   &lt;span class="nl"&gt;transform&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;translate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="m"&gt;100px&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nf"&gt;scale&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we add a new class to define the final state of its animation, let’s call it &lt;code&gt;.show&lt;/code&gt;, and put it in the &lt;code&gt;:hover&lt;/code&gt; event of Like button:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight scss"&gt;&lt;code&gt;&lt;span class="k"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nd"&gt;:hover&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
   &lt;span class="nc"&gt;...&lt;/span&gt; 
   &lt;span class="nc"&gt;.reaction-box&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
      &lt;span class="nc"&gt;...&lt;/span&gt; 
      &lt;span class="nc"&gt;.reaction-icon&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
         &lt;span class="k"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nc"&gt;.show&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
            &lt;span class="nl"&gt;opacity&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
            &lt;span class="nl"&gt;transform&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;translate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nf"&gt;scale&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; 
         &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So, what will happen here? When the user hovers on the Like button, we search for the emoticons and assign the class &lt;code&gt;.show&lt;/code&gt; to activate the animation. We can do it with JavaScript (jQuery - lol, it's okay, you can use anything else):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;.like-btn&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;hover&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;.reaction-icon&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;each&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;element&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nx"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="nx"&gt;$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;element&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;addClass&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;show&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="nx"&gt;index&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;});&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;.reaction-icon&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;removeClass&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;show&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You may notice the &lt;code&gt;setTimeout()&lt;/code&gt;, we use it to delay the &lt;code&gt;addClass&lt;/code&gt; action on each emoticon, based on its index. From the first emoticon (index = 0) to the last one (index = 5), we have the particular delay time is 0, 100ms, 200ms, 300ms, 400ms and 500ms. So we have the chasing effect of emoticons show up.&lt;/p&gt;

&lt;p&gt;Now refresh and see:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--b7i1PyG2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://cdn-images-1.medium.com/max/800/0%2AiZ0mNTv2DGVWzo9e.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--b7i1PyG2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://cdn-images-1.medium.com/max/800/0%2AiZ0mNTv2DGVWzo9e.gif" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We’re done!&lt;/p&gt;

&lt;p&gt;If you get lost somewhere in the middle of this post, don’t panic, let’s head to this page to see the source code: &lt;a href="http://codepen.io/huytd/pen/beGKWB"&gt;Codepen — Facebook Reaction Animation — Part 2&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;What’s left to be done is reskin the elements to get more realistic result like this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--elwUIX4z--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://cdn-images-1.medium.com/max/800/0%2A18lv6hj6T_R-epu8.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--elwUIX4z--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://cdn-images-1.medium.com/max/800/0%2A18lv6hj6T_R-epu8.gif" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;See higher resolution video here &lt;a href="https://gfycat.com/ZigzagForthrightCob"&gt;https://gfycat.com/ZigzagForthrightCob&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;If you want more improvement, you may want to consider &lt;a href="http://bjk5.com/post/44698559168/breaking-down-amazons-mega-dropdown"&gt;Amazon’s Magic Triangle&lt;/a&gt; (and &lt;a href="https://css-tricks.com/dropdown-menus-with-more-forgiving-mouse-movement-paths/"&gt;its implementation here&lt;/a&gt;) to get more stable when moving your cursor.&lt;/p&gt;

&lt;p&gt;Hope you like this post. If you spot any error or have any question, feel free to leave a comment so we can discuss.&lt;/p&gt;

&lt;p&gt;See you in the next posts. Happy CSSing ^^&lt;/p&gt;

</description>
      <category>css</category>
      <category>scss</category>
      <category>frontend</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Turning off the syntax highlighter</title>
      <dc:creator>Huy Tr.</dc:creator>
      <pubDate>Thu, 12 Oct 2017 19:08:57 +0000</pubDate>
      <link>https://dev.to/huytd/turning-off-the-syntax-highlighter-8af</link>
      <guid>https://dev.to/huytd/turning-off-the-syntax-highlighter-8af</guid>
      <description>&lt;p&gt;Well... first of all, this is a well-known topic that widely discussed everywhere already.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;[1] &lt;a href="https://www.robertmelton.com/project/syntax-highlighting-off/" rel="noopener noreferrer"&gt;https://www.robertmelton.com/project/syntax-highlighting-off/&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;[2] &lt;a href="https://dudzik.co/digress-into-development/syntax-off/" rel="noopener noreferrer"&gt;https://dudzik.co/digress-into-development/syntax-off/&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;[3] &lt;a href="http://www.linusakesson.net/programming/syntaxhighlighting/" rel="noopener noreferrer"&gt;http://www.linusakesson.net/programming/syntaxhighlighting/&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;[4] &lt;a href="https://groups.google.com/forum/#!msg/golang-nuts/hJHCAaiL0so/kG3BHV6QFfIJ" rel="noopener noreferrer"&gt;https://groups.google.com/forum/#!msg/golang-nuts/hJHCAaiL0so/kG3BHV6QFfIJ&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;[5] &lt;a href="http://howivim.com/2016/damian-conway/" rel="noopener noreferrer"&gt;http://howivim.com/2016/damian-conway/&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;[6] &lt;a href="https://twitter.com/enneff/status/710071512330477568" rel="noopener noreferrer"&gt;https://twitter.com/enneff/status/710071512330477568&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;[7] &lt;a href="https://www.youtube.com/watch?feature=player_embedded&amp;amp;v=dkZFtimgAcM#t=15m58s" rel="noopener noreferrer"&gt;https://www.youtube.com/watch?feature=player_embedded&amp;amp;v=dkZFtimgAcM#t=15m58s&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So I started turning off my Vim's syntax color from May this year, it's 5 months already.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthefullsnack.com%2Fposts%2Fimg%2Fno-color-vim.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthefullsnack.com%2Fposts%2Fimg%2Fno-color-vim.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;There some real benefits for it that I can confirmed now:'&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Getting more focus on the work, that mean, more productivity&lt;/li&gt;
&lt;li&gt;Read/understand the code better, and even faster&lt;/li&gt;
&lt;li&gt;Vim run faster as well&lt;/li&gt;
&lt;li&gt;
&lt;del&gt;Being way more cooler&lt;/del&gt;, my colleagues now look at me as a guy from Mars&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Is there anyone doing the same thing?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;P/S:&lt;/strong&gt; This is the color scheme I'm using for my vim &lt;a href="https://gist.github.com/huytd/8394f21bda3a08be025813c060d64e75" rel="noopener noreferrer"&gt;https://gist.github.com/huytd/8394f21bda3a08be025813c060d64e75&lt;/a&gt;&lt;/p&gt;

</description>
      <category>vim</category>
      <category>code</category>
      <category>programming</category>
      <category>discuss</category>
    </item>
    <item>
      <title>Coder's Life Comic 003: RegEx</title>
      <dc:creator>Huy Tr.</dc:creator>
      <pubDate>Sun, 08 Oct 2017 20:04:50 +0000</pubDate>
      <link>https://dev.to/huytd/coders-life-comic-003-regex-ab4</link>
      <guid>https://dev.to/huytd/coders-life-comic-003-regex-ab4</guid>
      <description>&lt;p&gt;The risk of overusing RegEx in your project ðŸ˜‚&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--0q9wC5HK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/v80sccjuqgcq71a22wpm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--0q9wC5HK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/v80sccjuqgcq71a22wpm.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>fun</category>
      <category>comic</category>
    </item>
    <item>
      <title>Coder's Life Comic 002: Learning JavaScript</title>
      <dc:creator>Huy Tr.</dc:creator>
      <pubDate>Sun, 08 Oct 2017 20:02:06 +0000</pubDate>
      <link>https://dev.to/huytd/coders-life-comic-002-learning-javascript-84g</link>
      <guid>https://dev.to/huytd/coders-life-comic-002-learning-javascript-84g</guid>
      <description>&lt;p&gt;Being a JavaScript developer these days is tough...&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--bDqyBrRR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/jlw1vek74esh8wh8dwsa.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--bDqyBrRR--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/jlw1vek74esh8wh8dwsa.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>fun</category>
      <category>comic</category>
    </item>
    <item>
      <title>Coder's Life Comic 001: Algorithm</title>
      <dc:creator>Huy Tr.</dc:creator>
      <pubDate>Sun, 08 Oct 2017 19:59:49 +0000</pubDate>
      <link>https://dev.to/huytd/coders-life-comic-001-algorithm-8jj</link>
      <guid>https://dev.to/huytd/coders-life-comic-001-algorithm-8jj</guid>
      <description>&lt;p&gt;Don't you think &lt;code&gt;npm&lt;/code&gt; got everything we need?&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--9foZmaPE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/57345ul20xw0dcgo4dss.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--9foZmaPE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/57345ul20xw0dcgo4dss.png" alt=""&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>comic</category>
      <category>fun</category>
    </item>
    <item>
      <title>Setup Travis CI for Rust Diesel project</title>
      <dc:creator>Huy Tr.</dc:creator>
      <pubDate>Fri, 06 Oct 2017 22:43:06 +0000</pubDate>
      <link>https://dev.to/huytd/setup-travis-ci-for-rust-diesel-project-amj</link>
      <guid>https://dev.to/huytd/setup-travis-ci-for-rust-diesel-project-amj</guid>
      <description>&lt;p&gt;Rust is &lt;a href="https://docs.travis-ci.com/user/languages/rust/"&gt;supported on Travis CI&lt;/a&gt;, there are three main channels of Rust you can use: &lt;em&gt;stable&lt;/em&gt;, &lt;em&gt;beta&lt;/em&gt; and &lt;em&gt;nightly&lt;/em&gt;. You can just follow their instruction to get your Rust project build smoothly.&lt;/p&gt;

&lt;p&gt;But when it come to &lt;a href="https://diesel.rs"&gt;diesel.rs&lt;/a&gt;, it will be a bit tricky.&lt;/p&gt;

&lt;p&gt;The reason is, in order to build a diesel-based project, you will need to have:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A database on your build machine&lt;/li&gt;
&lt;li&gt;A &lt;code&gt;.env&lt;/code&gt; file with a configuration to connect to that database&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;diesel-cli&lt;/strong&gt; installed to run migration&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So, it will require some more configuration.&lt;/p&gt;

&lt;p&gt;Let's create a &lt;code&gt;.travis.yml&lt;/code&gt; file in your project's root folder.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 1: Rust version
&lt;/h2&gt;

&lt;p&gt;You can select any version of Rust in your Travis CI, put this to your config file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;language&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;rust&lt;/span&gt;
&lt;span class="na"&gt;rust&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;nightly&lt;/span&gt;
  &lt;span class="s"&gt;// or&lt;/span&gt;
  &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;1.22.0&lt;/span&gt;
  &lt;span class="s"&gt;// or&lt;/span&gt;
  &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;stable&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 2: PostgreSQL (or what ever database you use)
&lt;/h2&gt;

&lt;p&gt;Next, we need to add PostgreSQL as an extra service:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;services&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;postgresql&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you're using a different database, just change it. For the complete list of supported database, please take a look at &lt;a href="https://docs.travis-ci.com/user/database-setup/"&gt;this document&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 3: Init your database and migration
&lt;/h2&gt;

&lt;p&gt;Now we need to do a lot of things, create a databse, install &lt;strong&gt;diesel-cli&lt;/strong&gt;, create a database and run migration, let create a &lt;code&gt;before_script&lt;/code&gt; section:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;before_script&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;psql -c 'create database build_db;' -U postgres&lt;/span&gt;
  &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;echo "DATABASE_URL=postgres://postgres@localhost/build_db" &amp;gt; .env&lt;/span&gt;
  &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;cargo install diesel_cli --no-default-features --features=postgres&lt;/span&gt;
  &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;diesel migration run&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Step 4: Build and Test
&lt;/h2&gt;

&lt;p&gt;Finally, build and test your project with &lt;code&gt;cargo&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;script&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt;
  &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;cargo build --verbose --all&lt;/span&gt;
  &lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;cargo test --verbose --all&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's all you need!&lt;/p&gt;

&lt;p&gt;If you are too lazy to follow the four steps, &lt;a href="https://gist.github.com/huytd/8438a0d3fe1510a483a0499e63a07925"&gt;this is a gist&lt;/a&gt; for you.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;P/S:&lt;/strong&gt; It's worth to mention that due to a lot of &lt;code&gt;cargo&lt;/code&gt; run, the build time is so freaking slow, with the above gist, my project took approximately 8 to 9 mins per build.&lt;/p&gt;

&lt;p&gt;You might want to add some caching for &lt;code&gt;cargo&lt;/code&gt; in your &lt;code&gt;.travis.yml&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="na"&gt;cache&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;cargo&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;See more about &lt;a href="https://docs.travis-ci.com/user/caching/"&gt;dependencies caching here&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>rust</category>
      <category>travisci</category>
    </item>
    <item>
      <title>Rust for the web</title>
      <dc:creator>Huy Tr.</dc:creator>
      <pubDate>Mon, 02 Oct 2017 20:39:17 +0000</pubDate>
      <link>https://dev.to/huytd/rust-for-the-web-55</link>
      <guid>https://dev.to/huytd/rust-for-the-web-55</guid>
      <description>&lt;p&gt;&lt;em&gt;Original posted on &lt;a href="https://thefullsnack.com/en/rust-for-the-web.html" rel="noopener noreferrer"&gt;my blog&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;




&lt;p&gt;&lt;em&gt;"Can we create web apps in Rust?"&lt;/em&gt; - Many people asked. So am I.&lt;/p&gt;

&lt;p&gt;My primary goal when I started learning Rust is to make a move from my ducky JavaScript tech stack (Node.js Express), that means, to make web apps in Rust.&lt;/p&gt;

&lt;p&gt;And after a year, I learned something. So I write this post to tell you my journal into Rust Web Programming.&lt;/p&gt;

&lt;p&gt;There are many approach to creating web apps in Rust, for example, compile Rust code to client-side JavaScript, writing a RESTful API, or building an isomorphic web app just like it's 2012. I'll go one by one.&lt;/p&gt;

&lt;h2&gt;
  
  
  Client-side JavaScript in Rust
&lt;/h2&gt;

&lt;p&gt;The first thing that came to my mind is to have component-based architecture just like React. This mean, you need to find a way to run Rust code on the web browser.&lt;/p&gt;

&lt;p&gt;Thanks to ASM.js and WebAssembly, this is possible, with &lt;a href="https://users.rust-lang.org/t/compiling-to-the-web-with-rust-and-emscripten/7627" rel="noopener noreferrer"&gt;a quick setup&lt;/a&gt;, you can compile your Rust code to ASM.js/WebAssembly in few minutes.&lt;/p&gt;

&lt;p&gt;Now, for the component-based architecture, I just created some macros to wrap over the &lt;a href="https://crates.io/crates/stdweb" rel="noopener noreferrer"&gt;stdweb&lt;/a&gt; crate and rendering HTML elements via browser's DOM API.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/huytd/rust-webapp-template/blob/master/src/main.rs#L13-L58" rel="noopener noreferrer"&gt;https://github.com/huytd/rust-webapp-template/blob/master/src/main.rs#L13-L58&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Don't be scared if you think I just posted an alien language. The &lt;code&gt;component!&lt;/code&gt; macro is used to define a custom element. The &lt;code&gt;mount_component!&lt;/code&gt; macro is used to append an element to the document, and &lt;code&gt;html!&lt;/code&gt; macro is to create an element from HTML string.&lt;/p&gt;

&lt;p&gt;Here is how I use it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="nd"&gt;component!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;AppComponent&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;init&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;Element&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nd"&gt;html!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"
        &amp;lt;div&amp;gt;
            &amp;lt;p&amp;gt;
                &amp;lt;span&amp;gt;Hello&amp;lt;/span&amp;gt;
                &amp;lt;span&amp;gt;World&amp;lt;/span&amp;gt;
            &amp;lt;/p&amp;gt;
            &amp;lt;GreenButton /&amp;gt;
        &amp;lt;/div&amp;gt;
        "&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;button&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;GreenButton&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="nd"&gt;mount_component!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"GreenButton"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;button&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="n"&gt;e&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="n"&gt;render&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;|&lt;/span&gt;&lt;span class="n"&gt;this&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;Element&lt;/span&gt;&lt;span class="p"&gt;|&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="nd"&gt;component!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;GreenButton&lt;/span&gt; &lt;span class="k"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;init&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;button&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;document&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="nf"&gt;.create_element&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"button"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;button&lt;/span&gt;&lt;span class="nf"&gt;.add_event_listener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;move&lt;/span&gt; &lt;span class="p"&gt;|&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;ClickEvent&lt;/span&gt;&lt;span class="p"&gt;|&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nd"&gt;js!&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="nf"&gt;alert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Yo! This is the Green Button!"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;});&lt;/span&gt;
        &lt;span class="n"&gt;button&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="n"&gt;render&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;|&lt;/span&gt;&lt;span class="n"&gt;this&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;Element&lt;/span&gt;&lt;span class="p"&gt;|&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;this&lt;/span&gt;&lt;span class="nf"&gt;.set_text_content&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"This is a button"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nn"&gt;stdweb&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;initialize&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="k"&gt;mut&lt;/span&gt; &lt;span class="n"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nn"&gt;AppComponent&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="nd"&gt;mount_component!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;document&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="s"&gt;"#root"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="nn"&gt;stdweb&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;event_loop&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Who need React anymore when you can even write an &lt;code&gt;onClick&lt;/code&gt; event in your Rust code :D (just kidding).&lt;/p&gt;

&lt;p&gt;You might want to &lt;a href="https://github.com/huytd/rust-webapp-template" rel="noopener noreferrer"&gt;take a look at the full project&lt;/a&gt; to see in details how to use &lt;code&gt;stdweb&lt;/code&gt; with these macros.&lt;/p&gt;

&lt;h2&gt;
  
  
  RESTful API in Rust
&lt;/h2&gt;

&lt;p&gt;If you don't want to give up your JavaScript frontend, this is the better approach: Just write an API server in Rust to make it work with your exist frontend.&lt;/p&gt;

&lt;p&gt;There are a handful number of frameworks available for you to choose. Let's take a look at &lt;a href="http://www.arewewebyet.org/" rel="noopener noreferrer"&gt;AreWeWebYet&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;For people who prefer to have slimmer framework, &lt;a href="https://crates.io/crates/tiny_http" rel="noopener noreferrer"&gt;tiny_http&lt;/a&gt; is the best choice.&lt;/p&gt;

&lt;p&gt;For simplicty, you can use &lt;a href="https://github.com/nickel-org/nickel.rs" rel="noopener noreferrer"&gt;nickel.rs&lt;/a&gt;, it's one of the most lightweight framework in Rust that inspired by Node's Express.&lt;/p&gt;

&lt;p&gt;For HTTP/2 support, &lt;a href="https://github.com/mlalic/solicit" rel="noopener noreferrer"&gt;solicit&lt;/a&gt; is the only choice you have right now.&lt;/p&gt;

&lt;p&gt;Personally, I prefer to use &lt;a href="https://rocket.rs" rel="noopener noreferrer"&gt;Rocket&lt;/a&gt; as it is a very good framework that has many features, but still keep your code simple, with very elegant syntax. It also added built-in TLS support recently. The only minus point for this framework is it required to use nightly Rust version.&lt;/p&gt;

&lt;p&gt;This is the simple route handler for a &lt;code&gt;GET&lt;/code&gt; method in &lt;code&gt;Rocket&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="nd"&gt;#[get(&lt;/span&gt;&lt;span class="s"&gt;"/posts"&lt;/span&gt;&lt;span class="nd"&gt;,&lt;/span&gt; &lt;span class="nd"&gt;format&lt;/span&gt; &lt;span class="nd"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"application/json"&lt;/span&gt;&lt;span class="nd"&gt;)]&lt;/span&gt;
&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;get_posts&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;page&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;i64&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;Json&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Value&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;Json&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nd"&gt;json!&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
        &lt;span class="s"&gt;"result"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;
    &lt;span class="p"&gt;}))&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I usually let &lt;code&gt;Rocket&lt;/code&gt; serve the static files for me, just like what I did in Express:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="nd"&gt;#[get(&lt;/span&gt;&lt;span class="s"&gt;"/"&lt;/span&gt;&lt;span class="nd"&gt;)]&lt;/span&gt;
&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;index&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nn"&gt;io&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nb"&gt;Result&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;NamedFile&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nn"&gt;NamedFile&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"www/index.html"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nd"&gt;#[get(&lt;/span&gt;&lt;span class="s"&gt;"/&amp;lt;file..&amp;gt;"&lt;/span&gt;&lt;span class="nd"&gt;,&lt;/span&gt; &lt;span class="nd"&gt;rank&lt;/span&gt; &lt;span class="nd"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="nd"&gt;)]&lt;/span&gt;
&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;files&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;PathBuf&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;Option&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;NamedFile&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nn"&gt;NamedFile&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nn"&gt;Path&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"www/"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="nf"&gt;.join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;&lt;span class="nf"&gt;.ok&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So I can put all my frontend code inside &lt;code&gt;www&lt;/code&gt; folder, and access it along my RESTful API. For example, a typical project struct will be:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;â”œâ”€â”€ Cargo.toml
â”œâ”€â”€ README.md
â”œâ”€â”€ src
â”‚   â”œâ”€â”€ main.rs
â”‚Â Â  â””â”€â”€ ...Rust code here...
â””â”€â”€ www
    â”œâ”€â”€ index.html
    â”œâ”€â”€ main.js
    â”œâ”€â”€ package.json
    â”œâ”€â”€ webpack.config.js
    â””â”€â”€ ...JavaScript code here...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To work with database, it's a good idea to use &lt;a href="https://diesel.rs" rel="noopener noreferrer"&gt;Diesel&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;If you want to see a &lt;code&gt;Rocket&lt;/code&gt; + &lt;code&gt;Diesel&lt;/code&gt; + &lt;code&gt;React&lt;/code&gt; project in action, I will shamelessly plug one of &lt;a href="https://news.kipalog.com" rel="noopener noreferrer"&gt;my side project here&lt;/a&gt; and &lt;a href="https://github.com/huytd/codedaily-v3" rel="noopener noreferrer"&gt;its source code&lt;/a&gt; on Github. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthefullsnack.com%2Fen%2Fimg%2Frust-kipalog.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthefullsnack.com%2Fen%2Fimg%2Frust-kipalog.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Please feel free take a look or use it if it good enough for you.&lt;/p&gt;

&lt;h2&gt;
  
  
  Isomorphic web app in Rust
&lt;/h2&gt;

&lt;p&gt;The last one, my favorite one, like it's 2012, when I started my programming career with my first PHP job.&lt;/p&gt;

&lt;p&gt;No more single page applications, no more client-side rendered UI. No more broken web pages when people &lt;a href="https://www.quora.com/Why-do-some-people-disable-JavaScript-in-their-browser" rel="noopener noreferrer"&gt;disabled JavaScript on their browsers&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Rocket&lt;/code&gt; and many other frameworks has the ability to render the HTML templates after binding some data into it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="nd"&gt;#[get(&lt;/span&gt;&lt;span class="s"&gt;"/"&lt;/span&gt;&lt;span class="nd"&gt;)]&lt;/span&gt;
&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;index&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;Template&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;news&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;fetch_from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;RSS_URL&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="nf"&gt;.ok&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="nf"&gt;.expect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Could not read RSS"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nn"&gt;Template&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;render&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"index"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;news&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I recently built a &lt;a href="https://codedaily.xyz" rel="noopener noreferrer"&gt;Hacker News reader&lt;/a&gt; using &lt;code&gt;Rocket&lt;/code&gt; + &lt;code&gt;Handlebars&lt;/code&gt; templates, you can take a look at &lt;a href="https://github.com/huytd/hackernews-rss-reader" rel="noopener noreferrer"&gt;its source code here&lt;/a&gt; (hey, I know, it's a shameless PR again).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthefullsnack.com%2Fen%2Fimg%2Frust-hackernews.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthefullsnack.com%2Fen%2Fimg%2Frust-hackernews.png"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;p&gt;I hope the three approaches I listed here will be helpful if you're the one who asking: &lt;em&gt;"Can Rust make webs?"&lt;/em&gt;. Each one has pros and cons, and depends on your project, you can pick one of them or mixing them all together.&lt;/p&gt;

&lt;p&gt;Please feel free to leave a comment on &lt;a href="https://news.ycombinator.com/item?id=15014557" rel="noopener noreferrer"&gt;Hacker News&lt;/a&gt; or &lt;a href="https://www.reddit.com/r/rust/comments/6tqzka/rust_for_the_web/" rel="noopener noreferrer"&gt;Reddit&lt;/a&gt; to share your thoughts.&lt;/p&gt;

</description>
      <category>rust</category>
      <category>frontend</category>
      <category>react</category>
      <category>webassembly</category>
    </item>
    <item>
      <title>Recursive type problem in Rust</title>
      <dc:creator>Huy Tr.</dc:creator>
      <pubDate>Mon, 02 Oct 2017 20:36:48 +0000</pubDate>
      <link>https://dev.to/huytd/recursive-type-problem-in-rust-bcf</link>
      <guid>https://dev.to/huytd/recursive-type-problem-in-rust-bcf</guid>
      <description>&lt;p&gt;&lt;em&gt;Original posted on &lt;a href="https://thefullsnack.com/en/recursive-rust.html" rel="noopener noreferrer"&gt;my blog&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;




&lt;p&gt;After spending much time reading about Rust, I decided to &lt;a href="https://github.com/huytd/thingsinrust/blob/master/binary_tree/binary_tree.rs" rel="noopener noreferrer"&gt;give it a&lt;br&gt;
try&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flzag3yl0h430mv49oq0h.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flzag3yl0h430mv49oq0h.png" width="800" height="493"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It was a very simple implementation of &lt;strong&gt;Binary Tree Traversal&lt;/strong&gt; algorithm.&lt;br&gt;
Surprisingly, by doing it, I learned a lot of Rust’s basic concepts!&lt;/p&gt;

&lt;h2&gt;
  
  
  Rust tell us what’s wrong in our code
&lt;/h2&gt;

&lt;p&gt;One of the first things that the smartass Rust compiler threw to my face was the lovely error message:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ rustc binary_tree.rs -o binary_tree
error[E0072]: recursive type `Node` has infinite size
 --&amp;gt; binary_tree.rs:1:1
  |
  |
  = help: insert indirection (e.g., a `Box`, `Rc`, or `&amp;amp;`) at some point to make `Node` representable
error: aborting due to previous error
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;So yes, this is the code:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;struct Node {
    value: i32,
    left: Option&amp;lt;Node&amp;gt;,
    right: Option&amp;lt;Node&amp;gt;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;This code is a very obvious way to implement a binary tree node in other&lt;br&gt;
programming languages like C/C++ or Java. However, Rust compiler just doesn’t agree with us. Also, this is the interesting part.&lt;/p&gt;

&lt;p&gt;Take a closer look. The error message said that our &lt;strong&gt;Node&lt;/strong&gt; struct is a&lt;br&gt;
&lt;strong&gt;recursive type&lt;/strong&gt; and it has infinite size. What does it mean?&lt;/p&gt;

&lt;p&gt;In Rust, all values are allocated in stack by default. So the compiler needs to&lt;br&gt;
know the size of each. The size of a struct is the sum of all its fields size.&lt;/p&gt;

&lt;p&gt;For example, with this struct:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;struct Point {
    x: i32,
    y: u8
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;So the size of Point struct is:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;size_of::&amp;lt;Point&amp;gt;() == size_of::&amp;lt;i32&amp;gt;() + size_of::&amp;lt;u8&amp;gt;()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Back to our implementation, how do we calculate our Node’s size?&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;size_of::&amp;lt;i32&amp;gt;() + 2 * size_of::&amp;lt;Option&amp;gt;() + 2 * size_of::&amp;lt;Node&amp;gt;()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Let’s expand this equation:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fk5gbrrif7rrxszu1o3ow.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fk5gbrrif7rrxszu1o3ow.png" width="800" height="271"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Hey hey! Hey! Stop it! You can do this for all day long. There’s no way to stop the expanding process.&lt;/p&gt;

&lt;p&gt;So, the size of &lt;strong&gt;Node&lt;/strong&gt; would be infinite and become impossible for Rust&lt;br&gt;
compiler to calculate.&lt;/p&gt;

&lt;h2&gt;
  
  
  And Rust also tell us how to fix it
&lt;/h2&gt;

&lt;p&gt;Let’s take a look at the error message again. You can see the kindness of Rust when it tries to teach us how to repair the broken:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ rustc binary_tree.rs -o binary_tree

error[E0072]: recursive type `Node` has infinite size
 --&amp;gt; binary_tree.rs:1:1
  |
1 | struct Node {
  | ^ recursive type has infinite size
  |

error: aborting due to previous error
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;If we follow the hint and add &lt;strong&gt;Box&lt;/strong&gt; to our implementation, the problem will be solved:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;struct Node {
    value: i32,
    left: Option&amp;lt;Box&amp;lt;Node&amp;gt;&amp;gt;,
    right: Option&amp;lt;Box&amp;lt;Node&amp;gt;&amp;gt;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;However, what is &lt;strong&gt;Box&lt;/strong&gt;? How does it solve our recursive reference problem?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Box&lt;/strong&gt; is a **pointer **that pointed to a heap allocated memory space.&lt;/p&gt;

&lt;p&gt;So when we declare a reference to &lt;strong&gt;Node&lt;/strong&gt; using &lt;strong&gt;Box&lt;/strong&gt;, &lt;em&gt;the size of this reference is the size of a pointer&lt;/em&gt;, not the size of the &lt;strong&gt;Node&lt;/strong&gt; type, and it is defined, so Rust compiler now aware how much memory needed to allocate for a &lt;strong&gt;Node&lt;/strong&gt;. And the recursive type problem now solved!&lt;/p&gt;




&lt;p&gt;I hope you enjoy the post and now understand the problem of recursive type, how to fix it.&lt;/p&gt;

&lt;p&gt;Please feel free to leave a comment if you want to discuss or subscribe to my blog to keep updated about my next posts in Rust (and other technical stuff, of course).&lt;/p&gt;

</description>
      <category>rust</category>
    </item>
  </channel>
</rss>
