4 Best practices
This chapter covers
- Established patterns for developing web components
- A sample web component that demonstrates each practice
- A project that shows how to use the example component
The website web.dev provides a checklist of Custom Element Best Practices. These best practices provide tips for improving the quality of your web components, and they’re an invaluable resource for any developer working with web components. This chapter builds on that checklist by adding TypeScript code examples. We provide details on the support provided by the web component libraries covered in this book: Lit, Stencil, FAST, and wrec.
These practices were developed and refined by software developers who work closely with the modern web platform, drawing on real-world experience building and maintaining web applications. In this chapter, you’ll learn about each of the best practices, and why you should follow them.
Many of these practices focus on ensuring that custom elements encapsulate their styling and functionality, integrate cleanly with HTML and CSS semantics, respond correctly to attribute and property changes, and properly dispatch events.
In the following sections, we’ll walk through each best practice and demonstrate how to follow it. To do this, we’ll gradually build a vanilla sortable-table web component to highlight each best practice in action.
4.1 Example component
Let’s start by introducing the project we’ll be working on: a sortable-table.