Sub topic 1
Quick Recap: Anatomy of a Lightning Web Component
Prerequisites
Assume you know basic HTML, CSS, modern JavaScript, and the file structure of a Lightning Web Component (LWC). You also need access to a Salesforce org or scratch org to run examples.
Component Anatomy
An LWC usually has an HTML template, a JavaScript controller, and an optional CSS file. The template declares the UI. The controller stores state and handles events. The stylesheet adds component-specific rules.
A component folder commonly contains files such as contactSearch.html, contactSearch.js, and contactSearch.css. The JavaScript class extends LightningElement, the base class for LWCs.
What Lightning Base Components Are
Lightning base components are ready-made lightning-* elements supplied by Salesforce. They provide behavior, styling, keyboard support, and accessibility features. Examples include lightning-button, lightning-input, lightning-card, and lightning-icon.
Use documented attributes and events rather than changing a base component's internal markup.
<template>
<lightning-card title="Contact search" icon-name="standard:contact">
<div class="slds-var-p-around_medium">
<lightning-input
type="search"
label="Name"
value={searchKey}
onchange={handleChange}
></lightning-input>
</div>
</lightning-card>
</template>What SLDS Provides
The Salesforce Lightning Design System (SLDS) is Salesforce's design language. It defines visual patterns, spacing, typography, colors, and responsive layouts. Base components implement many SLDS patterns internally. You can also apply SLDS utility classes to elements around them.
Utility Classes and the Grid
Utility classes are small, reusable CSS classes. For example, slds-var-p-around_medium adds responsive padding. Grid classes create responsive layouts without custom media queries.
<template>
<div class="slds-grid slds-gutters slds-wrap">
<div class="slds-col slds-size_1-of-1 slds-medium-size_1-of-2">
<lightning-input label="First name"></lightning-input>
</div>
<div class="slds-col slds-size_1-of-1 slds-medium-size_1-of-2">
<lightning-input label="Last name"></lightning-input>
</div>
</div>
</template>On small screens, each column occupies the full width. On medium and larger screens, each occupies half the width.
Exercise
Which class makes the two inputs stack on small screens?
Show answer
slds-size_1-of-1; the medium-size class changes each column to half width on larger screens.