# @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-15Cori: c-blosc Support (#2636)Gravatar Axel Huebl 1-3/+24 2021-12-15Examples: Python ShellBang & Executable (#2673)Gravatar Axel Huebl 84-66/+107 2021-12-15CI: Remove (`particle_`)`tolerance` (#2669)Gravatar Axel Huebl 2-189/+16 2021-12-15pre-commit: check json (#2670)Gravatar Axel Huebl 1-0/+1 2021-12-14Fix: Missing EB `analysis.py` Permissions (#2672)Gravatar Axel Huebl 1-0/+0 2021-12-14Fix: `pre-commit run -a` (#2671)Gravatar Axel Huebl 4-4/+1 2021-12-14ABLASTR: Fix Doxygen in `DepositCharge`Gravatar Axel Huebl 1-1/+1 2021-12-14Refactor DepositCharge so it can be called from ImpactX. (#2652)Gravatar Andrew Myers 11-178/+304 2021-12-13Adding EB multifabs to the Python interface (#2647)Gravatar Lorenzo Giacomel 8-0/+493 2021-12-13Add load balancing test (#2561)Gravatar Kevin Z. Zhu 4-26/+41 2021-12-14AMReX/PICSAR: Weekly Update (#2666)Gravatar Axel Huebl 7-44/+44 2021-12-13Add anisotropic mesh refinement example (#2650)Gravatar Remi Lehe 2-0/+63 2021-12-13Add high energy asymptotic fit for Proton-Boron total cross section (#2408)Gravatar Neïl Zaim 1-26/+72 2021-12-13Fix the scope of profiler for SYCL (#2668)Gravatar Weiqun Zhang 1-5/+5 2021-12-13Add PML Support for multi-J Algorithm (#2603)Gravatar Edoardo Zoni 5-8/+233 2021-12-13Fixing the initialization of the EB data in ghost cells (#2635)Gravatar Lorenzo Giacomel 4-57/+44 2021-12-13Python: Add 3.10, Relax upper bound (#2664)Gravatar Axel Huebl 1-1/+2 2021-12-13Docs: Fix Warning Logger Typo (#2667)Gravatar Phil Miller 1-1/+1 2021-12-11ABLASTR Library (#2263)Gravatar Axel Huebl 6-41/+78 2021-12-11C++17: if constexpr for templates in ShapeFactors (#2659)Gravatar Luca Fedeli 1-150/+84 2021-12-11QED openPMD Tests: Specify H5 Backend (#2661)Gravatar Axel Huebl 1-2/+2 2021-12-11ICC CI: Unbound Vars (`setvars.sh`) (#2663)Gravatar Axel Huebl 2-4/+4 2021-12-10`use_default_v_<galilean,comoving>` Only w/ Boosted Frame (#2654)Gravatar Edoardo Zoni 1-3/+18 2021-12-10fix for setting the boundary condition potentials in 1D ES simulations (#2649)Gravatar Roelof Groenewald 1-5/+5 2021-12-10use if constexpr to replace template specialization (#2660)Gravatar Luca Fedeli 1-106/+67 2021-12-10fix check for absolute library install path (#2646)Gravatar s9105947 1-1/+1 2021-12-10CMake: 3.18+ (#2651)Gravatar Axel Huebl 4-6/+6 2021-12-10Docs: `python3 -m pip` & Virtual Env (#2656)Gravatar Axel Huebl 7-14/+22 2021-12-10PWFA 1D: Fix output nameGravatar Axel Huebl 3-2/+2 2021-12-10Fix: analysis_default_regression.pyGravatar Axel Huebl 1-1/+2 2021-12-10Python/setup.py: picmistandard==0.0.18Gravatar Axel Huebl 1-1/+1 2021-12-09Azure: Ensure latest venv installedGravatar Axel Huebl 1-0/+1 2021-12-09Python GNUmake: Remove Prefix HacksGravatar Axel Huebl 1-20/+20 2021-12-09GNUmake `installwarpx`: `mv` -> `cp`Gravatar Axel Huebl 1-1/+1 2021-12-09Azure: pre-install `setuptools` upgradeGravatar Axel Huebl 2-9/+9 2021-12-09Regression/requirements.txt: openpmd-apiGravatar Axel Huebl 1-0/+1 2021-12-09Fix missing checksums1d (#2657)Gravatar Axel Huebl 3-0/+47 2021-12-09GNUmake & `WarpX-test.ini`: `python` -> `python3`Gravatar Axel Huebl 2-21/+21 2021-12-09Azure: `set -eu -o pipefail`Gravatar Axel Huebl 1-0/+2