Button Component
Last updated
Last updated
Buttons are some of those things that are used over and over on a website. For this reason it makes sense to create a new component for it.
Inside components create a new folder called button
Inside the button folder create a new file called button.json
Inside button.json add the following code:
We created an object called button
and added several properties or attributes such as text
, url
, and modifier
. By now we've seen this kind of data structure on other components.
Inside the button folder 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>
tag with the class of button, otherwise we use a <button>
tag. 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 link 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
folder create a new file called button.scss
Inside button.scss
add the following code:
Some basic css styles to make our button look decent. We can probably improve them but for now this will do.
Now that the Button component is done, let's compile the code so we can see it in Pattern Lab. If you already have Pattern Lab running you should see the new Button component. Otherwise, run the following command in your command line and press Return