Skip to content

Commit df8ab43

Browse files
author
jonatansalas
committed
feat: implement support for withDefaults property, and more stories
1 parent 5bd810a commit df8ab43

File tree

2 files changed

+215
-9
lines changed

2 files changed

+215
-9
lines changed

‎src/components/Particles/Particles.tsx

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export interface ParticlesProps {
99
width?: string;
1010
height?: string;
1111
className?: string;
12+
withDefaults?: boolean;
1213
}
1314

1415
class Particles extends React.PureComponent<ParticlesProps> {
@@ -19,16 +20,14 @@ class Particles extends React.PureComponent<ParticlesProps> {
1920
width: 'auto',
2021
height: '100vh',
2122
id: 'particles-js',
23+
withDefaults: true,
2224
};
2325

2426
componentDidMount() {
2527
require('particles.js');
2628

2729
if ('particlesJS' in window) {
28-
window.particlesJS(
29-
this.props.id,
30-
merge({}, getParams(), this.props.params)
31-
);
30+
window.particlesJS(this.props.id, this.getParticles());
3231
}
3332
}
3433

@@ -38,15 +37,16 @@ class Particles extends React.PureComponent<ParticlesProps> {
3837
prevProps.id !== this.props.id &&
3938
prevProps.params !== this.props.params
4039
) {
41-
window.particlesJS(
42-
this.props.id,
43-
merge({}, getParams(), this.props.params)
44-
);
40+
window.particlesJS(this.props.id, this.getParticles());
4541
}
4642
}
4743

4844
componentWillUnmount() {
49-
if (window.pJSDom instanceof Array && window.pJSDom.length > 0) {
45+
if (
46+
'pJSDom' in window &&
47+
window.pJSDom instanceof Array &&
48+
window.pJSDom.length > 0
49+
) {
5050
window.pJSDom.forEach(({ pJS }) => pJS && pJS.fn.vendors.destroypJS());
5151
window.pJSDom = [];
5252
}
@@ -67,6 +67,14 @@ class Particles extends React.PureComponent<ParticlesProps> {
6767
/>
6868
);
6969
}
70+
71+
getParticles = () => {
72+
if (this.props.withDefaults) {
73+
return merge({}, getParams(), this.props.params);
74+
} else {
75+
return this.props.params;
76+
}
77+
};
7078
}
7179

7280
export default Particles;

‎stories/index.stories.tsx

Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,3 +258,201 @@ storiesOf('Particles', module)
258258
}}
259259
/>
260260
))
261+
.add('Nyan Cat', () => (
262+
<Particles
263+
id="nyan-cat"
264+
width="auto"
265+
height="100vh"
266+
style={{
267+
backgroundColor: 'rgb(4, 53, 100)',
268+
backgroundImage:
269+
'url("http://vincentgarreau.com/particles.js/assets/img/kbLd9vb_new.gif")',
270+
backgroundSize: '60%',
271+
backgroundRepeat: 'no-repeat',
272+
backgroundPosition: '0px 50%',
273+
}}
274+
params={{
275+
particles: {
276+
number: {
277+
value: 100,
278+
density: {
279+
enable: false,
280+
value_area: 800,
281+
},
282+
},
283+
color: {
284+
value: '#ffffff',
285+
},
286+
shape: {
287+
type: 'star',
288+
stroke: {
289+
width: 0,
290+
color: '#000000',
291+
},
292+
polygon: {
293+
nb_sides: 5,
294+
},
295+
image: {
296+
src:
297+
'http://wiki.lexisnexis.com/academic/images/f/fb/Itunes_podcast_icon_300.jpg',
298+
width: 100,
299+
height: 100,
300+
},
301+
},
302+
opacity: {
303+
value: 0.5,
304+
random: false,
305+
anim: {
306+
enable: false,
307+
speed: 1,
308+
opacity_min: 0.1,
309+
sync: false,
310+
},
311+
},
312+
size: {
313+
value: 4,
314+
random: true,
315+
anim: {
316+
enable: false,
317+
speed: 40,
318+
size_min: 0.1,
319+
sync: false,
320+
},
321+
},
322+
line_linked: {
323+
enable: false,
324+
distance: 150,
325+
color: '#ffffff',
326+
opacity: 0.4,
327+
width: 1,
328+
},
329+
move: {
330+
enable: true,
331+
speed: 14,
332+
direction: 'left',
333+
random: false,
334+
straight: true,
335+
out_mode: 'out',
336+
bounce: false,
337+
attract: {
338+
enable: false,
339+
rotateX: 600,
340+
rotateY: 1200,
341+
},
342+
},
343+
},
344+
interactivity: {
345+
detect_on: 'canvas',
346+
events: {
347+
onhover: {
348+
enable: false,
349+
mode: 'grab',
350+
},
351+
onclick: {
352+
enable: true,
353+
mode: 'repulse',
354+
},
355+
resize: true,
356+
},
357+
modes: {
358+
grab: {
359+
distance: 200,
360+
line_linked: {
361+
opacity: 1,
362+
},
363+
},
364+
bubble: {
365+
distance: 400,
366+
size: 40,
367+
duration: 2,
368+
opacity: 8,
369+
speed: 3,
370+
},
371+
repulse: {
372+
distance: 200,
373+
duration: 0.4,
374+
},
375+
push: {
376+
particles_nb: 4,
377+
},
378+
remove: {
379+
particles_nb: 2,
380+
},
381+
},
382+
},
383+
retina_detect: true,
384+
}}
385+
/>
386+
))
387+
.add('Shinning', () => (
388+
<Particles
389+
id="shine"
390+
width="auto"
391+
height="100vh"
392+
withDefaults={false}
393+
style={{
394+
backgroundColor: 'blue',
395+
}}
396+
params={{
397+
fps_limit: 28,
398+
particles: {
399+
number: {
400+
value: 200,
401+
density: {
402+
enable: false,
403+
},
404+
},
405+
line_linked: {
406+
enable: true,
407+
distance: 30,
408+
opacity: 0.4,
409+
},
410+
move: {
411+
speed: 1,
412+
},
413+
opacity: {
414+
anim: {
415+
enable: true,
416+
opacity_min: 0.05,
417+
speed: 2,
418+
sync: false,
419+
},
420+
value: 0.4,
421+
},
422+
},
423+
polygon: {
424+
enable: true,
425+
scale: 0.5,
426+
type: 'inline',
427+
move: {
428+
radius: 10,
429+
},
430+
url: 'https://rpj.bembi.org/small-deer.2a0425af.svg',
431+
inline: {
432+
arrangement: 'equidistant',
433+
},
434+
draw: {
435+
enable: true,
436+
stroke: {
437+
color: 'rgba(255, 255, 255, .2)',
438+
},
439+
},
440+
},
441+
retina_detect: false,
442+
interactivity: {
443+
events: {
444+
onhover: {
445+
enable: true,
446+
mode: 'bubble',
447+
},
448+
},
449+
modes: {
450+
bubble: {
451+
size: 6,
452+
distance: 40,
453+
},
454+
},
455+
},
456+
}}
457+
/>
458+
));

0 commit comments

Comments
 (0)