Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Building a Component Library Beyond Bootstrap

· 5 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

Building your own component library gives you full control over design, performance, and accessibility without being locked into Bootstrap's look or its heavy CSS and JavaScript. This guide walks you through creating a reusable, framework-agnostic component library from scratch, using modern CSS and vanilla JavaScript, so you can use it across projects and even with frameworks like React or Vue.

What You'll Need

  • Basic knowledge of HTML, CSS, and JavaScript (ES6+).
  • A code editor (VS Code, Sublime, etc.).
  • Node.js and npm installed (for package management and build tooling).
  • A version control system (Git) to track changes.
  • Familiarity with CSS preprocessors (like Sass) or CSS custom properties (variables) – we'll use CSS variables for theming.

Step-by-Step

  1. Define your design tokens. Start by establishing your design system's foundation: colors, typography, spacing, border-radius, shadows, and breakpoints. Create a CSS file (e.g., tokens.css) with CSS custom properties. For example:
    :root {
      --color-primary: #007bff;
      --color-text: #212529;
      --font-family-base: 'Segoe UI', sans-serif;
      --spacing-unit: 4px;
      --radius: 4px;
    }
  2. Set up your project structure. Create a folder structure like this:
    component-library/
      ├── src/
      │   ├── components/
      │   ├── tokens/
      │   └── utilities/
      ├── dist/
      ├── docs/
      ├── package.json
      └── README.md
  3. Initialize npm and install build tools. Run npm init -y. Install a bundler like Vite or Rollup, and a CSS processor like Sass (if you prefer). For simplicity, we'll use Vite: npm install --save-dev vite. Add a build script in package.json to compile your library.
  4. Create your first component: a button. Build a button component with variants (primary, secondary, outline) and sizes. Create a folder src/components/button/ with button.css and button.js. Use CSS custom properties for theming so users can override. Example CSS:
    .btn {
      padding: calc(var(--spacing-unit) * 2) calc(var(--spacing-unit) * 4);
      border-radius: var(--radius);
      border: 1px solid transparent;
      background: var(--color-primary);
      color: #fff;
    }
    .btn--secondary { background: var(--color-secondary); }
  5. Add interactivity with vanilla JS. For components that need interaction (e.g., dropdowns, modals), write plain JavaScript that attaches event listeners. Keep it dependency-free. For example, a simple modal:
    export function initModal(modalEl) {
      const closeBtn = modalEl.querySelector('.modal__close');
      closeBtn.addEventListener('click', () => modalEl.hidden = true);
    }
  6. Build a utility for responsive behavior. Use CSS Grid or Flexbox for layout components. Create a grid component that uses CSS variables for column counts and gaps. This ensures your library works on any screen size without requiring Bootstrap's grid classes.
  7. Document your components. Create a simple documentation page (in docs/) that shows each component with its variants and usage examples. You can use a static site generator like VitePress or just a plain HTML file. Include code snippets so users can copy-paste.
  8. Set up theming and customization. Allow users to override your CSS variables by exposing them in your main stylesheet. Document which variables exist and what they control. This is the key advantage over Bootstrap – your library is themeable without using Sass and !default.
  9. Write tests (optional but recommended). Use a testing framework like Jest and Testing Library to test your JavaScript components. For CSS, you can use visual regression tools like Percy or BackstopJS, but even simple unit tests for functions are valuable.
  10. Build and publish. Configure your build to output a CSS file and a JS file (or individual component files). Use Rollup or Vite's library mode. Then publish to npm with npm publish. Include a README.md with installation and usage instructions.

Common Mistakes

  • Not using CSS variables. Hardcoding colors and spacing makes theming impossible. Always use custom properties.
  • Over-engineering the first version. Don't try to build 50 components at once. Start with 5-6 core ones and iterate.
  • Forgetting accessibility. Ensure your components have proper ARIA attributes, keyboard navigation, and focus states. Bootstrap has these, but your custom library must too.
  • Ignoring browser support. Check if your CSS features (like :focus-visible) work in your target browsers. Use autoprefixer or specify a browserlist.
  • Not versioning your design tokens. If you change a color later, you should bump a version so users know.

Tips

  • Use a naming convention like BEM (Block Element Modifier) to keep your CSS predictable and avoid conflicts.
  • Keep your components framework-agnostic. Write plain HTML/CSS/JS and provide wrappers for React/Vue later if needed.
  • Use CSS nesting (if supported) or a preprocessor to keep your styles organized and reduce repetition.
  • Provide a demo page that is also your documentation – it doubles as a test environment.
  • Add a custom property for every design decision (like transition speed, z-index scales) to give users full control.

Troubleshooting

  • Components look different across browsers. Use a CSS reset or normalize.css, and test in multiple browsers. Add vendor prefixes via autoprefixer.
  • JavaScript not working. Make sure your JS is loaded after the DOM (use defer or place scripts at the end). Check for typos in selectors. Use browser devtools to see console errors.
  • CSS variables not applying. Ensure the variables are defined on a parent element (like :root). If you're using scoped styles, define them on the component's root.
  • Build fails. Check your npm scripts and that all dependencies are installed. Look at the error message – it often points to a missing plugin or syntax error.
  • Users can't override your styles. Make sure your selectors aren't too specific (e.g., avoid #id). Use single class selectors and keep specificity low.

Frequently Asked Questions

Do I need to use a framework like React to build a component library?

No. You can build a library with vanilla HTML, CSS, and JS. This makes it usable in any project. You can later create framework-specific wrappers.

How is this different from just using Bootstrap and customizing it?

Bootstrap is a monolithic framework with a specific look and heavy CSS/JS. Your own library is lightweight, tailored to your design, and easier to maintain because you control every line.

What if I need a component I haven't built yet?

You can borrow ideas from Bootstrap or other libraries, but implement them in your own style. That's normal – you don't have to invent everything.

Should I use a CSS preprocessor like Sass?

It's optional. CSS custom properties handle theming well. Sass is helpful for nesting and mixins, but modern CSS (like nesting) is catching up. Choose what you're comfortable with.

How do I version my library?

Use semantic versioning (SemVer) – bump major for breaking changes, minor for new features, patch for bug fixes. This helps users know what to expect when updating.

Can I use this library in a WordPress theme or other CMS?

Yes. Since it's just CSS and JS, you can enqueue the built files in your theme. Just make sure to handle any conflicts with existing styles.