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

fix: mobile firefox inputs #489

Merged
merged 3 commits into from
Oct 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions src/browser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
export function isMobileFirefox(): boolean | undefined {
const userAgent = navigator.userAgent;
return (
typeof window !== 'undefined' &&
((/Firefox/.test(userAgent) && /Mobile/.test(userAgent)) || // Android Firefox
/FxiOS/.test(userAgent)) // iOS Firefox
);
}

export function isMac(): boolean | undefined {
return testPlatform(/^Mac/);
}

export function isIPhone(): boolean | undefined {
return testPlatform(/^iPhone/);
}

export function isSafari(): boolean | undefined {
return /^((?!chrome|android).)*safari/i.test(navigator.userAgent);
}

export function isIPad(): boolean | undefined {
return (
testPlatform(/^iPad/) ||
// iPadOS 13 lies and says it's a Mac, but we can distinguish by detecting touch support.
(isMac() && navigator.maxTouchPoints > 1)
);
}

export function isIOS(): boolean | undefined {
return isIPhone() || isIPad();
}

export function testPlatform(re: RegExp): boolean | undefined {
return typeof window !== 'undefined' && window.navigator != null ? re.test(window.navigator.platform) : undefined;
}
6 changes: 3 additions & 3 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as DialogPrimitive from '@radix-ui/react-dialog';
import React from 'react';
import { DrawerContext, useDrawerContext } from './context';
import './style.css';
import { usePreventScroll, isInput, isIOS } from './use-prevent-scroll';
import { usePreventScroll, isInput } from './use-prevent-scroll';
import { useComposedRefs } from './use-composed-refs';
import { useSnapPoints } from './use-snap-points';
import { set, getTranslate, dampenValue, isVertical, reset } from './helpers';
Expand All @@ -22,6 +22,7 @@ import { DrawerDirection } from './types';
import { useControllableState } from './use-controllable-state';
import { useScaleBackground } from './use-scale-background';
import { usePositionFixed } from './use-position-fixed';
import { isIOS, isMobileFirefox } from './browser';

export interface WithFadeFromProps {
/**
Expand Down Expand Up @@ -500,7 +501,6 @@ export function Root({
const activeSnapPointHeight = snapPointsOffset[activeSnapPointIndex] || 0;
diffFromInitial += activeSnapPointHeight;
}

previousDiffFromInitial.current = diffFromInitial;
// We don't have to change the height if the input is in view, when we are here we are in the opened keyboard state so we can correctly check if the input is in view
if (drawerHeight > visualViewportHeight || keyboardIsOpen.current) {
Expand All @@ -516,7 +516,7 @@ export function Root({
} else {
drawerRef.current.style.height = `${Math.max(newDrawerHeight, visualViewportHeight - offsetFromTop)}px`;
}
} else {
} else if (!isMobileFirefox()) {
drawerRef.current.style.height = `${initialDrawerHeight.current}px`;
}

Expand Down
2 changes: 1 addition & 1 deletion src/use-position-fixed.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';
import { isSafari } from './use-prevent-scroll';
import { isSafari } from './browser';

let previousBodyPosition: Record<string, string> | null = null;

Expand Down
29 changes: 1 addition & 28 deletions src/use-prevent-scroll.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// This code comes from https://github.com/adobe/react-spectrum/blob/main/packages/%40react-aria/overlays/src/usePreventScroll.ts

import { useEffect, useLayoutEffect } from 'react';
import { isIOS } from './browser';

const KEYBOARD_BUFFER = 24;

Expand All @@ -22,34 +23,6 @@ function chain(...callbacks: any[]): (...args: any[]) => void {
};
}

function isMac(): boolean | undefined {
return testPlatform(/^Mac/);
}

function isIPhone(): boolean | undefined {
return testPlatform(/^iPhone/);
}

export function isSafari(): boolean | undefined {
return /^((?!chrome|android).)*safari/i.test(navigator.userAgent);
}

function isIPad(): boolean | undefined {
return (
testPlatform(/^iPad/) ||
// iPadOS 13 lies and says it's a Mac, but we can distinguish by detecting touch support.
(isMac() && navigator.maxTouchPoints > 1)
);
}

export function isIOS(): boolean | undefined {
return isIPhone() || isIPad();
}

function testPlatform(re: RegExp): boolean | undefined {
return typeof window !== 'undefined' && window.navigator != null ? re.test(window.navigator.platform) : undefined;
}

// @ts-ignore
const visualViewport = typeof document !== 'undefined' && window.visualViewport;

Expand Down
Loading