<?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: Benjamin Bromberg</title>
    <description>The latest articles on DEV Community by Benjamin Bromberg (@benbao).</description>
    <link>https://dev.to/benbao</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%2F145703%2Fb5752fb2-572f-4707-9b64-434cb02d3640.jpeg</url>
      <title>DEV Community: Benjamin Bromberg</title>
      <link>https://dev.to/benbao</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/benbao"/>
    <language>en</language>
    <item>
      <title>Alpine hands on: simple countdown</title>
      <dc:creator>Benjamin Bromberg</dc:creator>
      <pubDate>Fri, 15 May 2020 12:57:05 +0000</pubDate>
      <link>https://dev.to/benbao/alpine-hands-on-simple-countdown-an1</link>
      <guid>https://dev.to/benbao/alpine-hands-on-simple-countdown-an1</guid>
      <description>&lt;p&gt;Alpine.js is a minimalist JavaScript framework aiming to bring the convenience of reactive and declarative programming, known from full fledged frameworks like React, with a much smaller footprint.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://blog.logrocket.com/getting-started-with-alpine-js/"&gt;LogRocket&lt;/a&gt; published a good starting point if this is your first time hearing about Alpine.js and a previous article of mine on &lt;a href="https://dev.to/benbao/animate-with-alpine-js-gbi"&gt;Animations with Alpine.js&lt;/a&gt; is here on dev.&lt;/p&gt;

&lt;h2&gt;
  
  
  Countdown with Alpine.js
&lt;/h2&gt;

&lt;p&gt;While the framework focuses on only few directives these unlock a great power to comfortably and quickly build interactivity into your websites.&lt;/p&gt;

&lt;p&gt;With a simple countdown as example app we can see how Alpine.js enables us to read data from the DOM and react to events, all while keeping our HTML connected to the JavaScript.&lt;/p&gt;

&lt;p&gt;To illustrate that I have created a simple form taking a number input to start our countdown from, like so:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--R-yf5AQJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/54k1t9tohs9wd110oxh8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--R-yf5AQJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/54k1t9tohs9wd110oxh8.png" alt="Form with Alpine.js"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  x-data
&lt;/h3&gt;

&lt;p&gt;Right on the first element we define our variables with the x-data attribute. This is like setting javascript variables with &lt;code&gt;var&lt;/code&gt; or &lt;code&gt;let&lt;/code&gt;. Values from x-data are available in all child nodes.&lt;/p&gt;

&lt;h3&gt;
  
  
  x-show
&lt;/h3&gt;

&lt;p&gt;This directive allows us to control the visibility of an element based on the provided boolean value.&lt;/p&gt;

&lt;h3&gt;
  
  
  @submit.prevent
&lt;/h3&gt;

&lt;p&gt;This command actually combines a lot of helpful actions. The &lt;code&gt;@&lt;/code&gt; is an alias for &lt;code&gt;on-&lt;/code&gt;, which allows us to capture events, in this case the form submit event.&lt;/p&gt;

&lt;p&gt;With the modifier &lt;code&gt;prevent&lt;/code&gt; we let Alpine.js take care of the default event behaviour, similar to what &lt;code&gt;event.preventDefault()&lt;/code&gt; would do in vanilla JS.&lt;/p&gt;

&lt;p&gt;As we can write any valid JavaScript code in our directives I use the event handler to set the active status to true, read the countdown start value from our input with &lt;code&gt;$refs&lt;/code&gt; and start the countdown through &lt;code&gt;setInterval&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  x-ref
&lt;/h3&gt;

&lt;p&gt;This sets a reference to the node, which allows us to access it with the global &lt;code&gt;$refs&lt;/code&gt; variable later on in the code.&lt;/p&gt;

&lt;p&gt;Now onto our actual countdown:&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--2aSljV3X--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/f15ife7u7ol6ewfqjan2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--2aSljV3X--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/f15ife7u7ol6ewfqjan2.png" alt="Countdown with Alpine.js"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Again we are using the &lt;code&gt;x-show&lt;/code&gt; directive to control visibility of our form or the countdown elements.&lt;/p&gt;

&lt;h3&gt;
  
  
  x-if
&lt;/h3&gt;

&lt;p&gt;This is similar to &lt;code&gt;x-show&lt;/code&gt; but can only be applied to &lt;code&gt;template&lt;/code&gt; elements. With the &lt;code&gt;if&lt;/code&gt; directive we can control what elements will be rendered, rather than just setting the display value.&lt;/p&gt;

&lt;h3&gt;
  
  
  x-text
&lt;/h3&gt;

&lt;p&gt;Alpine.js also provides a convenient way to set the &lt;code&gt;text&lt;/code&gt; and &lt;code&gt;html&lt;/code&gt; property of nodes with the respective directives. This will set the content to the value of the variable passed into the directive.&lt;/p&gt;

