agentleFS
mazreanmazrean/agent-skills/skills/building-lit-components/SKILL.md

building-lit-components

Skill2 starsChanged 8 months agoMITView on GitHub

Builds fast, lightweight Web Components using Lit library. Use when creating Lit components, defining reactive properties, handling lifecycle, using directives, styling with Shadow DOM, implementing Context API, Task for async, or SSR with Lit.

The file

We split this skill into the rules it gives an agent

2 rules. About 0 are common, 2 are distinctive, and 0 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈2

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 0

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Keep render() pure: Don't modify state in render

    line 192
  2. Distinctive:

    Clean up in disconnectedCallback: Remove external listeners

    line 196

≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Building Lit Components

Lit is a lightweight (~5KB) library for building fast Web Components with reactive state, scoped styles, and declarative templates.

Use this skill when creating web components with Lit, optimizing rendering, implementing reactive properties, using directives, or building component libraries.

Supporting files: COMPONENTS.md for component patterns, DIRECTIVES.md for built-in directives, STYLING.md for styling best practices, ADVANCED.md for Context, Task, SSR.

Quick Start

import {LitElement, html, css} from 'lit';
import {customElement, property} from 'lit/decorators.js';

@customElement('my-element')
export class MyElement extends LitElement {
  static styles = css`
    :host { display: block; }
    .greeting { color: blue; }
  `;

  @property() name = 'World';

  render() {
    return html`<p class="greeting">Hello, ${this.name}!</p>`;
  }
}

Core Concepts

Component Structure

PartPurpose
@customElementRegister as custom element
static stylesScoped CSS (Shadow DOM)
@property()Reactive public property
@state()Internal reactive state
render()Return template

Reactive Properties

// Public property (reflects to attribute)
@property({type: String, reflect: true})
name?: string;

// Internal state (no attribute)
@state()
private _count = 0;

// Complex objects
@property({type: Object, attribute: false})
data?: { id: number; name: string };

Property Options:

  • type: Converter type (String, Number, Boolean, Array, Object)
  • reflect: Sync to attribute (default: false)
  • attribute: Custom attribute name or false
  • state: Mark as internal state

Lifecycle Methods

class MyElement extends LitElement {
  // Called when added to DOM
  connectedCallback() {
    super.connectedCallback();
    // Add external event listeners
  }

  // Called before render when props change
  willUpdate(changedProperties: PropertyValues) {
    // Compute derived values
  }

  // Called after first render
  firstUpdated() {
    // DOM is available, focus inputs, etc.
  }

  // Called after every render
  updated(changedProperties: PropertyValues) {
    // React to specific prop changes
  }

  // Called when removed from DOM
  disconnectedCallback() {
    super.disconnectedCallback();
    // Clean up listeners
  }
}

Templates & Events

render() {
  return html`
    <!-- Property binding -->
    <input .value=${this.inputValue}>

    <!-- Attribute binding -->
    <div id=${this.elementId}></div>

    <!-- Boolean attribute -->
    <button ?disabled=${this.isDisabled}>Click</button>

    <!-- Event listener -->
    <button @click=${this._handleClick}>Submit</button>

    <!-- Event with options -->
    <div @scroll=${this._onScroll} @scroll=${(e) => this._onScroll(e)}></div>
  `;
}

private _handleClick(e: Event) {
  this.dispatchEvent(new CustomEvent('item-selected', {
    detail: { id: this.itemId },
    bubbles: true,
    composed: true
  }));
}

Essential Decorators

DecoratorUsagePurpose
@customElement('tag')ClassRegister custom element
@property()FieldReactive public property
@state()FieldReactive internal state
@query('#id')FieldSelect single element
@queryAll('.class')FieldSelect all elements
@eventOptions({...})MethodEvent listener options
@customElement('my-element')
class MyElement extends LitElement {
  @property({type: Number}) count = 0;
  @state() private _active = false;

  @query('#input') input!: HTMLInputElement;
  @queryAll('.item') items!: NodeListOf<HTMLElement>;

  @eventOptions({passive: true})
  private _onScroll(e: Event) { /* ... */ }
}

Common Directives

DirectiveUsagePurpose
repeatListsEfficient list rendering with keys
mapListsSimple list transformation
whenConditionalIf-else rendering
classMapClassesDynamic class names
styleMapStylesDynamic inline styles
liveFormsSync with live DOM value
refDOMGet element reference
untilAsyncShow placeholder until resolved
import {repeat} from 'lit/directives/repeat.js';
import {classMap} from 'lit/directives/class-map.js';
import {live} from 'lit/directives/live.js';

render() {
  return html`
    <!-- Efficient list -->
    ${repeat(this.items, (i) => i.id, (i) => html`<li>${i.name}</li>`)}

    <!-- Dynamic classes -->
    <div class=${classMap({active: this.isActive, error: this.hasError})}></div>

    <!-- Sync with user input -->
    <input .value=${live(this.inputValue)}>
  `;
}

Key Best Practices

  1. Keep render() pure: Don't modify state in render
  2. Use @state for internal data: Prevents attribute exposure
  3. Prefer property over attribute for objects: attribute: false
  4. Use live() for form inputs: Keeps DOM in sync
  5. Clean up in disconnectedCallback: Remove external listeners
  6. Use CSS custom properties for theming: Enables external customization

Common Anti-Patterns

Anti-PatternProblemSolution
Modify props in render()Infinite loopsUse willUpdate()
Dynamic tag namesPerformance issuesUse static tags
Objects as attributesSerialization overheadUse attribute: false
Skip super.connectedCallback()Breaks lifecycleAlways call super
DOM manipulation outside renderState desyncUse template bindings

Detailed Guides

  • Component patterns & composition: See COMPONENTS.md
  • All built-in directives: See DIRECTIVES.md
  • Styling & theming: See STYLING.md
  • Context, Task, SSR, Controllers: See ADVANCED.md

Resources

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.