In Part 1, we set up our style guide and put some basic styling in place. In this article, we’ll work on building a simple component: the Callout. Let’s take a look at the design:

Emulsify Callout

Using the language of Atomic Design, this can be defined as a “molecule,” because it is a collection of a few atoms (two heading elements and a link). In terms of creating those atoms, we can simply use the heading code covered in the Typography section in the first article for the two heading elements. That only leaves us needing to define a button element.

Buttons

Let’s open up components/01-atoms/buttons/_buttons.scss. Here, we will style the buttons (and any variations). Here’s the scss mixin code for the button we see above:

Save this file and you will see your new button styles reflected in the Button atom in Storybook. Because these styles are in Sass mixins, we can reuse them across components easily—even for links! And now that we have our button styles, let’s create our Callout component.

Callout

Here’s our new Callout component directory structure (inside components/02-molecules):

Let’s start with our markup. In callout.twig we’ll want the following:

Let’s break down this code to understand the different parts.

Variables and Outer Elements

At the beginning, we’re setting our Callout base class to a Twig variable since we want to reuse it frequently. In our first outer div element, you’ll notice we’re using the BEM function to set our classes. If you’re new to Emulsify, you’ll want to get familiar with this function as it will become your new best friend (usage instructions here). This is a custom Twig function we wrote to make it faster and easier to pass in BEM classes as we build components (especially as you integrate them later).

For our purposes, just know this line will create <div class=”callout”>, but we’ll be expanding on its possibilities soon. The next element is similar and will create <div class=”callout__inner”> (callout being the block and inner being the element). Since the callout background fills the screen, and we need the content area to stay centered within a max-width, this .callout__inner element helps us accomplish that.

Inner Elements (Atoms)

All three of the atoms inside the callout element are similar, so let’s just break the link atom as an example. Here’s the code:

Since our callout interaction requires a traditional hyperlink, notice we’re using the link atom. By default, the link atom in Emulsify prints as <a class=”link”>, and we want a button with a BEM class specific to our component. The magic is in these two lines that pass into link.twig’s BEM function:

Here we’re populating link_blockname with callout and link’s base_class with button. As you can probably guess if you read through the BEM function usage above, this will print <a class=”callout__button”>. In fact, here’s the full markup that prints for this component:

Data

For the data you see in the mock above, we simply need to add the following to callout.yml:

Storybook

Now we’re ready to add the component to Storybook. In our callout.stories.js file, let’s add the following:

This will add our Callout Twig component populated with our YML data to Storybook under Molecules > Callout > Callout Default. Any time you save the Twig, YML, or Sass file, Storybook will hot reload your component to show your changes.

Styling

Now that we have the markup we need with the BEM classes we want, styling the elements is as easy as opening up _callout.scss and adding in our component styles using these classes. Don’t forget to make use of mixins, too. For instance, if your Callout button just uses the default button styles, your Callout button code might simply be:

Once you have your component styled the way you want it and have verified it in Storybook, you’re ready to integrate it in Drupal!

Drupal Theming

For our component, we’re first going to create a paragraph in Drupal named “Callout” with 3 fields: Title, Subtitle, and Button (two “Text (plain)” fields and one ”Link” field). Now, let’s go ahead and add that paragraph to a content type and then test adding a piece of content with our Callout fields populated. Once you’ve done this, you should see an unstyled piece of content. Also, if you have enabled Twig debugging, you should see something like this when inspecting your content:

Callout Drupal theming

These are our paragraph template override names. In our case, let’s use paragraph--callout.html.twig. I like to copy the existing paragraph template (templates/paragraphs/paragraph.html.twig) as a starting point. Once you’ve copied that file and renamed as paragraph--callout.html.twig, let’s replace the default markup part with this:

Assuming your field machine names are the same as the ones in bold above, once you’ve cleared the Drupal cache you should see your styled component with Drupal’s content!

For background, let’s break down the code a bit here as well. For starters, we’re using Twig’s include statement, which allows you to import the markup from any Twig file while replacing variables defined in that file. This kind of importing is what powers component-driven development in Drupal 8, because you can define a component once and reuse it in multiple places. Inside the with keyword, we’re passing our Drupal-specific content variables into the Twig variables defined in our imported component. The hardest part is figuring out the best way to get the value from your field. The devel/kint module can help you figure out the best way to get it (sometimes it’s as simple as content.field_title.value). But let’s not stop here; let’s allow content editors to add a differently styled Callout.

Component Variations

Here’s the design for our variation (dark background, light text):

Emulsify dark background light text component variation

First, let’s add a variation in Storybook. 

Storybook Variation

Let’s add a new file named callout-dark.yml in your component directory. Inside, simply put the following:

Now let’s adding the following to our Storybook file:

Notice the usage of spreading properties here. This allows us to add a new component and combine more than one set of data (speeding up creating variants). For example, even though our new data file only contains a “dark” modifier, you will now see the new “Callout Dark” component in Storybook with all of the same data from the original with our new modifier class: (<div class="callout callout--dark">). You can use this methodology to speed up creating variants, but you always have the power to create completely different data per component. Now we’re ready to add the ability to select this variation in Drupal.

Drupal Variation

Let’s add a new field to our Callout paragraph named “Variation” that is of the type “List (text).” In the Allowed Values list, simply add dark|Dark.  Then, in paragraph--callout.html.twig, add the following above your include statement:

Here, we’re adding a variable into the callout_modifiers array that is a ternary statement that essentially says, “If our variation dropdown has ‘dark’ selected, add ‘dark’ into the modifiers.” Now, when an admin selects “Dark” from the Variation dropdown, it will add dark to the  callout_modifiers variable, which shows the dark variation of your component in Drupal.

Recap

Now we have walked through how to set up a common component with a single variation in both Storybook and Drupal. In the next article in this series, we will cover some common complexity in component-driven development.