@konfetti-js/web-components 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Andrea Bettich
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # @konfetti-js/web-components
2
+
3
+ Custom elements for [konfetti.js](https://andreabettich.github.io/konfetti.js/), usable in any framework or plain HTML. Installs `@konfetti-js/core` for you and is safe to import during server rendering.
4
+
5
+ ```bash
6
+ npm install @konfetti-js/web-components
7
+ ```
8
+
9
+ ```html
10
+ <script type="module">
11
+ import { defineAllElements } from '@konfetti-js/web-components';
12
+ defineAllElements();
13
+ </script>
14
+
15
+ <!-- Fires from itself when clicked -->
16
+ <konfetti-trigger preset="fireworks">
17
+ <button>Click me!</button>
18
+ </konfetti-trigger>
19
+
20
+ <!-- Fire from your own code -->
21
+ <konfetti-burst id="burst" preset="cannon" particle-count="150"></konfetti-burst>
22
+ <script>
23
+ document.getElementById('burst').fire();
24
+ </script>
25
+ ```
26
+
27
+ Attributes use kebab-case option names (`particle-count`, `start-velocity`, `origin-x`, `z-index`, comma-separated `colors`). `trigger` changes the event for `<konfetti-trigger>`, and both elements dispatch `konfetti-fired`. See the [full documentation](https://github.com/andreabettich/konfetti.js#readme).
28
+
29
+ MIT licensed.
package/dist/index.cjs ADDED
@@ -0,0 +1,168 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
+ KonfettiElement: () => KonfettiElement,
24
+ KonfettiTriggerElement: () => KonfettiTriggerElement,
25
+ defineAllElements: () => defineAllElements,
26
+ defineKonfettiElement: () => defineKonfettiElement,
27
+ defineKonfettiTriggerElement: () => defineKonfettiTriggerElement
28
+ });
29
+ module.exports = __toCommonJS(index_exports);
30
+
31
+ // src/KonfettiElement.ts
32
+ var import_core2 = require("@konfetti-js/core");
33
+
34
+ // src/attributes.ts
35
+ var import_core = require("@konfetti-js/core");
36
+ var BaseElement = typeof HTMLElement === "undefined" ? class {
37
+ } : HTMLElement;
38
+ var NUMBER_ATTRIBUTES = {
39
+ "particle-count": "particleCount",
40
+ angle: "angle",
41
+ spread: "spread",
42
+ "start-velocity": "startVelocity",
43
+ decay: "decay",
44
+ gravity: "gravity",
45
+ drift: "drift",
46
+ ticks: "ticks",
47
+ scalar: "scalar",
48
+ "z-index": "zIndex"
49
+ };
50
+ function readNumber(el, name) {
51
+ const raw = el.getAttribute(name);
52
+ if (raw === null || raw.trim() === "") return void 0;
53
+ const value = Number(raw);
54
+ return Number.isFinite(value) ? value : void 0;
55
+ }
56
+ function readList(el, name) {
57
+ const raw = el.getAttribute(name);
58
+ if (!raw) return void 0;
59
+ const items = raw.split(",").map((item) => item.trim()).filter(Boolean);
60
+ return items.length > 0 ? items : void 0;
61
+ }
62
+ function readOptions(el) {
63
+ const options = {};
64
+ for (const [attribute, option] of Object.entries(NUMBER_ATTRIBUTES)) {
65
+ const value = readNumber(el, attribute);
66
+ if (value !== void 0) options[option] = value;
67
+ }
68
+ const origin = {};
69
+ const originX = readNumber(el, "origin-x");
70
+ const originY = readNumber(el, "origin-y");
71
+ if (originX !== void 0) origin.x = originX;
72
+ if (originY !== void 0) origin.y = originY;
73
+ if (Object.keys(origin).length > 0) options.origin = origin;
74
+ const colors = readList(el, "colors");
75
+ if (colors) options.colors = colors;
76
+ const shapes = readList(el, "shapes");
77
+ if (shapes) options.shapes = shapes;
78
+ return options;
79
+ }
80
+ function readPreset(el) {
81
+ const preset = el.getAttribute("preset");
82
+ return (0, import_core.isPresetName)(preset) ? preset : void 0;
83
+ }
84
+
85
+ // src/KonfettiElement.ts
86
+ var KonfettiElement = class extends BaseElement {
87
+ /** Fire konfetti with the element's attributes, optionally overridden */
88
+ fire(overrides) {
89
+ const options = { ...readOptions(this) };
90
+ for (const [key, value] of Object.entries(overrides ?? {})) {
91
+ if (value !== void 0) options[key] = value;
92
+ }
93
+ const preset = readPreset(this);
94
+ if (preset) {
95
+ (0, import_core2.firePreset)(preset, options);
96
+ } else {
97
+ (0, import_core2.fire)(options);
98
+ }
99
+ this.dispatchEvent(new CustomEvent("konfetti-fired", { detail: options }));
100
+ }
101
+ /** Clear all konfetti */
102
+ reset() {
103
+ (0, import_core2.reset)();
104
+ }
105
+ };
106
+ function defineKonfettiElement(tagName = "konfetti-burst") {
107
+ if (typeof customElements === "undefined") return;
108
+ if (!customElements.get(tagName)) {
109
+ customElements.define(tagName, KonfettiElement);
110
+ }
111
+ }
112
+
113
+ // src/KonfettiTriggerElement.ts
114
+ var import_core3 = require("@konfetti-js/core");
115
+ var KonfettiTriggerElement = class extends BaseElement {
116
+ constructor() {
117
+ super(...arguments);
118
+ /** The event the listener is currently attached to, if any */
119
+ this.boundTrigger = null;
120
+ this.handleTrigger = () => {
121
+ const options = readOptions(this);
122
+ (0, import_core3.fireFromElement)(this, { ...options, preset: readPreset(this) });
123
+ this.dispatchEvent(new CustomEvent("konfetti-fired", { detail: options }));
124
+ };
125
+ }
126
+ connectedCallback() {
127
+ this.bind();
128
+ }
129
+ disconnectedCallback() {
130
+ this.unbind();
131
+ }
132
+ attributeChangedCallback() {
133
+ if (this.isConnected) this.bind();
134
+ }
135
+ bind() {
136
+ const trigger = this.getAttribute("trigger") || "click";
137
+ if (trigger === this.boundTrigger) return;
138
+ this.unbind();
139
+ this.addEventListener(trigger, this.handleTrigger);
140
+ this.boundTrigger = trigger;
141
+ }
142
+ unbind() {
143
+ if (this.boundTrigger === null) return;
144
+ this.removeEventListener(this.boundTrigger, this.handleTrigger);
145
+ this.boundTrigger = null;
146
+ }
147
+ };
148
+ KonfettiTriggerElement.observedAttributes = ["trigger"];
149
+ function defineKonfettiTriggerElement(tagName = "konfetti-trigger") {
150
+ if (typeof customElements === "undefined") return;
151
+ if (!customElements.get(tagName)) {
152
+ customElements.define(tagName, KonfettiTriggerElement);
153
+ }
154
+ }
155
+
156
+ // src/define.ts
157
+ function defineAllElements() {
158
+ defineKonfettiElement();
159
+ defineKonfettiTriggerElement();
160
+ }
161
+ // Annotate the CommonJS export names for ESM import in node:
162
+ 0 && (module.exports = {
163
+ KonfettiElement,
164
+ KonfettiTriggerElement,
165
+ defineAllElements,
166
+ defineKonfettiElement,
167
+ defineKonfettiTriggerElement
168
+ });
@@ -0,0 +1,95 @@
1
+ import { KonfettiOptions } from '@konfetti-js/core';
2
+
3
+ /**
4
+ * Register all konfetti custom elements
5
+ *
6
+ * @example
7
+ * ```html
8
+ * <script type="module">
9
+ * import { defineAllElements } from '@konfetti-js/web-components';
10
+ * defineAllElements();
11
+ * </script>
12
+ *
13
+ * <konfetti-trigger preset="fireworks">
14
+ * <button>Click me!</button>
15
+ * </konfetti-trigger>
16
+ * ```
17
+ */
18
+ declare function defineAllElements(): void;
19
+
20
+ /**
21
+ * Base class that does not touch HTMLElement on the server, so importing this
22
+ * package during server rendering does not throw
23
+ */
24
+ declare const BaseElement: typeof HTMLElement;
25
+
26
+ /**
27
+ * Custom element for firing konfetti from your own code
28
+ *
29
+ * @example
30
+ * ```html
31
+ * <!-- Register the element -->
32
+ * <script type="module">
33
+ * import { defineKonfettiElement } from '@konfetti-js/web-components';
34
+ * defineKonfettiElement();
35
+ * </script>
36
+ *
37
+ * <!-- Use it -->
38
+ * <konfetti-burst
39
+ * particle-count="100"
40
+ * spread="70"
41
+ * preset="fireworks"
42
+ * ></konfetti-burst>
43
+ *
44
+ * <!-- Trigger via JavaScript -->
45
+ * <script>
46
+ * document.querySelector('konfetti-burst').fire();
47
+ * </script>
48
+ * ```
49
+ */
50
+ declare class KonfettiElement extends BaseElement {
51
+ /** Fire konfetti with the element's attributes, optionally overridden */
52
+ fire(overrides?: KonfettiOptions): void;
53
+ /** Clear all konfetti */
54
+ reset(): void;
55
+ }
56
+ /** Register the konfetti-burst custom element */
57
+ declare function defineKonfettiElement(tagName?: string): void;
58
+
59
+ /**
60
+ * Custom element that fires konfetti from itself on click (or the `trigger` event)
61
+ *
62
+ * @example
63
+ * ```html
64
+ * <!-- Register the element -->
65
+ * <script type="module">
66
+ * import { defineKonfettiTriggerElement } from '@konfetti-js/web-components';
67
+ * defineKonfettiTriggerElement();
68
+ * </script>
69
+ *
70
+ * <!-- Use it -->
71
+ * <konfetti-trigger particle-count="100" spread="70">
72
+ * <button>Click me!</button>
73
+ * </konfetti-trigger>
74
+ *
75
+ * <!-- With preset -->
76
+ * <konfetti-trigger preset="fireworks">
77
+ * <button>Fireworks!</button>
78
+ * </konfetti-trigger>
79
+ * ```
80
+ */
81
+ declare class KonfettiTriggerElement extends BaseElement {
82
+ static observedAttributes: string[];
83
+ /** The event the listener is currently attached to, if any */
84
+ private boundTrigger;
85
+ private readonly handleTrigger;
86
+ connectedCallback(): void;
87
+ disconnectedCallback(): void;
88
+ attributeChangedCallback(): void;
89
+ private bind;
90
+ private unbind;
91
+ }
92
+ /** Register the konfetti-trigger custom element */
93
+ declare function defineKonfettiTriggerElement(tagName?: string): void;
94
+
95
+ export { KonfettiElement, KonfettiTriggerElement, defineAllElements, defineKonfettiElement, defineKonfettiTriggerElement };
@@ -0,0 +1,95 @@
1
+ import { KonfettiOptions } from '@konfetti-js/core';
2
+
3
+ /**
4
+ * Register all konfetti custom elements
5
+ *
6
+ * @example
7
+ * ```html
8
+ * <script type="module">
9
+ * import { defineAllElements } from '@konfetti-js/web-components';
10
+ * defineAllElements();
11
+ * </script>
12
+ *
13
+ * <konfetti-trigger preset="fireworks">
14
+ * <button>Click me!</button>
15
+ * </konfetti-trigger>
16
+ * ```
17
+ */
18
+ declare function defineAllElements(): void;
19
+
20
+ /**
21
+ * Base class that does not touch HTMLElement on the server, so importing this
22
+ * package during server rendering does not throw
23
+ */
24
+ declare const BaseElement: typeof HTMLElement;
25
+
26
+ /**
27
+ * Custom element for firing konfetti from your own code
28
+ *
29
+ * @example
30
+ * ```html
31
+ * <!-- Register the element -->
32
+ * <script type="module">
33
+ * import { defineKonfettiElement } from '@konfetti-js/web-components';
34
+ * defineKonfettiElement();
35
+ * </script>
36
+ *
37
+ * <!-- Use it -->
38
+ * <konfetti-burst
39
+ * particle-count="100"
40
+ * spread="70"
41
+ * preset="fireworks"
42
+ * ></konfetti-burst>
43
+ *
44
+ * <!-- Trigger via JavaScript -->
45
+ * <script>
46
+ * document.querySelector('konfetti-burst').fire();
47
+ * </script>
48
+ * ```
49
+ */
50
+ declare class KonfettiElement extends BaseElement {
51
+ /** Fire konfetti with the element's attributes, optionally overridden */
52
+ fire(overrides?: KonfettiOptions): void;
53
+ /** Clear all konfetti */
54
+ reset(): void;
55
+ }
56
+ /** Register the konfetti-burst custom element */
57
+ declare function defineKonfettiElement(tagName?: string): void;
58
+
59
+ /**
60
+ * Custom element that fires konfetti from itself on click (or the `trigger` event)
61
+ *
62
+ * @example
63
+ * ```html
64
+ * <!-- Register the element -->
65
+ * <script type="module">
66
+ * import { defineKonfettiTriggerElement } from '@konfetti-js/web-components';
67
+ * defineKonfettiTriggerElement();
68
+ * </script>
69
+ *
70
+ * <!-- Use it -->
71
+ * <konfetti-trigger particle-count="100" spread="70">
72
+ * <button>Click me!</button>
73
+ * </konfetti-trigger>
74
+ *
75
+ * <!-- With preset -->
76
+ * <konfetti-trigger preset="fireworks">
77
+ * <button>Fireworks!</button>
78
+ * </konfetti-trigger>
79
+ * ```
80
+ */
81
+ declare class KonfettiTriggerElement extends BaseElement {
82
+ static observedAttributes: string[];
83
+ /** The event the listener is currently attached to, if any */
84
+ private boundTrigger;
85
+ private readonly handleTrigger;
86
+ connectedCallback(): void;
87
+ disconnectedCallback(): void;
88
+ attributeChangedCallback(): void;
89
+ private bind;
90
+ private unbind;
91
+ }
92
+ /** Register the konfetti-trigger custom element */
93
+ declare function defineKonfettiTriggerElement(tagName?: string): void;
94
+
95
+ export { KonfettiElement, KonfettiTriggerElement, defineAllElements, defineKonfettiElement, defineKonfettiTriggerElement };
package/dist/index.js ADDED
@@ -0,0 +1,137 @@
1
+ // src/KonfettiElement.ts
2
+ import { fire, firePreset, reset } from "@konfetti-js/core";
3
+
4
+ // src/attributes.ts
5
+ import { isPresetName } from "@konfetti-js/core";
6
+ var BaseElement = typeof HTMLElement === "undefined" ? class {
7
+ } : HTMLElement;
8
+ var NUMBER_ATTRIBUTES = {
9
+ "particle-count": "particleCount",
10
+ angle: "angle",
11
+ spread: "spread",
12
+ "start-velocity": "startVelocity",
13
+ decay: "decay",
14
+ gravity: "gravity",
15
+ drift: "drift",
16
+ ticks: "ticks",
17
+ scalar: "scalar",
18
+ "z-index": "zIndex"
19
+ };
20
+ function readNumber(el, name) {
21
+ const raw = el.getAttribute(name);
22
+ if (raw === null || raw.trim() === "") return void 0;
23
+ const value = Number(raw);
24
+ return Number.isFinite(value) ? value : void 0;
25
+ }
26
+ function readList(el, name) {
27
+ const raw = el.getAttribute(name);
28
+ if (!raw) return void 0;
29
+ const items = raw.split(",").map((item) => item.trim()).filter(Boolean);
30
+ return items.length > 0 ? items : void 0;
31
+ }
32
+ function readOptions(el) {
33
+ const options = {};
34
+ for (const [attribute, option] of Object.entries(NUMBER_ATTRIBUTES)) {
35
+ const value = readNumber(el, attribute);
36
+ if (value !== void 0) options[option] = value;
37
+ }
38
+ const origin = {};
39
+ const originX = readNumber(el, "origin-x");
40
+ const originY = readNumber(el, "origin-y");
41
+ if (originX !== void 0) origin.x = originX;
42
+ if (originY !== void 0) origin.y = originY;
43
+ if (Object.keys(origin).length > 0) options.origin = origin;
44
+ const colors = readList(el, "colors");
45
+ if (colors) options.colors = colors;
46
+ const shapes = readList(el, "shapes");
47
+ if (shapes) options.shapes = shapes;
48
+ return options;
49
+ }
50
+ function readPreset(el) {
51
+ const preset = el.getAttribute("preset");
52
+ return isPresetName(preset) ? preset : void 0;
53
+ }
54
+
55
+ // src/KonfettiElement.ts
56
+ var KonfettiElement = class extends BaseElement {
57
+ /** Fire konfetti with the element's attributes, optionally overridden */
58
+ fire(overrides) {
59
+ const options = { ...readOptions(this) };
60
+ for (const [key, value] of Object.entries(overrides ?? {})) {
61
+ if (value !== void 0) options[key] = value;
62
+ }
63
+ const preset = readPreset(this);
64
+ if (preset) {
65
+ firePreset(preset, options);
66
+ } else {
67
+ fire(options);
68
+ }
69
+ this.dispatchEvent(new CustomEvent("konfetti-fired", { detail: options }));
70
+ }
71
+ /** Clear all konfetti */
72
+ reset() {
73
+ reset();
74
+ }
75
+ };
76
+ function defineKonfettiElement(tagName = "konfetti-burst") {
77
+ if (typeof customElements === "undefined") return;
78
+ if (!customElements.get(tagName)) {
79
+ customElements.define(tagName, KonfettiElement);
80
+ }
81
+ }
82
+
83
+ // src/KonfettiTriggerElement.ts
84
+ import { fireFromElement } from "@konfetti-js/core";
85
+ var KonfettiTriggerElement = class extends BaseElement {
86
+ constructor() {
87
+ super(...arguments);
88
+ /** The event the listener is currently attached to, if any */
89
+ this.boundTrigger = null;
90
+ this.handleTrigger = () => {
91
+ const options = readOptions(this);
92
+ fireFromElement(this, { ...options, preset: readPreset(this) });
93
+ this.dispatchEvent(new CustomEvent("konfetti-fired", { detail: options }));
94
+ };
95
+ }
96
+ connectedCallback() {
97
+ this.bind();
98
+ }
99
+ disconnectedCallback() {
100
+ this.unbind();
101
+ }
102
+ attributeChangedCallback() {
103
+ if (this.isConnected) this.bind();
104
+ }
105
+ bind() {
106
+ const trigger = this.getAttribute("trigger") || "click";
107
+ if (trigger === this.boundTrigger) return;
108
+ this.unbind();
109
+ this.addEventListener(trigger, this.handleTrigger);
110
+ this.boundTrigger = trigger;
111
+ }
112
+ unbind() {
113
+ if (this.boundTrigger === null) return;
114
+ this.removeEventListener(this.boundTrigger, this.handleTrigger);
115
+ this.boundTrigger = null;
116
+ }
117
+ };
118
+ KonfettiTriggerElement.observedAttributes = ["trigger"];
119
+ function defineKonfettiTriggerElement(tagName = "konfetti-trigger") {
120
+ if (typeof customElements === "undefined") return;
121
+ if (!customElements.get(tagName)) {
122
+ customElements.define(tagName, KonfettiTriggerElement);
123
+ }
124
+ }
125
+
126
+ // src/define.ts
127
+ function defineAllElements() {
128
+ defineKonfettiElement();
129
+ defineKonfettiTriggerElement();
130
+ }
131
+ export {
132
+ KonfettiElement,
133
+ KonfettiTriggerElement,
134
+ defineAllElements,
135
+ defineKonfettiElement,
136
+ defineKonfettiTriggerElement
137
+ };
package/package.json ADDED
@@ -0,0 +1,65 @@
1
+ {
2
+ "name": "@konfetti-js/web-components",
3
+ "version": "0.3.0",
4
+ "description": "Web Components wrapper for konfetti.js",
5
+ "type": "module",
6
+ "main": "./dist/index.cjs",
7
+ "module": "./dist/index.js",
8
+ "types": "./dist/index.d.ts",
9
+ "exports": {
10
+ ".": {
11
+ "import": {
12
+ "types": "./dist/index.d.ts",
13
+ "default": "./dist/index.js"
14
+ },
15
+ "require": {
16
+ "types": "./dist/index.d.cts",
17
+ "default": "./dist/index.cjs"
18
+ }
19
+ },
20
+ "./package.json": "./package.json"
21
+ },
22
+ "sideEffects": false,
23
+ "files": [
24
+ "dist"
25
+ ],
26
+ "publishConfig": {
27
+ "access": "public",
28
+ "provenance": true
29
+ },
30
+ "keywords": [
31
+ "konfetti",
32
+ "confetti",
33
+ "web-components",
34
+ "custom-elements",
35
+ "animation"
36
+ ],
37
+ "author": "Andrea Bettich",
38
+ "license": "MIT",
39
+ "repository": {
40
+ "type": "git",
41
+ "url": "git+https://github.com/andreabettich/konfetti.js.git",
42
+ "directory": "packages/web-components"
43
+ },
44
+ "homepage": "https://andreabettich.github.io/konfetti.js/",
45
+ "bugs": {
46
+ "url": "https://github.com/andreabettich/konfetti.js/issues"
47
+ },
48
+ "dependencies": {
49
+ "@konfetti-js/core": "^0.3.0"
50
+ },
51
+ "devDependencies": {
52
+ "jsdom": "^30.1.2",
53
+ "tsup": "^8.3.0",
54
+ "typescript": "^5.9.3",
55
+ "vitest": "^5.0.3"
56
+ },
57
+ "scripts": {
58
+ "build": "tsup",
59
+ "dev": "tsup --watch",
60
+ "test": "vitest run",
61
+ "typecheck": "tsc --noEmit",
62
+ "lint": "biome lint src",
63
+ "check": "biome check src --write"
64
+ }
65
+ }