<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Branko Tomić on Medium]]></title>
        <description><![CDATA[Stories by Branko Tomić on Medium]]></description>
        <link>https://medium.com/@klomontes?source=rss-1edff98ee367------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*RXtzrLhAyeRFl0Tk2YqCoA.jpeg</url>
            <title>Stories by Branko Tomić on Medium</title>
            <link>https://medium.com/@klomontes?source=rss-1edff98ee367------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 23:27:48 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@klomontes/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[How to Handle Spacing Between UI Components in React]]></title>
            <link>https://medium.com/@klomontes/how-to-handle-spacing-between-ui-components-in-react-81f70d94c2de?source=rss-1edff98ee367------2</link>
            <guid isPermaLink="false">https://medium.com/p/81f70d94c2de</guid>
            <category><![CDATA[development]]></category>
            <category><![CDATA[best-practices]]></category>
            <category><![CDATA[design-patterns]]></category>
            <category><![CDATA[ui]]></category>
            <category><![CDATA[react]]></category>
            <dc:creator><![CDATA[Branko Tomić]]></dc:creator>
            <pubDate>Fri, 10 Nov 2023 23:46:47 GMT</pubDate>
            <atom:updated>2023-11-13T10:02:52.597Z</atom:updated>
            <content:encoded><![CDATA[<h4>TL;DR as a rule of thumb, parent component should maintain spacing between child components in order to preserve modularity of the design system.</h4><p><em>This story is initially published on </em><a href="https://www.brankotomic.com/"><em>brankotomic.com</em></a></p><p>In my experience, improper spacing management is one of the most common ways a design system becomes misused.</p><p>In order to preserve modularity of UI components in a design system, you should properly separate spacing concerns in your layout.</p><p>When you are driving your car at a high speed on a highway, you need to maintain greater distance between your car and the car in front of you than if you are driving in a city. To ensure optimal space between them, imagine that each car on the highway had a long rod protruding from the front of it.</p><p>Now imagine if you dropped these cars with their rods in a busy city setting. The traffic would grind to a halt.</p><pre>const Car = () =&gt; (<br>  &lt;div style={{<br>    width: 50,<br>    height: 100,<br>    backgroundColor: &#39;red&#39;,<br>    marginTop: 200 // the “rod” of our car<br>  }} /&gt;<br>)<br><br>const Highway = ({children}) =&gt; (<br>  &lt;div style={{display: &#39;flex&#39;, flexDirection: &#39;column&#39;}}&gt;<br>    {children}<br>  &lt;/div&gt;<br>)<br><br>const City = ({children}) =&gt; (<br>  &lt;div style={{display: &#39;flex&#39;, flexDirection: &#39;column&#39;}}&gt;<br>    {children}<br>  &lt;/div&gt;<br>)<br>  <br>const World = () =&gt; (<br>  &lt;Traffic&gt;<br>    {/* All is good ✅ */}<br>    &lt;Highway&gt;<br>      &lt;Car /&gt;<br>      &lt;Car /&gt;<br>      &lt;Car /&gt;<br>    &lt;/Highway&gt;<br><br>    {/* Chaos! Panic! Mayhem! 💥 */}<br>    &lt;City&gt;<br>      &lt;Car /&gt;<br>      &lt;Car /&gt;<br>      &lt;Car /&gt;<br>    &lt;/City&gt;<br>  &lt;/Traffic&gt;<br>)</pre><p>It is the same with components (if you disregard all the ways cars and UI components differ).</p><p>If you have a button or a card component with built-in margins, you are likely going to run into a scenario where these margins are a hindrance to your layout.</p><p>Component should not blindly assume spacing in different contexts.</p><p>The trade-off of modularity is a little bit of extra work (and possibly, code).</p><p>So, what options do we have?</p><h3>Solutions</h3><p>As a rule of thumb, the parent which contains the component should worry about spacing between its children and not the child itself.</p><h3>Use “Stack” component</h3><p>The sole purpose of this component is to maintain proper spacing between its children. It is a very useful tool and easy to build if you are creating components from scratch.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tScpB9ZkZwTT5PKCQNBZNw.png" /></figure><pre>&lt;Stack spacing={20}&gt;<br>  &lt;Button/&gt;<br>  &lt;Button/&gt;<br>  &lt;Button/&gt;<br>  &lt;Button/&gt;<br>&lt;/Stack&gt;</pre><p>Or, if you want to stick to the (this time, rodless) car analogy:</p><pre>&lt;Stack spacing={200}&gt; // Highway<br>  &lt;Car/&gt;<br>  &lt;Car/&gt;<br>  &lt;Car/&gt;<br>&lt;/Stack&gt;<br><br>&lt;Stack spacing={15}&gt; // City<br>  &lt;Car/&gt;<br>  &lt;Car/&gt;<br>  &lt;Car/&gt;<br>&lt;/Stack&gt;</pre><p>Here is how <a href="https://mui.com/material-ui/react-stack/">MUI</a>, <a href="https://ant.design/components/space">Ant</a> and <a href="https://getbootstrap.com/docs/5.3/helpers/stacks/">Bootstrap</a> implement these components.</p><p>Pros</p><ul><li>Keeps the code clean</li><li>It’s sole purpose is to be concerned with spacing</li></ul><p>Cons</p><ul><li>Not very flexible if you need to have different spacing between children</li></ul><h3>Use wrapper component</h3><p>This works best when you have a single component that needs spacing.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jlWov5dyZHCnXD8gH1gedg.png" /></figure><p>The purpose of the wrapper is just to position the nested component in the layout.</p><pre>&lt;Layout&gt;<br>  &lt;SomeComponent/&gt;<br>  &lt;Box marginRight={20}&gt;<br>    &lt;Component/&gt;<br>  &lt;/Box&gt;<br>  &lt;Component/&gt;<br>  &lt;Box marginLeft={40}&gt;<br>    &lt;Component/&gt;<br>  &lt;/Box&gt;<br>&lt;/Layout&gt;</pre><p>Pros</p><ul><li>Easy to use in any context</li></ul><p>Cons</p><ul><li>Clutters up the code a bit</li></ul><h3>Provide the component with optional spacing</h3><p>Provide your component with a spacing prop that can be used to maintain spacing between components.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*LFJLXqPafsMgqwIm9_Qrhg.png" /></figure><pre>const Component = ({spacing}) =&gt; (<br>  &lt;div style={{marginRight: spacing || false}}/&gt;<br>)<br><br>const App = () =&gt; (<br>  &lt;Layout&gt;<br>    &lt;Component spacing={20}/&gt;<br>  &lt;/Layout&gt;<br>)</pre><p>Pros</p><ul><li>Keeps the code clean</li><li>Intuitive in a way that it follows the traditional HTML api</li></ul><p>Cons</p><ul><li>You need to implement spacing prop in every component to keep the API consistent</li><li>Kind of feels like going against the idea of separation of concerns because on the same principle, you can apply any other styling property to the component</li><li>Adds a bit of clutter to the component itself</li></ul><h3>Conclusion</h3><p>There is no one-size-fits-all solution. Choose a solution depending on the use case. Sometimes, the best course of action is to use a combination of these solutions.</p><p>The rule of thumb is, let the parent component worry about spacing between its children.</p><h3>Important note</h3><p>Spacing concerns do not apply to the <em>padding</em> inside the component itself. Padding should provide permanent spacing between the content and the edge of the component.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=81f70d94c2de" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>