<?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: Juan R. Rodríguez</title>
    <description>The latest articles on DEV Community by Juan R. Rodríguez (@jrx2).</description>
    <link>https://dev.to/jrx2</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%2F699970%2Fbec2e77e-51e2-49f2-99b7-ce966a7bb731.jpeg</url>
      <title>DEV Community: Juan R. Rodríguez</title>
      <link>https://dev.to/jrx2</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/jrx2"/>
    <language>en</language>
    <item>
      <title>Responsive design in React components?</title>
      <dc:creator>Juan R. Rodríguez</dc:creator>
      <pubDate>Mon, 06 Sep 2021 01:01:09 +0000</pubDate>
      <link>https://dev.to/jrx2/responsive-design-in-react-components-7d1</link>
      <guid>https://dev.to/jrx2/responsive-design-in-react-components-7d1</guid>
      <description>&lt;h3&gt;
  
  
  Motivation:
&lt;/h3&gt;

&lt;p&gt;The other day I found myself looking for info on how to implement responsive design in React components, I could not find anything clear, nothing that could make reference about any pattern or recommended method, so I decided to start thinking a little about this subject.&lt;/p&gt;

&lt;p&gt;As soon as I started to search information about responsive design, the use of media queries come up quickly, but commonly related to the device's window in which it is being drawn, which does not seem to contribute much for isolated components.&lt;/p&gt;

&lt;p&gt;Making a component to respond to the changes of the entire window dimensions does not seem to make sense, the component should respond to it's own dimensions, shouldn't it??&lt;/p&gt;

&lt;p&gt;It is also true that some css tools can be used to manage the layout of the elements within the available space, for example with flexbox or css-grid some responsive behavior can be given to the elements but I don't think that can get to the same level as using media queries.&lt;/p&gt;

&lt;p&gt;For this reason I thought that maybe using the same concept of media queries but oriented to components could be a good idea.&lt;/p&gt;




&lt;h3&gt;
  
  
  What do we want to achieve?
&lt;/h3&gt;

&lt;p&gt;Something like this...&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%2Fuploads%2Farticles%2F28layyp1uz5z4vtqi61t.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%2Fuploads%2Farticles%2F28layyp1uz5z4vtqi61t.gif" alt="Responsive component"&gt;&lt;/a&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  How to implement it?
&lt;/h3&gt;

&lt;p&gt;As soon as I started to wonder how I could implement something like this the ResizeObserver appeared, a browser API that allows us to detect the component's size changes and react to that, so it seems that it could be useful for what I want to do.&lt;/p&gt;

&lt;p&gt;The other thing that would be needed is to provide a standard way to define breakpoints for the element and a method to detect the component's size range in any given time, both of which can be implemented without much difficulties.&lt;/p&gt;

&lt;p&gt;My approach for this task was:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;First, choose a structure to establish how the breakpoints for the component should be defined.&lt;/li&gt;
&lt;li&gt;From those breakpoints identify a list of size ranges and generate a css class for each one of them.&lt;/li&gt;
&lt;li&gt;Also it will be needed to identify the component's size after each change, find in which range it's on and assign the corresponding css class to it.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This way it could have the same behavior as with media queries. Each time a component changes its range we can assign the propper css class and the necessary styles will be applied.&lt;/p&gt;

&lt;p&gt;As you can see the idea is simple and so is the procedure. I decided to encapsulate the logic in a hook to be able to reuse it in a fast way where it is necessary. &lt;a href="https://www.npmjs.com/package/@jrx2-dev/use-responsive-class" rel="noopener noreferrer"&gt;https://www.npmjs.com/package/@jrx2-dev/use-responsive-class&lt;/a&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  How does this hook work?
&lt;/h3&gt;

&lt;p&gt;The hook receives a reference to the component to be controlled and optionally breakpoints to be used.&lt;br&gt;
In case of not receiving breakpoints, predefined ones will be used.&lt;/p&gt;

