<?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: Andrea Berardi</title>
    <description>The latest articles on DEV Community by Andrea Berardi (@andberry).</description>
    <link>https://dev.to/andberry</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%2F713616%2Fc321b077-507c-4738-9271-387c6e55ae91.jpg</url>
      <title>DEV Community: Andrea Berardi</title>
      <link>https://dev.to/andberry</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/andberry"/>
    <language>en</language>
    <item>
      <title>BEM, CSS Namespaces and ITCSS</title>
      <dc:creator>Andrea Berardi</dc:creator>
      <pubDate>Mon, 21 Mar 2022 02:13:07 +0000</pubDate>
      <link>https://dev.to/andberry/bem-css-namespaces-and-itcss-4lp9</link>
      <guid>https://dev.to/andberry/bem-css-namespaces-and-itcss-4lp9</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;because writing CSS is easy; looking after it is not.&lt;/p&gt;

&lt;p&gt;-- &lt;cite&gt;&lt;a href="https://twitter.com/csswizardry" rel="noopener noreferrer"&gt;csswizardry&lt;/a&gt;&lt;/cite&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Yes, even though currenly there are lots of tools involved in CSS landscape nowadays, it's a pretty easy language to start with, and due to its design (pitfalls ?) it's also pretty easy to find yourself soon wasting time on deciding classes names and struggling to make your code more maintainable and scalable.&lt;/p&gt;

&lt;p&gt;Basically HTML and CSS shares classes (and ids) to shape the final UI and reusing classes being sure to do exactly what you want, where you want, without introducing problems in a totally different part of the ui requires you, developer, to be really accurate/diligent.&lt;/p&gt;

&lt;p&gt;Methodologies, naming conventions and architectures help you a lot.&lt;/p&gt;

&lt;p&gt;This is what is currently helping me a lot.&lt;/p&gt;

&lt;h2&gt;
  
  
  BEM
&lt;/h2&gt;

&lt;p&gt;Block Element Modifier is a methodolgy, or a naming convention suggestion that &lt;strong&gt;helps you giving classes a role, relationship, responsibilities and states&lt;/strong&gt; in a clear way.&lt;/p&gt;

&lt;p&gt;Definitely a way to save time when deciding class names.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Block&lt;/strong&gt;: the parent, the standalone entity that has a meaning by itself (top level abstraction of your component)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Element&lt;/strong&gt;: a part of the block, without meaning standalont by itself (a child item)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Modifier&lt;/strong&gt;: a flag, a variation, a state to a block or element&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here is an example of BEM applied to a button&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;a class="button button--big"&amp;gt;
    &amp;lt;span class="button__icon"&amp;gt;&amp;lt;/span&amp;gt;
