Upgrade guide
Overview
Permalink to “Overview”Lit 2.0 is designed to work with most code written for LitElement 2.x and lit-html 1.x. There are a small number of changes required to migrate your code to Lit 2.0. The high-level changes required include:
- Updating npm packages and import paths.
- Loading
polyfill-support
script when loading the web components polyfills. - Updating any custom directive implementations to use new class-based API and associated helpers.
- Updating code to renamed APIs.
- Adapting to minor breaking changes, mostly in uncommon cases.
The following sections will go through each of these changes in detail.
Update packages and import paths
Permalink to “Update packages and import paths”Use the lit
package
Permalink to “Use the lit package” Lit 2.0 ships with a one-stop-shop lit
package, which consolidates lit-html
and lit-element
into an easy-to-use package. Use the following commands to upgrade:
And re-write your module imports appropriately:
From:
To:
Although the lit-element@^3
and lit-html@^2
packages should be largely backward-compatible, we recommend updating to the lit
package as the other packages are moving towards eventual deprecation.
Update decorator imports
Permalink to “Update decorator imports”The previous version of lit-element
exported all TypeScript decorators from the main module. In Lit 2.0, these have been moved to a separate module, to enable smaller bundle sizes when the decorators are unused.
From:
To:
Update directive imports
Permalink to “Update directive imports”Built-in lit-html directives are also now exported from the lit
package.
From:
To:
Update standalone lit-html imports
Permalink to “Update standalone lit-html imports”If using lit-html standalone (outside of LitElement), you can import standalone-specific API's like render
from the lit/html.js
entrypoint:
From:
To:
Load polyfill-support
when using web components polyfills
Permalink to “Load polyfill-support when using web components polyfills” Lit 2.0 still supports the same browsers down to IE11. However, given the broad adoption of Web Components APIs in modern browsers, we have taken the opportunity to move all of the code required for interfacing with the web components polyfills out of the core libraries and into an opt-in support file, so that the tax for supporting older browsers is only paid when required.
In general, any time you use the web components polyfills, you should also load the lit/polyfill-support.js
support file once on the page, similar to a polyfill. For example:
If using @web/test-runner
or @web/dev-server
with the legacyPlugin
for development, adding the following configuration to your web-test-runner.config.js
or web-dev-server.config.js
file will configure it to automatically inject the support file when needed:
Update to renamed APIs
Permalink to “Update to renamed APIs”The following advanced API's have been renamed in Lit 2.0. It should be safe to simply rename these across your codebase if used:
Previous name | New name | Notes |
---|---|---|
UpdatingElement | ReactiveElement | The base class underpinning LitElement . Naming now aligns with terminology we use to describe its reactive lifecycle. |
@internalProperty | @state | Decorator for LitElement / ReactiveElement used to denote private state that trigger updates, as opposed to public properties on the element settable by the user which use the @property decorator. |
static getStyles() | static finalizeStyles(styles) | Method on LitElement and ReactiveElement class used for overriding style processing. Note it now also takes an argument reflecting the static styles for the class. |
_getUpdateComplete() | getUpdateComplete() | Method on LitElement and ReactiveElement class used for overriding the updateComplete promise |
NodePart | ChildPart | Typically only used in directive code; see below. |
Update custom directive implementations
Permalink to “Update custom directive implementations”While the API for using directives should be 100% backward-compatible with 1.x, there is a breaking change to how custom directives are authored. The API change improves ergonomics around making stateful directives while providing a clear pattern for SSR-compatible directives: only render
will be called on the server, while update
will not be.
Overview of directive API changes
Permalink to “Overview of directive API changes”Concept | Previous API | New API |
---|---|---|
Code idiom | Function that takes directive arguments, and returns function that takes part and returns value | Class that extends Directive with update & render methods which accept directive arguments |
Declarative rendering | Pass value to part.setValue() | Return value from render() method |
DOM manipulation | Implement in directive function | Implement in update() method |
State | Stored in WeakMap keyed on part | Stored in class instance fields |
Part validation | instanceof check on part in every render | part.type check in constructor |
Async updates | part.setValue(v); part.commit(); | Extend AsyncDirective instead of Directive and call this.setValue(v) |
Example directive migration
Permalink to “Example directive migration”Below is an example of a lit-html 1.x directive, and how to migrate it to the new API:
1.x Directive API:
2.0 Directive API:
Adapt to minor breaking changes
Permalink to “Adapt to minor breaking changes”For completeness, the following is a list of minor but notable breaking changes that you may need to adapt your code to. We expect these changes to affect relatively few users.
LitElement
Permalink to “LitElement” - For simplicity,
requestUpdate
no longer returns a Promise. Instead await theupdateComplete
Promise. - Errors that occur during the update cycle were previously squelched to allow subsequent updates to proceed normally. Now errors are re-fired asynchronously so they can be detected. Errors can be observed via an
unhandledrejection
event handler on window. - Creation of
shadowRoot
viacreateRenderRoot
and support for applyingstatic styles
to theshadowRoot
has moved fromLitElement
toReactiveElement
. - The
createRenderRoot
method is now called just before the first update rather than in the constructor. Element code can not assume therenderRoot
exists before the elementhasUpdated
. This change was made for compatibility with server-side rendering. ReactiveElement
'sinitialize
method has been removed. This work is now done in the element constructor.- The static
render
method on theLitElement
base class has been removed. This was primarily used for implementing ShadyDOM integration, and was not intended as a user-overridable method. ShadyDOM integration is now achieved via thepolyfill-support
module. - When a property declaration is
reflect: true
and itstoAttribute
function returnsundefined
the attribute is now removed where previously it was left unchanged (#872). - The dirty check in
attributeChangedCallback
has been removed. While technically breaking, in practice it should very rarely be (#699). - LitElement's
adoptStyles
method has been removed. Styling is now adopted increateRenderRoot
. This method may be overridden to customize this behavior. - Removed
requestUpdateInternal
. TherequestUpdate
method is now identical to this method and should be used instead.
lit-html
Permalink to “lit-html” render()
no longer clears the container it's rendered to on first render. It now appends to the container by default.- Expressions in comments are not rendered or updated.
- Template caching happens per call site, not per template-tag/call-site pair. This means some rare forms of highly dynamic template tags are no longer supported.
- Arrays and other iterables passed to attribute bindings are not specially handled. Arrays will be rendered with their default toString representation. This means that
html`<div class=${['a', 'b']}>
will render<div class="a,b">
instead of<div class="a b">
. To get the old behavior, usearray.join(' ')
. - The
templateFactory
option ofRenderOptions
has been removed. TemplateProcessor
has been removed.- Symbols are not converted to a string before mutating DOM, so passing a Symbol to an attribute or text binding will result in an exception.
- The
ifDefined
directive in an attribute expression now removes the attribute for bothnull
andundefined
, not justundefined
. - Rendering the value
nothing
to an attribute expression causes the attribute to be removed—even if there are multiple expressions in the attribute value position and only one isnothing
. For example givensrc="${baseurl}/${filename}"
, thesrc
attribute is removed if eitherbaseurl
orfilename
evaluate tonothing
. - Rendering the value
nothing
,null
, orundefined
in theunsafeHTML
orunsafeSVG
directive will now result in no content being rendered (previously it would render'[object Object]'
,'null'
, or'undefined'
, respectively).