<?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: Akansh</title>
    <description>The latest articles on DEV Community by Akansh (@akanshgulati).</description>
    <link>https://dev.to/akanshgulati</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%2F438249%2F20e72d8f-a5f0-4808-9673-5b0971696df1.jpeg</url>
      <title>DEV Community: Akansh</title>
      <link>https://dev.to/akanshgulati</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/akanshgulati"/>
    <language>en</language>
    <item>
      <title>How to add fade-in Transition on Text Changes</title>
      <dc:creator>Akansh</dc:creator>
      <pubDate>Tue, 28 Jul 2020 02:47:01 +0000</pubDate>
      <link>https://dev.to/akanshgulati/how-to-add-fade-in-transition-on-text-changes-kck</link>
      <guid>https://dev.to/akanshgulati/how-to-add-fade-in-transition-on-text-changes-kck</guid>
      <description>&lt;p&gt;Text transitions are usually preferred when you need to notify a user of any text &lt;br&gt;
being changed on screen and it just not make the experience better, but, also &lt;br&gt;
important when text content on website changes based on the user actions on the screen.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;For example, if you're on an e-commerce website and read the price of&lt;br&gt;
an item, but, you changed the quantity of the item and suddenly your&lt;br&gt;
shipping charges are increased, making it important to highlight those&lt;br&gt;
changes to the user.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I believe this is one of the use-cases where text transitions are required, &lt;br&gt;
also, React and Vue frameworks support Hot Module Replacement, updating only the particular modules. See the example below to understand how simple text transition can &lt;br&gt;
have an impact on user experience. &lt;/p&gt;

&lt;p&gt;For example: &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%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fde055hf6b9x66mz9icp6.gif" 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%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fde055hf6b9x66mz9icp6.gif" alt="animated correct "&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can observe the difference when the UI has no transition, we might have missed the change in the heading at first look. &lt;/p&gt;

&lt;p&gt;Before we start with any framework, the following things are common among various frameworks for transitions in entering and leaving transition states:&lt;/p&gt;
&lt;h3&gt;
  
  
  Transition Stages
&lt;/h3&gt;

&lt;p&gt;There are three stages in each state of transitioning, initial stage, transitioning stage and transition done stage, &lt;br&gt;
giving us total 3+3 stages for entering and leaving altogether. The naming convention may vary with a different framework, &lt;br&gt;
see in below image for better understanding.&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%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F054ohmmz8m1lxjodvv8d.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%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F054ohmmz8m1lxjodvv8d.png" alt="three-stages-of-transition"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Transition Triggers
&lt;/h3&gt;

&lt;p&gt;To trigger the transition, there are multiple ways, a few of them are as followed:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Adding/Removing a node from the DOM tree&lt;/li&gt;
&lt;li&gt;Showing and hiding a node from UI (using display: none)&lt;/li&gt;
&lt;li&gt;Adding/Removing items into the list&lt;/li&gt;
&lt;li&gt;Dynamically updating the current node with a new node&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;For &lt;strong&gt;text replace&lt;/strong&gt; transition, we will be using Dynamic node updating criteria where a new node/text content &lt;br&gt;
takes place of the old node/text content. &lt;/p&gt;

&lt;p&gt;Let's see how it can be implemented and understand the complexity with different frameworks.&lt;/p&gt;
&lt;h2&gt;
  
  
  Vue JS Implementation
&lt;/h2&gt;

&lt;p&gt;Vue has in-built &lt;code&gt;&amp;lt;Transition&amp;gt;&lt;/code&gt; wrapper component which allows adding entering and leaving transitions for a component &lt;br&gt;
or a parent node wrapping multiple nodes. Refer below image for different classes available. &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%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F5tjc3nvd2prj1b5cqngt.jpg" 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%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F5tjc3nvd2prj1b5cqngt.jpg" alt="animated correct "&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We will use Dynamic Component loading, mentioned above as one of the Transition triggers, for text change transition &lt;br&gt;
using &lt;code&gt;key&lt;/code&gt; attribute to inform Vue of any change in node/content. &lt;/p&gt;

&lt;p&gt;Once we implement this, we see transitioning working as below: &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%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fjl0ad4g8qrj1txaan5oh.gif" 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%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fjl0ad4g8qrj1txaan5oh.gif" alt="vue transition default mode"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can see that this is &lt;strong&gt;NOT&lt;/strong&gt; what we desire, the problem is that the new text content is coming before the previous &lt;br&gt;
text content is hidden and due to this, both contents are visible for a fraction of second during transitioning.&lt;br&gt;
This is what we call as &lt;code&gt;in-out&lt;/code&gt; mode, which is set by default.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How to solve this flickering issue?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To make this transition smooth, we have to make sure that the transition of old text content is completed &lt;br&gt;
before new content transition starts. For that, we specify mode as &lt;code&gt;out-in&lt;/code&gt; to prevent flickering. &lt;br&gt;
You can read more about transitions and modes in-depth &lt;a href="https://vuejs.org/v2/guide/transitions.html" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;iframe src="https://codesandbox.io/embed/vue-text-transition-example-nlvog?runonclick=1"&gt;
&lt;/iframe&gt;
&lt;/p&gt;



