<?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>Forem: Alireza</title>
    <description>The latest articles on Forem by Alireza (@alirezace).</description>
    <link>https://forem.com/alirezace</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%2F691107%2F60ce1eb2-0fe2-4417-a31d-6209ac564b9d.jpeg</url>
      <title>Forem: Alireza</title>
      <link>https://forem.com/alirezace</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/alirezace"/>
    <language>en</language>
    <item>
      <title>Improve performance with a simple css tip</title>
      <dc:creator>Alireza</dc:creator>
      <pubDate>Thu, 26 Sep 2024 12:39:37 +0000</pubDate>
      <link>https://forem.com/alirezace/why-you-should-use-transform-instead-of-margin-for-css-animations-performance-tip-2hn4</link>
      <guid>https://forem.com/alirezace/why-you-should-use-transform-instead-of-margin-for-css-animations-performance-tip-2hn4</guid>
      <description>&lt;p&gt;CSS animations have become a powerful tool for creating engaging user experiences. Animations can guide users, provide feedback, or simply enhance the overall aesthetics of an interface. However, not all CSS properties are equally efficient when it comes to animating elements on a webpage. One common mistake is using &lt;code&gt;margin&lt;/code&gt; for animations instead of &lt;code&gt;transform&lt;/code&gt;. In this article, we'll explore why &lt;code&gt;transform&lt;/code&gt; is a far better choice for performance and smoothness, and how it differs from &lt;code&gt;margin&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  How CSS Animations Work in the Browser
&lt;/h3&gt;

&lt;p&gt;To understand why &lt;code&gt;transform&lt;/code&gt; is preferred over &lt;code&gt;margin&lt;/code&gt; for animations, it’s important to know how browsers process animations. Browsers go through several stages to render a webpage:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Layout (Reflow)&lt;/strong&gt;: The browser calculates the size and position of each element based on the document’s structure and CSS rules.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Paint&lt;/strong&gt;: The browser fills in pixels, including background colors, borders, shadows, text, and images.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Composite&lt;/strong&gt;: The browser layers the elements together and displays them on the screen.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Animations that trigger layout or paint are generally more expensive in terms of performance, as they force the browser to recalculate positions, sizes, and the visuals of elements. This is where &lt;code&gt;transform&lt;/code&gt; shines compared to &lt;code&gt;margin&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Problem with Animating &lt;code&gt;margin&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;When you animate an element using &lt;code&gt;margin&lt;/code&gt;, the browser has to recalculate the layout every frame of the animation. For example, if you animate &lt;code&gt;margin-left&lt;/code&gt; to move an element across the screen, the browser needs to reflow (recalculate the layout) to account for the new margin values. This triggers both &lt;strong&gt;layout recalculation&lt;/strong&gt; and sometimes a &lt;strong&gt;repaint&lt;/strong&gt;, which can be costly, especially if the page has a complex structure.&lt;/p&gt;

&lt;p&gt;For example, consider this CSS animation that moves an element using &lt;code&gt;margin&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="c"&gt;/* Margin-based animation */&lt;/span&gt;
&lt;span class="k"&gt;@keyframes&lt;/span&gt; &lt;span class="n"&gt;moveWithMargin&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="err"&gt;0&lt;/span&gt;&lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;margin-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;span class="err"&gt;100&lt;/span&gt;&lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;margin-left&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;100px&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="nc"&gt;.element&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;animation&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;moveWithMargin&lt;/span&gt; &lt;span class="m"&gt;1s&lt;/span&gt; &lt;span class="n"&gt;linear&lt;/span&gt; &lt;span class="n"&gt;infinite&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 animation triggers a layout recalculation at each step of the animation because the margin affects the flow of the document. As a result, it can slow down the rendering process, especially on pages with many elements or on devices with limited resources.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why &lt;code&gt;transform&lt;/code&gt; is a Better Alternative
&lt;/h3&gt;

&lt;p&gt;Unlike &lt;code&gt;margin&lt;/code&gt;, the &lt;code&gt;transform&lt;/code&gt; property doesn’t affect the layout of the document. It works by creating a new "layer" for the element and applying visual changes without recalculating the positions or dimensions of any other elements on the page. This is where the &lt;strong&gt;GPU (Graphics Processing Unit)&lt;/strong&gt; comes into play. When you use &lt;code&gt;transform&lt;/code&gt;, the browser can offload the task to the GPU, which handles the animation more efficiently.&lt;/p&gt;

