<?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 Hirokazu Tei on Medium]]></title>
        <description><![CDATA[Stories by Hirokazu Tei on Medium]]></description>
        <link>https://medium.com/@hirokazutei?source=rss-46532d840c01------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*TCGQWpskC4LELYHuCj2neQ.jpeg</url>
            <title>Stories by Hirokazu Tei on Medium</title>
            <link>https://medium.com/@hirokazutei?source=rss-46532d840c01------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 29 Nov 2026 09:57:59 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@hirokazutei/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[Enforcing Component Spacing in React & React Native]]></title>
            <link>https://medium.com/@hirokazutei/enforcing-component-spacing-in-react-react-native-556b8ef90dea?source=rss-46532d840c01------2</link>
            <guid isPermaLink="false">https://medium.com/p/556b8ef90dea</guid>
            <category><![CDATA[react-native]]></category>
            <category><![CDATA[margin]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[spacing]]></category>
            <category><![CDATA[padding]]></category>
            <dc:creator><![CDATA[Hirokazu Tei]]></dc:creator>
            <pubDate>Mon, 19 Aug 2019 08:59:33 GMT</pubDate>
            <atom:updated>2019-08-19T17:09:12.654Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*v-qNyXj6n3TNAl8xlt_oYw.png" /><figcaption>Spacing with Inset, Stack &amp; Queue</figcaption></figure><h3>Acknowledgements</h3><p>The following blurb is heavily influenced by Nathan Curtis’s <a href="https://medium.com/eightshapes-llc/space-in-design-systems-188bcbae0d62">Space in Design Systems</a>.<br>The React Native library I made was inspired by a similar React library named <a href="https://github.com/nathanwinder/react-spacing">react-spacing</a> by Nathan Winder.</p><h3>A Library!</h3><p>I made a React Native library about this blog so if you are interested, please check out its <a href="https://github.com/hirokazutei/react-native-spacing-system">GitHub</a> and <a href="https://www.npmjs.com/package/react-native-spacing-system">NPM</a>!</p><h3>Making Sense of Padding vs. Margin</h3><p>As many frontend software developers are already aware, <strong>padding</strong> defines the spacing <strong>inside</strong> of an element while <strong>margin</strong> defines the space <strong>outside</strong> of the element. For example, if you have a div or a View element with a border, increasing padding would expand the space <strong>inside</strong> of the border by the defined unit while increasing margin would expand the space <strong>outside</strong> of the border.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*5b98fqanHVkFZV9kcEce_g.jpeg" /><figcaption>Image from Kunal’s <a href="https://medium.com/frontendshortcut/margin-vs-padding-c1fc8ea8bfaf"><strong>Margin vs. Padding</strong></a></figcaption></figure><p>When we build components and subsequently arrange them, we often utilize an empty div or View to surround the component and apply spacing properties in order to achieve the spacial arrangement we desire. However, since both styling properties can be used to achieve similar goals, many questions begin to arise:</p><ul><li>When background color or border is not a concern, do we use margin or padding to space components?</li><li>If we want to space out two components aligned in a column, do we apply spacing to the one above or the one below? (Or half-half?)</li><li>Should we come up with a rule to standardize our spacing practices, is there a way to enforce those rules aside from code-review?</li></ul><p>While having a scattered method to space components would not break your application’s design, it does incur some development costs since a non-standardized spacing practice makes the resulting design a little more difficult to picture, thus more difficult to modify.</p><h3>Establishing Rules</h3><p>Given the roles of margin and padding, we begin to reason what their use-cases are within the context of React.</p><h4>Margin</h4><p>Since margin extends the space outside of an element:</p><blockquote><em>Margin’s concern is to separate adjacent components away from each other..</em></blockquote><h4>Padding</h4><p>Since padding extends the space inside fo an element:</p><blockquote><em>Padding’s concern is either to extend the additional properties (such as background color or border) of the component, or adding a consistent spacing between the parent component and its child/children.</em></blockquote><h4>Layout</h4><p>When we layout components in React, the default is that components are inversely stacked from top to bottom, and if we change the flexDirection to row, the components are queued from left to right. This gives us the order of operations for component layouting:</p><blockquote><em>Left to right, then top to bottom.</em></blockquote><p>Given these use-cases and rules, we can begin to formulate restrictions:</p><ul><li>Since we are building left to right, top to bottom:</li><li>The left component should always use marginRight to space itself from the component to the right.</li><li>Subsequently, the top component should always use marginBottom to space itself from the component to the bottom.</li><li>Since the above covers all margin use-cases, aside from exceptions, other forms of the margin can be restricted.</li><li>Since there are reasonable use-cases where you would want to increase the component property in each of the four directions, padding must be able to be used in all directions.</li></ul><p>To enforce these rules, spacing components can be made to take on the task of adding padding and margin.</p><h3>INSET / OUTSET</h3><h4>Inset</h4><p>Inset is a spacing component that warps around multiple children components and adds padding on all or any of the four sides and their combination. Aside from padding properties, it also accepts flex for convenience.</p><pre>// Inset Example<br>​<br>&lt;Inset all={16}&gt;<br>  &lt;Text&gt;Hello&lt;/Text&gt;<br>  &lt;Text&gt;World!&lt;/Text&gt;<br>&lt;/Inset&gt;</pre><p>The “Hello” and “World!” Text components are situated within a View with a padding of 16 units.</p><h4>Outset</h4><p>Outset is a spacing component that warps a child component and adds margin on the right of right or bottom on behalf of the child component. Aside from marginRight and marginBottom, it also accepts flex for convenience.</p><pre>// Outset Example<br>&lt;View&gt;<br>  &lt;Outset bottom={16}&gt;<br>    &lt;Text&gt;Hello&lt;/Text&gt;<br>  &lt;/Outset&gt;<br>  &lt;Text&gt;World!&lt;/Text&gt;<br>&lt;/View&gt;</pre><p><em>The “Hello” and “World!” </em><em>Text components separated vertically by 16 units</em></p><p>With this approach, theoretically, we should be able to standardize the way we layout components; however, after using them for a while, we have encountered a few problems with Outset.</p><h3>PROBLEMS WITH OUTSET</h3><p>Once we began using Outset we have noticed that there were some issues with this component.</p><ol><li>In use-cases where we want to map an array of items and apply spacing between each elements except for the last, we must solve the problem by either verbosely pass in the child component without the Outset wrapper or pass in a 0 into the Outset&#39;s bottom or right margin prop, which made us feel uneasy.</li></ol><pre>/* A Sample Solution */<br>items.map((item, index) =&gt; {<br>  const isLastItem = idx =&gt; items.length === idx + 1;<br>  return isLastItem ? { item } : &lt;Outset bottom={8}&gt;{item}&lt;/Outset&gt;;<br>});<br>​<br>/* Another Solution */<br>items.map((item, index) =&gt; {<br>  const isLastItem = idx =&gt; items.length === idx + 1;<br>  return &lt;Outset bottom={isLastItem ? 0 : 8}&gt;{item}&lt;/Outset&gt;;<br>});</pre><ol><li>Outset also increases the layer of nesting in our code, which reduces legibility.</li><li>When we want to equally space between two components using flex with a defined space between the two components, Outset poses a problem since the left or top component is made inherently larger with the margin property.</li></ol><h3>STACK ・ QUEUE</h3><p>Instead we have opted to create Stack and Queue. Since Stack was already a term used in Nathan&#39;s blog, we figured Queue would be a great complement as both being computer science terms and the idea that stacking things are vertical and being in a queue is a horizontal act. Stack and Queue are both simple div or View elements that has a certain height (Stack) or width (Queue). They act as shims between two components.</p><h4>Stack</h4><p>Stack is a vertical spacing component, think of a pile of plates at a buffet.</p><pre>&lt;View&gt;<br>  &lt;BuffetPlates /&gt;<br>  &lt;Stack size={16} /&gt;<br>  &lt;BuffetPlates /&gt;<br>  &lt;Stack size={16} /&gt;<br>  &lt;BuffetPlates /&gt;<br>  &lt;Stack size={16} /&gt;<br>  &lt;BuffetPlates /&gt;<br>&lt;/View&gt;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/456/1*FWK4oSRCuGE-UWBGtNQNgA.png" /><figcaption><strong>Stack</strong>ing buffet plates</figcaption></figure><h4>Queue</h4><pre>&lt;View style={{ flexDirection: &quot;row&quot; }}&gt;<br>  &lt;PatientPatron /&gt;<br>  &lt;Queue size={16} /&gt;<br>  &lt;PatientPatron /&gt;<br>  &lt;Queue size={16} /&gt;<br>  &lt;PatientPatron /&gt;<br>  &lt;Queue size={16} /&gt;<br>  &lt;PatientPatron /&gt;<br>&lt;/View&gt;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/318/1*SQ5_QQrlvBN0thCvXSPWNQ.png" /><figcaption><strong>Queue</strong>ing for bubbletea</figcaption></figure><p>Although Stack and Queue have more potential to be misused since they can be place above or to the left of a top left corner element for example, where an Inset is often the correct solution. However, the freedom also means that these components can be utilized much easier and the misuses are not too difficult to spot.</p><p>Stack and Queue removes the need for the child component to wrapped around thereby solving both the flex problem as well as the nesting problem. In situations where items are mapped, it is easy to add or remove Stack or Queue using a conditional {isLast &amp;&amp; &lt;Queue size={16} /&gt;}, which applies the condition only to the spacing of the last element and not the element itself. Finally, the existence of verticality and horizontality in the component allows us to infer the flex direction of components without looking at the style of the surrounding div or View component.</p><p>While Stack, Queue and Inset are not perfect, it has been the most promising space design system that we have tried.</p><h3>CONCLUSION</h3><p>Through the use of components as spacing elements, we have been able to reduce the ability to misuse padding and margin as well as standardize the way we layout components. While Inset, Stack and Queue cannot remove misuse entirely and comes at a cost of slight complexity and problems, we find that the cost is worthwhile for the benefits that they provide.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=556b8ef90dea" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>