&amp;lt;/a&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;.button {
    display: inline-flex;
    justify-content: center;
    align-items: center;
    border-radius: 6px;
    padding: 8px 12px;
}
.button__icon {
    margin-right: 6px;
    font-size: 0.5rem;
}
.button--big {
    padding: 12px 16px;
    font-size: 20px;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  CSS Namespaces
&lt;/h2&gt;

&lt;p&gt;How many times have you looked at a piece of HTML only to wonder:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;which classes do what&lt;/li&gt;
&lt;li&gt;which classes are related to each other&lt;/li&gt;
&lt;li&gt;which classes are optional&lt;/li&gt;
&lt;li&gt;which classes are recyclable&lt;/li&gt;
&lt;li&gt;which classes can you delete, and so on?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A lot of times, I bet.&lt;/p&gt;

&lt;p&gt;A CSS Namespace will tell you exactly how a class behave in a more global sense.&lt;/p&gt;

&lt;p&gt;Again, this methodolgy is meant to be used &lt;strong&gt;when deciding class names&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;ere are the individual namespaces and a brief description.&lt;/p&gt;

&lt;h3&gt;
  
  
  Object: o-
&lt;/h3&gt;

&lt;p&gt;A class name that starts with "o-" is an &lt;strong&gt;Object&lt;/strong&gt;, an &lt;strong&gt;abstract layout implementation&lt;/strong&gt;. Used in any number of unrelated contexts: tread carefully.&lt;/p&gt;

&lt;p&gt;Eg. &lt;strong&gt;o-grid, o-sidebar-left&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Component: c-
&lt;/h3&gt;

&lt;p&gt;A class name that starts with "c-" is a &lt;strong&gt;Component&lt;/strong&gt;: this is a concrete, &lt;strong&gt;implementation-specific piece of UI&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Eg. &lt;strong&gt;c-hero, c-card&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Utility: u-
&lt;/h3&gt;

&lt;p&gt;A class name that starts with "u-" is a &lt;strong&gt;Utility&lt;/strong&gt; class. It has a &lt;strong&gt;very specific role&lt;/strong&gt; and it can be &lt;strong&gt;reused&lt;/strong&gt; and is &lt;strong&gt;not tied to any specific piece of UI&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Eg. &lt;strong&gt;u-color-primary, u-text-center&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Theme: t-
&lt;/h3&gt;

&lt;p&gt;A class name that starts with "t-" adds a &lt;strong&gt;adds a theme to a view&lt;/strong&gt;. Overrides default style due to the presence of a theme.&lt;/p&gt;

&lt;p&gt;Eg. &lt;strong&gt;t-light, t-saint-valentine&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  State: is- / has-
&lt;/h3&gt;

&lt;p&gt;A class that starts with &lt;strong&gt;is-&lt;/strong&gt; or &lt;strong&gt;has-&lt;/strong&gt; indicates that this piece of UI is currently styled a certain way because of a state or condition. Used everywhere, not tighten to specific UI block&lt;/p&gt;

&lt;p&gt;Eg. &lt;strong&gt;is-active, has-2-items&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Javascript Hooks: js-
&lt;/h3&gt;

&lt;p&gt;A class that starts with - &lt;strong&gt;js-&lt;/strong&gt; indicates that this piece of the DOM has some JavaScript binds onto it.&lt;/p&gt;

&lt;p&gt;Eg. &lt;strong&gt;js-carousel, js-accordion&lt;/strong&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  ITCSS: Inverted Triangle CSS
&lt;/h2&gt;

&lt;p&gt;It helps us to &lt;strong&gt;organize our CSS files&lt;/strong&gt; to better deal with CSS specifics/issues/pitfalls like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;global namespace&lt;/li&gt;
&lt;li&gt;cascade&lt;/li&gt;
&lt;li&gt;selectors specificity.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;At the end, we split CSS properties based on their level of specificity and importance.&lt;/p&gt;

&lt;p&gt;Going from top to bottom symbolizes an increase in specificity, and each subsection of the triangle may be considered a separate file or group of files.&lt;/p&gt;

&lt;p&gt;My personal implementation:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;1-settings&lt;/strong&gt;: sass variables: &lt;em&gt;font, colors, spacings&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;2-tools&lt;/strong&gt;: mixins and functions: &lt;em&gt;bg-image(), abs-center()&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;3-base&lt;/strong&gt;: generic + elements, normalize/reset + base html elements style without classes: &lt;em&gt;body, h1, table&lt;/em&gt; (this is the first CSS output)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;4-layout&lt;/strong&gt;: (remember objects?) abstract layouts, unstyles patterns: &lt;em&gt;grid, layout-sidebar, media&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;5-components&lt;/strong&gt;: style for a specific piece of UI: &lt;em&gt;c-hero, c-footer&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;6-modules&lt;/strong&gt;: aggregation of modules: &lt;em&gt;c-cards-list, c-cards-carousel&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;7-trumps/utilities&lt;/strong&gt;: utilities and helper classes with ability to override anything which goes before, all &lt;strong&gt;CMS specific stuff like base overrides, views, specific page style&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Links
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;My ITCSS Github repo: &lt;a href="https://github.com/andberry/berry-itcss" rel="noopener noreferrer"&gt;https://github.com/andberry/berry-itcss&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;My Component-based Drupal 8/9 theme implemented with ITCSS: &lt;a href="https://github.com/andberry/berrydrupal" rel="noopener noreferrer"&gt;https://github.com/andberry/berrydrupal&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Namespaces Article from CSSWizardry: &lt;a href="https://csswizardry.com/2015/03/more-transparent-ui-code-with-namespaces/" rel="noopener noreferrer"&gt;https://csswizardry.com/2015/03/more-transparent-ui-code-with-namespaces/&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>css</category>
      <category>itcss</category>
      <category>frontend</category>
    </item>
    <item>
      <title>Eleventy (11ty) setup for multilingual component-based flexible pages (aka One Page Websites with i18n)</title>
      <dc:creator>Andrea Berardi</dc:creator>
      <pubDate>Sun, 20 Mar 2022 05:32:54 +0000</pubDate>
      <link>https://dev.to/andberry/eleventy-11ty-setup-for-multilingual-component-based-flexible-pages-aka-one-page-websites-with-i18n-2ilo</link>
      <guid>https://dev.to/andberry/eleventy-11ty-setup-for-multilingual-component-based-flexible-pages-aka-one-page-websites-with-i18n-2ilo</guid>
      <description>&lt;h2&gt;
  
  
  Intro
&lt;/h2&gt;

&lt;p&gt;Such a huge hype about Eleventy (11ty) in the last year that I wanted to give it a try.&lt;/p&gt;

&lt;h3&gt;
  
  
  Documentation
&lt;/h3&gt;

&lt;p&gt;The documentations is definitely good, but it's lacking exactly what I was looking for: how to use structure multi-sections pages built using reusable UI components (hero, text, text-image, gallery, etc.).&lt;/p&gt;

&lt;p&gt;I was able to find just a couple of devs asking for the same setup, and at the end I figured out that this tool is mainly designed to be used as a data-driven static generator with simple content structure: 1 to 1 relationship between content and template. &lt;/p&gt;

&lt;p&gt;Luckily, I found this interesting article, written by Laurence Hughes, that was my starting point for this setup: &lt;a href="https://fuzzylogic.me/posts/reusable-code-snippets-and-components-in-eleventy/" rel="noopener noreferrer"&gt;Reusable code snippets and components in Eleventy&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Frontend toolchain and libraries
&lt;/h3&gt;

&lt;p&gt;11ty is not coupled with any kind of frontend library or framework. No Sass compilation in place, no postcss, no babel, no webpack... nothing. No specific tool used by default, no toolchain setup for you: you are free to use whatever you want, but, at the same time, you're going to spend just a bit of time to setup whatever you want.&lt;/p&gt;

&lt;h3&gt;
  
  
  Configuration file
&lt;/h3&gt;

&lt;p&gt;Even if Eleventy can be used with zero configuration, be comfortable as early as possible with playing with its config file, since you'll need it to define custom collections, filters, basic setup (etc.)&lt;/p&gt;

&lt;h2&gt;
  
  
  11ty basic concepts
&lt;/h2&gt;

&lt;p&gt;With 11ty, content files are called &lt;strong&gt;templates&lt;/strong&gt;, and what I usually call templates are called &lt;strong&gt;layouts&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;There are lots of engines available and you can even mix them.&lt;br&gt;
I used Markdown for templates (content files), and Nunjucks for layouts (templates).&lt;/p&gt;

&lt;p&gt;Some key points:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;by default every template you write automatically generates a final html page&lt;/li&gt;
&lt;li&gt;you use Front Matter to drive 11ty behaviours and define custom content you might need inside layouts&lt;/li&gt;
&lt;li&gt;you use the &lt;strong&gt;layout&lt;/strong&gt; Front Matter key to indicate which layout has to be used to generate the page&lt;/li&gt;
&lt;li&gt;collections are groups of content you can play with using APIs: you can define collections in different ways: tags in Front Matter, tags in directory-specific json file, 11ty configuration&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Nunjucks basics
&lt;/h2&gt;

&lt;p&gt;I love &lt;strong&gt;component-based development&lt;/strong&gt;: the idea is that you have a piece of reusable encapsulated UI you can use thoughtout your project, passing the scoped data you want time by time.&lt;/p&gt;

&lt;p&gt;With Twig this is typically implemented writing 1 markup file for each components (partial) and then including it using &lt;code&gt;include&lt;/code&gt; and the &lt;code&gt;with only&lt;/code&gt; keywords, passing the only data required by that component.&lt;/p&gt;

&lt;p&gt;Nunjucks is a template language for JavaScript with syntax pretty similar to Twig, supporting template inheritance and partials definitions and includes, but one main difference I found, is that its &lt;code&gt;include&lt;/code&gt; doesn't support that so-practical &lt;code&gt;with&lt;/code&gt; keywork.&lt;/p&gt;

&lt;p&gt;With Nunjucks the best way to write component based templates is to use the &lt;code&gt;macro&lt;/code&gt; stratement.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    {% macro field(name, value='', type='text') %}
        &amp;lt;div class="field"&amp;gt;
            &amp;lt;input type="{{ type }}" name="{{ name }}" value="{{ value | escape }}" /&amp;gt;
        &amp;lt;/div&amp;gt;
    {% endmacro %}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Pay attention: &lt;code&gt;include&lt;/code&gt; seems to do the job, but actually it's good to be used just for pieces of templates that don't require params. The problem is params scoping: you can try to use the &lt;code&gt;{% set %}&lt;/code&gt; statement for "passing data" just before the include statement, but there is no scope: all variables you have set before the include call are visible inside the component.&lt;/p&gt;

&lt;h2&gt;
  
  
  Structuring the content and layout for multi-sections page
&lt;/h2&gt;

&lt;p&gt;For each page create one directory containing:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;one data file configuring 11ty to not generate a page for every file inside (&lt;code&gt;"permalink": false&lt;/code&gt;) and create a collection, setting &lt;code&gt;tags&lt;/code&gt; for every file inside the directory&lt;/li&gt;
&lt;li&gt;one file for each section of the page using a custom "clayout" Front Matter key to indicate the UI component to use&lt;/li&gt;
&lt;li&gt;one page file (template) "index.md" with permalink Front Matter key set to the page's URL you want&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Nunjucks and UI components
&lt;/h3&gt;

&lt;p&gt;And this is how I structured the layouts part:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;1 specific directory containing all reusable UI components markup&lt;/li&gt;
&lt;li&gt;1 "macros" file defining all availble UI components using &lt;strong&gt;include&lt;/strong&gt; and &lt;strong&gt;macro&lt;/strong&gt; statements, to be used inside layouts files&lt;/li&gt;
&lt;li&gt;1 template logic for/each cycle (DRY principle) to be used when cycling all different page setion files (collections) and rendering the relevant component&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Frontend toolchain
&lt;/h2&gt;

&lt;p&gt;Gulp FTW here.&lt;/p&gt;

&lt;p&gt;I used Gulp to compile scss files, process JavaScript files with Babel and bundle with rollup, and setup a watch task to process scss and js when there's an update source file.&lt;/p&gt;

&lt;p&gt;All processed files (final style.min.css and app.min.js) are saved in a "processed" folder under the src/assets folder, and then 11ty is configured to watch for changes and simply copy those files.&lt;/p&gt;

&lt;h2&gt;
  
  
  Multilanguage
&lt;/h2&gt;

&lt;p&gt;Another common request is to manage i18n in your project.&lt;/p&gt;

&lt;p&gt;As far as I know there's no official plugin for this, and as usual in JavaScript world, there are multiple ways of doing thigs. This is my approach: &lt;/p&gt;

&lt;h3&gt;
  
  
  Translating page content
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Create one directory for each language you want to support ("en", "fr"): it will contain all templates for that language&lt;/li&gt;
&lt;li&gt;Set the locale using a custom "lang" Front Matter key in templates: it will be used for getting the right content from multilanguage globals data files (labels, header, footer, navigation, etc...)
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
    ---
    layout: layouts/pages/home-fr.njk
    title: "Homepage FR"
    permalink: /fr/
    lang: fr
    ---
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Translating global content (static labels, navigations, etc.)
&lt;/h3&gt;

&lt;p&gt;Structure your globals data files with multiple keys (one per language) and code your templates and partials to include the relavanta data only&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    {
        "en": {
            "websitename": "Berry is the best",
            "footercopyright": "Foo bar"
        },

        "fr": {
            "websitename": "Berry est la meilleure ",
            "footercopyright": "Foo bar fr"
        }
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    {% set navMain = navMain[lang] if lang else navMain['en'] %}

    &amp;lt;nav id="main-nav" class="c-nav-main u-show-on-desktop"&amp;gt;
        &amp;lt;ul&amp;gt;
            {% for item in navMain %}
            [...]
        &amp;lt;/ul&amp;gt;
    &amp;lt;/nav&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Show me the code!
&lt;/h2&gt;

&lt;p&gt;Far from being perfect, here you can find my repo with all these things implemented: &lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/andberry/berry-11ty" rel="noopener noreferrer"&gt;https://github.com/andberry/berry-11ty&lt;/a&gt;&lt;/p&gt;

</description>
      <category>eleventy</category>
      <category>11ty</category>
      <category>ssg</category>
    </item>
    <item>
      <title>var, let, const: declaring variables in JavaScript</title>
      <dc:creator>Andrea Berardi</dc:creator>
      <pubDate>Mon, 27 Sep 2021 12:28:52 +0000</pubDate>
      <link>https://dev.to/andberry/var-let-const-declaring-variables-in-javascript-2k5o</link>
      <guid>https://dev.to/andberry/var-let-const-declaring-variables-in-javascript-2k5o</guid>
      <description>&lt;p&gt;In Javascript we can declare variables using &lt;strong&gt;var&lt;/strong&gt;, &lt;strong&gt;const&lt;/strong&gt; and &lt;strong&gt;let&lt;/strong&gt; keywords&lt;/p&gt;

&lt;h2&gt;
  
  
  var
&lt;/h2&gt;

&lt;p&gt;var is the old way (pre es6/es2015) of declaring variables. It's a "weak" variable declaration because it doesn't help us understanding if binding could change durign execution.&lt;/p&gt;

&lt;h2&gt;
  
  
  let
&lt;/h2&gt;

&lt;p&gt;We use let to declare bindings that could chage during execution (eg. a loop counter, or a variable used in math).&lt;/p&gt;

&lt;h2&gt;
  
  
  const
&lt;/h2&gt;

&lt;p&gt;const stands for constant. We use const for bindings that will not change during execution.&lt;/p&gt;

&lt;p&gt;Pay attention: this means that identifiers declared with const cannot be re-assigned, but in case of objects we can change its attributes after binding:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const str = "foobar"
str = "barfoo"
Uncaught TypeError: Assignment to constant variable.

const obj = { foo: 'foo', bar: 'bar' }
obj = { foo: 'foo2', bar: 'bar2' }
Uncaught TypeError: Assignment to constant variable.
obj.foo = 'foo3'
obj
{ foo: 'foo3', bar: 'bar' }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Scoping
&lt;/h2&gt;

&lt;p&gt;Variables declared with &lt;strong&gt;var&lt;/strong&gt; are only function-scoped or globally-scoped.&lt;/p&gt;

&lt;p&gt;On the other end, the scope of variables declared with &lt;strong&gt;const&lt;/strong&gt; and &lt;strong&gt;let&lt;/strong&gt; is the enclosing block (code between curly braces { } ) and not the whole function.&lt;/p&gt;

&lt;h2&gt;
  
  
  Global object (window)
&lt;/h2&gt;

&lt;p&gt;Variables declared outside functions (aka in the top level of program) with &lt;strong&gt;var&lt;/strong&gt; become properties of the global object (window), while variables declared with &lt;strong&gt;let&lt;/strong&gt; or &lt;strong&gt;const&lt;/strong&gt; don't.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var foo = "foo";
let bar = "bar";
window.foo
"foo"
window.bar
undefined
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;ul&gt;
&lt;li&gt;I don't use &lt;strong&gt;var&lt;/strong&gt; anymore&lt;/li&gt;
&lt;li&gt;I use &lt;strong&gt;let&lt;/strong&gt; only when it's required that binding can change during execution (eg. loop variables)&lt;/li&gt;
&lt;li&gt;I use &lt;strong&gt;const&lt;/strong&gt; almost for everything&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
    </item>
    <item>
      <title>jQuery $(document).ready() in vanilla JavaScript</title>
      <dc:creator>Andrea Berardi</dc:creator>
      <pubDate>Mon, 27 Sep 2021 05:20:31 +0000</pubDate>
      <link>https://dev.to/andberry/jquery-document-ready-in-vanilla-javascript-5e7j</link>
      <guid>https://dev.to/andberry/jquery-document-ready-in-vanilla-javascript-5e7j</guid>
      <description>&lt;p&gt;&lt;strong&gt;jQuery's $(document).ready()&lt;/strong&gt; method allows us to safely run code only once &lt;strong&gt;the DOM is loaded and parsed&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Not to be confused with &lt;strong&gt;$( window ).on( "load")&lt;/strong&gt;, to be used if we want to run code only if &lt;strong&gt;the whole page content (DOM, and assets as well) is loaded&lt;/strong&gt; in the browser.&lt;/p&gt;

&lt;p&gt;We can achieve the same result with vanilla js and Web APIs, in particular using:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Window: DOMContentLoaded event&lt;/li&gt;
&lt;li&gt;document.readyState&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The tricky part is considering that when the browser runs our code it may have already loaded and parsed the DOM, so the best practice is to first check the document.readyState variable.&lt;/p&gt;

&lt;p&gt;Here is my complete gist:&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


</description>
      <category>webdev</category>
      <category>javascript</category>
    </item>
    <item>
      <title>How to integrate UIkit in Drupal</title>
      <dc:creator>Andrea Berardi</dc:creator>
      <pubDate>Mon, 27 Sep 2021 04:56:16 +0000</pubDate>
      <link>https://dev.to/andberry/how-to-integrate-uikit-in-drupal-1mp6</link>
      <guid>https://dev.to/andberry/how-to-integrate-uikit-in-drupal-1mp6</guid>
      <description>&lt;p&gt;UIkit (&lt;a href="https://getuikit.com/" rel="noopener noreferrer"&gt;https://getuikit.com/&lt;/a&gt;) is an amazing lightweight and modular front-end framework that can help you, frontend developer, a lot.&lt;/p&gt;

&lt;p&gt;Super clean black/white style, it can be seen as a library of components you can integrate in your project.&lt;/p&gt;

&lt;p&gt;Components list ranges from a sort of utility classes like "Align" or "Padding" to more complex and js driven building blocks like Accordion, Dropdown, Lightbox, Scrollspy and Slideshow just to cite a few.&lt;/p&gt;

&lt;h2&gt;
  
  
  1) Using the Drupal theme
&lt;/h2&gt;

&lt;p&gt;The simplest way to start using UIkit in Drupal is to simply install the theme &lt;a href="https://www.drupal.org/project/uikit" rel="noopener noreferrer"&gt;https://www.drupal.org/project/uikit&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;composer require 'drupal/uikit:^3.15'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You get the all the framework css and js loaded and also a useful bunch of views styles/formatters.&lt;/p&gt;

&lt;h2&gt;
  
  
  2) Loading it as library from CDN
&lt;/h2&gt;

&lt;p&gt;A second way to integrate UIkit in your theme is to load it from CND defining a library in your theme.&lt;br&gt;
Something like this (pay attention that this file must follow YAML 2 spaces indentation and that uikit library has to be included in .info.yml file):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;global-css:
  version: 1.0
  css:
    theme:
      css/style.min.css: { minified: false }
global-js:
  version: 1.0
  js:
    js/app.min.js: {}
uikit:
  js:
    "//cdn.jsdelivr.net/npm/uikit@3.7.3/dist/js/uikit.min.js": { type: external, minified: true }
    "//cdn.jsdelivr.net/npm/uikit@3.7.3/dist/js/uikit-icons.min.js": { type: external, minified: true }
  css:
    base:
      "//cdn.jsdelivr.net/npm/uikit@3.7.3/dist/css/uikit.min.css": { type: external, minified: true }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You get the all the framework css and js loaded in your theme&lt;/p&gt;

&lt;h2&gt;
  
  
  3) Install with npm and integrate in scss/js
