@konfetti-js/react 0.0.0-stage → 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 CHANGED
@@ -1,3 +1,31 @@
1
- # Temporary Holding Version
1
+ # @konfetti-js/react
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ React hook and component for [konfetti.js](https://andreabettich.github.io/konfetti.js/). Installs `@konfetti-js/core` for you.
4
+
5
+ ```bash
6
+ npm install @konfetti-js/react
7
+ ```
8
+
9
+ ```tsx
10
+ import { useState } from 'react';
11
+ import { useKonfetti, Konfetti } from '@konfetti-js/react';
12
+
13
+ function SaveButton() {
14
+ const { cannon } = useKonfetti();
15
+ return <button onClick={() => cannon()}>Save</button>;
16
+ }
17
+
18
+ function Celebration() {
19
+ const [done, setDone] = useState(false);
20
+ return (
21
+ <>
22
+ <button onClick={() => setDone(true)}>Finish</button>
23
+ <Konfetti fire={done} preset="fireworks" onFired={() => setDone(false)} />
24
+ </>
25
+ );
26
+ }
27
+ ```
28
+
29
+ `useKonfetti()` returns stable functions and stops its `continuous()` stream on unmount. `<Konfetti>` accepts every core option as a prop and fires once each time `fire` turns true. See the [full documentation](https://github.com/andreabettich/konfetti.js#readme).
30
+
31
+ MIT licensed.
package/dist/index.cjs ADDED
@@ -0,0 +1,94 @@
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
+ Konfetti: () => Konfetti,
24
+ useKonfetti: () => useKonfetti
25
+ });
26
+ module.exports = __toCommonJS(index_exports);
27
+
28
+ // src/Konfetti.tsx
29
+ var import_core = require("@konfetti-js/core");
30
+ var import_react = require("react");
31
+ function Konfetti({ fire: shouldFire = false, preset, onFired, ...options }) {
32
+ const latest = (0, import_react.useRef)({ preset, onFired, options });
33
+ latest.current = { preset, onFired, options };
34
+ const firedRef = (0, import_react.useRef)(false);
35
+ (0, import_react.useEffect)(() => {
36
+ if (!shouldFire) {
37
+ firedRef.current = false;
38
+ return;
39
+ }
40
+ if (firedRef.current) return;
41
+ firedRef.current = true;
42
+ const { preset: preset2, onFired: onFired2, options: options2 } = latest.current;
43
+ if (preset2) {
44
+ (0, import_core.firePreset)(preset2, options2);
45
+ } else {
46
+ (0, import_core.fire)(options2);
47
+ }
48
+ onFired2?.();
49
+ }, [shouldFire]);
50
+ return null;
51
+ }
52
+
53
+ // src/useKonfetti.ts
54
+ var import_core2 = require("@konfetti-js/core");
55
+ var import_react2 = require("react");
56
+ function useKonfetti() {
57
+ const stopRef = (0, import_react2.useRef)(null);
58
+ (0, import_react2.useEffect)(
59
+ () => () => {
60
+ stopRef.current?.();
61
+ stopRef.current = null;
62
+ },
63
+ []
64
+ );
65
+ return (0, import_react2.useMemo)(
66
+ () => ({
67
+ fire: import_core2.fire,
68
+ cannon: import_core2.cannon,
69
+ explosion: import_core2.explosion,
70
+ fireworks: import_core2.fireworks,
71
+ rain: import_core2.rain,
72
+ snow: import_core2.snow,
73
+ sideCannons: import_core2.sideCannons,
74
+ pride: import_core2.pride,
75
+ continuous: (options, interval) => {
76
+ stopRef.current?.();
77
+ const stop = (0, import_core2.continuous)(options, interval);
78
+ stopRef.current = stop;
79
+ return stop;
80
+ },
81
+ reset: () => {
82
+ stopRef.current?.();
83
+ stopRef.current = null;
84
+ (0, import_core2.reset)();
85
+ }
86
+ }),
87
+ []
88
+ );
89
+ }
90
+ // Annotate the CommonJS export names for ESM import in node:
91
+ 0 && (module.exports = {
92
+ Konfetti,
93
+ useKonfetti
94
+ });
@@ -0,0 +1,71 @@
1
+ import { KonfettiOptions, PresetName } from '@konfetti-js/core';
2
+
3
+ interface KonfettiProps extends KonfettiOptions {
4
+ /** Fire when this turns true */
5
+ fire?: boolean;
6
+ /** Preset to fire; other props override the preset's values */
7
+ preset?: PresetName;
8
+ /** Called right after firing, for example to set your `fire` state back to false */
9
+ onFired?: () => void;
10
+ }
11
+ /**
12
+ * Declarative Konfetti component. Fires once each time `fire` turns true.
13
+ *
14
+ * @example
15
+ * ```tsx
16
+ * function App() {
17
+ * const [celebrate, setCelebrate] = useState(false);
18
+ *
19
+ * return (
20
+ * <>
21
+ * <button onClick={() => setCelebrate(true)}>Celebrate!</button>
22
+ * <Konfetti fire={celebrate} preset="fireworks" onFired={() => setCelebrate(false)} />
23
+ * </>
24
+ * );
25
+ * }
26
+ * ```
27
+ */
28
+ declare function Konfetti({ fire: shouldFire, preset, onFired, ...options }: KonfettiProps): null;
29
+
30
+ type FireFn = (options?: KonfettiOptions) => void;
31
+ interface UseKonfettiReturn {
32
+ /** Fire konfetti with optional options */
33
+ fire: FireFn;
34
+ /** Fire cannon preset */
35
+ cannon: FireFn;
36
+ /** Fire explosion preset */
37
+ explosion: FireFn;
38
+ /** Fire fireworks preset */
39
+ fireworks: FireFn;
40
+ /** Fire rain preset */
41
+ rain: FireFn;
42
+ /** Fire snow preset */
43
+ snow: FireFn;
44
+ /** Fire side cannons preset */
45
+ sideCannons: FireFn;
46
+ /** Fire pride preset */
47
+ pride: FireFn;
48
+ /** Start a stream (stopped automatically on unmount); returns a stop function */
49
+ continuous: (options?: KonfettiOptions, interval?: number) => () => void;
50
+ /** Clear all konfetti and stop streams */
51
+ reset: () => void;
52
+ }
53
+ /**
54
+ * React hook for konfetti.js. The returned functions are stable across renders.
55
+ *
56
+ * @example
57
+ * ```tsx
58
+ * function App() {
59
+ * const { fire, cannon } = useKonfetti();
60
+ *
61
+ * return (
62
+ * <button onClick={() => fire({ particleCount: 100 })}>
63
+ * Celebrate!
64
+ * </button>
65
+ * );
66
+ * }
67
+ * ```
68
+ */
69
+ declare function useKonfetti(): UseKonfettiReturn;
70
+
71
+ export { Konfetti, type KonfettiProps, type UseKonfettiReturn, useKonfetti };
@@ -0,0 +1,71 @@
1
+ import { KonfettiOptions, PresetName } from '@konfetti-js/core';
2
+
3
+ interface KonfettiProps extends KonfettiOptions {
4
+ /** Fire when this turns true */
5
+ fire?: boolean;
6
+ /** Preset to fire; other props override the preset's values */
7
+ preset?: PresetName;
8
+ /** Called right after firing, for example to set your `fire` state back to false */
9
+ onFired?: () => void;
10
+ }
11
+ /**
12
+ * Declarative Konfetti component. Fires once each time `fire` turns true.
13
+ *
14
+ * @example
15
+ * ```tsx
16
+ * function App() {
17
+ * const [celebrate, setCelebrate] = useState(false);
18
+ *
19
+ * return (
20
+ * <>
21
+ * <button onClick={() => setCelebrate(true)}>Celebrate!</button>
22
+ * <Konfetti fire={celebrate} preset="fireworks" onFired={() => setCelebrate(false)} />
23
+ * </>
24
+ * );
25
+ * }
26
+ * ```
27
+ */
28
+ declare function Konfetti({ fire: shouldFire, preset, onFired, ...options }: KonfettiProps): null;
29
+
30
+ type FireFn = (options?: KonfettiOptions) => void;
31
+ interface UseKonfettiReturn {
32
+ /** Fire konfetti with optional options */
33
+ fire: FireFn;
34
+ /** Fire cannon preset */
35
+ cannon: FireFn;
36
+ /** Fire explosion preset */
37
+ explosion: FireFn;
38
+ /** Fire fireworks preset */
39
+ fireworks: FireFn;
40
+ /** Fire rain preset */
41
+ rain: FireFn;
42
+ /** Fire snow preset */
43
+ snow: FireFn;
44
+ /** Fire side cannons preset */
45
+ sideCannons: FireFn;
46
+ /** Fire pride preset */
47
+ pride: FireFn;
48
+ /** Start a stream (stopped automatically on unmount); returns a stop function */
49
+ continuous: (options?: KonfettiOptions, interval?: number) => () => void;
50
+ /** Clear all konfetti and stop streams */
51
+ reset: () => void;
52
+ }
53
+ /**
54
+ * React hook for konfetti.js. The returned functions are stable across renders.
55
+ *
56
+ * @example
57
+ * ```tsx
58
+ * function App() {
59
+ * const { fire, cannon } = useKonfetti();
60
+ *
61
+ * return (
62
+ * <button onClick={() => fire({ particleCount: 100 })}>
63
+ * Celebrate!
64
+ * </button>
65
+ * );
66
+ * }
67
+ * ```
68
+ */
69
+ declare function useKonfetti(): UseKonfettiReturn;
70
+
71
+ export { Konfetti, type KonfettiProps, type UseKonfettiReturn, useKonfetti };
package/dist/index.js ADDED
@@ -0,0 +1,77 @@
1
+ // src/Konfetti.tsx
2
+ import { fire, firePreset } from "@konfetti-js/core";
3
+ import { useEffect, useRef } from "react";
4
+ function Konfetti({ fire: shouldFire = false, preset, onFired, ...options }) {
5
+ const latest = useRef({ preset, onFired, options });
6
+ latest.current = { preset, onFired, options };
7
+ const firedRef = useRef(false);
8
+ useEffect(() => {
9
+ if (!shouldFire) {
10
+ firedRef.current = false;
11
+ return;
12
+ }
13
+ if (firedRef.current) return;
14
+ firedRef.current = true;
15
+ const { preset: preset2, onFired: onFired2, options: options2 } = latest.current;
16
+ if (preset2) {
17
+ firePreset(preset2, options2);
18
+ } else {
19
+ fire(options2);
20
+ }
21
+ onFired2?.();
22
+ }, [shouldFire]);
23
+ return null;
24
+ }
25
+
26
+ // src/useKonfetti.ts
27
+ import {
28
+ cannon,
29
+ continuous as coreContinuous,
30
+ reset as coreReset,
31
+ explosion,
32
+ fire as fire2,
33
+ fireworks,
34
+ pride,
35
+ rain,
36
+ sideCannons,
37
+ snow
38
+ } from "@konfetti-js/core";
39
+ import { useEffect as useEffect2, useMemo, useRef as useRef2 } from "react";
40
+ function useKonfetti() {
41
+ const stopRef = useRef2(null);
42
+ useEffect2(
43
+ () => () => {
44
+ stopRef.current?.();
45
+ stopRef.current = null;
46
+ },
47
+ []
48
+ );
49
+ return useMemo(
50
+ () => ({
51
+ fire: fire2,
52
+ cannon,
53
+ explosion,
54
+ fireworks,
55
+ rain,
56
+ snow,
57
+ sideCannons,
58
+ pride,
59
+ continuous: (options, interval) => {
60
+ stopRef.current?.();
61
+ const stop = coreContinuous(options, interval);
62
+ stopRef.current = stop;
63
+ return stop;
64
+ },
65
+ reset: () => {
66
+ stopRef.current?.();
67
+ stopRef.current = null;
68
+ coreReset();
69
+ }
70
+ }),
71
+ []
72
+ );
73
+ }
74
+ export {
75
+ Konfetti,
76
+ useKonfetti
77
+ };
package/package.json CHANGED
@@ -1,6 +1,70 @@
1
1
  {
2
2
  "name": "@konfetti-js/react",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "0.3.0",
4
+ "description": "React 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
+ "react",
34
+ "animation"
35
+ ],
36
+ "author": "Andrea Bettich",
37
+ "license": "MIT",
38
+ "repository": {
39
+ "type": "git",
40
+ "url": "git+https://github.com/andreabettich/konfetti.js.git",
41
+ "directory": "packages/react"
42
+ },
43
+ "homepage": "https://andreabettich.github.io/konfetti.js/",
44
+ "bugs": {
45
+ "url": "https://github.com/andreabettich/konfetti.js/issues"
46
+ },
47
+ "dependencies": {
48
+ "@konfetti-js/core": "^0.3.0"
49
+ },
50
+ "peerDependencies": {
51
+ "react": ">=18.0.0"
52
+ },
53
+ "devDependencies": {
54
+ "@types/react": "^19.2.10",
55
+ "jsdom": "^30.1.2",
56
+ "react": "^19.3.0",
57
+ "react-dom": "^19.3.0",
58
+ "tsup": "^8.3.0",
59
+ "typescript": "^5.9.3",
60
+ "vitest": "^5.0.3"
61
+ },
62
+ "scripts": {
63
+ "build": "tsup",
64
+ "dev": "tsup --watch",
65
+ "test": "vitest run",
66
+ "typecheck": "tsc --noEmit",
67
+ "lint": "biome lint src",
68
+ "check": "biome check src --write"
69
+ }
6
70
  }