├── .gitignore ├── .vscode ├── extensions.json └── launch.json ├── README.md ├── example ├── .gitignore ├── README.md ├── astro.config.mjs ├── package-lock.json ├── package.json ├── public │ └── favicon.svg ├── src │ ├── components │ │ └── Card.astro │ ├── env.d.ts │ ├── layouts │ │ └── Layout.astro │ └── pages │ │ ├── about.astro │ │ └── index.astro └── tsconfig.json ├── package-lock.json ├── package.json └── src └── index.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | dist -------------------------------------------------------------------------------- /.vscode/extensions.json: -------------------------------------------------------------------------------- 1 | { 2 | "recommendations": ["astro-build.astro-vscode"], 3 | "unwantedRecommendations": [] 4 | } 5 | -------------------------------------------------------------------------------- /.vscode/launch.json: -------------------------------------------------------------------------------- 1 | { 2 | "version": "0.2.0", 3 | "configurations": [ 4 | { 5 | "command": "./node_modules/.bin/astro dev", 6 | "name": "Development server", 7 | "request": "launch", 8 | "type": "node-terminal" 9 | } 10 | ] 11 | } 12 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # astro-htmx 2 | 3 | This **[Astro integration](https://astro.build/integrations/)** adds [HTMX.org](https://htmx.org) to your project so that you can use HTMX.org anywhere on your page. 4 | 5 | - [Inst**al**lation](#installation) 6 | - [Usage](#usage) 7 | - [Configuration](#configuration) 8 | - [Examples](#examples) 9 | - [Troubleshooting](#troubleshooting) 10 | - [Contributing](#contributing) 11 | - [Changelog](#changelog) 12 | 13 | ## Installation 14 | 15 | ### Manual Install 16 | 17 | First, install the `astro-htmx` package using your package manager. If you're using npm or aren't sure, run this in the terminal: 18 | 19 | ```sh 20 | npm install astro-htmx htmx.org 21 | ``` 22 | 23 | Then, apply this integration to your `astro.config.*` file using the `integrations` property: 24 | 25 | ```diff lang="js" "htmx()" 26 | // astro.config.mjs 27 | import { defineConfig } from 'astro/config'; 28 | + import htmx from 'astro-htmx'; 29 | 30 | export default defineConfig({ 31 | // ... 32 | integrations: [htmx()], 33 | // ^^^^^^^^ 34 | }); 35 | ``` 36 | 37 | ## Usage 38 | 39 | Once the integration is installed, you can use [HTMX.org](https://htmx.org) directives and syntax inside any Astro component. The HTMX.org script is automatically added and enabled on every page of your website. 40 | 41 | Check [Astro Integration Documentation](https://astro.build/integrations/) for more on integrations. 42 | 43 | ## Limitations 44 | 45 | The HTMX.org integration does not give you control over how the script is loaded or initialized. If you require this control, consider [installing and using HTMX.org manually](https://htmx.org/docs/#installing). Astro supports all officially documented HTMX.org manual setup instructions, using ` 56 | ``` 57 | 58 | ## Configuration 59 | 60 | The HTMX.org integration does not support any custom configuration at this time. 61 | 62 | ## Examples 63 | 64 | - The [Astro HTMX example](https://github.com/xstevenyung/astro-htmx/tree/main/example) shows how to use HTMX in an Astro project. 65 | 66 | ## Contributing 67 | 68 | This package is maintained by [xstevenyung](https://github.com/xstevenyung). You're welcome to submit an issue or PR! 69 | 70 | [astro-integration]: https://docs.astro.build/en/guides/integrations-guide/ 71 | [astro-ui-frameworks]: https://docs.astro.build/en/core-concepts/framework-components/#using-framework-components 72 | [htmx]: https://htmx.org/ 73 | -------------------------------------------------------------------------------- /example/.gitignore: -------------------------------------------------------------------------------- 1 | # build output 2 | dist/ 3 | 4 | # generated types 5 | .astro/ 6 | 7 | # dependencies 8 | node_modules/ 9 | 10 | # logs 11 | npm-debug.log* 12 | yarn-debug.log* 13 | yarn-error.log* 14 | pnpm-debug.log* 15 | 16 | # environment variables 17 | .env 18 | .env.production 19 | 20 | # macOS-specific files 21 | .DS_Store 22 | -------------------------------------------------------------------------------- /example/README.md: -------------------------------------------------------------------------------- 1 | # Astro Starter Kit: Basics 2 | 3 | ```sh 4 | npm create astro@latest -- --template basics 5 | ``` 6 | 7 | [![Open in StackBlitz](https://developer.stackblitz.com/img/open_in_stackblitz.svg)](https://stackblitz.com/github/withastro/astro/tree/latest/examples/basics) 8 | [![Open with CodeSandbox](https://assets.codesandbox.io/github/button-edit-lime.svg)](https://codesandbox.io/p/sandbox/github/withastro/astro/tree/latest/examples/basics) 9 | [![Open in GitHub Codespaces](https://github.com/codespaces/badge.svg)](https://codespaces.new/withastro/astro?devcontainer_path=.devcontainer/basics/devcontainer.json) 10 | 11 | > 🧑‍🚀 **Seasoned astronaut?** Delete this file. Have fun! 12 | 13 | ![just-the-basics](https://github.com/withastro/astro/assets/2244813/a0a5533c-a856-4198-8470-2d67b1d7c554) 14 | 15 | ## 🚀 Project Structure 16 | 17 | Inside of your Astro project, you'll see the following folders and files: 18 | 19 | ```text 20 | / 21 | ├── public/ 22 | │ └── favicon.svg 23 | ├── src/ 24 | │ ├── components/ 25 | │ │ └── Card.astro 26 | │ ├── layouts/ 27 | │ │ └── Layout.astro 28 | │ └── pages/ 29 | │ └── index.astro 30 | └── package.json 31 | ``` 32 | 33 | Astro looks for `.astro` or `.md` files in the `src/pages/` directory. Each page is exposed as a route based on its file name. 34 | 35 | There's nothing special about `src/components/`, but that's where we like to put any Astro/React/Vue/Svelte/Preact components. 36 | 37 | Any static assets, like images, can be placed in the `public/` directory. 38 | 39 | ## 🧞 Commands 40 | 41 | All commands are run from the root of the project, from a terminal: 42 | 43 | | Command | Action | 44 | | :------------------------ | :----------------------------------------------- | 45 | | `npm install` | Installs dependencies | 46 | | `npm run dev` | Starts local dev server at `localhost:4321` | 47 | | `npm run build` | Build your production site to `./dist/` | 48 | | `npm run preview` | Preview your build locally, before deploying | 49 | | `npm run astro ...` | Run CLI commands like `astro add`, `astro check` | 50 | | `npm run astro -- --help` | Get help using the Astro CLI | 51 | 52 | ## 👀 Want to learn more? 53 | 54 | Feel free to check [our documentation](https://docs.astro.build) or jump into our [Discord server](https://astro.build/chat). 55 | -------------------------------------------------------------------------------- /example/astro.config.mjs: -------------------------------------------------------------------------------- 1 | import { defineConfig } from "astro/config"; 2 | import htmx from "astro-htmx"; 3 | 4 | // https://astro.build/config 5 | export default defineConfig({ 6 | integrations: [htmx()], 7 | }); 8 | -------------------------------------------------------------------------------- /example/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "galactic-giant", 3 | "type": "module", 4 | "version": "0.0.1", 5 | "scripts": { 6 | "dev": "astro dev", 7 | "start": "astro dev", 8 | "build": "astro build", 9 | "preview": "astro preview", 10 | "astro": "astro" 11 | }, 12 | "dependencies": { 13 | "astro": "^4.9.2", 14 | "astro-htmx": "file:.." 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /example/public/favicon.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 9 | 10 | -------------------------------------------------------------------------------- /example/src/components/Card.astro: -------------------------------------------------------------------------------- 1 | --- 2 | interface Props { 3 | title: string; 4 | body: string; 5 | href: string; 6 | } 7 | 8 | const { href, title, body } = Astro.props; 9 | --- 10 | 11 | 22 | 62 | -------------------------------------------------------------------------------- /example/src/env.d.ts: -------------------------------------------------------------------------------- 1 | /// 2 | -------------------------------------------------------------------------------- /example/src/layouts/Layout.astro: -------------------------------------------------------------------------------- 1 | --- 2 | import { ViewTransitions } from "astro:transitions"; 3 | 4 | interface Props { 5 | title: string; 6 | } 7 | 8 | const { title } = Astro.props; 9 | --- 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | {title} 20 | 21 | 22 | 23 | 27 | 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /example/src/pages/about.astro: -------------------------------------------------------------------------------- 1 | --- 2 | import Layout from "../layouts/Layout.astro"; 3 | --- 4 | 5 | 6 |

