# @astrojs/lit 🔥
This **[Astro integration][astro-integration]** enables server-side rendering and client-side hydration for your [Lit](https://lit.dev/) custom elements.
## Installation
There are two ways to add integrations to your project. Let's try the most convenient option first!
### `astro add` command
Astro includes a CLI tool for adding first party integrations: `astro add`. This command will:
1. (Optionally) Install all necessary dependencies and peer dependencies
2. (Also optionally) Update your `astro.config.*` file to apply this integration
To install `@astrojs/lit`, run the following from your project directory and follow the prompts:
```sh
# Using NPM
npx astro add lit
# Using Yarn
yarn astro add lit
# Using PNPM
pnpx astro add lit
```
If you run into any hiccups, [feel free to log an issue on our GitHub](https://github.com/withastro/astro/issues) and try the manual installation steps below.
### Install dependencies manually
First, install the `@astrojs/lit` integration like so:
```sh
npm install @astrojs/lit
```
Most package managers will install associated peer dependencies as well. Still, if you see a "Cannot find package 'lit'" (or similar) warning when you start up Astro, you'll need to install `lit` and `@webcomponents/template-shadowroot`:
```sh
npm install lit @webcomponents/template-shadowroot
```
Now, apply this integration to your `astro.config.*` file using the `integrations` property:
__`astro.config.mjs`__
```js
import lit from '@astrojs/lit';
export default {
// ...
integrations: [lit()],
}
```
## Getting started
To use your first Lit component in Astro, head to our [UI framework documentation][astro-ui-frameworks]. This explains:
- 📦 how framework components are loaded,
- 💧 client-side hydration options, and
- 🪆 opportunities to mix and nest frameworks together
However, there's a key difference with Lit _custom elements_ over conventional _components_: you can use the element tag name directly.
Astro needs to know which tag is associated with which component script. We expose this through exporting a `tagName` variable from the component script. It looks like this:
__`src/components/my-element.js`__
```js
import { LitElement, html } from 'lit';
export const tagName = 'my-element';
class MyElement extends LitElement {
render() {
return html`
Hello world! From my-element
`;
}
}
customElements.define(tagName, MyElement);
```
> Note that exporting the `tagName` is __required__ if you want to use the tag name in your templates. Otherwise you can export and use the constructor, like with non custom element frameworks.
In your Astro template import this component as a side-effect and use the element.
__`src/pages/index.astro`__
```astro
---
import '../components/my-element.js';
---
```
> Note that Lit requires browser globals such as `HTMLElement` and `customElements` to be present. For this reason the Lit renderer shims the server with these globals so Lit can run. You *might* run into libraries that work incorrectly because of this.
### Polyfills & Hydration
The renderer automatically handles adding appropriate polyfills for support in browsers that don't have Declarative Shadow DOM. The polyfill is about *1.5kB*. If the browser does support Declarative Shadow DOM then less than 250 bytes are loaded (to feature detect support).
Hydration is also handled automatically. You can use the same hydration directives such as `client:load`, `client:idle` and `client:visible` as you can with other libraries that Astro supports.
```astro
---
import '../components/my-element.js';
---
```
The above will only load the element's JavaScript when the user has scrolled it into view. Since it is server rendered they will not see any jank; it will load and hydrate transparently.
### More documentation
Check our [Astro Integration Documentation][astro-integration] for more on integrations.
[astro-integration]: https://docs.astro.build/en/guides/integrations-guide/
[astro-ui-frameworks]: https://docs.astro.build/en/core-concepts/framework-components/#using-framework-components
avatar.com/avatar/81e1ef75c8481b89748d2b10a3d702e9?s=13&d=retro' width='13' height='13' alt='Gravatar' /> Axel Huebl
1 | -0/+2 |
2021-12-15 | Cori: c-blosc Support (#2636) | Axel Huebl
| 1 | -3/+24 |
2021-12-15 | Examples: Python ShellBang & Executable (#2673) | Axel Huebl
| 84 | -66/+107 |
2021-12-15 | CI: Remove (`particle_`)`tolerance` (#2669) | Axel Huebl
| 2 | -189/+16 |
2021-12-15 | pre-commit: check json (#2670) | Axel Huebl
| 1 | -0/+1 |
2021-12-14 | Fix: Missing EB `analysis.py` Permissions (#2672) | Axel Huebl
| 1 | -0/+0 |
2021-12-14 | Fix: `pre-commit run -a` (#2671) | Axel Huebl
| 4 | -4/+1 |
2021-12-14 | ABLASTR: Fix Doxygen in `DepositCharge` | Axel Huebl
| 1 | -1/+1 |
2021-12-14 | Refactor DepositCharge so it can be called from ImpactX. (#2652) | Andrew Myers
| 11 | -178/+304 |
2021-12-13 | Adding EB multifabs to the Python interface (#2647) | Lorenzo Giacomel
| 8 | -0/+493 |
2021-12-13 | Add load balancing test (#2561) | Kevin Z. Zhu
| 4 | -26/+41 |
2021-12-14 | AMReX/PICSAR: Weekly Update (#2666) | Axel Huebl
| 7 | -44/+44 |
2021-12-13 | Add anisotropic mesh refinement example (#2650) | Remi Lehe
| 2 | -0/+63 |
2021-12-13 | Add high energy asymptotic fit for Proton-Boron total cross section (#2408) | Neïl Zaim
| 1 | -26/+72 |
2021-12-13 | Fix the scope of profiler for SYCL (#2668) | Weiqun Zhang
| 1 | -5/+5 |
2021-12-13 | Add PML Support for multi-J Algorithm (#2603) | Edoardo Zoni
| 5 | -8/+233 |
2021-12-13 | Fixing the initialization of the EB data in ghost cells (#2635) | Lorenzo Giacomel
| 4 | -57/+44 |
2021-12-13 | Python: Add 3.10, Relax upper bound (#2664) | Axel Huebl
| 1 | -1/+2 |
2021-12-13 | Docs: Fix Warning Logger Typo (#2667) | Phil Miller
| 1 | -1/+1 |
2021-12-11 | ABLASTR Library (#2263) | Axel Huebl
| 6 | -41/+78 |
2021-12-11 | C++17: if constexpr for templates in ShapeFactors (#2659) | Luca Fedeli
| 1 | -150/+84 |
2021-12-11 | QED openPMD Tests: Specify H5 Backend (#2661) | Axel Huebl
| 1 | -2/+2 |
2021-12-11 | ICC CI: Unbound Vars (`setvars.sh`) (#2663) | Axel Huebl
| 2 | -4/+4 |
2021-12-10 | `use_default_v_<galilean,comoving>` Only w/ Boosted Frame (#2654) | Edoardo Zoni
| 1 | -3/+18 |
2021-12-10 | fix for setting the boundary condition potentials in 1D ES simulations (#2649) | Roelof Groenewald
| 1 | -5/+5 |
2021-12-10 | use if constexpr to replace template specialization (#2660) | Luca Fedeli
| 1 | -106/+67 |
2021-12-10 | fix check for absolute library install path (#2646) | s9105947
| 1 | -1/+1 |
2021-12-10 | CMake: 3.18+ (#2651) | Axel Huebl
| 4 | -6/+6 |
2021-12-10 | Docs: `python3 -m pip` & Virtual Env (#2656) | Axel Huebl
| 7 | -14/+22 |
2021-12-10 | PWFA 1D: Fix output name | Axel Huebl
| 3 | -2/+2 |
2021-12-10 | Fix: analysis_default_regression.py | Axel Huebl
| 1 | -1/+2 |
2021-12-10 | Python/setup.py: picmistandard==0.0.18 | Axel Huebl
| 1 | -1/+1 |
2021-12-09 | Azure: Ensure latest venv installed | Axel Huebl
| 1 | -0/+1 |
2021-12-09 | Python GNUmake: Remove Prefix Hacks | Axel Huebl
| 1 | -20/+20 |
2021-12-09 | GNUmake `installwarpx`: `mv` -> `cp` | Axel Huebl
| 1 | -1/+1 |
2021-12-09 | Azure: pre-install `setuptools` upgrade | Axel Huebl
| 2 | -9/+9 |
2021-12-09 | Regression/requirements.txt: openpmd-api | Axel Huebl
| 1 | -0/+1 |
2021-12-09 | Fix missing checksums1d (#2657) | Axel Huebl
| 3 | -0/+47 |
2021-12-09 | GNUmake & `WarpX-test.ini`: `python` -> `python3` | Axel Huebl
| 2 | -21/+21 |
2021-12-09 | Azure: `set -eu -o pipefail` | Axel Huebl
| 1 | -0/+2 |