A slot (slotslot) is a placeholder for markup that a parent component passes into a component's body. LWC (Lightning web components).
- Lightning Web Components Slots Games
- Lightning Web Components Slots Free
- Lightning Web Components Slots Free Play
- Salesforce Lightning Web Component Slot
What are Lightning Web Components, and how will they make my life better?
- Web Components are all over Twitter (again), thanks to a great post by Rich Harris about Why he Doesn't Use Web Components. Rich details a number of technical (and non-technical) issues with the raw Web Components standard, specifically the Custom Elements and Shadow DOM APIs.
- Accessing assignedNodesor assignedElementson a is the canonical way to verify if it has slotted content. In the case of modal with a header, for example, the component needs to handle the case where a slotted content is added or removed.
It's almost that time of the year again - the Spring ‘19 release is upon us!
There's loads of goodies coming our way, but one of the most exciting for developers is the release of Lightning Web Components. It's a new programming model for Lightning component development, built on top of cutting-edge web technologies - and we think it's going to bring a wealth of benefits to Salesforce developers.
What's a web component?
To explain what Lightning Web Components are all about, it's useful to take a look at the tech they're built on top of, and how it came about.
The concept of 'components' on the web has existed for a long time. The general idea is to split your UI component into modularized chunks, each of which is self-contained and re-usable. There are many benefits to this approach, not least of which are the reduction of duplication in your logic and UI, and the maintenance advantages of building your application out of small, easy to understand pieces of functionality.
Modern web frameworks such as React, Angular and Vue all put some concept of a 'component' front and centre as core primitives, with great results. However, even as this approach has become more and more common, the frameworks themselves have remained fragmented in their implementations - you can't (easily) drop an Angular component into a React project or vice-versa, and once you've learned how to create components in Vue you might have to re-learn a lot of concepts before you can be productive in one of the other frameworks.
Web components are an attempt to address this fragmentation, and create a set of standards for how components can be natively supported by all browsers. By standardizing the underlying technology, we can both pave the way for interoperability between different frameworks, and reduce the friction for web developers and teams who want to migrate between different frameworks.
..OK, but what's a web component?
Web components are a set of standards. There are several different specifications that make up what people refer to as 'web components':
- Custom elements - These allow you to define a custom HTML tag that can be used inside your existing markup. Instead of an endlessly nested set of
tags, you can create your own
element which has its own encapsulated UI and behaviour.
- HTML templates - HTML templates allow you define a chunk of HTML markup which is not rendered until you need to insert it. They can contain
elements which allow you to dynamically inject content into certain parts of the markup. You can think of this as a standardized version of Handlebars.js (or your HTML templating library of choice).
- Shadow DOM - Much less ominous than it sounds, the shadow DOM (Document Object Model) is a way of 'componentizing' the DOM . The DOM is a representation of all of the elements in your web page - but accessing the DOM is usually done in a global manner by querying the entire document for nodes matching certain properties, which causes issues when you're trying to encapsulate components and stop them treading on each others' toes. The shadow DOM gives you a way of creating a 'shadow tree' which only contains the elements in your component. This allows you to hide elements inside the tree from elements outside it, and vice-versa, creating the encapsulation that components are all about.
What does any of this have to do with my Lightning components?
This standardization push opens the door for a new breed of web frameworks, which are more focused on providing specialized functionality on top of the platform, rather than plugging gaps in the platform itself. We've touched on the benefits already, but they include the prospect of:
- Interoperable components between frameworks, making it easier to migrate between frameworks and re-use functionality that other developers have built using other frameworks
- More transferable skills for developers: developers will be able to get up and running across different technology stacks with less ramp-up
- Improved performance, because more of the functionality will be implemented natively in the browsers rather than in Javascript as framework abstraction layers
Lightning Web Components are a Salesforce implementation of this new breed of lightweight frameworks. They build on top of the standards mentioned above, along with modern Javascript standards (ECMAScript 7, which contains many benefits of its own), and add functionality tailored to Salesforce. This extra functionality includes declarative access to Salesforce data and metadata, and over 70 base UI components built using the custom elements web standard.
What about my existing Lightning components?
With the addition of Lightning Web Components, the existing development model for Lightning components is being renamed back to the Aura programming model. Aura Components and Lightning Web Components can coexist and interoperate, and they share the same high level services for accessing Salesforce data and metadata, as well as the same base Lightning components (which were already implemented as Lightning Web Components!) You can put both types of component on the same page, and Aura components can include Lightning Web Components.
The upshot of this is that you can manage your migration to the new programming model however you like - you can continue to build Aura Components, or you can start to slowly transition your new components over to Lightning Web Components, without worrying about interoperability.
If you're starting a new project then we'd recommend jumping straight in with Lightning Web Components. It's built on modern web standards, it's here to stay, and most importantly it delivers a whole host of benefits that you can start to realize straight away.
Deploying Lightning Web Components with Gearset
Of course, these fancy new components won't be much fun if you can't move them easily between environments! Luckily, we're already working on support for them in Gearset, and we'll have that support ready before the release of Spring ‘19.
An exciting step forwards
Lightning Web Components represent an exciting step-change in front end development for Salesforce. It's a technology stack built from the ground up on cutting edge web standards, but it's available to you right now - and better still, it integrates with your current codebase so that you can handle the migration seamlessly.
If you want to read more, there's some great blog posts on the Salesforce developer blog introducing the new components and going into a bit more detail about developer tooling. There's also a Trailhead module if you want to try out making your first Lightning Web Component. If you want to know more about the web components standards themselves, I'd recommend the MDN documentation as a good starting point.
We're always keen to talk Salesforce developer tooling, so if you've got any thoughts you'd like to share don't hesitate to get in touch via email, Twitter or the in-app chat!
You can add components within the body of another component. Composition enables you to build complex components from simpler building-block components. Composing apps and components from a set of smaller components makes code more reusable and maintainable.
Let's look at a simple app. The components are in the example
namespace. The markup is contrived because we want to illustrate the concepts of owner and container.
Owner
The owner is the component that owns the template. In this example, the owner is the example-todo-app
component. The owner controls all the composed (child) components that it contains. An owner can:
- Set public properties on composed components
- Call public methods on composed components
- Listen for events dispatched by composed components
Container
A container contains other components but itself is contained within the owner component. In this example, example-todo-wrapper
is a container. A container is less powerful than the owner. A container can:
- Read, but not change, public properties in contained components
- Call public methods on composed components
- Listen for some, but not necessarily all, events bubbled up by components that it contains. See Configure Event Propagation.
Parent and child
When a component contains another component, which, in turn, can contain other components, we have a containment hierarchy. In the documentation, we sometimes talk about parent and child components. A parent component contains a child component. A parent component can be the owner or a container.
# Set a Property on a Child Component
Lightning Web Components Slots Games
To communicate down the containment hierarchy, an owner can set a property on a child component. An attribute in HTML turns into a property assignment in JavaScript.
Let's look at how the owner, example-todo-app
, sets public properties on the two instances of example-todo-item
.
In the playground, select todoItem.js
. The @api
decorator exposes the itemName
field as a public property.
Select todoApp.html
. To set the public itemName
property, it sets the item-name
attribute on each example-todo-item
component. Change Milk
to Hummus
to see an item name change.
Property names in JavaScript are in camel case while HTML attribute names are in kebab case (dash-separated) to match HTML standards. In todoApp.html
, the item-name
attribute in markup maps to the itemName
JavaScript property of todoItem.js
.
Tip
This example uses the static values Milk
and Bread
. A real-world component would typically use a for:each
iteration over a collection computed in the owner's JavaScript file, todoApp.js
.
Let's look at another example of composition and data binding. The example-contact-tile
component exposes a contact
public property (@api contact
in contactTile.js
). The parent component, example-composition-basics
, sets the value of contact
.
The contact
public property in example-contact-tile
is initialized based on the contact
property value of its owner. The data binding for property values is one way. If the property value changes in the owner, the updated value propagates to the child. For example, in compositionBasics.js
, change Name: 'Amy Taylor',
to Name: 'Stacy Adams',
. The change is reflected in the example-contact-tile
child component.
Example
See the Composition recipes in the Lightning Web Components recipes app.
# Send an Event from a Child to an Owner
When a component decorates a field with @api
to expose it as a public property, it should set the value only when it initializes the field, if at all. After the field is initialized, only the owner component should set the value.
To trigger a mutation for a property value provided by an owner component, a child component can send an event to the owner. The owner can change the property value, which propagates down to the child.
Example
The Lightning Web Components recipes app has a more realistic example. Click Child-to-Parent and look at the EventWithData example, which has a list of contacts. When a user clicks a contact, the component sends an event to the parent, which sends a new contact
object to the child.
# Data Flow Considerations
To prevent code complexity and unexpected side effects, data should flow in one direction, from owner to child.
# Primitive Property Values
We recommend using primitive data types for public properties instead of using object data types. Slice complex data structures in a higher-level component and pass the primitive values to the component descendants.
Primitive values require specific @api
properties that clearly define the data shape. Accepting an object or an array requires documentation to specify the shape. If an object shape changes, consumers break.
Standard HTML elements accept only primitive values for attributes. When a standard HTML element needs a complex shape, it uses child components. For example, a A non-primitive value (like an object or array) passed to a component is read-only. The component cannot change the content of the object or array. If the component tries to change the content, you see an error in the browser console. To mutate the data, the component can make a shallow copy of the objects it wants to mutate. More commonly, the component can send an event to the owner of the object. When the owner receives the event, it mutates the data, which is sent back down to the child. Tip The next example attempts to help you fully understand that objects passed to a component are read-only. The example doesn't contain best practices, instead, it shows code that doesn't work. In the following playground, The browser console displays the error: This is a tricky but important thing to understand. The Again, the table
element uses tr
and td
elements. Only primitive types can be defined in HTML. For example, isn't a value in HTML. However, you could create a
table
Lightning web component with a data
API.# Objects Passed to Components Are Read-Only
example-contact-tile-object
exposes its contact
field as a public property. The parent, example-composition-basics-object
, defines a contact
object and passes it to example-contact-tile-object
. The child component can't mutate the object.contact-tile-object
component tries and fails to update the name
property of the object that it received from its owner.Uncaught Error: Invalid mutation: Cannot set 'name' on '[object Object]'. '[object Object]' is read-only.
The component doesn't own the object passed to it and can't mutate it.contact
object passed to example-contact-tile-object
is read-only. That object is owned by the component that passed it to the child. However, the child owns its contact
field and can set that field to a new value. Let's look at some code to see how this works.contact-tile-object
component successfully sets a new value for the contact
field.contact-tile-object
component owns its contact
field, and it can assign a new value to that field.
Note
After clicking Update contact field, the child component owns the object, which means that the child can mutate the object. Therefore, if you click Update contact.name again, you may expect the component to display Jennifer Wu
, but it doesn't. The value is set, and Jennifer Wu
displays in the console, which means that the component did mutate the object. However, the component doesn't rerender. Why? Because LWC can't see that a property of the contact
object changed. The contact
field is decorated with @api
, not @track
. (A field can have only one decorator.) The @api
decorator simply makes a field public and allows its value to be set from the outside. The @api
decorator doesn't tell LWC to observe mutations like @track
does.
But why does the component rerender when we assign a new value directly to the contact
field? Because LWC observes all fields for mutation. If you assign a new value to a field, the component rerenders. However, if an object is assigned to a field, LWC doesn't observe the internal properties of that object. Therefore, the component doesn't update when you assign a new value to contact.name
. To tell LWC to observe the internal properties of an object, decorate the field with @track
. See Field Reactivity.
# Call a Method on a Child Component
To expose a public method, decorate it with @api
. Public methods are part of a component's API. To communicate down the containment hierarchy, owner and parent components can call JavaScript methods (or set properties) on child components.
Example
See the Parent-to-Child recipes in the Lightning Web Components recipes app.
# Define a Method
This example exposes isPlaying()
, play()
, and pause()
methods in a example-video-player
component by adding the @api
decorator to the methods. A parent component that contains example-video-player
can call these methods. Here's the JavaScript file.
videoUrl
is a public reactive property. The @api
decorator can be used to define a public reactive property, and a public JavaScript method, on a component. Public reactive properties are another part of the component's public API.
Note
To access elements that the template owns, the code uses the template
property.
Now, let's look at the HTML file where the video element is defined.
In a real-world component, example-video-player
would typically have controls to play or pause the video itself. For this example to illustrate the design of a public API, the controls are in the parent component that calls the public methods.
# Call a Method
The example-method-caller
component contains example-video-player
and has buttons to call the play()
and pause()
methods in example-video-player
. Here's the HTML.
Clicking the buttons in example-method-caller
plays or pauses the video in example-video-player
after we wire up the handlePlay
and handlePause
methods in example-method-caller
.
Here's the JavaScript file for example-method-caller
.
The handlePlay()
function in example-method-caller
calls the play()
method in the example-video-player
element. this.template.querySelector('example-video-player')
returns the example-video-player
element in methodCaller.html. The this.template.querySelector()
call is useful to get access to a child component so that you can call a method on the component.
The handlePause()
function in example-method-caller
calls the pause()
method in the example-video-player
element.
# Return Values
To return a value from a JavaScript method, use the return
statement. For example, see the isPlaying()
method in example-video-player
.
# Method Parameters
To pass data to a JavaScript method, define one or more parameters for the method. For example, you could define the play()
method to take a speed
parameter that controls the video playback speed.
# Pass Markup into Slots
Lightning Web Components Slots Free
With the addition of Lightning Web Components, the existing development model for Lightning components is being renamed back to the Aura programming model. Aura Components and Lightning Web Components can coexist and interoperate, and they share the same high level services for accessing Salesforce data and metadata, as well as the same base Lightning components (which were already implemented as Lightning Web Components!) You can put both types of component on the same page, and Aura components can include Lightning Web Components.
The upshot of this is that you can manage your migration to the new programming model however you like - you can continue to build Aura Components, or you can start to slowly transition your new components over to Lightning Web Components, without worrying about interoperability.
If you're starting a new project then we'd recommend jumping straight in with Lightning Web Components. It's built on modern web standards, it's here to stay, and most importantly it delivers a whole host of benefits that you can start to realize straight away.
Deploying Lightning Web Components with Gearset
Of course, these fancy new components won't be much fun if you can't move them easily between environments! Luckily, we're already working on support for them in Gearset, and we'll have that support ready before the release of Spring ‘19.
An exciting step forwards
Lightning Web Components represent an exciting step-change in front end development for Salesforce. It's a technology stack built from the ground up on cutting edge web standards, but it's available to you right now - and better still, it integrates with your current codebase so that you can handle the migration seamlessly.
If you want to read more, there's some great blog posts on the Salesforce developer blog introducing the new components and going into a bit more detail about developer tooling. There's also a Trailhead module if you want to try out making your first Lightning Web Component. If you want to know more about the web components standards themselves, I'd recommend the MDN documentation as a good starting point.
We're always keen to talk Salesforce developer tooling, so if you've got any thoughts you'd like to share don't hesitate to get in touch via email, Twitter or the in-app chat!
You can add components within the body of another component. Composition enables you to build complex components from simpler building-block components. Composing apps and components from a set of smaller components makes code more reusable and maintainable.
Let's look at a simple app. The components are in the example
namespace. The markup is contrived because we want to illustrate the concepts of owner and container.
Owner
The owner is the component that owns the template. In this example, the owner is the example-todo-app
component. The owner controls all the composed (child) components that it contains. An owner can:
- Set public properties on composed components
- Call public methods on composed components
- Listen for events dispatched by composed components
Container
A container contains other components but itself is contained within the owner component. In this example, example-todo-wrapper
is a container. A container is less powerful than the owner. A container can:
- Read, but not change, public properties in contained components
- Call public methods on composed components
- Listen for some, but not necessarily all, events bubbled up by components that it contains. See Configure Event Propagation.
Parent and child
When a component contains another component, which, in turn, can contain other components, we have a containment hierarchy. In the documentation, we sometimes talk about parent and child components. A parent component contains a child component. A parent component can be the owner or a container.
# Set a Property on a Child Component
Lightning Web Components Slots Games
To communicate down the containment hierarchy, an owner can set a property on a child component. An attribute in HTML turns into a property assignment in JavaScript.
Let's look at how the owner, example-todo-app
, sets public properties on the two instances of example-todo-item
.
In the playground, select todoItem.js
. The @api
decorator exposes the itemName
field as a public property.
Select todoApp.html
. To set the public itemName
property, it sets the item-name
attribute on each example-todo-item
component. Change Milk
to Hummus
to see an item name change.
Property names in JavaScript are in camel case while HTML attribute names are in kebab case (dash-separated) to match HTML standards. In todoApp.html
, the item-name
attribute in markup maps to the itemName
JavaScript property of todoItem.js
.
Tip
This example uses the static values Milk
and Bread
. A real-world component would typically use a for:each
iteration over a collection computed in the owner's JavaScript file, todoApp.js
.
Let's look at another example of composition and data binding. The example-contact-tile
component exposes a contact
public property (@api contact
in contactTile.js
). The parent component, example-composition-basics
, sets the value of contact
.
The contact
public property in example-contact-tile
is initialized based on the contact
property value of its owner. The data binding for property values is one way. If the property value changes in the owner, the updated value propagates to the child. For example, in compositionBasics.js
, change Name: 'Amy Taylor',
to Name: 'Stacy Adams',
. The change is reflected in the example-contact-tile
child component.
Example
See the Composition recipes in the Lightning Web Components recipes app.
# Send an Event from a Child to an Owner
When a component decorates a field with @api
to expose it as a public property, it should set the value only when it initializes the field, if at all. After the field is initialized, only the owner component should set the value.
To trigger a mutation for a property value provided by an owner component, a child component can send an event to the owner. The owner can change the property value, which propagates down to the child.
Example
The Lightning Web Components recipes app has a more realistic example. Click Child-to-Parent and look at the EventWithData example, which has a list of contacts. When a user clicks a contact, the component sends an event to the parent, which sends a new contact
object to the child.
# Data Flow Considerations
To prevent code complexity and unexpected side effects, data should flow in one direction, from owner to child.
# Primitive Property Values
We recommend using primitive data types for public properties instead of using object data types. Slice complex data structures in a higher-level component and pass the primitive values to the component descendants.
Primitive values require specific @api
properties that clearly define the data shape. Accepting an object or an array requires documentation to specify the shape. If an object shape changes, consumers break.
Standard HTML elements accept only primitive values for attributes. When a standard HTML element needs a complex shape, it uses child components. For example, a A non-primitive value (like an object or array) passed to a component is read-only. The component cannot change the content of the object or array. If the component tries to change the content, you see an error in the browser console. To mutate the data, the component can make a shallow copy of the objects it wants to mutate. More commonly, the component can send an event to the owner of the object. When the owner receives the event, it mutates the data, which is sent back down to the child. Tip The next example attempts to help you fully understand that objects passed to a component are read-only. The example doesn't contain best practices, instead, it shows code that doesn't work. In the following playground, The browser console displays the error: This is a tricky but important thing to understand. The Again, the Note After clicking Update contact field, the child component owns the object, which means that the child can mutate the object. Therefore, if you click Update contact.name again, you may expect the component to display But why does the component rerender when we assign a new value directly to the To expose a public method, decorate it with Example See the Parent-to-Child recipes in the Lightning Web Components recipes app. This example exposes Note To access elements that the template owns, the code uses the Now, let's look at the HTML file where the video element is defined. In a real-world component, The Clicking the buttons in Here's the JavaScript file for The The To return a value from a JavaScript method, use the To pass data to a JavaScript method, define one or more parameters for the method. For example, you could define the Add a slot to a component's HTML file so a parent component can pass markup into the component. A component can have zero or more slots. A slot is a placeholder for markup that a parent component passes into a component's body. Slots are part of the Web Component specification. To define a slot in markup, use the In the playground, click When If a component has more than one unnamed slot, the markup passed into the body of the component is inserted into all the unnamed slots. This UI pattern is unusual. A component usually has zero or one unnamed slot. This example component has two named slots and one unnamed slot. Here's the markup for a parent component that uses The Here's the rendered output. All Changes within the children of the This example contains a The component The console prints the first time the component is rendered, and if the flag The The Call these methods differently depending on whether you want to access elements the component owns or access elements passed via slots. Important Don't pass an 15 Motor City Casino jobs available on Indeed.com. Apply to Registered Nurse Manager, Psychologist, Security Officer and more! 65 Motorcity Casino jobs available on Indeed.com. Apply to Guest Service Agent, Security Supervisor, Compensation Specialist and more! 37 Motorcity Casino jobs available in Detroit, MI on Indeed.com. Apply to Guest Service Agent, Security Officer, Security Supervisor and more! Motor city casino jobs downtown detroit. Search job openings at Motorcity Hotel & Casino. 19 Motorcity Hotel & Casino jobs including salaries, ratings, and reviews, posted by Motorcity Hotel & Casino employees. Great Jobs The first step in creating the ultimate escape for our guests is to make sure MotorCity Casino Hotel is a great place to work. Our associates are proud to come to work every single day knowing they're part of an exciting industry where people come to relax and forget about the outside world. To access elements rendered by your component, use the Important Don't use the A component doesn't own DOM elements that are passed to it via slots. These DOM elements aren't in the component's shadow tree. To access DOM elements passed in via slots, call This example shows how to get the DOM elements passed to a child component from the child's context. Pass the selector name, such as an element, to When creating components that contain other components, consider the lifecycle and construction of the component hierarchy using the declarative (slots) ors hidden from the document that contains it. The shadow tree affects how you work with the DOM, CSS, and events. Shadow DOM is a web standard that encapsulates the elements of a component to keep styling and behavior consistent in any context. Since not all browsers implement Shadow DOM, Lightning Web Components uses a shadow DOM polyfill ( Casino point dive park catalina island. To better understand how to work with the shadow tree, let's look at some markup. This markup contains two Lightning web components: Note The shadow root isn't an element, it's a document fragment. Let's look at how to work with the shadow tree in each of these areas. CSS CSS styles defined in a parent component don't leak into a child. In our example, a Events If an event bubbles up and crosses the shadow boundary, to hide the internal details of the component that dispatched the event, some property values change to match the scope of the listener. See Event Retargeting. Access Elements To access elements a component renders from the component's JavaScript class, use the Access Slots A slot is a placeholder for markup that a parent component passes into a component's body. DOM elements that are passed to a component via slots aren't owned by the component and aren't in the component's shadow tree. To access DOM elements passed in via slots, call Don't use these DOM APIs to reach into a component's shadow tree. Important The shadow DOM polyfill includes a patch to the table
element uses tr
and td
elements. Only primitive types can be defined in HTML. For example, isn't a value in HTML. However, you could create a
table
Lightning web component with a data
API.# Objects Passed to Components Are Read-Only
example-contact-tile-object
exposes its contact
field as a public property. The parent, example-composition-basics-object
, defines a contact
object and passes it to example-contact-tile-object
. The child component can't mutate the object.contact-tile-object
component tries and fails to update the name
property of the object that it received from its owner.Uncaught Error: Invalid mutation: Cannot set 'name' on '[object Object]'. '[object Object]' is read-only.
The component doesn't own the object passed to it and can't mutate it.contact
object passed to example-contact-tile-object
is read-only. That object is owned by the component that passed it to the child. However, the child owns its contact
field and can set that field to a new value. Let's look at some code to see how this works.contact-tile-object
component successfully sets a new value for the contact
field.contact-tile-object
component owns its contact
field, and it can assign a new value to that field.Jennifer Wu
, but it doesn't. The value is set, and Jennifer Wu
displays in the console, which means that the component did mutate the object. However, the component doesn't rerender. Why? Because LWC can't see that a property of the contact
object changed. The contact
field is decorated with @api
, not @track
. (A field can have only one decorator.) The @api
decorator simply makes a field public and allows its value to be set from the outside. The @api
decorator doesn't tell LWC to observe mutations like @track
does.contact
field? Because LWC observes all fields for mutation. If you assign a new value to a field, the component rerenders. However, if an object is assigned to a field, LWC doesn't observe the internal properties of that object. Therefore, the component doesn't update when you assign a new value to contact.name
. To tell LWC to observe the internal properties of an object, decorate the field with @track
. See Field Reactivity.# Call a Method on a Child Component
@api
. Public methods are part of a component's API. To communicate down the containment hierarchy, owner and parent components can call JavaScript methods (or set properties) on child components.# Define a Method
isPlaying()
, play()
, and pause()
methods in a example-video-player
component by adding the @api
decorator to the methods. A parent component that contains example-video-player
can call these methods. Here's the JavaScript file.videoUrl
is a public reactive property. The @api
decorator can be used to define a public reactive property, and a public JavaScript method, on a component. Public reactive properties are another part of the component's public API.template
property.example-video-player
would typically have controls to play or pause the video itself. For this example to illustrate the design of a public API, the controls are in the parent component that calls the public methods.# Call a Method
example-method-caller
component contains example-video-player
and has buttons to call the play()
and pause()
methods in example-video-player
. Here's the HTML.example-method-caller
plays or pauses the video in example-video-player
after we wire up the handlePlay
and handlePause
methods in example-method-caller
.example-method-caller
.handlePlay()
function in example-method-caller
calls the play()
method in the example-video-player
element. this.template.querySelector('example-video-player')
returns the example-video-player
element in methodCaller.html. The this.template.querySelector()
call is useful to get access to a child component so that you can call a method on the component.handlePause()
function in example-method-caller
calls the pause()
method in the example-video-player
element.# Return Values
return
statement. For example, see the isPlaying()
method in example-video-player
.# Method Parameters
play()
method to take a speed
parameter that controls the video playback speed.# Pass Markup into Slots
Lightning Web Components Slots Free
tag, which has an optional
name
attribute.# Unnamed Slots
slotDemo.html
to see an unnamed slot. The slotWrapper
component passes content into the slot.example-slot-demo
is rendered, the unnamed slot is replaced with Content from Slot Wrapper. Here's the rendered HTML of example-slot-wrapper
.# Named Slots
example-named-slots
.example-slots-wrapper
component passes:Willy
into the firstName
slotWonka
into the lastName
slotChocolatier
into the unnamed slot# Run Code on
slotchange
elements support the
slotchange
event. The slotchange
event fires when a direct child of a node in a element changes, such as when new content is appended or deleted. Only
elements support this event.
element don't trigger a
slotchange
event. element that handles the
slotchange
event.example-child
is passed into the slot.addOneMore
is set to true.slotchange
event is not triggered even when showFooter
is true and the footer element is appended.# Query Selectors
querySelector()
and querySelectorAll()
methods are standard DOM APIs. querySelector()
returns the first element that matches the selector. querySelectorAll()
returns an array of DOM Elements.id
to these query methods. When an HTML template renders, id
values are transformed into globally unique values. If you use an id
selector in JavaScript, it won't match the transformed id
. If you're iterating over an array, consider adding some other attribute to the element, like a class
or data-*
value, and use it to select the element. LWC uses id
values for accessibility only.# Access Elements the Component Owns
template
property to call a query method.querySelector()
result.window
or document
global properties to query for DOM elements. Also, we don't recommend using JavaScript to manipulate the DOM. It's better to use HTML directives to write declarative code.# Access Elements Passed Via Slots
this.querySelector()
and this.querySelectorAll()
. Because the component doesn't own these elements, you don't use this.template.querySelector()
or this.template.querySelectorAll()
.this.querySelector()
and this.querySelectorAll()
. This example passes the span
element.# Compose Components Using Slots Versus Data
@lwc/synthetic-shadow
). A polyfill is code that allows a feature to work in a web browser.example-todo-app
and example-todo-item
. The shadow root defines the boundary between the DOM and the shadow tree. This boundary is called the shadow boundary.p
style defined in the todoApp.css
style sheet doesn't style the p
element in the example-todo-item
component, because the styles don't reach into the shadow tree. See CSS.template
property. See Access Elements the Component Owns.this.querySelector()
and this.querySelectorAll()
. The component doesn't own these elements, so you don't use template
. See Pass Markup into Slots.# DOM APIs
Lightning Web Components Slots Free Play
Document.prototype.getElementById
Document.prototype.querySelector
Document.prototype.querySelectorAll
Document.prototype.getElementsByClassName
Document.prototype.getElementsByTagName
Document.prototype.getElementsByTagNameNS
Document.prototype.getElementsByName
document.body.querySelector
document.body.querySelectorAll
document.body.getElementsByClassName
document.body.getElementsByTagName
document.body.getElementsByTagNameNS
Salesforce Lightning Web Component Slot
MutationObserver
interface. If you use MutationObserver
to watch changes in a DOM tree, disconnect it or you will cause a memory leak. Note that a component can observe mutations only in its own template. It can't observe mutations within the shadow tree of other custom elements.