Skip to content

A full-featured CRM built with React, shadcn/ui, and Supabase.

Fix global search input doesn't get focused on Mobile

fzaninotto50239533b2c0
diff --git a/src/components/atomic-crm/search/GlobalSearchButton.tsx b/src/components/atomic-crm/search/GlobalSearchButton.tsx
index b037bc96..61b86f4e 100644
--- a/src/components/atomic-crm/search/GlobalSearchButton.tsx
+++ b/src/components/atomic-crm/search/GlobalSearchButton.tsx
@@ -1,7 +1,8 @@
import { useQuery } from "@tanstack/react-query";
import { ChevronDown, Search } from "lucide-react";
import { useDataProvider, useDebouncedEvent, useTranslate } from "ra-core";
-import { useEffect, useMemo, useState } from "react";
+import { type RefObject, useEffect, useMemo, useRef, useState } from "react";
+import { flushSync } from "react-dom";
import { useNavigate } from "react-router";
import { Button } from "@/components/ui/button";
@@ -69,6 +70,15 @@ export const GlobalSearchButton = ({
}) => {
const translate = useTranslate();
const [open, setOpen] = useState(false);
+ const inputRef = useRef<HTMLInputElement>(null);
+
+ // iOS only opens the virtual keyboard for a focus made during the tap
+ // itself, while Base UI focuses the dialog a frame later: render it now and
+ // focus its input before the click handler returns.
+ const openDialog = () => {
+ flushSync(() => setOpen(true));
+ inputRef.current?.focus();
+ };
useEffect(() => {
const onKeyDown = (event: KeyboardEvent) => {
@@ -93,7 +103,7 @@ export const GlobalSearchButton = ({
size="icon"
// same shape and icon size as its neighbour, MobileRefreshButton
className="rounded-full"
- onClick={() => setOpen(true)}
+ onClick={openDialog}
aria-label={translate("crm.search.title")}
>
<Search className="size-5" />
@@ -103,7 +113,7 @@ export const GlobalSearchButton = ({
// real input lives in the dialog it opens.
<Button
variant="outline"
- onClick={() => setOpen(true)}
+ onClick={openDialog}
aria-label={translate("crm.search.title")}
aria-keyshortcuts="Control+K Meta+K"
className="mr-2 w-40 justify-start bg-background px-3 font-normal text-muted-foreground lg:w-64"
@@ -117,7 +127,11 @@ export const GlobalSearchButton = ({
</kbd>
</Button>
)}
- <GlobalSearchDialog open={open} onOpenChange={setOpen} />
+ <GlobalSearchDialog
+ open={open}
+ onOpenChange={setOpen}
+ inputRef={inputRef}
+ />
</>
);
};
@@ -188,9 +202,11 @@ const Highlighted = ({ text, terms }: { text: string; terms: string[] }) =>
export const GlobalSearchDialog = ({
open,
onOpenChange,
+ inputRef,
}: {
open: boolean;
onOpenChange: (open: boolean) => void;
+ inputRef?: RefObject<HTMLInputElement | null>;
}) => {
const translate = useTranslate();
const navigate = useNavigate();
@@ -267,6 +283,7 @@ export const GlobalSearchDialog = ({
className="[&_[cmdk-group-heading]]:text-muted-foreground **:data-[slot=command-input-wrapper]:h-12 [&_[cmdk-group-heading]]:px-2 [&_[cmdk-group-heading]]:font-medium [&_[cmdk-group]]:px-2 [&_[cmdk-group]:not([hidden])_~[cmdk-group]]:pt-0 [&_[cmdk-input-wrapper]_svg]:h-5 [&_[cmdk-input-wrapper]_svg]:w-5 [&_[cmdk-input]]:h-12 [&_[cmdk-item]]:px-2 [&_[cmdk-item]]:py-3 [&_[cmdk-item]_svg]:h-5 [&_[cmdk-item]_svg]:w-5"
>
<CommandInput
+ ref={inputRef}
placeholder={translate("crm.search.placeholder")}
value={query}
onValueChange={(value) => {