<?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: Paul Mercieca</title>
    <description>The latest articles on DEV Community by Paul Mercieca (@paulmercieca).</description>
    <link>https://dev.to/paulmercieca</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%2F592521%2F13e54eb1-bfbe-47d0-a292-aab72ec61b3f.jpeg</url>
      <title>DEV Community: Paul Mercieca</title>
      <link>https://dev.to/paulmercieca</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/paulmercieca"/>
    <language>en</language>
    <item>
      <title>Create Random Cards With Pure CSS</title>
      <dc:creator>Paul Mercieca</dc:creator>
      <pubDate>Sun, 14 Mar 2021 10:07:57 +0000</pubDate>
      <link>https://dev.to/paulmercieca/create-random-cards-with-pure-css-1gdl</link>
      <guid>https://dev.to/paulmercieca/create-random-cards-with-pure-css-1gdl</guid>
      <description>&lt;p&gt;There was no JavaScript, Sass, or other external scripts or preprocessors used in the development of these cards.&lt;/p&gt;

&lt;p&gt;Every time you click on a card, it draws a different one; it may be a King of Hearts, an Ace of Spades, a 9 of diamonds, or any other card in a regular deck of cards.&lt;/p&gt;

&lt;h5&gt;
  
  
  HTML
&lt;/h5&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;input type="checkbox" id="card_01" checked&amp;gt;
&amp;lt;label for="card_01"&amp;gt;
  &amp;lt;div class="cardWrapper"&amp;gt;
    &amp;lt;div class="cardRandom"&amp;gt;&amp;lt;/div&amp;gt;
  &amp;lt;/div&amp;gt;
&amp;lt;/label&amp;gt;
&amp;lt;input type="checkbox" id="card_02" checked&amp;gt;
&amp;lt;label for="card_02"&amp;gt;
  &amp;lt;div class="cardWrapper"&amp;gt;
    &amp;lt;div class="cardRandom"&amp;gt;&amp;lt;/div&amp;gt;
  &amp;lt;/div&amp;gt;
&amp;lt;/label&amp;gt;
&amp;lt;a href="https://uxdesign.cc/creating-randomness-with-pure-css-a990dafcd569" target="_blank" class="linkElement"&amp;gt;Read &amp;lt;i&amp;gt;"Creating randomness with pure CSS"&amp;lt;/i&amp;gt;&amp;lt;/a&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h5&gt;
  
  
  CSS
&lt;/h5&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;body {
  background-color: #ececec;
  transform-style: preserve-3d;
  perspective: 2000px;
  display: flex;
  justify-content: center;
}

label .cardWrapper {
  position: relative;
  height: 40vmin;
  width: calc(40vmin / 1.4);
  background-color: royalblue;
  background-image: repeating-linear-gradient(
      120deg,
      rgba(255, 0, 0, 0.5),
      rgba(255, 0, 0, 0.5) 1px,
      transparent 1px,
      transparent 60px
    ),
    repeating-linear-gradient(
      60deg,
      rgba(255, 0, 0, 0.5),
      rgba(255, 0, 0, 0.5) 1px,
      transparent 1px,
      transparent 60px
    ),
    linear-gradient(
      60deg,
      rgba(0, 0, 0, 0.1) 25%,
      transparent 25%,
      transparent 75%,
      rgba(0, 0, 0, 0.1) 75%,
      rgba(0, 0, 0, 0.1)
    ),
    linear-gradient(
      120deg,
      rgba(0, 0, 0, 0.1) 25%,
      transparent 25%,
      transparent 75%,
      rgba(0, 0, 0, 0.1) 75%,
      rgba(0, 0, 0, 0.1)
    );
  background-size: 70px 120px;
  margin: calc(40vmin / 10);
  border-radius: calc(40vmin / 20);
  box-shadow: 0 calc(40vmin / 40) calc(40vmin / 10) 0 rgba(0, 0, 0, 0.2);
  overflow: hidden;
  float: left;
  transition: transform 200ms linear, box-shadow 100ms linear;
  transform: rotateY(0deg);
}