&lt;p&gt;Breakpoints must implement the following interface:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;breakpointsInput&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt; &lt;span class="kr"&gt;number&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;Example (defaults breakpoints):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;MEDIA_BREAKPOINTS&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;breakpointsInput&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;small&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;420&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;big&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;768&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;Width ranges (mediaBreakpoints) will be created according to the breakpoints used (with their respective generated css classes).&lt;/p&gt;

&lt;p&gt;The generated mediaBreakpoints will comply with the following interface:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;mediaBreakpoints&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;class&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;from&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nl"&gt;toUnder&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&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...&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nf"&gt;createMediaBreakpoints&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;MEDIA_BREAKPOINTS&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;...should return:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="p"&gt;[&lt;/span&gt;
  &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;class&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;to-small&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;from&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;toUnder&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;420&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="na"&gt;class&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;from-small-to-under-big&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;from&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;420&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;toUnder&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;768&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="na"&gt;class&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;from-big&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;from&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;768&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;toUnder&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;Infinity&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;Whenever a change in the size of the component is detected, the getCurrentSizeClass method will be called and the css class corresponding to that width range will be returned.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;  &lt;span class="nf"&gt;getCurrentSizeClass&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;elementWidth&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;mediaBreakpoints&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  How to use this hook:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm i @jrx2-dev/use-responsive-class
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useResponsiveClass&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@jrx2-dev/use-responsive-class&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="cm"&gt;/*
const elementBreakpoints: breakpointsInput = {
  small: 420,
  big: 768,
};
*/&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;elRef&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;createRef&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;HTMLDivElement&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;responsiveClass&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useResponsiveClass&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;elRef&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// const [responsiveClass] = useResponsiveClass(elRef, elementBreakpoints);&lt;/span&gt;
&lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
   &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt; &lt;span class="nx"&gt;ref&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;elRef&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="nx"&gt;className&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;classes&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;responsiveClass&lt;/span&gt;&lt;span class="p"&gt;]}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="nx"&gt;Some&lt;/span&gt; &lt;span class="nx"&gt;content&lt;/span&gt;
   &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The styles should be something like this (css modules are used in demo project):&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="nc"&gt;.root&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="err"&gt;&amp;amp;.to-small&lt;/span&gt; &lt;span class="err"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;background-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;green&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nc"&gt;.from-small-to-under-big&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;background-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;yellow&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="nc"&gt;.from-big&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;background-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;red&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="err"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Demo:
&lt;/h3&gt;

&lt;p&gt;I used this custom hook in a component library that I made to use in personal demo projects . &lt;a href="https://www.npmjs.com/package/@jrx2-dev/react-components" rel="noopener noreferrer"&gt;https://www.npmjs.com/package/@jrx2-dev/react-components&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can see this technique at work with an example component in the project's Storybook. &lt;a href="https://jrx2-dev.github.io/react-components" rel="noopener noreferrer"&gt;https://jrx2-dev.github.io/react-components&lt;/a&gt;&lt;/p&gt;




&lt;h3&gt;
  
  
  Note:
&lt;/h3&gt;

&lt;p&gt;I have to say that I got a little distracted adding an animation between the change of layouts of the component, the logic is encapsulated in the hook useFadeOnSizeChange, I think it was necessary to make the transition between layouts a little more fluid.&lt;/p&gt;




&lt;h2&gt;
  
  
  Conclusion:
&lt;/h2&gt;

&lt;p&gt;This experiment served me as a proof of concept to develop a system that allows the design of truly responsive components in react.&lt;/p&gt;

&lt;p&gt;Obviously the code can be improved, any comments or suggestions are welcome. The idea of this article was mostly a veiled question... how would you do it? :)&lt;/p&gt;

&lt;p&gt;Regarding the peformance, a third party hook (@react-hook/resize-observer) optimized for the ResizeObserver implementation is being used and seems to give good results.&lt;/p&gt;

&lt;p&gt;What I am interested in highlighting here is not so much the implementation itself but the concept used, I would like to hear opinions and suggestions on how you handle this issue.&lt;/p&gt;

</description>
      <category>react</category>
      <category>css</category>
      <category>javascript</category>
      <category>typescript</category>
    </item>
  </channel>
</rss>
