Closed SajawalHassan closed 3 weeks ago
I've found a workaround for now which is simply render two different components using javascript / typescript. Instead of using css to display none on the components (which doesn't work for some reason) I am rendering two entirely different components based on the window.innerWidth
. Thought this might help!
"use client";
import { useEffect, useState } from "react";
import Particles, { initParticlesEngine } from "@tsparticles/react";
import { IOptions, RecursivePartial, type Container, type ISourceOptions } from "@tsparticles/engine";
import { loadSlim } from "@tsparticles/slim";
// import { loadFull } from "tsparticles";
import { cn } from "@/lib/utils";
interface Props {
optionsMobile: RecursivePartial<IOptions>;
optionsLaptop: RecursivePartial<IOptions>;
className?: string;
}
export const ParticleBackground = ({ optionsMobile, optionsLaptop, className }: Props) => {
const [init, setInit] = useState(false);
useEffect(() => {
initParticlesEngine(async (engine) => {
await loadSlim(engine);
}).then(() => {
setInit(true);
});
}, []);
const particlesLoaded = async (_container?: Container): Promise<void> => {};
if (init) {
return window.innerWidth > 1024 ? (
<Particles id="tsparticles" particlesLoaded={particlesLoaded} options={optionsLaptop} className={cn(className)} />
) : (
<Particles id="tsparticles" particlesLoaded={particlesLoaded} options={optionsMobile} className={cn(className)} />
);
}
return <></>;
};
I've tested this in a vanilla environment, and it worked. I don't know if it's some issues with the React environment or Next.js but nice that you find a workaround.
Contact Details
sajawalhassan.1feb@gmail.com
What happened?
Using
Problem
The responsive object does not seem to work In my options, I want to disable interactivity below a screen width of 1024px, I am trying to accomplish this by adding a responsive array in the options but it has no effect.
I am loading the options object here (being passed as props)
particles-background.tsx
Expected results
Interactivity is disabled below a screen width of 1024px
Actual results
No effect is made to interactivity at all no matter the screen size
Tried
loadFull
instead ofloadSlim
.interactivityDisabled
andinteractivityEnabled
objects positions.tsParticles Version
tsParticles Configuration
What browsers are you seeing the problem on?
Firefox, Chrome, Safari, Microsoft Edge
Relevant log output
No response
Code of Conduct