&lt;p&gt;A live example of the complete code is available on &lt;a href="https://jsfiddle.net/benbao/5o36wcL9/1/"&gt;JSFiddle&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Code images are generated with &lt;a href="https://carbon.now.sh/"&gt;carbon&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Cover photo by &lt;a href="https://burst.shopify.com/@sarahpflugphoto?utm_campaign=photo_credit&amp;amp;utm_content=Free+Pocket+Watch+On+Black+Photo+%E2%80%94+High+Res+Pictures&amp;amp;utm_medium=referral&amp;amp;utm_source=credit"&gt;Sarah Pflug&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>alpinejs</category>
      <category>javascript</category>
      <category>framework</category>
    </item>
    <item>
      <title>Animate with Alpine.js </title>
      <dc:creator>Benjamin Bromberg</dc:creator>
      <pubDate>Sat, 11 Apr 2020 15:56:53 +0000</pubDate>
      <link>https://dev.to/benbao/animate-with-alpine-js-gbi</link>
      <guid>https://dev.to/benbao/animate-with-alpine-js-gbi</guid>
      <description>&lt;p&gt;Alpine.js is a minimalist JavaScript framework that leverages modern syntax and reactive features similar to Vue, intend to offer an alternative to jQuery.&lt;/p&gt;

&lt;p&gt;There are a couple of great introductions to Alpine, its benefits and use cases, so I will not repeat that and just refer to this article by &lt;a href="https://www.smashingmagazine.com/2020/03/introduction-alpinejs-javascript-framework/" rel="noopener noreferrer"&gt;Phil Smith&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;One aspect I liked while working with Alpine recently was the ease and simplicity to add small transitions and effects to elements, especially in combination with a utility CSS library like tailwind.&lt;/p&gt;

&lt;h3&gt;
  
  
  Alpine Animations
&lt;/h3&gt;

&lt;p&gt;Arguably one of the things jQuery is most used for is adding effects, such as visibility toggles, sliding and fading animations.&lt;/p&gt;

&lt;p&gt;While nice to have, it probably rarely justifies bloating up a website. Thankfully Alpine.js can handle this with the built-in transition directive, that allows to add CSS classes to granular stages of visibility changes.&lt;/p&gt;

&lt;h3&gt;
  
  
  Toggle visibility
&lt;/h3&gt;

&lt;p&gt;Lets look at how we can show and hide a message easily with a few lines of code.&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%2Fi%2F9bm4r5q2vn3m9f365fmg.png" 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%2Fi%2F9bm4r5q2vn3m9f365fmg.png" alt="Toogle visibility"&gt;&lt;/a&gt;&lt;br&gt;
A live example of this can be found on &lt;a href="https://jsfiddle.net/benbao/4b26gmxh/2/" rel="noopener noreferrer"&gt;JSFiddle&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;While this is not using transitions, it illustrates a couple of the things that make Alpine great. A button can be animated to show and hide an element and dynamically update its label. All without writing any traditional script code.&lt;/p&gt;

&lt;p&gt;If you have worked with Vue before the syntax will look very familiar to you, as Alpine is heavily inspired by it.&lt;/p&gt;

&lt;p&gt;To make the above example work we make use of some simple directives:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;x-data&lt;/code&gt; sets variables&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;x-text&lt;/code&gt; can update the text of its node reactively&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;@click&lt;/code&gt; is an alias for &lt;code&gt;x-on-click&lt;/code&gt;, essentially a click event listener&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;x-show&lt;/code&gt; controls the visibility of its node by the boolean of its value&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Fade transitions
&lt;/h3&gt;

&lt;p&gt;Now onto some actual animations. For that we can use the &lt;code&gt;x-transition&lt;/code&gt; directive, which allows us to apply CSS classes at specific animation phases.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Remember that the classes have to be defined in CSS, so you will need to either add your own utility classes or use the ones provided by tailwind.&lt;/em&gt;&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%2Fi%2Frqh332aaxxg5kv8cgzir.png" 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%2Fi%2Frqh332aaxxg5kv8cgzir.png" alt="Fade Effect"&gt;&lt;/a&gt;&lt;br&gt;
Check this fiddle for a &lt;a href="https://jsfiddle.net/benbao/Lvxrp47k/1/" rel="noopener noreferrer"&gt;live demo&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;With just a couple of attributes on our element we tell Alpine to transition the opacity and transform the scale of our element, to create a simple and pleasant effect.&lt;/p&gt;

&lt;p&gt;What Alpine is doing for us here is attaching the CSS classes from the attribute value to our element at the appropriate stages of the transition.&lt;/p&gt;

&lt;p&gt;As this is based on CSS we can get extremely creative and have full flexibility. If, for example, I want the button to scale up instead of down when fading out all I have to is change the end stage of the transition like so.&lt;br&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%2Fi%2Fzr8db4fsb2dc7e9jykbf.png" 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%2Fi%2Fzr8db4fsb2dc7e9jykbf.png" alt="Scale up and out"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Bottom line
&lt;/h3&gt;

&lt;p&gt;Alpine.js is a lightweight framework that brings reactive coding and syntax known from platforms like Vue in a smaller form factor. &lt;/p&gt;

