Skip to content

[IMPROVEMENT] Refactor to web components for better encapsulation and reus... #13

Description

@devwif

[IMPROVEMENT] Refactor Core Components to Web Components for Enhanced Encapsulation & Reusability


🚀 Problem Statement

The current implementation of WebTUI components relies on framework-specific or loosely encapsulated approaches that hinder reusability, cross-framework compatibility, and maintainability. To elevate developer experience and unlock broader adoption potential, we must refactor key UI components into native Web Components. This will improve encapsulation, enforce style and behavior isolation, and enable seamless integration in any modern frontend environment — including Astro, React, Vue, and vanilla JS.


🧠 Technical Context

  • Current State: WebTUI is implemented primarily in Astro with TypeScript, using modular CSS. Components do not leverage native browser encapsulation features like Shadow DOM or custom elements.
  • Why Web Components?
    • Native browser support for encapsulation with Shadow DOM
    • Framework-agnostic usage without reimplementation
    • Easier distribution as standalone UI elements
    • Simplified styling and event management boundaries
  • Challenges:
    • Ensuring smooth migration without breaking existing consumers
    • Retaining accessibility and performance
    • Updating test suites to handle component lifecycle and shadow DOM
  • Repository Tech Stack & Constraints:
    • Astro + TypeScript environment
    • Build tooling powered by @biomejs/biome, Turbo
    • Modular CSS approach needs alignment with encapsulated styles

🛠️ Detailed Implementation Plan

1. Analyze & Document Current Components

  • Audit existing core components (e.g., buttons, inputs, dialogs, tooltips) for:
    • Public APIs (props, events)
    • Styling approach and scope
    • Inter-component dependencies
    • Accessibility features
  • Document findings in the repo wiki or /docs folder for transparency and reference.

2. Define Web Component Architecture & Patterns

  • Select base Web Component helper libraries if needed (e.g., Lit, Stencil) or use vanilla Custom Elements API.
  • Establish consistent patterns for:
    • Property and attribute reflection
    • Event dispatching
    • Styling via Shadow DOM and CSS custom properties
    • Accessibility (ARIA roles, keyboard navigation)
  • Create a shared base class or mixin to encapsulate common logic.

3. Incremental Refactoring

  • Start with smaller, low-dependency components to build confidence and patterns.
  • Refactor each component to a Web Component, using Shadow DOM for style encapsulation.
  • Ensure each refactored component exposes the same or improved API for backward compatibility.
  • Maintain parallel support if feasible (deprecated non-web-component version) during transition.

4. Backward Compatibility & Migration Strategy

  • Provide clear migration guides for consumers.
  • Implement feature flags or versioning to toggle new Web Component versions.
  • Preserve current styling and behavior exactly or improve with minimal breaking changes.

5. Testing & Validation

  • Update existing tests or create new ones using tools that support Web Components (e.g., @web/test-runner, Testing Library for Web Components).
  • Include tests for lifecycle callbacks, attribute/property syncing, event dispatch, and Shadow DOM styling.
  • Assess performance impact via benchmarking (e.g., Lighthouse, WebPageTest).

6. Documentation & Examples

  • Update component docs to reflect new usage patterns and APIs.
  • Add interactive demos showcasing usage in Astro and other frameworks (React/Vue).
  • Document accessibility considerations and keyboard navigation.

⚙️ Technical Specifications

  • Component APIs: Align with existing props/events but utilize observedAttributes and getters/setters in Web Components.
  • Styling: Use Shadow DOM with scoped styles; expose CSS custom properties for theme overrides.
  • Events: Dispatch CustomEvents with namespaced event names for clarity (e.g., webtui-select-change).
  • Accessibility: ARIA roles and keyboard support must be explicitly implemented.
  • Build: Integrate Web Component compilation/bundling into existing build pipeline with @biomejs/biome or rollup.
  • Testing: Use headless browsers or JSDOM with Shadow DOM support.

✅ Acceptance Criteria

  • Comprehensive audit of current components documented
  • Web Component base class/mixin created and documented
  • At least 3 core components refactored to Web Components with Shadow DOM
  • Backward compatibility layer or migration strategy defined and implemented
  • Updated test suite covering new Web Components passing
  • Performance benchmarks show no regressions
  • Updated docs and usage examples published
  • Accessibility compliance verified (WCAG 2.1 AA level minimum)

🧪 Testing Requirements

  • Unit tests for each Web Component’s lifecycle, attribute/property sync, and events
  • Integration tests for component composition in Astro and vanilla JS contexts
  • Accessibility tests (using axe-core or similar tools)
  • Performance profiling pre/post refactor
  • Manual cross-browser testing (Chrome, Firefox, Safari, Edge) for Shadow DOM quirks

📚 Documentation Needs

  • New or updated component README files detailing:
    • How to use the Web Component version
    • Migration steps from old to new components
    • Styling customization via CSS variables
    • Accessibility notes
  • Update main WebTUI documentation site with:
    • Web Component architecture overview
    • Live demos and code snippets
    • Contribution guidelines for future Web Component additions

⚠️ Potential Challenges

  • Shadow DOM support inconsistencies in legacy browsers (polyfills may be needed)
  • Preserving existing styling behaviors when moving from global/modular CSS to Shadow DOM
  • Complexity in re-implementing accessibility features for native elements
  • Ensuring smooth developer transition with minimal friction
  • Build tooling integration and bundle size impact

🔗 Resources & References


This refactor is a high ROI investment that modernizes WebTUI architecture, enhancing maintainability, scalability, and ecosystem integration. Let’s wield the power of native Web Components to make our terminal UI library truly future-proof and versatile — the impossible is just the untried! 💥


Task Checklist

  • Audit and document current components
  • Design Web Component base architecture
  • Refactor 3 core components as Web Components
  • Implement backward compatibility/migration strategy
  • Update and expand test coverage
  • Run performance and accessibility validations
  • Update documentation and demos
  • Review and merge with main branch

Ready to push the frontier of terminal UI libraries? Let’s code the web’s future—one component at a time!

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions