Skip to main content

Claude Prompt to Build Framework-Agnostic Web Components

Build reusable Web Components with Custom Elements and Shadow DOM: clean encapsulation, accessibility and framework-agnostic, drop-in design.

Fill in the placeholders

Edit the values, then copy your finished prompt.

Your Prompt
prompt.txt
Create a production-ready Web Component called data-table-pro that renders a sortable, filterable, paginated data table with virtual scrolling. The component should work in React, Vue, Angular, and vanilla HTML without any framework dependencies. Build it following these steps: 1) Define the Custom Element class extending HTMLElement — implement the full lifecycle: constructor (set up Shadow DOM with open mode, create template), connectedCallback (add event listeners, fetch data if needed, initial render), disconnectedCallback (cleanup listeners, abort pending requests), attributeChangedCallback (handle columns, page-size, sortable, filterable, theme changes reactively), and adoptedCallback if the component may move between documents. 2) Design the component API with 8 observed attributes and corresponding properties — implement the attribute-property reflection pattern where setting an attribute updates the property and vice versa. Support complex data via JSON attribute parsing and provide sensible defaults for all properties. 3) Build the Shadow DOM template with header, row, cell, pagination, search-input CSS ::part() selectors for external styling hooks and 3 (header-actions, empty-state, footer) named <slot> elements for content projection. Style the shadow DOM with CSS using :host, :host(), and :host-context() selectors while respecting inherited properties. 4) Implement keyboard accessibility: focus management with tabindex, arrow keys for cell navigation, Enter to sort, Escape to clear filters keyboard interactions following WAI-ARIA grid pattern with row/columnheader/gridcell roles pattern, and proper role, aria-label, and aria-live attributes. 5) Add custom event dispatching for row-select, sort-change, page-change, filter-change using composed: true and bubbles: true so events cross Shadow DOM boundaries and parent frameworks can listen. 6) Implement a form-associated custom element if no (standalone component) by adding static formAssociated = true, implementing formStateRestoreCallback, and properly reporting value and validity. 7) Write the registration with customElements.define() and provide a CDN-ready ES module that auto-registers on import, plus a deferred registration export for consumers who want control over the timing.

What this prompt does

This prompt builds a production-ready Web Component named [component_name] that works across [target_frameworks] with no framework dependencies. It walks the full Custom Element lifecycle (constructor, connectedCallback, disconnectedCallback, attributeChangedCallback, adoptedCallback), sets up a Shadow DOM in [shadow_mode] mode, defines the component's API, adds accessibility, dispatches custom events, optionally makes it form-associated, and registers it as a CDN-ready ES module. The lifecycle-first approach is what makes the element behave the same wherever it is dropped in.

The variables define the component's surface. [observed_attributes] and [attribute_count] set which attributes react via attribute-property reflection, while [css_parts] exposes ::part() styling hooks and [slot_count] enables content projection through named slots. [keyboard_interactions] and [aria_pattern] drive WAI-ARIA-compliant accessibility with proper roles and focus management, [custom_events] are dispatched with composed: true so they cross the Shadow DOM boundary and parent frameworks can listen, and [form_associated] decides whether the element participates in native forms with validity and state-restoration reporting. Getting these right is what makes one component behave identically across every framework that consumes it.

When to use it

  • Building a widget that must work identically in React, Vue, Angular, and plain HTML.
  • Shipping an embeddable component to third parties who use unknown frameworks.
  • Needing strong style isolation via Shadow DOM so host-page CSS cannot leak in.
  • Exposing controlled styling hooks through ::part() instead of leaking internals.
  • Creating a form-associated element that reports value and validity to native forms.
  • Distributing a component as a CDN-ready ES module that auto-registers on import.

Example output

You get a complete component implementation: a class extends HTMLElement with every lifecycle callback wired up, Shadow DOM in [shadow_mode] mode, [attribute_count] observed attributes implementing attribute-property reflection with JSON parsing for complex data, a template exposing [css_parts] parts and [slot_count] named slots, keyboard handling for [keyboard_interactions] following the [aria_pattern] pattern, [custom_events] dispatched with composed: true and bubbles: true, optional form-association code, and a customElements.define() registration plus a deferred-registration export.

Pro tips

  • Use a hyphenated [component_name]; custom element names must contain a dash or registration fails silently.
  • Default [shadow_mode] to open unless you have a strong reason; closed mode blocks legitimate external access and testing.
  • Keep [observed_attributes] minimal and reflect only what genuinely drives rendering to avoid reflection loops.
  • Expose styling through [css_parts] rather than letting consumers reach into the shadow tree, which keeps the API stable.
  • Set composed: true on every event in [custom_events]; without it, events stop at the Shadow DOM boundary and parent frameworks never see them.
  • Only set [form_associated] to yes when you actually need form participation; it adds real complexity around validity and state restoration.
  • Ship both an auto-registering ES module and a deferred-registration export, so consumers who need to control customElements.define() timing can.

Frequently Asked Questions

Will the component really work in React without a wrapper?
Largely yes, because it is a standard Custom Element. React's older event handling can miss custom events, which is why the prompt dispatches them with `composed: true` and `bubbles: true`. For complex props you may still pass JSON via attributes as the prompt suggests.
What is attribute-property reflection and why does it matter?
It keeps an element's HTML attributes and JavaScript properties in sync, so setting either updates the other. This lets frameworks and plain HTML both configure the component naturally, which is essential for the cross-framework compatibility this prompt targets.
Can it participate in native HTML forms?
Yes, when `[form_associated]` is set to yes. The prompt adds `static formAssociated = true`, implements `formStateRestoreCallback`, and reports value and validity. If you do not need form behavior, leaving it off keeps the component simpler.
How do consumers style the inside of the Shadow DOM?
Through the `[css_parts]` CSS `::part()` selectors the component exposes, plus inherited properties and `currentColor`. This gives controlled styling hooks without letting host CSS leak in, preserving the encapsulation that makes the component predictable.
Engr Mejba Ahmed

Need this built for real?

Engr Mejba Ahmed

AI Developer · Software Engineer

I'm Mejba — I design and ship production AI systems, automations, and full-stack apps. If you want this turned into a working solution for your team, let's talk.

More in Frontend Development Prompts

Engr Mejba Ahmed

Engr Mejba Ahmed

AI assistant · trained on my work

👋

Hey there!

Quick Actions

WhatsApp Direct line to me

Chat on WhatsApp

+880 1723 741224 · Replies within the hour on working days

Popular Questions

Engr Mejba Ahmed is connected
Engr Mejba Ahmed is typing...
Engr Mejba Ahmed avatar

✉ Want me to follow up? Drop your email

Engr Mejba Ahmed avatar

📞 Connect Directly

Choose how you'd like to reach me

WhatsApp

+880 1723 741224

Email

mejba.13@gmail.com

✓ Details sent! I'll get back to you shortly.

Powered by OpenAI

335+

Blog Posts

25

AI Courses

63

Projects

Services & Expertise

Pricing & Process

Learning & Resources

Connect & Support