Skip to content

Commit d13209a

Browse files
authored
feat: add htmx-toasts slots
1 parent 16d9dad commit d13209a

File tree

7 files changed

+278
-27
lines changed

7 files changed

+278
-27
lines changed

.eslintrc.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"plugin:github/typescript"
77
],
88
"globals": {
9-
"ExampleElement": "readonly"
9+
"HTMXToastsElement": "readonly"
1010
},
1111
"overrides": [
1212
{

README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,31 @@
77

88
This is a simple HTMX element that can be used to display toasts in your web application.
99

10+
## Usage
11+
12+
```html
13+
<template id="htmx-toasts-template">
14+
<div class="alert" slot="alert">
15+
<span slot="message"></span>
16+
<button type="button" class="btn btn-sm btn-outline" aria-label="Close" slot="close">Close</button>
17+
</div>
18+
</template>
19+
<htmx-toasts timeout="3000" class="toast" role="status" aria-live="polite" error-class="alert-error"
20+
info-class="alert-info" warn-class="alert-warning"></htmx-toasts>
21+
```
22+
23+
## Installation
24+
25+
```bash
26+
npm install @htmx/template-element
27+
```
28+
29+
```html
30+
<script src="https://unpkg.com/@htmx/htmx-toasts@latest/dist/index.js" type="module"></script>
31+
```
32+
33+
```bash
34+
1035
## License
1136

1237
[MIT](/LICENSE)

examples/index.html

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,22 @@
11
<!doctype html>
2-
<html>
3-
<head>
4-
<title>example-element examples</title>
5-
<link href="https://unpkg.com/[email protected]/dist/out.css" rel="stylesheet">
6-
</head>
7-
<body>
8-
<div class="container py-4">
9-
<example-element></example-element>
10-
</div>
2+
<html data-theme="light">
3+
4+
<head>
5+
<title>htmx-toasts examples</title>
6+
<link href="https://unpkg.com/[email protected]/dist/out.css" rel="stylesheet">
7+
</head>
8+
9+
<body>
10+
<div class="container py-4">
11+
<template id="htmx-toasts-template">
12+
<div class="alert" slot="alert">
13+
<span slot="message"></span>
14+
<button type="button" class="btn btn-sm btn-outline" aria-label="Close" slot="close">Close</button>
15+
</div>
16+
</template>
17+
<htmx-toasts timeout="10000" class="toast" role="status" aria-live="polite" error-class="alert-error"
18+
info-class="alert-info" warn-class="alert-warning"></htmx-toasts>
19+
<button class="btn" onclick="notify()">Notify</button>
1120
<script>
1221
const script = document.createElement('script')
1322
if (window.location.hostname.endsWith('github.io') || window.location.hostname.endsWith('github.com')) {
@@ -18,5 +27,16 @@
1827
script.type = 'module'
1928
document.body.appendChild(script)
2029
</script>
21-
</body>
30+
<script>
31+
function notify() {
32+
window.dispatchEvent(new CustomEvent('htmx-toasts:notify', {
33+
detail: {
34+
message: 'Hello, World!',
35+
level: 'error'
36+
}
37+
}))
38+
}
39+
</script>
40+
</body>
41+
2242
</html>

package-lock.json

Lines changed: 135 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
{
2-
"name": "@htmx/template-element",
2+
"name": "@htmx/htmx-toasts",
33
"version": "1.0.0",
4-
"description": "A custom element for adding a toolbar to a textarea for markdown editing.",
5-
"repository": "zeiss/template-element",
4+
"description": "A simple toast notification system for htmx",
5+
"repository": "zeiss/htmx-toasts",
66
"type": "module",
77
"main": "dist/index.js",
88
"module": "dist/index.js",
99
"types": "dist/index.d.ts",
1010
"scripts": {
1111
"clean": "rm -rf dist",
12-
"dev": "http-server",
12+
"dev": "tsc-watch --onSuccess 'http-server . --port 8080 -c-1'",
1313
"lint": "eslint src/*.ts",
1414
"prebuild": "npm run clean && npm run lint && mkdir dist",
1515
"build": "tsc",
@@ -36,6 +36,7 @@
3636
"eslint-plugin-github": "^5.0.2",
3737
"http-server": "^14.1.1",
3838
"jsdom": "^25.0.1",
39+
"tsc-watch": "^6.2.0",
3940
"typescript": "^5.6.3",
4041
"vite": "^5.4.10",
4142
"vitest": "^2.1.4"

src/index.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import {fixture, html} from '@open-wc/testing-helpers'
22
import {HTMXToastsElement} from './index'
33
import {describe, it, expect, beforeEach} from 'vitest'
44

5-
describe('example-element', () => {
5+
describe('htmx-toasts', () => {
66
let element: HTMXToastsElement
77

88
beforeEach(async () => {

0 commit comments

Comments
 (0)