About

7 |
8 | -------------------------------------------------------------------------------- /example/src/pages/index.astro: -------------------------------------------------------------------------------- 1 | --- 2 | import Layout from "../layouts/Layout.astro"; 3 | 4 | let isSubmitted = false; 5 | if (Astro.request.method === "POST") { 6 | isSubmitted = true; 7 | } 8 | --- 9 | 10 | 11 |
12 |
13 | {isSubmitted &&

Submitted

} 14 | 15 |
16 |
17 |
18 | -------------------------------------------------------------------------------- /example/tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "astro/tsconfigs/strict" 3 | } -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "astro-htmx", 3 | "version": "1.0.6", 4 | "license": "MIT", 5 | "type": "module", 6 | "repository": { 7 | "type": "git", 8 | "url": "https://github.com/xstevenyung/astro-htmx.git" 9 | }, 10 | "exports": { 11 | ".": "./src/index.js", 12 | "./package.json": "./package.json" 13 | }, 14 | "keywords": [ 15 | "astro", 16 | "withastro", 17 | "astro-integration", 18 | "astro-component" 19 | ], 20 | "devDependencies": { 21 | "astro": "^4.9.2", 22 | "htmx.org": "^1.9.0" 23 | }, 24 | "peerDependencies": { 25 | "astro": ">= 3.0.0", 26 | "htmx.org": ">= 1.9.0" 27 | } 28 | } -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | export default function createPlugin() { 2 | return { 3 | name: "astro-htmx", 4 | hooks: { 5 | "astro:config:setup": ({ injectScript }) => { 6 | injectScript( 7 | "page", 8 | `import * as htmx from "htmx.org"; 9 | document.addEventListener('astro:after-swap', () => { 10 | htmx.process(document.body) 11 | }) 12 | window.htmx = htmx;` 13 | ); 14 | }, 15 | }, 16 | }; 17 | } 18 | --------------------------------------------------------------------------------