When should we use components and when should we just use HTML?

by
, , 3 min read

A lot of UI frameworks or design systems include a component library. This is a series of components that can be dropped into a page to add functionality.

Sometimes these components are like HTML tags that don't exist yet, e.g. a card, or they add functionality easily without having to write JavaScript, e.g. a number formatter.

There are some components that don't really seem to add any new functionality but just control styling or the arrangement of inner tags. For example, most component libraries have their own button, which is a standard native HTML button but wrapped in another element, basically a fancy named span. What are the pros and cons of doing this and what are the alternatives? Let's assume we're adding the custom element <my-button>.

Pros of using components

Cons of using components

Alternatives to components

For a simple component like this button example, we could simply use a documentation site or style guide, documenting what HTML and CSS to use.

We could make code snippets available including the full feature set, e.g. icons before and after button text, and the developer can simply copy the full markup removing the parts that are not needed.

Probably not a very popular opinion right now but AI tools like GitHub Copilot can help add the right markup. If a similar block has been used before it can be context aware and keep things consistent with other code in your codebase.

When to use components

There no right or wrong answer here. There are a few considerations.

It's down to the nature of the project, how much reusability and change we expect and how much we value developer experience compared to user experience.

There's nothing wrong with using components for consistency and ease of development but we need to be wary not to constantly over-engineer very simple pages that could just be HTML.

Are the people writing the frontend UI frontend developers who are comfortable writing HTML and CSS, or are they more backend or full-stackers who would prefer picking elements from an existing library?

Do the page elements require lots of JavaScript to function? If there is no HTML only option this may sway our decision. An encapulated component may be preferable to separate JavaScript which adds functionality.