Skip to content
← All writing

How we built our button component

Orvorkay · Jan 25, 2025 · 5 min read

TLDR:

The button component is one of the most used components in our UI library (opens in a new tab), alongside the input fields. It appears in all our dialogs and under every form as we have over 88+ forms in our app. It is also used as a trigger for our menus.

With that in mind, careful consideration had to be taken when building this component.

We used Tailwind-v3 and Angular 19.

TLDR: Here is a stackblitz of the button (opens in a new tab) using Tailwind-v3 and Angular19. You can also see the button in Storybook (opens in a new tab) here. Here is also a visualisation of how the button was built (opens in a new tab).

Before we build the button, let us first consider how developers will use it.

Button Architecture

  1. What features should our button support?
  2. What is the ideal developer experience of using the button?

What features should our button support?

Our button should support:

  1. Customizable sizes, via the size input with three options: xs, sm, md
  2. Different button types/variants using the variant input with the options - default, danger and outline
  3. Disabling the button using the input disabled. The default value will be false
  4. Showing a loading state using the loading input. This is useful during form submissions where we will show a spinner SVG and the Loading... text.
Step 1 of 3

The basic button

Given all that we have outlined, our button will be used like this:

Step 2 of 3

Loading text with context

The above button is fine for some forms. What if we have a button that is submitting a user creation form? With the current button the user will see Loading... but what if we could show them a different text that captured the current context of the form they just submitted? We could define a new button input called loadingText where we can control exactly what text will be displayed when the button is loading.

Now our button will be used like this:

Step 3 of 3

A foundation for other components

At this point, the button works for most cases but we had one more consideration. Our button is used as a foundation for other components. We need more control over other parts of the button.

The button has some animation which is acceptable when used on its own, but when it is used as a trigger to open a menu, we need to disable this animation. I will name this input animate and its default value will be true.

The other considerations were the font-weight of the button which I will name weight and the border-radius of the button which I will name radius . Now, when our button is used to build other reusable components, it can now be used like this:

usage.html
<button 
  size="sm" 
  variant="default" 
  [loading]="false" 
  [disabled]="false"
> 
Submit Form 
</button>

We have outlined the button architecture and answered our initial questions of:

  1. What features should our button support?
  2. What's the ideal developer experience of using the button?

Now let's build the button.

Building the button

The button went through several iterations, each teaching valuable lessons about component design. Here's the journey:

  1. In my first version, I built a basic button with the essential features but the code had some issues:
    • Classes were tightly coupled in the component
    • The template logic was complex
    • State management wasn't as clean as it could be
  2. Looking for a solution, I turned to the CVA (opens in a new tab) (Class Variance Authority) library. The code became cleaner and more organized. When I proudly showed this version (opens in a new tab) to my mentor Bonnie Brennan (opens in a new tab), she asked a simple but profound question: "Why are you using this library?"
  3. This question led to an important discussion about dependencies. In our fintech environment, every dependency is a security/compliance risk and while CVA solved my immediate problems, it added an external dependency that might not be necessary. Bonnie challenged me to achieve the same clean structure without the library.

The Code

Step 1 of 5

Define the features

We will define all the button features we support.

Notice we use the host element - this is a recommendation from the Angular docs (opens in a new tab)

Always prefer using the host property over @HostBinding and @HostListener. These decorators exist exclusively for backwards compatibility.

Step 2 of 5

Shape the data, then the interface

All the buttons share some styles, but each variant has its own unique style for when it is disabled or loading. We want this to be configurable. Let us first define how our data will look like and then build the interface.

I've found it better to define how my data will be structured first and then define the interface and at other times, I found it easier to define the interface first. For this case, I first defined some parts of the configuration, then I defined some parts of the interface and then went back and forth refining it.

This is our final ButtonClasses interface:

Step 3 of 5

Focus, variants and data attributes

Now we manage button focus for accessibility and use HTML data attributes (opens in a new tab) to manange the different button variants

Step 4 of 5

Class composition

The final piece is how we combine all these classes together. We use Angular's computed signals to create a reactive class string:

Step 5 of 5

Bind it to the host

This computed signal automatically updates whenever any input changes, and we bind it to the host element:

The data attributes (data-loading, data-disabled) work with our Tailwind classes to show the correct state styling.

src/button.component.ts
type ButtonSize = 'xs' |'sm' | 'md';
type ButtonVariant = 'default' | 'outline' | 'danger';
type ButtonRadius = 'sm' | 'md' | 'lg';
type ButtonWeight = 'normal' | 'bold' | 'semibold';

@Component({
  selector: 'button[bootstrap-button]',
  template: `
    @if (!loading()) {
    <ng-container>
      <ng-content></ng-content>
    </ng-container>
    } @else {
    <ng-container>
      <div>
        <svg></svg>
        <!-- loading SVG spinner -->
        {{ loadingText() }}
      </div>
    </ng-container>
    }
  `,
  host: {
    '[disabled]': 'isDisabled()',
    '[attr.aria-disabled]': 'isDisabled()',
  },
})
export class ButtonComponent {
  size = input<ButtonSize>('md');
  variant = input<ButtonVariant>('default');
  loading = input<boolean>(false);
  disabled = input<boolean>(false);

  loadingText = input<string, string>('Loading...', {
    transform: (value: string) => `${value}...`,
  });

  radius = input<ButtonRadius>('lg');
  weight = input<ButtonWeight>('semibold');
  animate = input<boolean>(true);

  protected isDisabled = computed(() => this.loading() || this.disabled());
}
size
variant
radius
weight
loading
disabled
animate

Here is a visualizer I built to showcase how I iterated through the classes: