This React library is a wrapper around PowerGlitch. PowerGlitch is a standalone library with no external dependencies. It leverages CSS animations to glitch anything on the web, without using a canvas. It weights less than 2kb minified and gzipped.
- Original project repository and documentation
- Code coverage report for react-powerglitch
- API reference for react-powerglitch
Install → Glitch → Customize → Controls / TypeScript
-
Install
react-powerglitch
using a package managernpm i --save react-powerglitch # or yarn add react-powerglitch
-
Import the
useGlitch
hook fromreact-powerglitch
anytime you want to use it.import { useGlitch } from 'react-powerglitch'
In order to glitch something, call useGlitch()
and store its result in a variable.
function App() {
const glitch = useGlitch();
return (
<h1>react-powerglitch <span ref={glitch.ref}>🌎</span></h1>
);
}
One limitation, when having the createContainers
option set to true (which is the default), to not place the glitched element as the direct child of a component or a conditional rendering block. E.g. this will not work:
<>
{/* Will not work */}
{condition &&
<span ref={glitch.ref}>🌎</span>
}
</>
Instead, wrap the glitched element with a container:
<>
{/* Will work */}
{condition &&
<div>
<span ref={glitch.ref}>🌎</span>
</div>
}
</>
You can pass options to customize the glitch effect to useGlitch
:
function App() {
const glitch = useGlitch({ glitchTimeSpan: false });
return (
<h1>react-powerglitch <span ref={glitch.ref}>🌎</span></h1>
);
}
The options
props accept any value defined in the original PowerGlitch library.
Take a look at the playground to visually find out the best glitch options for your element.
The useGlitch
hook returns an object containing:
ref
: A function ref which you should use on the element you want to glitch, as shown in previous sections.startGlitch
: Glitch control functions to start the glitch animation.stopGlitch
: Glitch control functions to stop the glitch animation.setOptions
: A function to change the glitch options. This will update the glitched element with the new options.
Here is an example:
function App() {
const glitch = useGlitch({ glitchTimeSpan: false });
return (
<>
<div>
<h1 ref={glitch.ref}>react-powerglitch 🌎</h1>
</div>
<button onClick={glitch.startGlitch}>
Start
</button>
<button onClick={glitch.stopGlitch}>
Stop
</button>
</>
);
}
The type GlitchHandle
represents the return type of the useGlitch
hook.
Your IDE should automatically identify the return type of useGlitch
to be GlitchHandle
and assign it to any variable you assign useGlitch()
to. In case you want to statically use it, import GlitchHandle
from react-powerglitch
.
import { useGlitch, GlitchHandle } from 'react-powerglitch';
function App() {
const glitch: GlitchHandle = useGlitch({ glitchTimeSpan: false });
return (
<>
<div>
<h1 ref={glitch.ref}>react-powerglitch 🌎</h1>
</div>
<button onClick={glitch.startGlitch}>
Start
</button>
<button onClick={glitch.stopGlitch}>
Stop
</button>
</>
);
}