&lt;/h2&gt;

&lt;p&gt;If you have a workflow tool or task runner in place I guess you'll prefer to install UIkit with npm and then integrate the scss and js parts.&lt;/p&gt;

&lt;p&gt;Install framework with NPM&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install -P uikit
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;CSS/Scss: where to put these code snippets actually depends on how you structured your scss files,&lt;br&gt;
but the concept is that you need first to include variables and mixins (eg. _settings.scss)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@import "uikit/src/scss/variables";
@import "uikit/src/scss/mixins";&amp;lt;/pre&amp;gt;&amp;lt;/code&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and the you can include the CSS for the components you're using (eg. _components.scss):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@import "uikit/src/scss/components/visibility";
@import "uikit/src/scss/components/accordion";
@import "uikit/src/scss/components/animation";
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;JavaScript: simply import the framework (eg. in app.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 UIkit from 'uikit';
import Icons from 'uikit/dist/js/uikit-icons';
UIkit.use(Icons);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This way you load all the JavaScript part of the framework but only the style files you're actually using in your frontend,&lt;br&gt;
and you can easily get the framework css and js files bundles with your codebase (reducing the number of browser requests)&lt;/p&gt;

</description>
      <category>drupal</category>
      <category>webdev</category>
      <category>frontend</category>
    </item>
    <item>
      <title>Component-Based Frontend in Drupal</title>
      <dc:creator>Andrea Berardi</dc:creator>
      <pubDate>Mon, 27 Sep 2021 04:49:56 +0000</pubDate>
      <link>https://dev.to/andberry/component-based-frontend-in-drupal-2070</link>
      <guid>https://dev.to/andberry/component-based-frontend-in-drupal-2070</guid>
      <description>&lt;h2&gt;
  
  
  What is Component-Base Frontend Development
&lt;/h2&gt;

&lt;p&gt;First of all, &lt;strong&gt;Component-Based is an approach to UI Design and Development&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Shortly, your website is no more designed as a set of pages, but as a list of small and reusable blocks (components).&lt;br&gt;
In other words, your pages become "simply" compositions of these blocks, sorting them in different order.&lt;/p&gt;

&lt;p&gt;Components are not tied to specific page or sub-page but should be reused throughout the whole website.&lt;/p&gt;

&lt;p&gt;If you're implementing a theme for a CMS, it's up to you to decide to use components to just build a "fixed" set of templates or use them to let the client build pages in a flexible sort-of-composer way:&lt;br&gt;
you can use Paragraphs in Drupal, Matrix field in Craft CMS or Flexible Content field type of WordPress ACF Pro plugin just to cite a few.&lt;/p&gt;

&lt;p&gt;This is an example list of UI components:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;hero&lt;/li&gt;
&lt;li&gt;gallery&lt;/li&gt;
&lt;li&gt;card&lt;/li&gt;
&lt;li&gt;cards&lt;/li&gt;
&lt;li&gt;cards-carousel&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The actual names of your components is up to you, designers and developers. They should be as more abstract as possible, but I suggest you to find a good compromise avoiding overthinking.&lt;/p&gt;

&lt;p&gt;As I said before, Component-Based approach is not only a dev-tech-code thing, but it first involves designers.&lt;br&gt;
From the design point of view, &lt;strong&gt;the best is to have a complete and clear collection of all components involved in the project, and then the actual design of pages&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;As a developer you start coding first by building the collection of all components and then you can go on building pages.&lt;/p&gt;

&lt;p&gt;In my humble opinion this paradigm is amazing when working with a CMS since &lt;strong&gt;it lets you write the frontend code you want detached from the underlying CMS, in a sort of decoupled way&lt;/strong&gt;: this way CMS becomes more your source of data rather then a magic box where your markup comes from.&lt;/p&gt;

&lt;p&gt;Yes, if you're used to just style the HTML coming from the default templates of your CMS, this approach is going ask you to write more html, in some cases lots of templates (Drupal), but you'll have full control over it and it'll be easier to integrate 3rd party frontend libraries/components.&lt;/p&gt;

&lt;p&gt;Actual implementation details depend on the CMS you're building the frontend for, but more or less this is path for the win: &lt;strong&gt;you first code the abstract components, then you connect dynamic data coming from the CMS to the components params/props, and then you build the page&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why you should start using it
&lt;/h2&gt;

&lt;p&gt;Switching to a Component-Based Design and Development paradigm means:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;more collaboration between designers and developers at the early stages of the project;&lt;/li&gt;
&lt;li&gt;developers can concentrate on implementing pieces of UI unchained from the underlying CMS used: your frontend code is less dependant by the CMS, so you're free to use whatever markup/classes/architecture you want;&lt;/li&gt;
&lt;li&gt;you can implement a style guide shared between the team that can help a lot in terms of keeping consistency during the project&lt;/li&gt;
&lt;li&gt;your client or content editor people will easily get one "flexible" page template rather than a set of prefixed page templates. I guess it's a great value added to the project in the sense that your clients will be able to build whatever kind of page they'd like in the future just using and rearranging the building blocks.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  How to implement Component-Based Frontend Development in Drupal
&lt;/h2&gt;

&lt;p&gt;This is my way of implementing Component-Based Frontend Dev in Drupal.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Markup (HTML)&lt;/strong&gt;: components are implemented as Twig files, and live in '/templates/components' folder inside your theme folder.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;CSS&lt;/strong&gt;: a separate scss file for each component (I love ITCSS architecture and I use gulp and sass to build the final compressed css file). BEM methodology drives me for classes names.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;JavaScript&lt;/strong&gt;: when required, JavaScript is implemented as ES6 module and bundled with other modules (using gulp, babel and rollup for the build). Using CSS classes with "js-" namespace is a great way to target blocks from JavaScript (eg. js-carousel)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This is an example of the markup of the hero component:&lt;br&gt;
&lt;a href="https://media2.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%2Fo5c122g5dnaleznlosdj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fo5c122g5dnaleznlosdj.png" alt="Alt Text" width="800" height="640"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see this component is expecting to receive just 3 parameters/data/props: &lt;strong&gt;image, title and text&lt;/strong&gt;.&lt;br&gt;
I'm able to use the markup I want with all the classes and methodologies / architectures I want: I'm using BEM, with namespaces and in particular in this case I'm using the 'js-hero' class to target this block from JavaScript.&lt;/p&gt;

&lt;p&gt;This is an excerpt of the related Scss file:&lt;br&gt;
&lt;a href="https://media2.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%2F3ha3uyy1yyo6yplndora.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F3ha3uyy1yyo6yplndora.png" alt="Alt Text" width="800" height="640"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Connecting Components to Drupal
&lt;/h2&gt;

&lt;p&gt;And now the most complicated part: passing data coming from Drupal to components as parameters/data/props.&lt;/p&gt;

&lt;p&gt;Inside each specific Drupal Twig file I simply go with a Twig include statement passing an object containing relevant data coming from fields:&lt;br&gt;
&lt;a href="https://media2.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%2Fohjr37ox2qigphhkm02q.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fohjr37ox2qigphhkm02q.png" alt="Alt Text" width="800" height="278"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The "specific Drupal Twig file" depends on how you structured the content, but this can be easily done for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;nodes templates (eg. node--page.html.twig)&lt;/li&gt;
&lt;li&gt;views templates (eg. views-view-fields--hero.html.twig)&lt;/li&gt;
&lt;li&gt;paragraphs templates (eg. paragraph--card.html.twig)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Often the tricky and time wasting part is to access field plain value.&lt;/p&gt;

&lt;p&gt;If you've ever worked with Craft CMS you know that accessing field values with this CMS is straightforward thanks to their amazing Twig API, but in Drupal it's not so easy.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Drupal makes the assumption you usually don't want plain value from a field to be used directly in Twig templates, but you usually want a formatted value or worse a prebuit markup depending on the field formatter and the templates coming from modules and base theme you're using&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;So, this is my short list of code snippets to access various fields type data:&lt;/p&gt;

&lt;h3&gt;
  
  
  simple text field (plain or long/html)
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fw2a8rwek06l7logbwtyh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fw2a8rwek06l7logbwtyh.png" alt="Alt Text" width="800" height="230"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  single image field
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fg1dc4vndpmolnc7a6ei7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fg1dc4vndpmolnc7a6ei7.png" alt="Alt Text" width="800" height="181"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  building a custom markup for a multi image field (pay attention: you have to use node.field_gallery and not content.field_gallery)
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fsmeaz2yhp9m5j5exx4bz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fsmeaz2yhp9m5j5exx4bz.png" alt="Alt Text" width="800" height="567"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="https://media2.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%2Fmtc3qg1vn02fbrn4fs1f.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fmtc3qg1vn02fbrn4fs1f.png" alt="Alt Text" width="800" height="302"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  url and text from link field type
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fi5u6nicwz57v7vwbcicn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fi5u6nicwz57v7vwbcicn.png" alt="Alt Text" width="800" height="278"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  File field with description
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F8lipeugc3r5iq5ov07to.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F8lipeugc3r5iq5ov07to.png" alt="Alt Text" width="800" height="422"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Interesting links about component-based development
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.droptica.com/blog/component-based-design/" rel="noopener noreferrer"&gt;https://www.droptica.com/blog/component-based-design/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.cmsdrupal.com/blog/how-component-based-approach-speeds-your-ui-design-process" rel="noopener noreferrer"&gt;https://www.cmsdrupal.com/blog/how-component-based-approach-speeds-your-ui-design-process&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>drupal</category>
      <category>components</category>
      <category>twig</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