&lt;h2&gt;
  
  
  React Implementation
&lt;/h2&gt;

&lt;p&gt;Unlike Vue, in React, we have to import a separate transition library, &lt;a href="https://reactcommunity.org/react-transition-group/" rel="noopener noreferrer"&gt;react-transition-group&lt;/a&gt;&lt;br&gt;
 which provides specific Component wrappers like CSSTransition, Transition, SwitchTransition and TransitionGroup. &lt;/p&gt;

&lt;p&gt;For text replace transition specifically, we need two components, &lt;code&gt;CSSTransition&lt;/code&gt; and &lt;code&gt;SwitchTransition&lt;/code&gt;. It also has 3 stages of &lt;br&gt;
the text transition in both entering and leaving transition states just like Vue. Refer to below diagram to understand the classes.&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%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fc3vl10kg2leh4rwsd3wh.jpg" 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%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fc3vl10kg2leh4rwsd3wh.jpg" alt="react transitioning classes"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  CSSTransition
&lt;/h3&gt;

&lt;p&gt;It handles the transition on the content, like the timeout, transition class name, etc., It has support for following props: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Timeout&lt;/strong&gt;: You can define the timeout of the entering and leaving transition separately or as a common numeric time in milliseconds. &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;addEndListener&lt;/strong&gt;: You can define the method here for ending the transition. &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;ClassNames&lt;/strong&gt;: The name has been set as plural (with s), just to not mix it with React default way of adding CSS classes 
on any node using className. This lets you define the parent class which will be used to define the transition. &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;In&lt;/strong&gt;: This prop lets you state when the transition classes are to be applied on the node/text content. It's a sort 
of switch to turn on and off transition effects dynamically on DOM.  Vue's &lt;code&gt;&amp;lt;Transiton&amp;gt;&lt;/code&gt; component has 
no direct prop or method to trigger such functionality. You can read more about its various options &lt;a href="http://reactcommunity.org/react-transition-group/css-transition" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/li&gt;
&lt;/ol&gt;
&lt;h3&gt;
  
  
  SwitchTransition
&lt;/h3&gt;

&lt;p&gt;Similar to Vue, we need to specifically select the &lt;code&gt;out-in&lt;/code&gt; mode for letting the old content transition to be over before new &lt;br&gt;
content transition kicks-off. For that, we need to use this separate component. You can read more about its various options &lt;a href="http://reactcommunity.org/react-transition-group/switch-transition" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;iframe src="https://codesandbox.io/embed/react-text-replace-transition-cz08h?runonclick=1"&gt;
&lt;/iframe&gt;
&lt;/p&gt;



&lt;h2&gt;
  
  
  Vanilla JS Implementation
&lt;/h2&gt;

&lt;p&gt;We are removing and adding the same &lt;code&gt;fade&lt;/code&gt; class with changed text content and using &lt;code&gt;innerHTML&lt;/code&gt; which repaints&lt;br&gt;
 the content enabling the transition effect (called as forced reflow). &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;By default, we add a fade out related properties to &lt;code&gt;.text&lt;/code&gt; class so&lt;br&gt;
that once the fade class is removed, it can work just like&lt;br&gt;
'fade-leave-active' in Vue or 'fade-exit-active' in React.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;There are two methods by which you can add transition: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Timeout Centric&lt;/strong&gt;: In this, you have to manually mention the timeout of the leaving &lt;br&gt;
state of the transition. This is not a recommended way, because same time needs to be &lt;br&gt;
entered in CSS styles. The timeout part is being handled with the &lt;code&gt;setTimeout&lt;/code&gt; method and &lt;code&gt;requestAnimationFrame&lt;/code&gt; &lt;br&gt;
callback to prevent any &lt;a href="https://googlechrome.github.io/devtools-samples/jank/" rel="noopener noreferrer"&gt;jank-like&lt;/a&gt; (drop in frame rate) experience. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;CSS Centric&lt;/strong&gt;: Here we leverage &lt;code&gt;transitionend&lt;/code&gt; JS event listener which updates when the one state of transition is over serving the purpose of timeout being used above. &lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;iframe src="https://codesandbox.io/embed/vanilla-js-text-transition-example-hc7kq?runonclick=1"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;&lt;br&gt;&lt;br&gt;
Hope you got some basic idea about how implementation is different as per framework and in vanilla JS. Do share your queries in comment section below.  &lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>vue</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