&lt;p&gt;Here’s the same animation, but using &lt;code&gt;transform&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="c"&gt;/* Transform-based animation */&lt;/span&gt;
&lt;span class="k"&gt;@keyframes&lt;/span&gt; &lt;span class="n"&gt;moveWithTransform&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="err"&gt;0&lt;/span&gt;&lt;span class="o"&gt;%&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="n"&gt;translateX&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;span class="err"&gt;100&lt;/span&gt;&lt;span class="o"&gt;%&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="n"&gt;translateX&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;100px&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="nc"&gt;.element&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;animation&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;moveWithTransform&lt;/span&gt; &lt;span class="m"&gt;1s&lt;/span&gt; &lt;span class="n"&gt;linear&lt;/span&gt; &lt;span class="n"&gt;infinite&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;In this case, the element’s position is updated by the &lt;code&gt;transform&lt;/code&gt; property, but the rest of the page layout remains unaffected. No reflow or repaint occurs, and the animation runs much smoother. The browser can handle the animation purely in the &lt;strong&gt;composite&lt;/strong&gt; phase, which is much faster and less resource-intensive.&lt;/p&gt;

&lt;h3&gt;
  
  
  Performance Benefits of &lt;code&gt;transform&lt;/code&gt;
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;No Layout Recalculations&lt;/strong&gt;: The biggest advantage of using &lt;code&gt;transform&lt;/code&gt; is that it doesn't trigger layout recalculation. The browser doesn't need to figure out the positions of other elements because the &lt;code&gt;transform&lt;/code&gt; property applies only to visual appearance, not layout.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;GPU Acceleration&lt;/strong&gt;: Many modern browsers use GPU acceleration for &lt;code&gt;transform&lt;/code&gt; animations. Offloading the work to the GPU allows for smoother animations, especially when dealing with 3D transformations or complex animations. This is not something the GPU can easily do with &lt;code&gt;margin&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Reduced Paint Costs&lt;/strong&gt;: While &lt;code&gt;margin&lt;/code&gt; changes can trigger a repaint (recalculating what the element looks like), &lt;code&gt;transform&lt;/code&gt; typically doesn’t. This means less time is spent rendering the visual updates, leading to better performance.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Smoother User Experience&lt;/strong&gt;: Since the browser skips the costly layout and paint steps, animations run at a higher frame rate, leading to a more fluid experience for users, especially on mobile devices or slower machines.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;Using the transform property for CSS animations is a more efficient and performant choice than using margin. By avoiding costly layout recalculations and taking advantage of GPU acceleration, you can ensure your animations run smoothly, even on resource-constrained devices. Additionally, any changes to the box model attributes—such as padding or border—trigger a reflow, which can further degrade performance. To create high-performance animations, avoid modifying properties that cause reflow and instead opt for transform to achieve a snappier, more fluid user experience.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>How to Build a React component library?</title>
      <dc:creator>Alireza</dc:creator>
      <pubDate>Thu, 12 Jan 2023 14:15:45 +0000</pubDate>
      <link>https://forem.com/alirezace/how-to-build-a-react-component-library-3c67</link>
      <guid>https://forem.com/alirezace/how-to-build-a-react-component-library-3c67</guid>
      <description>&lt;p&gt;In this article I tried to talk the steps of create a react component library.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why do we need to build a react component library?
&lt;/h2&gt;

&lt;p&gt;If you have several components that are shared between your projects or some js code that is copy-pasting and these things happen more than 2 or 3 times then you need to convert your shared code to a library.&lt;/p&gt;

&lt;p&gt;So let's create a react component library.&lt;/p&gt;

&lt;p&gt;First, we need to generate our component. We can create a React project and create a component. Our folder structure would be like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;├── src
│   ├── components
|   │   ├── Button
|   |   │   ├── Button.tsx
|   |   │   ├── Button.scss
|   |   │   └── index.ts
|   │   ├── TextField
|   |   │   ├── TextField.tsx
|   |   │   ├── TextField.scss
|   |   │   └── index.ts
|   │   └── index.ts
├── package.json
├── tsconfig.json
└── rollup.config.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;src/components/index.ts&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export { default as Button } from "./Button";
export { default as TextField } from "./TextField";
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After we create our component we need to config our rollup. We use rollup to bundle our library(also we could use webpack).&lt;/p&gt;

&lt;p&gt;Let's config our rollup to bundle our library:&lt;/p&gt;

