File size: 16,291 Bytes
fea495a | 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 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 | # unicornstudio-react
[](https://badge.fury.io/js/unicornstudio-react)
[](https://www.npmjs.com/package/unicornstudio-react)
[](https://opensource.org/licenses/MIT)
[](http://www.typescriptlang.org/)
[](https://reactjs.org/)
[](https://nextjs.org/)
[](https://sonarcloud.io/summary/new_code?id=diegopeixoto_unicornstudio-react)
[](https://stackblitz.com/edit/unicornstudio-react-example?file=app%2Fpage.tsx)
A React component for embedding [Unicorn.Studio](https://unicorn.studio) interactive scenes in your applications. Compatible with both React (Vite) and Next.js frameworks.
> โ ๏ธ **Important**: This package is a community-created wrapper component and is **not officially affiliated** with Unicorn.Studio. It depends on Unicorn.Studio's proprietary script and services.
## Features
- ๐ **Easy Integration** - Simple React component for Next.js projects
- ๐จ **Full TypeScript Support** - Complete type definitions included
- โก **Performance Optimized** - Lazy loading and efficient resource management
- ๐ฑ **Responsive** - Works seamlessly across devices
- ๐ฎ **Interactive** - Support for mouse/touch interactions
- ๐ง **Flexible** - Extensive customization options
- ๐งน **Clean Architecture** - Proper cleanup and memory management
- ๐ก๏ธ **Error Handling** - Comprehensive error states and fallbacks
## Installation
```bash
npm install unicornstudio-react
# or
yarn add unicornstudio-react
# or
pnpm add unicornstudio-react
```
## Usage
This package supports both React (Vite/CRA) and Next.js environments with optimized imports:
### For React (Vite, Create React App, etc.)
Use the default import for standard React applications:
```tsx
import UnicornScene from "unicornstudio-react";
export default function MyComponent() {
return (
<UnicornScene projectId="YOUR_PROJECT_EMBED_ID" width={800} height={600} />
);
}
```
### For Next.js
Use the Next.js-optimized version with enhanced performance:
```tsx
import UnicornScene from "unicornstudio-react/next";
export default function MyComponent() {
return (
<UnicornScene projectId="YOUR_PROJECT_EMBED_ID" width={800} height={600} />
);
}
```
> **Note**: The Next.js version uses Next.js `Script` and `Image` components for better performance and optimization. The React version uses standard `<script>` and `` elements for broader compatibility.
### With Custom JSON File
**React (Vite/CRA):**
```tsx
import UnicornScene from "unicornstudio-react";
export default function MyComponent() {
return (
<UnicornScene
jsonFilePath="/path/to/your/scene.json"
width="100%"
height="400px"
scale={0.8}
dpi={2}
/>
);
}
```
**Next.js:**
```tsx
import UnicornScene from "unicornstudio-react/next";
export default function MyComponent() {
return (
<UnicornScene
jsonFilePath="/path/to/your/scene.json"
width="100%"
height="400px"
scale={0.8}
dpi={2}
/>
);
}
```
### Advanced Configuration
Both React and Next.js versions support the same props:
```tsx
// For React: import UnicornScene from "unicornstudio-react";
// For Next.js: import UnicornScene from "unicornstudio-react/next";
import UnicornScene from "unicornstudio-react";
export default function MyComponent() {
const handleLoad = () => {
console.log("Scene loaded successfully!");
};
const handleError = (error: Error) => {
console.error("Scene loading failed:", error);
};
return (
<UnicornScene
projectId="YOUR_PROJECT_EMBED_ID"
width="100vw"
height="100vh"
scale={1}
dpi={1.5}
fps={60}
altText="Interactive 3D scene"
ariaLabel="Animated background scene"
className="my-custom-class"
lazyLoad={true}
production={true}
onLoad={handleLoad}
onError={handleError}
/>
);
}
```
### Accessing the Scene Instance
Use the `sceneRef` prop when you want access to the underlying Unicorn Studio scene object after it initializes.
```tsx
import { useRef } from "react";
import UnicornScene, { type UnicornStudioScene } from "unicornstudio-react";
export default function MyComponent() {
const sceneRef = useRef<UnicornStudioScene | null>(null);
const pauseScene = () => {
if (sceneRef.current) {
sceneRef.current.paused = true;
}
};
const resizeScene = () => {
sceneRef.current?.resize?.();
};
return (
<>
<UnicornScene
projectId="YOUR_PROJECT_EMBED_ID"
width={800}
height={600}
sceneRef={sceneRef}
/>
<button onClick={pauseScene}>Pause</button>
<button onClick={resizeScene}>Resize</button>
</>
);
}
```
The `sceneRef` value is assigned once the scene finishes loading and is cleared automatically if the scene is destroyed or re-initialized.
## Placeholder Support
The component now supports flexible placeholder options that can be displayed while loading, on error, or when WebGL is not supported.
### Image Placeholder
```tsx
<UnicornScene
projectId="YOUR_PROJECT_ID"
placeholder="/path/to/placeholder.jpg"
width={800}
height={600}
/>
```
### CSS/Tailwind Placeholder
```tsx
<UnicornScene
projectId="YOUR_PROJECT_ID"
placeholderClassName="bg-gradient-to-r from-blue-500 to-purple-600 animate-pulse"
width="100%"
height="400px"
/>
```
### Custom React Component Placeholder
```tsx
<UnicornScene
projectId="YOUR_PROJECT_ID"
placeholder={
<div className="flex items-center justify-center h-full bg-gray-100">
<div className="text-center">
<div className="animate-spin rounded-full h-12 w-12 border-b-2 border-gray-900 mx-auto"></div>
<p className="mt-4 text-gray-600">Loading 3D Scene...</p>
</div>
</div>
}
width={600}
height={400}
/>
```
### Placeholder Behavior Options
- `showPlaceholderWhileLoading`: Shows placeholder during scene initialization (default: `true`)
- `showPlaceholderOnError`: Shows placeholder when scene fails to load (default: `true`)
- The placeholder automatically shows when WebGL is not supported
## Props
| Prop | Type | Default | Description |
| ----------------------------- | --------------------------------- | --------- | -------------------------------------------------------------------------- |
| `projectId` | `string` | - | The Unicorn Studio project embed ID (required if not using `jsonFilePath`) |
| `jsonFilePath` | `string` | - | Path to a self-hosted JSON file (required if not using `projectId`) |
| `width` | `number \| string` | `"100%"` | Width of the scene container |
| `height` | `number \| string` | `"100%"` | Height of the scene container |
| `scale` | `number` | `1` | Rendering scale (0.25-1, lower values improve performance) |
| `dpi` | `number` | `1.5` | Pixel ratio for rendering quality |
| `fps` | `number` | `60` | Frames per second (0-120) |
| `altText` | `string` | `"Scene"` | Alternative text for accessibility |
| `ariaLabel` | `string` | - | ARIA label for the scene |
| `className` | `string` | `""` | Additional CSS classes |
| `lazyLoad` | `boolean` | `true` | Load scene only when scrolled into view |
| `production` | `boolean` | `true` | Use production CDN |
| `paused` | `boolean` | `false` | Pause or resume the scene animation |
| `placeholder` | `string \| ReactNode` | - | Placeholder content (image URL or React component) |
| `placeholderClassName` | `string` | - | CSS classes for placeholder div (when using CSS placeholder) |
| `showPlaceholderOnError` | `boolean` | `true` | Show placeholder when scene fails to load |
| `showPlaceholderWhileLoading` | `boolean` | `true` | Show placeholder while scene is loading |
| `onLoad` | `() => void` | - | Callback when scene loads successfully |
| `onError` | `(error: Error) => void` | - | Callback when scene fails to load |
| `sceneRef` | `Ref<UnicornStudioScene \| null>` | - | Ref that receives the initialized Unicorn Studio scene instance |
## Styling
The component uses inline styles for maximum compatibility. You can customize the appearance by:
1. **Using the `className` prop** to add your own CSS classes
2. **Wrapping the component** in a styled container
3. **Using CSS variables** for dynamic dimensions:
```tsx
<div style={{ "--unicorn-width": "100%", "--unicorn-height": "500px" }}>
<UnicornScene projectId="YOUR_PROJECT_ID" />
</div>
```
## Getting Your Project ID
1. Create your scene at [Unicorn Studio](https://unicorn.studio)
2. Click on "Embed" in the export options
3. Copy the project ID from the embed code
## Framework Compatibility
### React Version (Default)
- โ
**Vite** - Optimized for modern React development
- โ
**Create React App (CRA)** - Classic React setup
- โ
**Webpack** - Custom React builds
- โ
**Parcel** - Zero-configuration bundler
- โ
**Rollup** - ES modules bundler
### Next.js Version (`/next`)
- โ
**Next.js 13+** - App Router and Pages Router
- โ
**Next.js 14+** - Latest features and optimizations
- โ
**Vercel deployment** - Optimized hosting
- โ
**Static exports** - JAMstack compatibility
## Dependencies & Requirements
### Unicorn.Studio Script Dependency
This component **depends on Unicorn.Studio's proprietary script** (`unicornStudio.umd.js`) which:
- โ
Is automatically loaded from Unicorn.Studio's official CDN
- โ Is **NOT bundled** or stored in this package
- ๐ข Is **owned and hosted** by Unicorn.Studio (UNCRN LLC)
- โ๏ธ Is subject to [Unicorn.Studio's Terms of Service](https://unicorn.studio/terms)
### Custom Script URL (Advanced)
```tsx
// โ ๏ธ Use custom script URL (may violate Unicorn.Studio TOS)
// Only use if you have explicit permission from Unicorn.Studio
const constants = {
UNICORN_STUDIO_CDN_URL: "https://your-custom-cdn.com/unicornStudio.umd.js",
};
```
**Warning**: Using a custom script URL may violate Unicorn.Studio's Terms of Service. Consult their legal terms before implementing.
**React/Next.js Example usage:**
```tsx
<UnicornScene
projectId="YOUR_PROJECT_EMBED_ID"
sdkUrl="https://your-custom-cdn.com/unicornStudio.umd.js"
width={800}
height={600}
/>
```
### Browser Support
- Chrome (latest)
- Firefox (latest)
- Safari (latest)
- Edge (latest)
- Mobile browsers with WebGL support
## Versioning Strategy
This package's version **follows Unicorn.Studio's script version** (e.g., v1.4.26) to ensure compatibility. Version format:
- `1.4.26` - Matches Unicorn.Studio script v1.4.26
- Patch versions (e.g., `1.4.26-1`) may be added for component-specific fixes
## Performance Tips
1. **Use appropriate scale values** - Lower values (0.5-0.8) can significantly improve performance on lower-end devices
2. **Enable lazy loading** - Scenes will only initialize when visible
3. **Optimize your scenes** - Keep texture sizes reasonable in Unicorn Studio
4. **Set appropriate DPI** - Use lower DPI values for better performance
## Troubleshooting
### Scene not loading
- Verify your project ID is correct
- Check network requests for the Unicorn Studio script
- Ensure you're not mixing `projectId` and `jsonFilePath`
### Performance issues
- Reduce the `scale` prop value
- Lower the `dpi` setting
- Decrease `fps` for less demanding animations
### TypeScript errors
- Ensure you have the latest version installed
- Check that your tsconfig includes the necessary DOM types
## Development & Architecture
This component is based on the [official Unicorn.Studio React example](https://github.com/hiunicornstudio/unicornstudio.js/blob/main/components/unicornstudio-react.tsx) with the following improvements:
### Enhancements Over Official Example
1. **Modern React Patterns**
- Custom hooks for script loading and scene management
- Proper TypeScript integration with full type definitions
- Next.js `Script` component for optimized loading
2. **Better Architecture**
- Separation of concerns (hooks, types, constants)
- Proper cleanup and memory management
- Error boundaries and comprehensive error handling
3. **Enhanced Developer Experience**
- Complete TypeScript support
- Inline styles for zero-config styling
- Extensive prop customization options
- npm package distribution
4. **Production Ready**
- Proper build pipeline with tsup
- CommonJS and ESM support
- Automated script deduplication
- Comprehensive documentation
## Legal & Licensing
### This Package (MIT License)
This React component wrapper is released under the **MIT License** - see the [LICENSE](LICENSE) file for details.
### Unicorn.Studio Dependencies
- **Unicorn.Studio Script**: Proprietary software owned by UNCRN LLC
- **Terms**: Subject to [Unicorn.Studio's Terms of Service](https://unicorn.studio/terms)
- **Licensing**: Commercial use governed by Unicorn.Studio's licensing terms
### Disclaimer
This package is a **community-created wrapper** and is **not officially affiliated** with Unicorn.Studio or UNCRN LLC. The authors of this package:
- Do not store, distribute, or modify Unicorn.Studio's proprietary scripts
- Are not responsible for Unicorn.Studio's service availability or performance
- Recommend users comply with Unicorn.Studio's Terms of Service
- Provide this wrapper "as-is" without warranty
For official support or licensing questions regarding Unicorn.Studio, contact [Unicorn.Studio directly](https://unicorn.studio/contact).
## Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
### Development Setup
```bash
git clone https://github.com/diegopeixoto/unicornstudio-react.git
cd unicornstudio-react
npm install
npm run dev
```
## Links
- [Unicorn Studio](https://unicorn.studio) - Official website
- [Unicorn Studio Documentation](https://unicorn.studio/docs) - Official docs
- [GitHub Repository](https://github.com/diegopeixoto/unicornstudio-react) - This package
- [npm Package](https://www.npmjs.com/package/unicornstudio-react) - npm registry
---
**Author**: Diego Peixoto
**License**: MIT (for this wrapper component only)
**Not affiliated with**: Unicorn.Studio or UNCRN LLC
|