input:checked + label .cardWrapper {
  transform: rotateY(180deg);
}

.cardRandom {
  width: 100%;
  height: 100%;
  background-color: white;
  opacity: 1;
  cursor: pointer;
  transition: opacity 1ms linear 100ms;
}

.cardWrapper:hover {
  filter: brightness(0.98);
}

input:focus + label .cardWrapper {
  box-shadow: 0 0 calc(40vmin / 100) 0 rgba(0, 0, 0, 0.5);
}

input:checked + label .cardRandom {
  opacity: 0;
}

.cardRandom::before {
  content: "";
  position: absolute;
  width: 100%;
  height: 100%;
  background-image: url(https://assets.codepen.io/2722301/cards.jpg);
  background-size: cover;
  animation: randomCard 1000ms steps(51, end) infinite;
  animation-play-state: paused;
  transform: rotate(180deg);
}

input:checked + label .cardRandom::before {
  animation-play-state: running;
}

input {
  height: 0;
  width: 0;
  border: none;
  outline: none;
  display: none;
}

@keyframes randomCard {
  0% {
    background-position: 0 0;
  }
  100% {
    background-position: 100% 0;
  }
}

.linkElement {
  position: absolute;
  bottom: -40px;
  font-family: sans-serif;
  color: #4069e1;
  text-decoration: none;
}

.linkElement:hover {
  filter: hue-rotate(-45deg);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;First and foremost, there is no built-in “random” function in CSS, no &lt;code&gt;Math.random()&lt;/code&gt; equivalent, and no way to generate a random number or color at all.&lt;/p&gt;

&lt;h3&gt;
  
  
  So what is happening here?
&lt;/h3&gt;

&lt;p&gt;Since we don't have a random feature to regulate elements in CSS, they can't act randomly, but they can appear to act randomly by using an elaborate type of animation.&lt;/p&gt;

&lt;p&gt;The cards in the preceding example animate quickly through the various states, and the user's click on each card pauses the animation, causing the card to flip at each of the various states.&lt;/p&gt;

&lt;p&gt;The user can click on the cards at any time, but the animation is so fast that it serves as a random seed for our interaction; for example, these cards loop through all 52 states in less than one second.&lt;/p&gt;

&lt;p&gt;This trick is particularly useful if you can conceal the mechanism itself, such as our flipped cards, since the consumer would never think to time it, unlike a slot machine, which can be pulled at precisely the right time.&lt;/p&gt;

&lt;p&gt;Even if the user would try to time it, the animation between the different states is so fast it’s almost impossible to time correctly.&lt;/p&gt;

&lt;h5&gt;
  
  
  HTML
&lt;/h5&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;input type="checkbox"&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h5&gt;
  
  
  CSS
&lt;/h5&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;input::after {
  content: "";
  position: absolute;
  width: 200px;
  height: 200px;
  background-color: red;
  cursor: pointer;
  animation: randomAnim 1000ms steps(1, end) infinite;
  top: 50%;
  left: 50%;
  margin-top: -100px;
  margin-left: -100px;
}
@keyframes randomAnim {
  0% {
    background-color: red;
  }
  50% {
    background-color: blue;
  }
}

input:checked::after {
  animation-play-state: paused;
}

input {
  height: 0;
  width: 0;
  border: none;
  outline: none;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Why using random in pure CSS?
&lt;/h3&gt;

&lt;p&gt;This way of randomizing has a few key advantages:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;it’s fun and whimsical.&lt;/li&gt;
&lt;li&gt;As a CSS developer, you don’t always have access to the JavaScript code and using this trick, you don’t have to.&lt;/li&gt;
&lt;li&gt;It’s not a well-known method, which means neither your teammates nor your users will understand what’s going on, users won’t be able to game this system as easily and as a bonus — you can drive other developers in your team crazy trying to debug it.&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>css</category>
    </item>
    <item>
      <title>What’s Coming To VueX</title>
      <dc:creator>Paul Mercieca</dc:creator>
      <pubDate>Wed, 10 Mar 2021 09:20:14 +0000</pubDate>
      <link>https://dev.to/paulmercieca/what-s-coming-to-vuex-go4</link>
      <guid>https://dev.to/paulmercieca/what-s-coming-to-vuex-go4</guid>
      <description>&lt;p&gt;Quick summary:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Vuex is a state management pattern + library for Vue. js applications. It serves as a centralised store for all the components in an application, with rules ensuring that the state can only be mutated in a predictable fashion.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The next version of Vuex 4, is making its way through the final steps before officially releasing. This release will bring full compatibility with Vue 3, but doesn’t add new features. While Vuex has always been a powerful solution, and the first choice for many developers for state management in Vue, some developers had hoped to see more workflow issues addressed.&lt;/p&gt;

&lt;p&gt;With the advent of Vue 3 and it’s  &lt;a href="https://v3.vuejs.org/guide/composition-api-introduction.html#why-composition-api"&gt;composition API&lt;/a&gt; , people have been looking into hand-built simple alternatives. For example,  &lt;a href="https://dev.to/blacksonic/you-might-not-need-vuex-with-vue-3-52e4"&gt;You Might Not Need Vuex&lt;/a&gt;  demonstrates a relatively simple, yet flexible and robust pattern for using the composition API along with &lt;code&gt;provide/inject&lt;/code&gt; to create shared state stores. This and other alternatives should only be used in smaller applications because they lack all those things that aren’t directly about the code: community support, documentation, conventions, good  &lt;a href="https://nuxtjs.org/"&gt;Nuxt&lt;/a&gt;  integrations, and developer tools.&lt;/p&gt;

&lt;h3&gt;
  
  
  Defining a Store
&lt;/h3&gt;

&lt;p&gt;Before we can do anything with a Vuex store, we need to define one. In Vuex 4, a store definition will look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { createStore } from 'vuex'

export const counterStore = createStore({
  state: {
    count: 0
  },

  getters: {
    double (state) {
      return state.count * 2
    }
  },

  mutations: {
    increment (state) {
      state.count++
    }
  },

  actions: {
    increment (context) {
      context.commit('increment')
    }
  }
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each store has four parts: &lt;code&gt;state&lt;/code&gt; stores the data, &lt;code&gt;getters&lt;/code&gt; give you computed state, &lt;code&gt;mutations&lt;/code&gt; are used to mutate the state, and &lt;code&gt;actions&lt;/code&gt; are the methods that are called from outside the store to accomplish anything related to the store. Usually, actions don’t just commit a mutation as this example shows. Instead, they are used to do asynchronous tasks because mutations must be synchronous or they just implement more complicated or multi-step functionality.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { defineStore } from 'vuex'

export const counterStore = defineStore({
  name: 'counter',

  state() {
    return { count: 0 }
  },

  getters: {
    double () {
      return this.count * 2
    }
  },

  actions: {
    increment () {
      this.count++
    }
  }
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;First, instead of &lt;code&gt;createStore&lt;/code&gt;, we use &lt;code&gt;defineStore&lt;/code&gt;. This difference is negligible, but it’s there for semantic reasons, which we’ll go over later. Next, we need to provide a &lt;code&gt;name&lt;/code&gt; for the store, which we didn’t need before. In the past, modules got their own name, but they weren’t provided by the module itself; they were just the property name they were assigned to by the parent store that added them. Now, there are no modules. Instead, each module will be a separate store and have a name.&lt;/p&gt;

&lt;p&gt;After that, we need to make &lt;code&gt;state&lt;/code&gt; a function that returns the initial state instead of just setting it to the initial state. This is similar to the &lt;code&gt;data&lt;/code&gt; option on components. We write &lt;code&gt;getters&lt;/code&gt; very similar to the way we did in Vuex 4, but instead of using the &lt;code&gt;state&lt;/code&gt; as a parameter for each getter, you can just use &lt;code&gt;this&lt;/code&gt; to get to the state. In the same way, &lt;code&gt;actions&lt;/code&gt; don’t need to worry about a &lt;code&gt;context&lt;/code&gt; object being passed in: they can just use &lt;code&gt;this&lt;/code&gt; to access everything. Finally, there are no &lt;code&gt;mutations&lt;/code&gt;. Instead, mutations are combined with &lt;code&gt;actions&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Initiate the Store
&lt;/h3&gt;

&lt;p&gt;In Vuex 4, things have changed from Vuex 3, but I’ll just look at v4 to keep things from getting out of hand. In v4, when you called &lt;code&gt;createStore&lt;/code&gt;, you already instantiated it. You can then just use it in your app, either via &lt;code&gt;app.use&lt;/code&gt; or directly:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { createApp } from 'vue'
import App from './App.vue' // Your root component
import store from './store' // The store definition from earlier

const app = createApp(App)

app.use(store)
app.mount('#app')

// Now all your components can access it via `this.$store`
// Or you can use in composition components with `useStore()`

// -----------------------------------------------

// Or use directly... this is generally discouraged
import store from './store'

store.state.count // -&amp;gt; 0
store.commit('increment')
store.dispatch('increment')
store.getters.double // -&amp;gt; 4
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is one thing that Vuex 5 makes a bit more complicated than in v4. Each app now can get a separate instance of Vuex, which makes sure that each app can have separate instances of the same stores without sharing data between them. You can share an instance of Vuex if you want to share instances of stores between apps.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { createApp } from 'vue'
import { createVuex } from 'vuex'
import App from './App.vue' // Your root component

const app = createApp(App)
const vuex = createVuex() // create instance of Vuex

app.use(vuex) // use the instance
app.mount('#app')
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now all of your components have access to the Vuex instance. Instead of giving your store(s) definition directly, you then import them into the components you want to use them in and use the Vuex instance to instantiate and register them:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { defineComponent } from 'vue'
import store from './store'

export default defineComponent({
  name: 'App',

  computed: {
    counter () {
      return this.$vuex.store(store)
    }
  }
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Calling &lt;code&gt;$vuex.store&lt;/code&gt;, instantiates and registers the store in the Vuex instance. From that point on, any time you use &lt;code&gt;$vuex.store&lt;/code&gt; on that store, it’ll give you back the already instantiated store instead of instantiating it again. You can call the &lt;code&gt;store&lt;/code&gt; method straight on an instance of Vuex created by &lt;code&gt;createVuex()&lt;/code&gt;.&lt;br&gt;
Now your store is accessible on that component via &lt;code&gt;this.counter&lt;/code&gt;. If you’re using the composition API for your component, you can use &lt;code&gt;useStore&lt;/code&gt; instead of &lt;code&gt;this.$vuex.store&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { defineComponent } from 'vue'
import { useStore } from 'vuex' // import useStore
import store from './store'

export default defineComponent({
  setup () {
    const counter = useStore(store)

    return { counter }
  }
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Use the Store
&lt;/h3&gt;

&lt;p&gt;Here’s what it looks like to use a store in Vuex 4.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;store.state.count            // Access State
store.getters.double         // Access Getters
store.commit('increment')    // Mutate State
store.dispatch('increment')  // Run Actions
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;State&lt;/code&gt;, &lt;code&gt;getters&lt;/code&gt;, &lt;code&gt;mutations&lt;/code&gt;, and &lt;code&gt;actions&lt;/code&gt; are all handled in different ways via different properties or methods. This has the advantage of explicitness, which I praised earlier, but this explicitness doesn’t really gain us anything.&lt;/p&gt;

&lt;p&gt;Everything — the state, getters and actions — is available directly at the root of the store, making it simple to use with a lot less verbosity and practically removes all need for using &lt;code&gt;mapState&lt;/code&gt;, &lt;code&gt;mapGetters&lt;/code&gt;, &lt;code&gt;mapActions&lt;/code&gt; and &lt;code&gt;mapMutations&lt;/code&gt; for the options API.&lt;/p&gt;

&lt;h3&gt;
  
  
  Composing Stores
&lt;/h3&gt;

&lt;p&gt;The final aspect of Vuex 5 we’ll look at today is composability. Vuex 5 doesn’t have namespaced modules that are all accessible from the single store. Each of those modules would be split into a completely separate store. In v4, the namespacing convolutes the whole thing, so you need to use the namespace in your &lt;code&gt;commit&lt;/code&gt; and &lt;code&gt;dispatch&lt;/code&gt; calls, use &lt;code&gt;rootGetters&lt;/code&gt; and &lt;code&gt;rootState&lt;/code&gt; and then work your way up into the namespaces you want to access getters and state from. Here’s how it works in Vuex 5:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// store/greeter.js
import { defineStore } from 'vuex'

export default defineStore({
  name: 'greeter',
  state () {
    return { greeting: 'Hello' }
  }
})

// store/counter.js
import { defineStore } from 'vuex'
import greeterStore from './greeter' // Import the store you want to interact with

export default defineStore({
  name: 'counter',

  // Then `use` the store
  use () {
    return { greeter: greeterStore }
  },

  state () {
    return { count: 0 }
  },

  getters: {
    greetingCount () {
      return `${this.greeter.greeting} ${this.count}' // access it from this.greeter
    }
  }
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>vue</category>
      <category>javascript</category>
    </item>
    <item>
      <title>How To Use Styled-Components In React</title>
      <dc:creator>Paul Mercieca</dc:creator>
      <pubDate>Mon, 08 Mar 2021 13:40:48 +0000</pubDate>
      <link>https://dev.to/paulmercieca/how-to-use-styled-components-in-react-54o5</link>
      <guid>https://dev.to/paulmercieca/how-to-use-styled-components-in-react-54o5</guid>
      <description>&lt;p&gt;Styled components are a CSS-in-JS tool that bridges the gap between components and styling, offering numerous features to get you up and running in styling components in a functional and reusable way. In this article, you’ll learn the basics of styled components and how to properly apply them to your React applications. You should have worked on React previously before going through this tutorial.&lt;/p&gt;

&lt;p&gt;At the core of CSS is the capability to target any HTML element — globally — no matter its position in the DOM tree. This can be a hindrance when used with components, because components demand, to a reasonable extent, colocation (i.e. keeping assets such as states and styling) closer to where they’re used (known as localisation).&lt;/p&gt;

&lt;p&gt;In React’s own words, styled components are “visual primitives for components”, and their goal is to give us a flexible way to style components. The result is a tight coupling between components and their styles.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why Styled Components?
&lt;/h3&gt;

&lt;p&gt;Apart from helping you to scope styles, styled components include the following features:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Automatic vendor prefixing:&lt;/strong&gt; You can use standard CSS properties, and styled components will add vendor prefixes should they be needed.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Unique class names:&lt;/strong&gt; Styled components are independent of each other, and you do not have to worry about their names because the library handles that for you.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Elimination of dead styles:&lt;/strong&gt; Styled components remove unused styles, even if they’re declared in your code.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Installation
&lt;/h3&gt;

&lt;p&gt;Installing styled components is easy. You can do it through a CDN or with a package manager such as Yarn… &lt;code&gt;yarn add styled-components&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Our demo uses &lt;a href="https://create-react-app.dev/"&gt;create-react-app&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Starting Out
&lt;/h3&gt;

&lt;p&gt;Perhaps the first thing you’ll notice about styled components is their syntax, which can be daunting if you don’t understand the magic behind styled components. To put it briefly, styled components use JavaScript’s &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals"&gt;template literals&lt;/a&gt; to bridge the gap between components and styles. So, when you create a styled component, what you’re actually creating is a React component with styles. It looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import styled from "styled-components";

// Styled component named StyledButton
const StyledButton = styled.button`
  background-color: black;
  font-size: 32px;
  color: white;
`;

function Component() {
  // Use it like any other component.
  return &amp;lt;StyledButton&amp;gt; Login &amp;lt;/StyledButton&amp;gt;;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, &lt;code&gt;StyledButton&lt;/code&gt; is the styled component, and it will be rendered as an HTML button with the contained styles. styled is an internal utility method that transforms the styling from JavaScript into actual CSS.&lt;/p&gt;

&lt;p&gt;In raw HTML and CSS, we would have this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;button {
  background-color: black;
  font-size: 32px;
  color: white;
}

&amp;lt;button&amp;gt; Login &amp;lt;/button&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Adapting Based On Props
&lt;/h3&gt;

&lt;p&gt;Styled components are functional, so we can easily style elements dynamically. Let’s assume we have two types of buttons on our page, one with a black background, and the other blue. We do not have to create two styled components for them; we can adapt their styling based on their props.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import styled from "styled-components";

const StyledButton = styled.button`
  min-width: 200px;
  border: none;
  font-size: 18px;
  padding: 7px 10px;
  /* The resulting background color will be based on the bg props. */
  background-color: ${props =&amp;gt; props.bg === "black" ? "black" : "blue";
`;

function Profile() {
  return (
    &amp;lt;div&amp;gt;
      &amp;lt;StyledButton bg="black"&amp;gt;Button A&amp;lt;/StyledButton&amp;gt;
      &amp;lt;StyledButton bg="blue"&amp;gt;Button B&amp;lt;/StyledButton&amp;gt;
    &amp;lt;/div&amp;gt;
  )
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Because &lt;code&gt;StyledButton&lt;/code&gt; is a React component that accepts props, we can assign a different background color based on the existence or value of the &lt;code&gt;bg&lt;/code&gt; prop.&lt;/p&gt;

&lt;p&gt;You’ll notice, though, that we haven’t given our button a &lt;code&gt;type&lt;/code&gt;. Let’s do that:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function Profile() {
  return (
    &amp;lt;&amp;gt;
      &amp;lt;StyledButton bg="black" type="button"&amp;gt;
        Button A
      &amp;lt;/StyledButton&amp;gt;
      &amp;lt;StyledButton bg="blue" type="submit" onClick={() =&amp;gt; alert("clicked")}&amp;gt;
        Button B
      &amp;lt;/StyledButton&amp;gt;
    &amp;lt;/&amp;gt;
  );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Styled components can differentiate between the &lt;a href="https://styled-components.com/docs/basics#passed-props"&gt;types of props&lt;/a&gt; they receive. They know that &lt;code&gt;type&lt;/code&gt; is an HTML attribute, so they actually render &lt;code&gt;&amp;lt;button type="button"&amp;gt;Button A&amp;lt;/button&amp;gt;&lt;/code&gt;, while using the &lt;code&gt;bg&lt;/code&gt; prop in their own processing. Notice how we attached an event handler, too?&lt;/p&gt;

&lt;p&gt;Speaking of attributes, an extended syntax lets us manage props using the &lt;a href="https://styled-components.com/docs/api#attrs"&gt;attrs constructor&lt;/a&gt;. Check this out:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const StyledContainer = styled.section.attrs((props) =&amp;gt; ({
  width: props.width || "100%",
  hasPadding: props.hasPadding || false,
}))`
  --container-padding: 20px;
  width: ${(props) =&amp;gt; props.width}; // Falls back to 100%
  padding: ${(props) =&amp;gt;
    (props.hasPadding &amp;amp;&amp;amp; "var(--container-padding)") || "none"};
`;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice how we don’t need a ternary when setting the width? That’s because we’ve already set a default for it with &lt;code&gt;width: props.width || "100%",&lt;/code&gt;. Also, we used &lt;a href="https://styled-components.com/docs/basics#passed-props"&gt;CSS custom properties&lt;/a&gt; because we can!&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
