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

                                

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

Claude Code Expert · Online

👋

Hey there!

Quick Actions

WhatsApp Instant reply

Chat on WhatsApp

+880 1723 741224 · Instant reply

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

[email protected]

✓ 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