&lt;p&gt;Using it unlocks powerful features, makes code more logical and connected, is responsive all while being only a couple of kilobytes.&lt;/p&gt;

&lt;p&gt;With its simple directives Alpine is fast to learn and can often be enough for small projects, that don’t require full blown frameworks like Vue or React.&lt;/p&gt;

&lt;p&gt;Read the docs on &lt;a href="https://github.com/alpinejs/alpine" rel="noopener noreferrer"&gt;Alpine.js on GitHub&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Code screenshots were created by &lt;a href="https://carbon.now.sh/" rel="noopener noreferrer"&gt;Carbon&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>alpinejs</category>
      <category>vue</category>
      <category>javascript</category>
      <category>animations</category>
    </item>
    <item>
      <title>Bulma Style Erweiterungen mit Nuxt.js </title>
      <dc:creator>Benjamin Bromberg</dc:creator>
      <pubDate>Wed, 01 Jan 2020 09:30:55 +0000</pubDate>
      <link>https://dev.to/benbao/bulma-style-erweiterungen-mit-nuxt-js-i44</link>
      <guid>https://dev.to/benbao/bulma-style-erweiterungen-mit-nuxt-js-i44</guid>
      <description>&lt;p&gt;Mit der grossen Menge von mächtigen und vielseitigen JavaScript Frameworks, wie React und &lt;a href="https://vuejs.org/"&gt;Vue&lt;/a&gt;, die mit &lt;strong&gt;modernen Technologien und Workflows&lt;/strong&gt; das Programmieren von statischen Webseiten, Single Page Apps (SPAs) und anderen Web-Formaten ermöglichen, entstand eine Lücke für weitere Frameworks die &lt;strong&gt;Verwendung dieser Tools zu vereinfachen.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Ein solches Framework is &lt;a href="https://nuxtjs.org/"&gt;Nuxt.js&lt;/a&gt;, dass auf VueJS aufbaut und dessen Verwendung mit &lt;strong&gt;vorkonfigurierten Modulen&lt;/strong&gt; strukturiert und vereinfacht. Mit dem Nuxt Kommandozeilen Programm ist das schnelle einrichten von Grundstrukturen eine Sache von Minuten. Zahlreiche beliebte Style-Frameworks wie &lt;strong&gt;Bootstrap und &lt;a href="https://bulma.io/"&gt;Bulma&lt;/a&gt;&lt;/strong&gt; können dabei mit einem Befehl eingebunden werden.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Bulma Extensions&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Sollen diese Style-Frameworks aber über den in der Standart Version enthaltenen Umfang erweitert werden, ist das oft etwas komplizierter. Wie solche &lt;strong&gt;Style Erweiterungen geladen werden können&lt;/strong&gt; erklärt dieser Artikel am Beispiel von Bulma Extensions.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Einbinden mit NPM und SCSS&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Die meisten Bulma Extensions stehen bereits als &lt;strong&gt;NPM Paket&lt;/strong&gt; bereit, was die Installation und Einbindung in Nuxt deutlich vereinfach. Das benötige Packet für die gewünschte Extension muss dann einfach zur packages.json Datei hinzugefügt werden. Anschliessend können alle benötigten Informationen mit dem folgenden Befehl installiert werden:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Die Style Informationen sind nun installiert, müssen aber noch geladen werden bevor sie in einem Nuxt Projekt verwendet werden können. Das geschieht mit &lt;strong&gt;Hilfe von SCSS,&lt;/strong&gt; dafür müssen wir &lt;code&gt;index.scss&lt;/code&gt; als neue Datei im Ordner &lt;code&gt;assets/sass&lt;/code&gt; des Nuxt Projekts erstellen.&lt;/p&gt;

&lt;p&gt;In dieser Datei lade wir nun das &lt;strong&gt;Bulma Framework und unser frisch installiertes Extension Paket,&lt;/strong&gt; das geht so:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@import './node_modules/bulma/bulma.sass';
@import './node_modules/EXTENSION ORDNER/src/sass/index.sass';
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Wobei &lt;code&gt;EXTENSION ORDNER&lt;/code&gt; natürlich mit dem Ordner Namen der gewünschten Extension ersetzt werden muss.&lt;/p&gt;

&lt;p&gt;Im letzten Schritt konfigurieren wir Nuxt nun so, dass es die SCSS Datei einliest und die benötigten Styles lädt. Das passiert in der &lt;code&gt;nuxt.config.js&lt;/code&gt; Datei im Hauptordner des Projekts, hier muss im &lt;code&gt;css&lt;/code&gt; Block die erstellte &lt;code&gt;index.scss&lt;/code&gt; eingebunden werden, etwa so:&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;css: ['@/assets/sass/index.scss'],
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Und damit stehen alle Style-Element von Bulma und der gewählten Extension in &lt;strong&gt;jeder Seite und Komponente des Nuxt Projekts&lt;/strong&gt; mit dem jeweiligen CSS Klassennamen zur verfügung.&lt;/p&gt;

</description>
      <category>bulma</category>
      <category>nuxt</category>
      <category>vue</category>
    </item>
  </channel>
</rss>
