|
1 | 1 | # @semantic-org/astro-lit |
2 | 2 |
|
3 | | -> First party support for Lit was included up until Astro 5 but was removed. This community version preserves Lit SSR rendering for newer versions of Lit. |
| 3 | +[](https://www.npmjs.com/package/@semantic-ui/astro-lit) |
| 4 | +[](https://www.npmjs.com/package/@semantic-ui/astro-lit) |
| 5 | +[](https://github.com/Semantic-Org/Astro-Lit/blob/main/LICENSE) |
4 | 6 |
|
5 | | -This **[Astro integration](https://astro.build/integrations/)** enables server-side rendering and client-side hydration for your [Lit](https://lit.dev/) custom elements. |
| 7 | +> The official `@astrojs/lit` integration was deprecated in Astro v5. This community-maintained integration enables you to continue using Lit with modern versions of Astro. |
6 | 8 |
|
7 | | -## How To Use |
| 9 | +This [Astro integration](https://docs.astro.build/en/guides/integrations-guide/) enables server-side rendering and client-side hydration for your [Lit](https://lit.dev/) custom elements. |
8 | 10 |
|
9 | | -```javascript |
| 11 | +## Features |
| 12 | + |
| 13 | +* Server-side rendering (SSR) of Lit components. |
| 14 | +* Client-side hydration with Astro `client:*` directives. |
| 15 | +* Automatic polyfills for Declarative Shadow DOM in unsupported browsers. |
| 16 | +* Support for experimental decorators. |
| 17 | + |
| 18 | +## Installation |
| 19 | + |
| 20 | +### Prerequisites |
| 21 | + |
| 22 | +* Astro v5.0 or later. |
| 23 | +* Node.js v18.17.1, v20.3.0 or later. |
| 24 | + |
| 25 | +To use the Lit integration, you first install the required package and then update your Astro configuration. |
| 26 | + |
| 27 | +### Procedure |
| 28 | + |
| 29 | +1. Install the integration package into your project: |
| 30 | + |
| 31 | + * **npm** |
| 32 | + ```sh |
| 33 | + $ npm install @semantic-ui/astro-lit |
| 34 | + ``` |
| 35 | + * **pnpm** |
| 36 | + ```sh |
| 37 | + $ pnpm add @semantic-ui/astro-lit |
| 38 | + ``` |
| 39 | + * **yarn** |
| 40 | + ```sh |
| 41 | + $ yarn add @semantic-ui/astro-lit |
| 42 | + ``` |
| 43 | + |
| 44 | +2. Update your `astro.config.mjs` file to include the Lit integration. |
| 45 | + |
| 46 | + ```js title="astro.config.mjs" |
| 47 | + import { defineConfig } from 'astro/config'; |
| 48 | + import lit from '@semantic-ui/astro-lit'; |
| 49 | + |
| 50 | + export default defineConfig({ |
| 51 | + integrations: [lit()], |
| 52 | + }); |
| 53 | + ``` |
| 54 | +
|
| 55 | +## Usage |
| 56 | +
|
| 57 | +To learn about using framework components in Astro, refer to the [Astro Framework Components guide](https://docs.astro.build/en/guides/framework-components/). |
| 58 | +
|
| 59 | +### Creating and Using a Lit Component |
| 60 | +
|
| 61 | +To use a Lit component, create a component file and then import it into your `.astro` pages. |
| 62 | +
|
| 63 | +1. Create your Lit component. For example, create a new file named `my-element.js` in the `src/components/` directory. |
| 64 | +
|
| 65 | + ```javascript title="src/components/my-element.js" |
| 66 | + import { LitElement, html } from 'lit'; |
| 67 | + |
| 68 | + export class MyElement extends LitElement { |
| 69 | + render() { |
| 70 | + return html`<p>Hello world! This is my Lit component.</p>`; |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + customElements.define('my-element', MyElement); |
| 75 | + ``` |
| 76 | +
|
| 77 | +2. Import and use the component in an Astro page. Note that Astro requires you to use the class name, `MyElement`, as the component tag, not the custom element tag name `my-element`. |
| 78 | +
|
| 79 | + ```astro title="src/pages/index.astro" |
| 80 | + --- |
| 81 | + import { MyElement } from '../components/my-element.js'; |
| 82 | + --- |
| 83 | + <MyElement /> |
| 84 | + ``` |
| 85 | +
|
| 86 | +### Using Third-Party Lit Components |
| 87 | +
|
| 88 | +You can also use third-party web components that are built with Lit. To use a third-party component, import its JavaScript module, which typically registers the custom element as a side effect. Then, you can use the custom element tag directly in your template. |
| 89 | +
|
| 90 | +In the following example, you import a third-party button component and use it with its custom HTML tag, `<sbb-button>`: |
| 91 | +
|
| 92 | +```astro title="src/pages/buttons.astro" |
| 93 | +--- |
| 94 | +// This import registers the <sbb-button> custom element. |
| 95 | +import '@sbb-esta/lyne-elements/button.js'; |
| 96 | +--- |
| 97 | +<h1>Third-Party Component Example</h1> |
| 98 | +
|
| 99 | +<sbb-button>Click Me</sbb-button> |
| 100 | +``` |
| 101 | +
|
| 102 | +### Client-Side Hydration |
| 103 | +
|
| 104 | +To hydrate a component on the client, add an Astro `client:*` directive. These directives determine when the component's JavaScript is sent to the browser. |
| 105 | +
|
| 106 | +For example, to hydrate a component only when it becomes visible in the viewport, use the `client:visible` directive: |
| 107 | +
|
| 108 | +```astro title="src/pages/index.astro" |
| 109 | +--- |
| 110 | +import { MyElement } from '../components/my-element.js'; |
| 111 | +--- |
| 112 | +<MyElement client:visible /> |
| 113 | +``` |
| 114 | +
|
| 115 | +For a full list of available directives, refer to the official [Astro Client Directives documentation](https://docs.astro.build/en/reference/directives-reference/#client-directives). |
| 116 | +
|
| 117 | +### Using Experimental Decorators |
| 118 | +
|
| 119 | +To use experimental decorators in Lit, for example `@customElement` or `@state`, you must enable the `experimentalDecorators` option in your `tsconfig.json` file. |
| 120 | +
|
| 121 | +1. If you do not have one, create a `tsconfig.json` file in the root of your project. |
| 122 | +2. Add the `compilerOptions.experimentalDecorators` setting: |
| 123 | +
|
| 124 | + ```json title="tsconfig.json" |
| 125 | + { |
| 126 | + "compilerOptions": { |
| 127 | + "experimentalDecorators": true |
| 128 | + } |
| 129 | + } |
| 130 | + ``` |
| 131 | +
|
| 132 | +You can now use decorators in your Lit TypeScript components: |
| 133 | +
|
| 134 | +```typescript title="src/components/my-decorator-element.ts" |
| 135 | +import { LitElement, html } from "lit"; |
| 136 | +import { customElement, property } from "lit/decorators.js"; |
| 137 | +
|
| 138 | +@customElement("my-decorator-element") |
| 139 | +export class MyDecoratorElement extends LitElement { |
| 140 | + @property() |
| 141 | + name = "World"; |
| 142 | +
|
| 143 | + override render() { |
| 144 | + return html`<p>Hello, ${this.name}!</p>`; |
| 145 | + } |
| 146 | +} |
| 147 | +``` |
| 148 | +
|
| 149 | +## Troubleshooting |
| 150 | +
|
| 151 | +This section describes common issues and their solutions. |
| 152 | +
|
| 153 | +### Browser Global Interference |
| 154 | +
|
| 155 | +The Lit integration adds browser global properties, for example `window` and `document`, to the global scope during server-side rendering. These globals can interfere with other libraries that check for their existence to determine if they are running in a browser environment. This can cause unexpected behavior or errors with other libraries. |
| 156 | +
|
| 157 | +If you use multiple integrations, changing their order in the `astro.config.mjs` file can sometimes resolve the issue. |
| 158 | +
|
| 159 | +```javascript title="astro.config.mjs" |
| 160 | +import { defineConfig } from 'astro/config'; |
| 161 | +import vue from '@astrojs/vue'; |
10 | 162 | import lit from '@semantic-ui/astro-lit'; |
11 | 163 |
|
12 | 164 | export default defineConfig({ |
13 | | - integrations: [ |
14 | | - lit(), // add lit as integration |
15 | | - ], |
| 165 | + // If vue() causes issues, try placing lit() before it. |
| 166 | + integrations: [lit(), vue()] |
16 | 167 | }); |
17 | 168 | ``` |
18 | 169 |
|
| 170 | +### Component Not Updating in Development |
| 171 | +
|
| 172 | +During development, if you make changes to a Lit component and do not see the updates in your browser, you might need to restart the Astro development server. |
| 173 | +
|
| 174 | +### Strict Package Manager Errors |
| 175 | +
|
| 176 | +When you use a strict package manager like `pnpm`, you can encounter a `ReferenceError: module is not defined` error. To resolve this, create a `.npmrc` file in your project's root directory and add a hoist pattern for Lit dependencies: |
| 177 | +
|
| 178 | +```ini title=".npmrc" |
| 179 | +public-hoist-pattern[]=*lit* |
| 180 | +``` |
| 181 | +
|
| 182 | +## Contributing |
| 183 | +
|
| 184 | +This project is open source and maintained by the community. You are welcome to submit issues and pull requests. |
| 185 | +
|
| 186 | +* To report a bug or request a feature, [open an issue](https://github.com/Semantic-Org/Astro-Lit/issues). |
| 187 | +* To contribute code, please open a pull request. |
| 188 | +
|
19 | 189 | ## License |
20 | 190 |
|
21 | 191 | MIT |
0 commit comments