{"kind":"AgentDefinition","metadata":{"namespace":"community","name":"lwc","version":"0.1.0"},"spec":{"agents_md":"---\ndescription: 'Guidelines and best practices for developing Lightning Web Components (LWC) on Salesforce Platform.'\napplyTo: 'force-app/main/default/lwc/**'\n---\n\n# LWC Development\n\n## General Instructions\n\n- Each LWC should reside in its own folder under `force-app/main/default/lwc/`.\n- The folder name should match the component name (e.g., `myComponent` folder for the `myComponent` component).\n- Each component folder should contain the following files:\n    - `myComponent.html`: The HTML template file.\n    - `myComponent.js`: The JavaScript controller file.\n    - `myComponent.js-meta.xml`: The metadata configuration file.\n    - Optional: `myComponent.css` for component-specific styles.\n    - Optional: `myComponent.test.js` for Jest unit tests.\n\n## Core Principles\n\n### 1. Use Lightning Components Over HTML Tags\nAlways prefer Lightning Web Component library components over plain HTML elements for consistency, accessibility, and future-proofing.\n\n#### Recommended Approach\n```html\n\u003c!-- Use Lightning components --\u003e\n\u003clightning-button label=\"Save\" variant=\"brand\" onclick={handleSave}\u003e\u003c/lightning-button\u003e\n\u003clightning-input type=\"text\" label=\"Name\" value={name} onchange={handleNameChange}\u003e\u003c/lightning-input\u003e\n\u003clightning-combobox label=\"Type\" options={typeOptions} value={selectedType}\u003e\u003c/lightning-combobox\u003e\n\u003clightning-radio-group name=\"duration\" label=\"Duration\" options={durationOptions} value={duration} type=\"radio\"\u003e\u003c/lightning-radio-group\u003e\n```\n\n#### Avoid Plain HTML\n```html\n\u003c!-- Avoid these --\u003e\n\u003cbutton onclick={handleSave}\u003eSave\u003c/button\u003e\n\u003cinput type=\"text\" onchange={handleNameChange} /\u003e\n\u003cselect onchange={handleTypeChange}\u003e\n    \u003coption value=\"option1\"\u003eOption 1\u003c/option\u003e\n\u003c/select\u003e\n```\n\n### 2. Lightning Component Mapping Guide\n\n| HTML Element | Lightning Component | Key Attributes |\n|--------------|-------------------|----------------|\n| `\u003cbutton\u003e` | `\u003clightning-button\u003e` | `variant`, `label`, `icon-name` |\n| `\u003cinput\u003e` | `\u003clightning-input\u003e` | `type`, `label`, `variant` |\n| `\u003cselect\u003e` | `\u003clightning-combobox\u003e` | `options`, `value`, `placeholder` |\n| `\u003ctextarea\u003e` | `\u003clightning-textarea\u003e` | `label`, `max-length` |\n| `\u003cinput type=\"checkbox\"\u003e` | `\u003clightning-input type=\"checkbox\"\u003e` | `checked`, `label` |\n| `\u003cinput type=\"radio\"\u003e` | `\u003clightning-radio-group\u003e` | `options`, `type`, `name` |\n| `\u003cinput type=\"toggle\"\u003e` | `\u003clightning-input type=\"toggle\"\u003e` | `checked`, `variant` |\n| Custom pills | `\u003clightning-pill\u003e` | `label`, `name`, `onremove` |\n| Icons | `\u003clightning-icon\u003e` | `icon-name`, `size`, `variant` |\n\n### 3. Lightning Design System Compliance\n\n#### Use SLDS Utility Classes\nAlways use Salesforce Lightning Design System utility classes with the `slds-var-` prefix for modern implementations:\n\n```html\n\u003c!-- Spacing --\u003e\n\u003cdiv class=\"slds-var-m-around_medium slds-var-p-top_large\"\u003e\n    \u003cdiv class=\"slds-var-m-bottom_small\"\u003eContent\u003c/div\u003e\n\u003c/div\u003e\n\n\u003c!-- Layout --\u003e\n\u003cdiv class=\"slds-grid slds-wrap slds-gutters_small\"\u003e\n    \u003cdiv class=\"slds-col slds-size_1-of-2 slds-medium-size_1-of-3\"\u003e\n        \u003c!-- Content --\u003e\n    \u003c/div\u003e\n\u003c/div\u003e\n\n\u003c!-- Typography --\u003e\n\u003ch2 class=\"slds-text-heading_medium slds-var-m-bottom_small\"\u003eSection Title\u003c/h2\u003e\n\u003cp class=\"slds-text-body_regular\"\u003eDescription text\u003c/p\u003e\n```\n\n#### SLDS Component Patterns\n```html\n\u003c!-- Card Layout --\u003e\n\u003carticle class=\"slds-card slds-var-m-around_medium\"\u003e\n    \u003cheader class=\"slds-card__header\"\u003e\n        \u003ch2 class=\"slds-text-heading_small\"\u003eCard Title\u003c/h2\u003e\n    \u003c/header\u003e\n    \u003cdiv class=\"slds-card__body slds-card__body_inner\"\u003e\n        \u003c!-- Card content --\u003e\n    \u003c/div\u003e\n    \u003cfooter class=\"slds-card__footer\"\u003e\n        \u003c!-- Card actions --\u003e\n    \u003c/footer\u003e\n\u003c/article\u003e\n\n\u003c!-- Form Layout --\u003e\n\u003cdiv class=\"slds-form slds-form_stacked\"\u003e\n    \u003cdiv class=\"slds-form-element\"\u003e\n        \u003clightning-input label=\"Field Label\" value={fieldValue}\u003e\u003c/lightning-input\u003e\n    \u003c/div\u003e\n\u003c/div\u003e\n```\n\n### 4. Avoid Custom CSS\n\n#### Use SLDS Classes\n```html\n\u003c!-- Color and theming --\u003e\n\u003cdiv class=\"slds-theme_success slds-text-color_inverse slds-var-p-around_small\"\u003e\n    Success message\n\u003c/div\u003e\n\n\u003cdiv class=\"slds-theme_error slds-text-color_inverse slds-var-p-around_small\"\u003e\n    Error message\n\u003c/div\u003e\n\n\u003cdiv class=\"slds-theme_warning slds-text-color_inverse slds-var-p-around_small\"\u003e\n    Warning message\n\u003c/div\u003e\n```\n\n#### Avoid Custom CSS (Anti-Pattern)\n```css\n/* Don't create custom styles that override SLDS */\n.custom-button {\n    background-color: red;\n    padding: 10px;\n}\n\n.my-special-layout {\n    display: flex;\n    justify-content: center;\n}\n```\n\n#### When Custom CSS is Necessary\nIf you must use custom CSS, follow these guidelines:\n- Use CSS custom properties (design tokens) when possible\n- Prefix custom classes to avoid conflicts\n- Never override SLDS base classes\n\n```css\n/* Custom CSS example */\n.my-component-special {\n    border-radius: var(--lwc-borderRadiusMedium);\n    box-shadow: var(--lwc-shadowButton);\n}\n```\n\n### 5. Component Architecture Best Practices\n\n#### Reactive Properties\n```javascript\nimport { LightningElement, track, api } from 'lwc';\n\nexport default class MyComponent extends LightningElement {\n    // Use @api for public properties\n    @api recordId;\n    @api title;\n\n    // Primitive properties (string, number, boolean) are automatically reactive\n    // No decorator needed - reassignment triggers re-render\n    simpleValue = 'initial';\n    count = 0;\n\n    // Computed properties\n    get displayName() {\n        return this.name ? `Hello, ${this.name}` : 'Hello, Guest';\n    }\n\n    // @track is NOT needed for simple property reassignment\n    // This will trigger reactivity automatically:\n    handleUpdate() {\n        this.simpleValue = 'updated'; // Reactive without @track\n        this.count++; // Reactive without @track\n    }\n\n    // @track IS needed when mutating nested properties without reassignment\n    @track complexData = {\n        user: {\n            name: 'John',\n            preferences: {\n                theme: 'dark'\n            }\n        }\n    };\n\n    handleDeepUpdate() {\n        // Requires @track because we're mutating a nested property\n        this.complexData.user.preferences.theme = 'light';\n    }\n\n    // BETTER: Avoid @track by using immutable patterns\n    regularData = {\n        user: {\n            name: 'John',\n            preferences: {\n                theme: 'dark'\n            }\n        }\n    };\n\n    handleImmutableUpdate() {\n      // No @track needed - we're creating a new object reference\n      this.regularData = {\n        ...this.regularData,\n        user: {\n          ...this.regularData.user,\n          preferences: {\n            ...this.regularData.user.preferences,\n            theme: 'light'\n          }\n        }\n      };\n    }\n\n    // Arrays: @track is needed only for mutating methods\n    @track items = ['a', 'b', 'c'];\n\n    handleArrayMutation() {\n      // Requires @track\n      this.items.push('d');\n      this.items[0] = 'z';\n    }\n\n    // BETTER: Use immutable array operations\n    regularItems = ['a', 'b', 'c'];\n\n    handleImmutableArray() {\n      // No @track needed\n      this.regularItems = [...this.regularItems, 'd'];\n      this.regularItems = this.regularItems.map((item, idx) =\u003e\n        idx === 0 ? 'z' : item\n      );\n    }\n\n    // Use @track only for complex objects/arrays when you mutate nested properties.\n    // For example, updating complexObject.details.status without reassigning complexObject.\n    @track complexObject = {\n      details: {\n        status: 'new'\n      }\n    };\n}\n```\n\n#### Event Handling Patterns\n```javascript\n// Custom event dispatch\nhandleSave() {\n    const saveEvent = new CustomEvent('save', {\n        detail: {\n            recordData: this.recordData,\n            timestamp: new Date()\n        }\n    });\n    this.dispatchEvent(saveEvent);\n}\n\n// Lightning component event handling\nhandleInputChange(event) {\n    const fieldName = event.target.name;\n    const fieldValue = event.target.value;\n\n    // For lightning-input, lightning-combobox, etc.\n    this[fieldName] = fieldValue;\n}\n\nhandleRadioChange(event) {\n    // For lightning-radio-group\n    this.selectedValue = event.detail.value;\n}\n\nhandleToggleChange(event) {\n    // For lightning-input type=\"toggle\"\n    this.isToggled = event.detail.checked;\n}\n```\n\n### 6. Data Handling and Wire Services\n\n#### Use @wire for Data Access\n```javascript\nimport { getRecord } from 'lightning/uiRecordApi';\nimport { getObjectInfo } from 'lightning/uiObjectInfoApi';\n\nconst FIELDS = ['Account.Name', 'Account.Industry', 'Account.AnnualRevenue'];\n\nexport default class MyComponent extends LightningElement {\n    @api recordId;\n\n    @wire(getRecord, { recordId: '$recordId', fields: FIELDS })\n    record;\n\n    @wire(getObjectInfo, { objectApiName: 'Account' })\n    objectInfo;\n\n    get recordData() {\n        return this.record.data ? this.record.data.fields : {};\n    }\n}\n```\n\n### 7. Error Handling and User Experience\n\n#### Implement Proper Error Boundaries\n```javascript\nimport { ShowToastEvent } from 'lightning/platformShowToastEvent';\n\nexport default class MyComponent extends LightningElement {\n    isLoading = false;\n    error = null;\n\n    async handleAsyncOperation() {\n        this.isLoading = true;\n        this.error = null;\n\n        try {\n            const result = await this.performOperation();\n            this.showSuccessToast();\n        } catch (error) {\n            this.error = error;\n            this.showErrorToast(error.body?.message || 'An error occurred');\n        } finally {\n            this.isLoading = false;\n        }\n    }\n\n    performOperation() {\n        // Developer-defined async operation\n    }\n\n    showSuccessToast() {\n        const event = new ShowToastEvent({\n            title: 'Success',\n            message: 'Operation completed successfully',\n            variant: 'success'\n        });\n        this.dispatchEvent(event);\n    }\n\n    showErrorToast(message) {\n        const event = new ShowToastEvent({\n            title: 'Error',\n            message: message,\n            variant: 'error',\n            mode: 'sticky'\n        });\n        this.dispatchEvent(event);\n    }\n}\n```\n\n### 8. Performance Optimization\n\n#### Conditional Rendering\nPrefer `lwc:if`, `lwc:elseif` and `lwc:else` for conditional rendering (API v58.0+). Legacy `if:true` / `if:false` are still supported but should be avoided in new components.\n\n```html\n\u003c!-- Use template directives for conditional rendering --\u003e\n\u003ctemplate lwc:if={isLoading}\u003e\n    \u003clightning-spinner alternative-text=\"Loading...\"\u003e\u003c/lightning-spinner\u003e\n\u003c/template\u003e\n\u003ctemplate lwc:elseif={error}\u003e\n    \u003cdiv class=\"slds-theme_error slds-text-color_inverse slds-var-p-around_small\"\u003e\n        {error.message}\n    \u003c/div\u003e\n\u003c/template\u003e\n\u003ctemplate lwc:else\u003e\n    \u003ctemplate for:each={items} for:item=\"item\"\u003e\n        \u003cdiv key={item.id} class=\"slds-var-m-bottom_small\"\u003e\n            {item.name}\n        \u003c/div\u003e\n    \u003c/template\u003e\n\u003c/template\u003e\n```\n\n```html\n\u003c!-- Legacy approach (avoid in new components) --\u003e\n\u003ctemplate if:true={isLoading}\u003e\n    \u003clightning-spinner alternative-text=\"Loading...\"\u003e\u003c/lightning-spinner\u003e\n\u003c/template\u003e\n\u003ctemplate if:true={error}\u003e\n    \u003cdiv class=\"slds-theme_error slds-text-color_inverse slds-var-p-around_small\"\u003e\n        {error.message}\n    \u003c/div\u003e\n\u003c/template\u003e\n\u003ctemplate if:false={isLoading}\u003e\n  \u003ctemplate if:false={error}\u003e\n    \u003ctemplate for:each={items} for:item=\"item\"\u003e\n        \u003cdiv key={item.id} class=\"slds-var-m-bottom_small\"\u003e\n            {item.name}\n        \u003c/div\u003e\n    \u003c/template\u003e\n  \u003c/template\u003e\n\u003c/template\u003e\n```\n\n### 9. Accessibility Best Practices\n\n#### Use Proper ARIA Labels and Semantic HTML\n```html\n\u003c!-- Use semantic structure --\u003e\n\u003csection aria-label=\"Product Selection\"\u003e\n    \u003ch2 class=\"slds-text-heading_medium\"\u003eProducts\u003c/h2\u003e\n\n    \u003clightning-input\n        type=\"search\"\n        label=\"Search Products\"\n        placeholder=\"Enter product name...\"\n        aria-describedby=\"search-help\"\u003e\n    \u003c/lightning-input\u003e\n\n    \u003cdiv id=\"search-help\" class=\"slds-assistive-text\"\u003e\n        Type to filter the product list\n    \u003c/div\u003e\n\u003c/section\u003e\n```\n\n## Common Anti-Patterns to Avoid\n- **Direct DOM Manipulation**: Never use `document.querySelector()` or similar\n- **jQuery or External Libraries**: Avoid non-Lightning compatible libraries\n- **Inline Styles**: Use SLDS classes instead of `style` attributes\n- **Global CSS**: All styles should be scoped to the component\n- **Hardcoded Values**: Use custom labels, custom metadata, or constants\n- **Imperative API Calls**: Prefer `@wire` over imperative `import` calls when possible\n- **Memory Leaks**: Always clean up event listeners in `disconnectedCallback()`\n","description":"Guidelines and best practices for developing Lightning Web Components (LWC) on Salesforce Platform.","import":{"commit_sha":"541b7819d8c3545c6df122491af4fa1eae415779","imported_at":"2026-05-18T20:05:35Z","license_text":"MIT License\n\nCopyright GitHub, Inc.\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.","owner":"github","repo":"github/awesome-copilot","source_url":"https://github.com/github/awesome-copilot/blob/541b7819d8c3545c6df122491af4fa1eae415779/instructions/lwc.instructions.md"},"manifest":{}},"content_hash":[221,117,152,229,137,79,163,236,190,158,157,3,12,52,10,47,67,29,225,181,198,227,8,115,192,40,188,123,169,70,211,149],"trust_level":"unsigned","yanked":false}
