Skip to content

Instantly share code, notes, and snippets.

@ryanflorence
Last active February 22, 2020 16:42
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryanflorence/28a8d96e39ed3cbb661c5e28a48fadac to your computer and use it in GitHub Desktop.
Save ryanflorence/28a8d96e39ed3cbb661c5e28a48fadac to your computer and use it in GitHub Desktop.
function Expletives({ children }) {
let [, forceUpdate] = useState();
let mounted = useRef(false);
useEffect(() => {
mounted.current = true;
// don't want to cause any seizures.
if (window.matchMedia("(prefers-reduced-motion: reduce)").matches) {
return;
}
let timeout = setTimeout(() => {
forceUpdate({});
}, 200);
return () => clearTimeout(timeout);
});
let letters =
// don't do these tricks intially so server/client hydration matches
mounted.current === false
? children
: children.split("").map((letter, index) => {
let color = Math.round(Math.random() * 360);
let char =
Math.random() < 0.75 ? letter.toLowerCase() : letter.toUpperCase();
return (
<span style={{ color: `hsl(${color}, 30%, 50%)` }} key={index}>
{char}
</span>
);
});
return <b>{letters}</b>;
}
@zant
Copy link

zant commented Feb 22, 2020

Nice thing watching for reduced motion

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment