> For the complete documentation index, see [llms.txt](https://mariohernandez.gitbook.io/components/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://mariohernandez.gitbook.io/components/basic-components/button.md).

# Button Component

![](/files/-M7pHAvt_cbr8zGL5eoU)

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.

1. Inside *components* create a new folder called **button**
2. Inside the *button* folder create a new file called **button.json**
3. Inside *button.json* add the following code:

{% tabs %}
{% tab title="button.json" %}

```yaml
{
  "button": {
    "text": "Hi, I'm a button",
    "url": "",
    "modifier": ""
  }
}
```

{% endtab %}
{% endtabs %}

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.

## Writing the button's logic

1. Inside the *button* folder create a new file called **button.twig**
2. Inside `button.twig` add the following code:

{% tabs %}
{% tab title="button.twig" %}

```php

<div data-gb-custom-block data-tag="if">

  <a href="{{ button.url }}" class="button{{ button.modifier ? ' ' ~ button.modifier }}">
    {{ button.text }}
  </a>
  

<div data-gb-custom-block data-tag="else"></div>

  <button class="button{{ button.modifier ? ' ' ~ button.modifier }}">
    {{ button.text }}
  </button>

</div>

```

{% endtab %}
{% endtabs %}

* 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.

## Button Styles

1. Inside the `button` folder create a new file called **button.scss**
2. Inside `button.scss` add the following code:

{% tabs %}
{% tab title="button.scss" %}

```css
// Import site utilities
@import '../../global/utils/init';

.button {
  background-color: $color-navy-blue;
  border: 2px solid $color-navy-blue;
  color: $color-white;
  display: inline-block;
  line-height: 1.3;
  padding: 15px 40px;
  text-transform: uppercase;
  text-decoration: none;

  // Hover and focus states for base button
  &:hover,
  &:focus {
    color: $color-white;
  }

  // Styles for white button with gray outline.
  &.button--ghost {
    background-color: transparent;
    border: 2px solid $color-gray-dark;
    color: $color-gray-dark;
    padding: 8px 30px;

    // Hover and focus styles for ghost button.
    &:hover,
    &:focus {
      color: $color-gray-dark;
    }
  }
}
```

{% endtab %}
{% endtabs %}

* Some basic css styles to make our button look decent. We can probably improve them but for now this will do.

## Compiling the code

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**

```bash
npm run watch
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://mariohernandez.gitbook.io/components/basic-components/button.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
