Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[mobile]: resize event on keyboard open #81

Open
nichita-pasecinic opened this issue Sep 5, 2022 · 2 comments
Open

[mobile]: resize event on keyboard open #81

nichita-pasecinic opened this issue Sep 5, 2022 · 2 comments

Comments

@nichita-pasecinic
Copy link

On mobile, the resize event is triggered on input focus (keyboard open), but this should not be the case as it does not make any sense to shift the layout when keyboard is opened.

q2: Why not using orientationchange on mobile devices too ?

@anjeypakulnevich
Copy link

anjeypakulnevich commented Sep 5, 2022

+++ have the same issue on mobile with opened keyboard (android). In my case opened keyboard (especially with extra height) triggers landscape layout.

@nichita-pasecinic
Copy link
Author

@anjeypakulnevich I've found a work around, but unfortunately is not bullet proof and for me it works in most of the cases, but just does not work on Tablets landscape (idk why)

interface Props {
  maxWidth?: number;
}

export const useVH = ({ maxWidth }: Props = {}) => {
  const isTouchDevice = useIsTouchDevice();

  const setVH = () => {
    const { innerWidth, innerHeight, outerHeight } = window;

    /** On mobile, keyboard open event triggers a `resize` that we should ignore */
    const resizeTriggeredByKeyboardOpen =
      document?.activeElement?.getAttribute('type') === 'text';

    if (!resizeTriggeredByKeyboardOpen) {
      const vh = innerHeight * 0.01;
      document.documentElement.style.setProperty('--vh', `${vh}px`);
      const vhTotal = outerHeight * 0.01;
      document.documentElement.style.setProperty('--vh-total', `${vhTotal}px`);
      const width = maxWidth && innerWidth > maxWidth ? maxWidth : innerWidth;
      document.documentElement.style.setProperty('--vw', width * 0.01 + 'px');
    }
  };

  useEffect(() => {
    const dSetVH = debounce(setVH, 150);
    setVH();

    if (isTouchDevice) window.addEventListener('orientationchange', dSetVH);
    window.addEventListener('resize', dSetVH);
    return () => {
      if (isTouchDevice)
        window.removeEventListener('orientationchange', dSetVH);
      window.removeEventListener('resize', dSetVH);
    };
  }, [maxWidth, isTouchDevice]);
};

mb it can help you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants