<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Toolkit &#8211; Marcos Rego</title>
	<atom:link href="https://marcosrego.com/tag/toolkit/feed/" rel="self" type="application/rss+xml" />
	<link>https://marcosrego.com</link>
	<description>Web Development</description>
	<lastBuildDate>Mon, 05 Feb 2024 15:23:56 +0000</lastBuildDate>
	<language>en-GB</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.7.1</generator>
	<item>
		<title>How to avoid (CLS) Layout Shift when using JS Components</title>
		<link>https://marcosrego.com/blog/avoid-layout-shift-mrutils/</link>
		
		<dc:creator><![CDATA[Marcos Rego]]></dc:creator>
		<pubDate>Mon, 06 Jun 2022 22:26:37 +0000</pubDate>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[For developers]]></category>
		<category><![CDATA[Mr.Utils]]></category>
		<category><![CDATA[Toolkit]]></category>
		<category><![CDATA[Tutorial]]></category>
		<guid isPermaLink="false">https://marcosrego.com/?p=2627</guid>

					<description><![CDATA[When using components that are based on JavaScript, you might notice a layout shift on your content (CLS), especially if you delay the start of JavaScript in some way. In that situation, you&#8217;ll see the initial look of the elements until JavaScript kicks in. But of course, delaying JavaScript until user interaction or after some [&#8230;]]]></description>
										<content:encoded><![CDATA[<span id="more-2627"></span>
<!--noteaser-->



<p>When using components that are based on JavaScript, you might notice a layout shift on your content (CLS), especially if you delay the start of JavaScript in some way. In that situation, you&#8217;ll see the initial look of the elements until JavaScript kicks in.</p>



<p>But of course, delaying JavaScript until user interaction or after some seconds it&#8217;s a good practice and a good trick to increase the score of a website on<a href="https://pagespeed.web.dev/" data-type="URL" data-id="https://pagespeed.web.dev/" target="_blank" rel="noreferrer noopener"> pagespeed insights</a>. So the better option is to find ways to avoid layout shifts, fortunately it is not that complicated.</p>



<p>For this tutorial, I&#8217;ll be focusing on <a href="https://marcosrego.com/development/mr-utils/" data-type="post" data-id="473">Mr.Utils</a> JS components, but the same logic applies to other tools.</p>



<div style="height:40px" aria-hidden="true" class="  wp-block-spacer"></div>



<h2 class="wp-block-heading"><strong>Understand how general JS Components work</strong></h2>



<p>The way Mr.Utils JavaScript components work is by adding the necessary CSS inline properties to the element after page load, but only if the element doesn&#8217;t have a utility class that already uses some of those properties.</p>



<p>And as you might know, HTML classes don&#8217;t rely on JavaScript to apply the style of CSS properties. We&#8217;ll use that fact to our benefit, to avoid the layout shift when delaying JavaScript in this situation.</p>



<h2 class="wp-block-heading"><strong>Fixing the Content Layout Shift</strong></h2>



<h3 class="  wp-block-heading"><strong>Fix CLS for components that add properties</strong></h3>



<p>Components such as <strong>Mr.Utils Swipe component</strong> add the following inline properties to the element:</p>



<pre class="wp-block-code"><code>display: -webkit-inline-box;
flex-wrap: unset !important;
overflow-x: scroll !important;
-webkit-overflow-scrolling: touch !important;
overflow-y: hidden;</code></pre>



<p>But there&#8217;s one utility class that does the exact same thing, so all you need to do is to add the following utility class after adding the swipe class:</p>



<pre class="wp-block-code"><code>mr-horizontalscroll</code></pre>



<p>If you are using Mr.Utils as a plugin: Instead of adding the class, you can select the option at Misc &gt; Scroll &gt; Horizontal Scroll.</p>



<aside class="  wp-block-group sidebar has-main-text-color-color has-aside-background-color-background-color has-text-color has-background"><div class="wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow">
<section class="wp-block-group has-main-text-color-color has-main-background-color-background-color has-text-color has-background"><div class="wp-block-group__inner-container is-layout-flow wp-block-group-is-layout-flow">
<h3 class="   has-main-text-color-color has-text-color wp-block-heading"><strong>Did you know?</strong></h3>



<figure class="    wp-block-image size-large"><img fetchpriority="high" decoding="async" width="1024" height="538" src="https://marcosrego.com/wp-content/uploads/2021/06/mrutils-marcosrego-opengraph-1024x538.png" alt="Mr.Utils Opengraph Image" class="wp-image-1029" title="mrutils marcosrego opengraph - How to avoid (CLS) Layout Shift when using JS Components" srcset="https://marcosrego.com/wp-content/uploads/2021/06/mrutils-marcosrego-opengraph-1024x538.png 1024w, https://marcosrego.com/wp-content/uploads/2021/06/mrutils-marcosrego-opengraph-300x158.png 300w, https://marcosrego.com/wp-content/uploads/2021/06/mrutils-marcosrego-opengraph-768x403.png 768w, https://marcosrego.com/wp-content/uploads/2021/06/mrutils-marcosrego-opengraph.png 1200w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>



<p class="  has-main-text-color-color has-text-color"><strong><a href="https://marcosrego.com/development/mr-utils/" data-type="post" data-id="473">Mr.Utils</a></strong> can be installed as a <strong>WordPress plugin</strong>.<br>Give to the <strong>block editor</strong> super-powers such as <strong>pagination or components</strong> and apply changes <strong>depending of the device</strong>!</p>



<div class="wp-block-buttons is-layout-flex wp-block-buttons-is-layout-flex">
<div class="   wp-block-button"><a class="wp-block-button__link has-invert-color-color has-link-color-background-color has-text-color has-background" href="https://marcosrego.com/client-area/downloads/?product=mrutils"><strong>Get it now (free)</strong></a></div>
</div>
</div></section>
</div></aside>



<p>In the end, you&#8217;ll have the following classes on your element:</p>



<pre class="wp-block-code"><code>mr-swipe mr-horizontalscroll</code></pre>



<p>By doing this, the properties will be added before JavaScript runs, and you&#8217;ll be avoiding the layout shift, without losing the benefits of the swipe component (such as the ability to drag with a mouse).</p>



<h3 class="  wp-block-heading"><strong>Fix CLS for components that create new elements</strong></h3>



<p>Components such as <strong>Tabs </strong>and navigation options on <strong>Pagination (Radio, Arrows, Dropdown)</strong> add new elements to the page if those elements do not exist yet. So, the way to avoid layout shifting when JavaScript starts on this situation, is to create the elements manually, let&#8217;s take a look at that:</p>



<p>If you want to use the exact same elements that Mr.Utils&#8217; JavaScript would create, you can use the below HTML Codes after the element <em>(or before when using <a href="https://github.com/marcosrego-web/mr-utils/wiki/Utility-Classes#components" target="_blank" rel="noopener">the proper class</a>)</em>.</p>



<p><strong>Pagination Arrows</strong></p>



<pre class="wp-block-code"><code>&lt;div class="mr-pagination"&gt;
&lt;button class="mr-arrows mr-prev"&gt;⇦&lt;/button&gt;
&lt;button class="mr-arrows mr-next"&gt;⇨&lt;/button&gt;
&lt;/div&gt;</code></pre>



<p><strong>Pagination Radio Buttons</strong></p>



<p><em>Duplicate the last &#8220;mr-radio&#8221; to match the number of pages</em>.</p>



<pre class="wp-block-code"><code>&lt;div class="mr-pagination"&gt;
&lt;span class="mr-radios"&gt;
&lt;input name="mr-radio" title="1/2" class="mr-radio" type="radio" value="1" checked="checked"&gt;
&lt;input name="mr-radio" title="2/2" class="mr-radio" type="radio" value="2"&gt;
&lt;/span&gt;
&lt;/div&gt;</code></pre>



<p><strong>Pagination Dropdown</strong></p>



<p><em>Duplicate the last &#8220;option&#8221; to match the number of pages</em>.</p>



<pre class="wp-block-code"><code>&lt;div class="mr-pagination"&gt;
&lt;select class="mr-pageselect" title="/2"&gt;
&lt;option value="1"&gt;1&lt;/option&gt;
&lt;option value="2"&gt;2&lt;/option&gt;
&lt;/select&gt;
&lt;/div&gt;</code></pre>



<p><strong>Tabs</strong></p>



<p><em>Duplicate the last &#8220;mr-tab&#8221; to match the number of pages</em>.</p>



<pre class="wp-block-code"><code>&lt;div class="mr-tabsnav mr-horizontalscroll"&gt;
&lt;button class="mr-tab mr-active"&gt;Tab 01&lt;/button&gt;
&lt;button class="mr-tab"&gt;Tab 02&lt;/button&gt;
&lt;/div&gt;</code></pre>



<p>If you want a bit more control or if you use site-builders, you can also create those new elements differently, as long as the main/parent element uses the class <em>mr-pagination</em> or <em>mr-tabsnav</em>.</p>



<p>If you are using Mr.Utils as a plugin on WordPress, you can use the block editor to create buttons for the Tabs component. Then, by selecting the buttons&#8217; container, on Utilities choose Components &gt; Tabs Navigation.</p>



<h2 class="wp-block-heading"><strong>Live example of fixing Tabs CLS</strong></h2>



<p>A good live example of this is on <a href="https://shop.itemzero.com/" data-type="URL" data-id="https://shop.itemzero.com/" target="_blank" rel="noreferrer noopener">Itemzero&#8217;s Shop</a>, the tabs were created using the block editor and Mr.Utils.</p>



<figure class="  wp-block-gallery has-nested-images columns-default is-cropped wp-block-gallery-1 is-layout-flex wp-block-gallery-is-layout-flex">
<figure class="     wp-block-image size-large"><img decoding="async" width="1024" height="572" data-id="1611" src="https://marcosrego.com/wp-content/uploads/2021/04/itemzero-mrdev_widget-tabs-1024x572.jpg" alt="Itemzero&#039;s shop uses tabs" class="wp-image-1611" title="itemzero mrdev widget tabs - How to avoid (CLS) Layout Shift when using JS Components" srcset="https://marcosrego.com/wp-content/uploads/2021/04/itemzero-mrdev_widget-tabs-1024x572.jpg 1024w, https://marcosrego.com/wp-content/uploads/2021/04/itemzero-mrdev_widget-tabs-300x168.jpg 300w, https://marcosrego.com/wp-content/uploads/2021/04/itemzero-mrdev_widget-tabs-768x429.jpg 768w, https://marcosrego.com/wp-content/uploads/2021/04/itemzero-mrdev_widget-tabs-1536x858.jpg 1536w, https://marcosrego.com/wp-content/uploads/2021/04/itemzero-mrdev_widget-tabs.jpg 1920w" sizes="(max-width: 1024px) 100vw, 1024px" /><figcaption class="wp-element-caption">Itemzero&#8217;s shop uses tabs from Mr.Utils components.</figcaption></figure>
</figure>



<div style="height:40px" aria-hidden="true" class="  wp-block-spacer"></div>



<p>And that&#8217;s it! Those are some ways to avoid layout shift when using JS Components.</p>



<div style="height:40px" aria-hidden="true" class="  wp-block-spacer"></div>



<h2><b>Comments</b></h2>
<div id="disqus_thread"></div>
<script>
if(mrGetCookie('cookielawinfo-checkbox-functional') == 'yes') {
    var disqus_config = function () {
        this.page.url = (location.protocol+'//'+location.host+location.pathname).replace("/account/", "/");
        this.page.identifier = document.querySelector("article").id.replace("post-", "");
    };
    
    (function() {
    var d = document, s = d.createElement('script');
    s.src = 'https://marcosrego-web.disqus.com/embed.js';
    s.setAttribute('data-timestamp', +new Date());
    (d.head || d.body).appendChild(s);
    })();
} else {
    document.querySelector('#disqus_thread').innerHTML = 'You cannot view or add comments because you did not consent to the use of functional cookies: <a class="  cli_manage_current_consent" style="cursor:pointer;">Manage your consent.</a>';
}
</script>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Mr.Utils</title>
		<link>https://marcosrego.com/development/mr-utils/</link>
		
		<dc:creator><![CDATA[Marcos Rego]]></dc:creator>
		<pubDate>Sun, 31 Oct 2021 18:09:00 +0000</pubDate>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[React]]></category>
		<category><![CDATA[Toolkit]]></category>
		<guid isPermaLink="false">https://marcosrego.com/?p=473</guid>

					<description><![CDATA[A front-end toolkit with a syntax friendly to everyone, that gives you utility classes, custom breakpoints, css variables and javascript functions. Utilities used on Mr.Dev.&#8217;s plugins that you can also use on your own environment. If you are using WordPress you can even install it as a plugin. You&#8217;ll get friendly options on the block [&#8230;]]]></description>
										<content:encoded><![CDATA[<span id="more-473"></span>
<!--noteaser-->



<figure class=" fixed-video_mobile mr-fadein  wp-block-video fixed-video_mobile mr-fadein"><video autoplay loop muted preload="none" src="https://marcosrego.com/wp-content/uploads/2021/10/mrutils_phone.mp4" playsinline></video></figure>



<figure class=" fixed-video_desktop mr-fadein  wp-block-video fixed-video_desktop mr-fadein"><video autoplay loop muted preload="none" src="https://marcosrego.com/wp-content/uploads/2021/06/mrutils_desktop.mp4" playsinline></video></figure>



<h2 class="wp-block-heading" id="a-front-end-toolkit-with-a-syntax-friendly-to-everyone-that-gives-you-utility-classes-custom-breakpoints-css-variables-and-javascript-functions"><strong>A front-end toolkit with a syntax friendly to everyone, that gives you utility classes, custom breakpoints, css variables and javascript functions.</strong></h2>



<div class="wp-block-buttons is-layout-flex wp-block-buttons-is-layout-flex">
<div class="   wp-block-button"><a class="wp-block-button__link has-invert-color-color has-link-color-background-color has-text-color has-background wp-element-button" href="https://marcosrego.com/client-area/downloads/?product=mrutils"><strong>Get it now (free)</strong></a></div>



<div class="   wp-block-button"><a class="wp-block-button__link has-main-background-color-color has-main-text-color-background-color has-text-color has-background wp-element-button" href="https://github.com/marcosrego-web/mr-utils/wiki" style="border-radius:6px" target="_blank" rel="noreferrer noopener">See documentation</a></div>
</div>



<h3 class="  wp-block-heading" id="utilities-used-on-mr-dev-s-plugins-that-you-can-also-use-on-your-own-environment"><strong>Utilities used on Mr.Dev.&#8217;s plugins that you can also use on your own environment.</strong></h3>



<p>If you are using WordPress you can even install it as a plugin. You&#8217;ll get <a href="https://marcosrego.com/blog/mr-utils-block-editor-features/" data-type="post" data-id="1997">friendly options on the block editor</a>, which allow you or your clients to use all the features without needing to know the classes or functions.</p>
]]></content:encoded>
					
		
		<enclosure url="https://marcosrego.com/wp-content/uploads/2021/06/mrutils_desktop.mp4" length="4881295" type="video/mp4" />
<enclosure url="https://marcosrego.com/wp-content/uploads/2021/10/mrutils_phone.mp4" length="1072336" type="video/mp4" />

			</item>
	</channel>
</rss>
