I have a custom component, an animated hamburger menu button. Here’s the code in React:
import { useState } from "react";
import styles from "./HamburgerButton.module.css";
export const HamburgerButton = ({
color: backgroundColor,
}: {
color: string;
}) => {
const [checked, setChecked] = useState(false);
const handleChange = () => setChecked(!checked);
return (
<>
<input
type="checkbox"
id={styles.checkbox}
checked={checked}
onChange={handleChange}
/>
<label className={styles.label} htmlFor={styles.checkbox}>
<div style={{ backgroundColor }}></div>
<div style={{ backgroundColor }}></div>
<div style={{ backgroundColor }}></div>
</label>
</>
);
};
I’ve placed my button along with a mobile menu inside my Builder page. Then I:
- Added a click handler to the hamburger menu button, which toggles the boolean
state.isMobileMenuOpen
- Bound the mobile menu’s
transform
CSS property to depend on the value ofstate.isMobileMenuOpen
:state.isMobileMenuOpen ? 'translateX(0)' : 'translateX(-100%)'
. The idea is that the mobile menu will slide in from the left when open and slide back when it’s closed. Pretty standard.
Everything works great inside the Visual Editor. However, when I click on the hamburger menu button on my actual page, the binding callback seems to fire twice:
Since it’s toggling the value of isMobileMenuOpen
, the result is that my menu never opens, because it goes from true
to false
very rapidly.
How do I fix it and what’s the root cause? And why does it work inside the Visual Editor but not on my actual page?
Builder content link