Button
So you get the idea, inside the 01-patterns directory we create a new directory that matches the name of the component we want to create and then create a few files inside that directory. Let's repeat this process for the Button component.
Component's data
Inside
nitflex_dev_theme/src/_patterns/01-patterns/
create a new directory called button.Inside the button directory create a new file called button.yml.
Inside button.yml add the following code:
We just created an object for the button with key/value text, url, type, and modifier.
Inside the button directory create a new file called button.twig.
Inside
button.twig
add the following code:
We've added some logic to the button to ensure we render the right HTML element based on the data we receive. For example, if a URL is passed, we use an <a>
with the class of button, otherwise we use a <button>
tag. This is important in many ways; we always want to make sure we use proper semantic markup for accessibility and for the task at hand. An <a>
tag will allow us to be directed to another page or a section within the same page, whereas a <button>
element will allow us to perform an action such as submit content.
Inside the button directory create a new file called button.scss.
Inside
button.scss
add this code:
Compiling the code
Now that we have written all the necessary code to build the component, it's time to see the component in the style-guide. Let's compile our project first.
In your command line, navigate to
/themes/custom/nitflex_dev_theme
.Run this command:
What does this command do?
The command above runs all gulp tasks found inside the gulp-tasks directory in the theme. Keep in mind, we are using the word lando because our local environment was built with lando. Typically the build command would be npm run build.
Viewing the component
Open your Drupal site and point to the URL below:
http://nitflex.lndo.site/themes/custom/nitflex_dev_theme/dist/style-guide/?p=viewall-patterns-button
Under the Components category you should see the new Button component.
Last updated