&lt;p&gt;rollup.config.js&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import externalDeps from 'rollup-plugin-peer-deps-external';
import typescript from '@rollup/plugin-typescript';
import sourcemaps from 'rollup-plugin-sourcemaps';
import resolve from '@rollup/plugin-node-resolve';
import commonJS from 'rollup-plugin-commonjs';
import replace from '@rollup/plugin-replace';
import styles from 'rollup-plugin-styles';
import babel from 'rollup-plugin-babel';
import json from '@rollup/plugin-json';
import pkg from './package.json';

const globals = {
  react: 'React',
  'react-dom': 'ReactDOM',
  'react-query': 'ReactQuery',
};

const plugins = [
  externalDeps(), //prevents packages listed in peerDependencies from being bundled with our component library
  commonJS(), //convert common js modules to es6
  resolve(), // Locate and bundle third-party dependencies in node_modules
  typescript(), // transpiles our TypeScript code into JavaScript.
postcss() // transforms our Sass into CSS
];

export default {
  input: `./src/components/index.ts`,
  output: [
    {
      name: 'index',
      file: `lib/index.cj.js`,
      format: 'cjs',
      sourcemap: true,
    },
    {
      name: 'index' ,
      file: `lib/index.es.js`,
      format: 'esm',
      sourcemap: true,
    },
    {
      name: 'index',
      file: `lib/index.umd.js`,
      format: 'umd',
      sourcemap: true,
      globals,
    },
  ],
  plugins,
}));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;esm vs common js vs umd&lt;/strong&gt;&lt;br&gt;
These types are js module formats.&lt;br&gt;
Some tools like webpack and node use common js format and some tools like rollup and webpack2 use esm module. umd(Universal Module Definition) is the type of module that strive to work everywhere. Also, umd is a kind of fallback for other formats. If you want to include any of these files in the browser as standalone script tags without a build step like e.g. Webpack, you will want to use umd.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How to test our packages?&lt;/strong&gt;&lt;br&gt;
we have 3 options to test our package before publishing:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;npm link&lt;/li&gt;
&lt;li&gt;npm pack&lt;/li&gt;
&lt;li&gt;Storybook&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;npm link:&lt;br&gt;
npm link add our package in node_module universal of your system and install your package globally. every time we change our files this package will be updated in node_modules folder.&lt;/p&gt;

&lt;p&gt;npm pack:&lt;br&gt;
This command will pack our package and we can install the package in our project. with every change that we make in our package, we need to run this command and install our library again.&lt;/p&gt;

&lt;p&gt;Storybook:&lt;br&gt;
In my opinion, the best way to test our package locally is Storybook. &lt;br&gt;
In addition, to test our package, we can also have good documentation.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Final step:&lt;/strong&gt;&lt;br&gt;
Finally, we should publish our package.&lt;br&gt;
with &lt;code&gt;npm publish&lt;/code&gt; we can publish our package in the npm registry but before that, we should create an account in npm.&lt;br&gt;
also, we need to make some changes in our packge.json file.&lt;/p&gt;

&lt;p&gt;package.json:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
  "name": "@react-components-library/react-sample-components-library",
  "version": "0.0.20",
  "main": "lib/index.js", // common js 
  "module": "lib/index.es.js", //esm
  "browser": "lib/index.umd.js", //umd
  "files": [
    "/lib"
  ],
  "type": "module",
  "scripts": {
    "test": "jest",
    "start": "styleguidist server",
    "build": "rm -rf ./lib &amp;amp;&amp;amp; rollup -c",
    "prepublishOnly": "rm -rf lib &amp;amp;&amp;amp; npm run build",
    "postbuild": "npm pack &amp;amp;&amp;amp; tar -xvzf *.tgz &amp;amp;&amp;amp; rm -rf package *.tgz",
    "docs:build": "styleguidist build"
  },
  "peerDependencies": {
    "react": "17.0.2",
    "react-dom": "17.0.2"
  },
...
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There are some tips here:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;we should our react and react-dom in our peerDependencies to prevent rollup to add these packages in our bundle.&lt;/li&gt;
&lt;li&gt;when &lt;code&gt;npm publish&lt;/code&gt; execute, first run &lt;code&gt;prepublishOnly&lt;/code&gt; script. so we can add our build steps to this script.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;files&lt;/code&gt; indicates which files or folders should push to the npm registry.&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>package</category>
      <category>opensource</category>
    </item>
  </channel>
</rss>
