Spaces:
Running
Running
File size: 9,309 Bytes
990cf29 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 |
/**
* GlowParticleEffect Component
*
* A wrapper component that adds soft glowing particle effects to its children.
* Particles spawn at click locations and float upward while fading.
*
* VISUAL ARCHITECTURE:
* ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
* β Wrapper Container (position: relative) β
* β ββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
* β β Children (note content, wikilinks, etc.) β β
* β β β β
* β β Some text with [[wikilink]] that you can click β β
* β β β β β
* β β Click here β β
* β ββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
* β ββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
* β β Canvas Overlay (position: absolute, pointer-events:β β
* β β none) β β
* β β ββββββββββ β β
* β β ββββββββββ β β
* β β ββββββββββ <- Particles float up β β
* β β β β
* β ββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
* ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
*
* VISUAL FLOW:
* 1. User clicks on wikilink (or any child element)
* 2. Click event bubbles up to wrapper
* 3. Particles spawn at click coordinates
* 4. Particles animate independently on canvas
* 5. Canvas has pointer-events: none, so clicks pass through
*
* ACCESSIBILITY:
* - Respects prefers-reduced-motion
* - Canvas is purely decorative (no interaction)
* - Does not interfere with screen readers
*/
import React, { useRef, useCallback, useEffect, useState } from 'react';
import { useGlowParticles } from '@/hooks/useGlowParticles';
import { useReducedMotion } from '@/hooks/useReducedMotion';
import type { ParticleConfig, ParticlePreset } from '@/types/particles';
import { DEFAULT_PARTICLE_CONFIG, PARTICLE_PRESETS } from '@/types/particles';
export interface GlowParticleEffectProps {
/**
* Content to wrap with particle effect
*/
children: React.ReactNode;
/**
* Particle configuration (colors, sizes, speeds, etc.)
* Can be a preset name or custom config object
*/
config?: Partial<ParticleConfig> | ParticlePreset;
/**
* CSS class for the wrapper container
*/
className?: string;
/**
* Whether particles are enabled
* Automatically disabled when prefers-reduced-motion is set
*/
enabled?: boolean;
/**
* Optional callback when particles are spawned
* Useful for analytics or sound effects
*/
onParticleBurst?: (x: number, y: number) => void;
/**
* Selector for elements that should trigger particles
* If not provided, ANY click in the container triggers particles
* Example: '.wikilink' to only trigger on wikilink clicks
*/
triggerSelector?: string;
}
/**
* Resolve config from preset name or partial config
*/
function resolveConfig(
configProp?: Partial<ParticleConfig> | ParticlePreset
): ParticleConfig {
if (!configProp) {
return DEFAULT_PARTICLE_CONFIG;
}
// If it's a string, look up the preset
if (typeof configProp === 'string') {
return PARTICLE_PRESETS[configProp] || DEFAULT_PARTICLE_CONFIG;
}
// Merge with defaults
return { ...DEFAULT_PARTICLE_CONFIG, ...configProp };
}
/**
* GlowParticleEffect Component
*
* Wraps children with an invisible canvas overlay that renders particles.
*
* USAGE:
* ```tsx
* <GlowParticleEffect config="elegant" triggerSelector=".wikilink">
* <div>Content with [[wikilinks]]</div>
* </GlowParticleEffect>
* ```
*/
export function GlowParticleEffect({
children,
config: configProp,
className = '',
enabled = true,
onParticleBurst,
triggerSelector,
}: GlowParticleEffectProps) {
// Check accessibility preference
const prefersReducedMotion = useReducedMotion();
// Resolve final configuration
const config = resolveConfig(configProp);
// Determine if particles should be active
const isDisabled = prefersReducedMotion || !enabled;
// Reference to wrapper for coordinate calculation
const wrapperRef = useRef<HTMLDivElement>(null);
// Canvas dimensions state
const [dimensions, setDimensions] = useState({ width: 0, height: 0 });
// Initialize particle system
const { canvasRef, createParticleBurst } = useGlowParticles(config, isDisabled);
/**
* UPDATE CANVAS DIMENSIONS
*
* VISUAL PURPOSE: Canvas must match container size for correct positioning.
* If canvas is smaller than container, particles would be clipped.
* If larger, particles could render in invisible area.
*
* We use ResizeObserver to handle:
* - Initial mount
* - Window resize
* - Layout changes
*/
useEffect(() => {
const wrapper = wrapperRef.current;
if (!wrapper) return;
const updateDimensions = () => {
const rect = wrapper.getBoundingClientRect();
setDimensions({
width: rect.width,
height: rect.height,
});
};
// Initial measurement
updateDimensions();
// Watch for size changes
const resizeObserver = new ResizeObserver(updateDimensions);
resizeObserver.observe(wrapper);
return () => {
resizeObserver.disconnect();
};
}, []);
/**
* HANDLE CLICK FOR PARTICLE SPAWN
*
* VISUAL FLOW:
* 1. User clicks somewhere in the wrapper
* 2. We check if click target matches triggerSelector (if provided)
* 3. We calculate click position relative to canvas
* 4. We spawn particles at that position
*
* The particles appear exactly where the user clicked,
* creating a direct visual response to the interaction.
*/
const handleClick = useCallback(
(event: React.MouseEvent<HTMLDivElement>) => {
// Skip if disabled
if (isDisabled) return;
// Check if trigger selector is specified
if (triggerSelector) {
// Find if click target matches selector (or is inside matching element)
const target = event.target as HTMLElement;
const matchingElement = target.closest(triggerSelector);
if (!matchingElement) {
// Click was not on a triggering element
return;
}
}
const wrapper = wrapperRef.current;
if (!wrapper) return;
/**
* CALCULATE RELATIVE COORDINATES
*
* VISUAL: event.clientX/Y are viewport-relative
* We need coordinates relative to the canvas origin (top-left of wrapper)
*
* rect.left/top give us the wrapper's position in the viewport
* Subtracting gives us the click position within the wrapper
*/
const rect = wrapper.getBoundingClientRect();
const x = event.clientX - rect.left;
const y = event.clientY - rect.top;
// Spawn particles at click location
createParticleBurst(x, y);
// Notify callback if provided
onParticleBurst?.(x, y);
},
[isDisabled, triggerSelector, createParticleBurst, onParticleBurst]
);
return (
<div
ref={wrapperRef}
className={`relative ${className}`}
onClick={handleClick}
>
{/* Children (the actual content) */}
{children}
{/*
CANVAS OVERLAY
VISUAL ARCHITECTURE:
- Positioned absolutely to cover the entire wrapper
- pointer-events: none allows clicks to pass through to children
- z-index ensures particles render above content
VISUAL: The canvas is invisible until particles are drawn on it.
It acts as a transparent layer floating above the content.
*/}
{!isDisabled && (
<canvas
ref={canvasRef}
width={dimensions.width}
height={dimensions.height}
className="absolute inset-0 pointer-events-none z-10"
style={{
// Ensure canvas covers entire container
width: '100%',
height: '100%',
}}
// Accessibility: Mark as decorative/presentation
role="presentation"
aria-hidden="true"
/>
)}
</div>
);
}
/**
* Re-export presets for convenience
*/
export { PARTICLE_PRESETS } from '@/types/particles';
export type { ParticleConfig, ParticlePreset } from '@/types/particles';
export default GlowParticleEffect;
|