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: auto focus mobile #14

Merged
merged 1 commit into from
Dec 30, 2023
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
12 changes: 6 additions & 6 deletions src/components/Shell/Shell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Clear } from '@/components/Cli/cmd/clear';
import { PromptPrefix } from '@/components/Shell/PromptPrefix';
import { PromptHistoryContext } from '@/context/PromptHistoryContext/PromptHistoryContext';
import { PromptHistoryEntry } from '@/context/PromptHistoryContext/types';
import { useMobileDetect } from '@/hooks';
import { useTouchDetect } from '@/hooks';
import { CustomEvents, RunEvent, SearchParams } from '@/util/types';
import { useLocation } from '@reach/router';
import {
Expand All @@ -28,7 +28,7 @@ export const Shell = ({ username, domain }: Readonly<ShellProps>) => {
const [currentPrompt, setCurrentPrompt] = useState<string>(``);
const [tmpPrompt, setTmpPrompt] = useState<string>(``); // used for arrow up/down
const [isStandaloneOpen, setIsStandaloneOpen] = useState(false);
const isMobile = useMobileDetect();
const isTouch = useTouchDetect();

const location = useLocation();

Expand All @@ -43,11 +43,11 @@ export const Shell = ({ username, domain }: Readonly<ShellProps>) => {
setHistory((history) => [...history, cmdResTuple]);
updateCmdSearchParam(cmdResTuple.prompt);
}
if (!isMobile) {
if (!isTouch) {
textAreaRef.current?.focus();
}
},
[setHistory],
[isTouch, setHistory],
);

const handleClearEvent = useCallback(() => {
Expand Down Expand Up @@ -185,7 +185,7 @@ export const Shell = ({ username, domain }: Readonly<ShellProps>) => {
if (
e.target instanceof HTMLDivElement &&
e.target.nodeName === `MAIN` &&
!isMobile
!isTouch
) {
textAreaRef.current?.focus();
}
Expand Down Expand Up @@ -213,7 +213,7 @@ export const Shell = ({ username, domain }: Readonly<ShellProps>) => {
<textarea
id="prompt"
rows={1}
autoFocus={!isMobile}
autoFocus={!isTouch}
ref={textAreaRef}
onKeyDown={handleUserTextAreaKeyDown}
onChange={handleUserTextValueChange}
Expand Down
6 changes: 3 additions & 3 deletions src/hooks/useMobileDetect/useMobileDetect.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { useEffect, useState } from 'react';

export const useMobileDetect = () => {
const [isMobile, setIsMobile] = useState(false);
export const useTouchDetect = () => {
const [isMobile, setIsMobile] = useState(true);

useEffect(() => {
const handleResize = () => {
setIsMobile(window.innerWidth < 768); // 768px is a common breakpoint for mobile devices
setIsMobile(`ontouchstart` in window);
};

window.addEventListener(`resize`, handleResize);
Expand Down
Loading