collapsed icon indicating copy to clipboard operation
collapsed copied to clipboard

Media query collapsed

Open Qudusayo opened this issue 2 years ago • 2 comments

Couldn't find a way to make the dropdown component closed on mobile and open on the desktop without transition.

It works as I expected on mobile, but due to update from useEffect, there's transition on desktop as it's set to open

  const [isExpanded, setisExpanded] = useState<boolean>(false);
  const { getCollapseProps, getToggleProps } = useCollapse({
    isExpanded,
    defaultExpanded: false,
  });

  useEffect(() => {
    if (window.innerWidth > 700) {
      setisExpanded(true);
    }
  }, []);

With this code above, there's a transition on desktop when component mounts

https://user-images.githubusercontent.com/51055890/215252211-87c1253b-74dd-43b2-bf94-3b5a3ecf8bd1.mov

Qudusayo avatar Jan 28 '23 07:01 Qudusayo

If you're not server-side rendering, you can skip the useEffect altogether:

const [isExpanded, setisExpanded] = useState<boolean>(window.innerWidth > 700);
const { getCollapseProps, getToggleProps } = useCollapse({isExpanded});

You're also not supposed to use defaultExpanded and isExpanded together, so you should leave out defaultExpanded.

You can also use the hasDisabledAnimation to enable/disable transitions at any point in the hook's lifecycle.

roginfarrer avatar Jan 30 '23 06:01 roginfarrer

Can you help with a sample code ?

Qudusayo avatar Feb 03 '23 10:02 Qudusayo