@cntyclub/ui-react 0.9.0 → 0.10.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/dist/index.d.ts +60 -4
- package/dist/index.js +93 -2
- package/dist/index.js.map +1 -1
- package/package.json +3 -1
- package/src/components/ui/confetti.tsx +196 -0
- package/src/index.ts +1 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cntyclub/ui-react",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.10.0",
|
|
4
4
|
"description": "React component library for the Country Club UI Kit — Base UI primitives styled with the Country Club design system (Tailwind CSS v4)",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"sideEffects": [
|
|
@@ -39,6 +39,7 @@
|
|
|
39
39
|
"@tiptap/pm": "^3.26.1",
|
|
40
40
|
"@tiptap/react": "^3.26.1",
|
|
41
41
|
"@tiptap/starter-kit": "^3.26.1",
|
|
42
|
+
"canvas-confetti": "^1.9.3",
|
|
42
43
|
"class-variance-authority": "^0.7.1",
|
|
43
44
|
"clsx": "^2.1.1",
|
|
44
45
|
"date-fns": "^4.1.0",
|
|
@@ -64,6 +65,7 @@
|
|
|
64
65
|
"react-dom": "^19.0.0"
|
|
65
66
|
},
|
|
66
67
|
"devDependencies": {
|
|
68
|
+
"@types/canvas-confetti": "^1.9.0",
|
|
67
69
|
"@types/react": "^19.1.0",
|
|
68
70
|
"@types/react-dom": "^19.1.0",
|
|
69
71
|
"react": "^19.1.0",
|
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
import confetti from "canvas-confetti";
|
|
4
|
+
import type {
|
|
5
|
+
GlobalOptions as ConfettiGlobalOptions,
|
|
6
|
+
CreateTypes as ConfettiInstance,
|
|
7
|
+
Options as ConfettiOptions,
|
|
8
|
+
} from "canvas-confetti";
|
|
9
|
+
import {
|
|
10
|
+
createContext,
|
|
11
|
+
forwardRef,
|
|
12
|
+
useCallback,
|
|
13
|
+
useContext,
|
|
14
|
+
useEffect,
|
|
15
|
+
useImperativeHandle,
|
|
16
|
+
useMemo,
|
|
17
|
+
useRef,
|
|
18
|
+
type ReactNode,
|
|
19
|
+
} from "react";
|
|
20
|
+
import type * as React from "react";
|
|
21
|
+
|
|
22
|
+
import { Button, type ButtonProps } from "./button";
|
|
23
|
+
|
|
24
|
+
/** The brand celebration palette — soft violet / pink / peach / cream. */
|
|
25
|
+
export const CONFETTI_COLORS = ["#a786ff", "#fd8bbc", "#eca184", "#f8deb1"] as const;
|
|
26
|
+
|
|
27
|
+
function prefersReducedMotion(): boolean {
|
|
28
|
+
return (
|
|
29
|
+
typeof window !== "undefined" &&
|
|
30
|
+
!!window.matchMedia?.("(prefers-reduced-motion: reduce)").matches
|
|
31
|
+
);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export interface SideCannonsOptions {
|
|
35
|
+
/** How long the cannons keep firing, in ms (default 3000). */
|
|
36
|
+
durationMs?: number;
|
|
37
|
+
/** Confetti colors (default {@link CONFETTI_COLORS}). */
|
|
38
|
+
colors?: readonly string[];
|
|
39
|
+
/** Particles emitted per side per frame (default 2). */
|
|
40
|
+
particleCount?: number;
|
|
41
|
+
/** z-index of the confetti canvas (default 100). */
|
|
42
|
+
zIndex?: number;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Fires confetti from the left and right edges of the viewport for a few
|
|
47
|
+
* seconds — the "side cannons" celebration. Purely imperative, so it can be
|
|
48
|
+
* triggered from an effect (e.g. the first time a page loads) without rendering
|
|
49
|
+
* anything. No-ops on the server and when the user prefers reduced motion.
|
|
50
|
+
*
|
|
51
|
+
* @example
|
|
52
|
+
* useEffect(() => { sideCannons(); }, []);
|
|
53
|
+
*/
|
|
54
|
+
export function sideCannons(options: SideCannonsOptions = {}): void {
|
|
55
|
+
if (typeof window === "undefined" || prefersReducedMotion()) return;
|
|
56
|
+
|
|
57
|
+
const {
|
|
58
|
+
durationMs = 3000,
|
|
59
|
+
colors = CONFETTI_COLORS,
|
|
60
|
+
particleCount = 2,
|
|
61
|
+
zIndex = 100,
|
|
62
|
+
} = options;
|
|
63
|
+
|
|
64
|
+
const end = performance.now() + durationMs;
|
|
65
|
+
const shared = { particleCount, spread: 55, startVelocity: 60, colors: [...colors], zIndex };
|
|
66
|
+
|
|
67
|
+
const frame = () => {
|
|
68
|
+
if (performance.now() > end) return;
|
|
69
|
+
void confetti({ ...shared, angle: 60, origin: { x: 0, y: 0.5 } });
|
|
70
|
+
void confetti({ ...shared, angle: 120, origin: { x: 1, y: 0.5 } });
|
|
71
|
+
requestAnimationFrame(frame);
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
frame();
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
type ConfettiApi = {
|
|
78
|
+
/** Fire the confetti instance, merging `options` over the component's. */
|
|
79
|
+
fire: (options?: ConfettiOptions) => void;
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
export type ConfettiRef = ConfettiApi | null;
|
|
83
|
+
|
|
84
|
+
export type ConfettiProps = React.ComponentPropsWithRef<"canvas"> & {
|
|
85
|
+
/** Per-shot options merged into every `fire()` call. */
|
|
86
|
+
options?: ConfettiOptions;
|
|
87
|
+
/** Global options passed to `confetti.create` (resize / worker). */
|
|
88
|
+
globalOptions?: ConfettiGlobalOptions;
|
|
89
|
+
/** Skip the automatic fire on mount — drive it via the ref instead. */
|
|
90
|
+
manualstart?: boolean;
|
|
91
|
+
children?: ReactNode;
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
const ConfettiContext = createContext<ConfettiApi>({} as ConfettiApi);
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* A canvas-bound confetti emitter. Renders its own `<canvas>` and exposes an
|
|
98
|
+
* imperative `fire()` via ref (and to descendants through context). By default
|
|
99
|
+
* it fires once on mount; pass `manualstart` to control it yourself.
|
|
100
|
+
*
|
|
101
|
+
* For a full-screen, no-render celebration prefer {@link sideCannons}.
|
|
102
|
+
*/
|
|
103
|
+
const ConfettiComponent = forwardRef<ConfettiRef, ConfettiProps>((props, ref) => {
|
|
104
|
+
const {
|
|
105
|
+
options,
|
|
106
|
+
globalOptions = { resize: true, useWorker: true },
|
|
107
|
+
manualstart = false,
|
|
108
|
+
children,
|
|
109
|
+
...rest
|
|
110
|
+
} = props;
|
|
111
|
+
const instanceRef = useRef<ConfettiInstance | null>(null);
|
|
112
|
+
|
|
113
|
+
const canvasRef = useCallback(
|
|
114
|
+
(node: HTMLCanvasElement | null) => {
|
|
115
|
+
if (node !== null) {
|
|
116
|
+
if (instanceRef.current) return;
|
|
117
|
+
instanceRef.current = confetti.create(node, { ...globalOptions, resize: true });
|
|
118
|
+
} else if (instanceRef.current) {
|
|
119
|
+
instanceRef.current.reset();
|
|
120
|
+
instanceRef.current = null;
|
|
121
|
+
}
|
|
122
|
+
},
|
|
123
|
+
[globalOptions],
|
|
124
|
+
);
|
|
125
|
+
|
|
126
|
+
const fire = useCallback(
|
|
127
|
+
async (opts: ConfettiOptions = {}) => {
|
|
128
|
+
if (prefersReducedMotion()) return;
|
|
129
|
+
try {
|
|
130
|
+
await instanceRef.current?.({ ...options, ...opts });
|
|
131
|
+
} catch (error) {
|
|
132
|
+
console.error("Confetti error:", error);
|
|
133
|
+
}
|
|
134
|
+
},
|
|
135
|
+
[options],
|
|
136
|
+
);
|
|
137
|
+
|
|
138
|
+
const api = useMemo<ConfettiApi>(() => ({ fire }), [fire]);
|
|
139
|
+
|
|
140
|
+
useImperativeHandle(ref, () => api, [api]);
|
|
141
|
+
|
|
142
|
+
useEffect(() => {
|
|
143
|
+
if (!manualstart) void fire();
|
|
144
|
+
}, [manualstart, fire]);
|
|
145
|
+
|
|
146
|
+
return (
|
|
147
|
+
<ConfettiContext.Provider value={api}>
|
|
148
|
+
<canvas data-slot="confetti" ref={canvasRef} {...rest} />
|
|
149
|
+
{children}
|
|
150
|
+
</ConfettiContext.Provider>
|
|
151
|
+
);
|
|
152
|
+
});
|
|
153
|
+
ConfettiComponent.displayName = "Confetti";
|
|
154
|
+
|
|
155
|
+
export const Confetti = ConfettiComponent;
|
|
156
|
+
|
|
157
|
+
/** Access the nearest {@link Confetti}'s imperative `fire()` from a child. */
|
|
158
|
+
export function useConfetti(): ConfettiApi {
|
|
159
|
+
return useContext(ConfettiContext);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
export interface ConfettiButtonProps extends ButtonProps {
|
|
163
|
+
/** Confetti options; origin defaults to the button's center. */
|
|
164
|
+
options?: ConfettiOptions & ConfettiGlobalOptions & { canvas?: HTMLCanvasElement };
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* A {@link Button} that bursts confetti from its own center on click. Handy for
|
|
169
|
+
* "Claim", "Done", or other one-off celebratory actions.
|
|
170
|
+
*/
|
|
171
|
+
function ConfettiButtonComponent({ options, children, onClick, ...props }: ConfettiButtonProps) {
|
|
172
|
+
const handleClick = async (event: React.MouseEvent<HTMLButtonElement>) => {
|
|
173
|
+
onClick?.(event);
|
|
174
|
+
if (prefersReducedMotion()) return;
|
|
175
|
+
try {
|
|
176
|
+
const rect = event.currentTarget.getBoundingClientRect();
|
|
177
|
+
const x = rect.left + rect.width / 2;
|
|
178
|
+
const y = rect.top + rect.height / 2;
|
|
179
|
+
await confetti({
|
|
180
|
+
...options,
|
|
181
|
+
origin: { x: x / window.innerWidth, y: y / window.innerHeight },
|
|
182
|
+
});
|
|
183
|
+
} catch (error) {
|
|
184
|
+
console.error("Confetti button error:", error);
|
|
185
|
+
}
|
|
186
|
+
};
|
|
187
|
+
|
|
188
|
+
return (
|
|
189
|
+
<Button data-slot="confetti-button" onClick={handleClick} {...props}>
|
|
190
|
+
{children}
|
|
191
|
+
</Button>
|
|
192
|
+
);
|
|
193
|
+
}
|
|
194
|
+
ConfettiButtonComponent.displayName = "ConfettiButton";
|
|
195
|
+
|
|
196
|
+
export const ConfettiButton = ConfettiButtonComponent;
|
package/src/index.ts
CHANGED
|
@@ -27,6 +27,7 @@ export * from "./components/ui/checkbox-group";
|
|
|
27
27
|
export * from "./components/ui/collapsible";
|
|
28
28
|
export * from "./components/ui/combobox";
|
|
29
29
|
export * from "./components/ui/command";
|
|
30
|
+
export * from "./components/ui/confetti";
|
|
30
31
|
export * from "./components/ui/credit-card";
|
|
31
32
|
export * from "./components/ui/data-table-paged";
|
|
32
33
|
export * from "./components/ui/drawer";
|