kbar icon indicating copy to clipboard operation
kbar copied to clipboard

Using `autofocus` on an `input` outside or inside the kbar components has focus stolen away

Open aaronbushnell opened this issue 3 years ago • 3 comments

If a page renders an <input> tag that uses autofocus kbar will steal the focus away. Some example code below and a CodeSandbox replicating this issue.

import {
  KBarProvider,
  KBarPortal,
  KBarPositioner,
  KBarAnimator,
  KBarSearch,
  KBarResults,
  useMatches
} from "kbar";

function RenderResults() {
  const { results } = useMatches();

  return (
    <KBarResults
      items={results}
      onRender={({ item, active }) =>
        typeof item === "string" ? (
          <div>{item}</div>
        ) : (
          <div
            style={{
              background: active ? "#eee" : "transparent"
            }}
          >
            {item.name}
          </div>
        )
      }
    />
  );
}

export default function App() {
  const actions = [
    {
      id: "blog",
      name: "Blog",
      shortcut: ["b"],
      keywords: "writing words",
      perform: () => (window.location.pathname = "blog")
    },
    {
      id: "contact",
      name: "Contact",
      shortcut: ["c"],
      keywords: "email",
      perform: () => (window.location.pathname = "contact")
    }
  ];
  return (
    <div>
      {/* An outside input does not autofocus */}
      <input type="text" autoFocus />

      <KBarProvider actions={actions}>
        <KBarPortal>
          <KBarPositioner>
            <KBarAnimator>
              <KBarSearch />
              <RenderResults />
            </KBarAnimator>
          </KBarPositioner>
        </KBarPortal>

        {/* An inside input does not autofocus either */}
        <input type="text" autoFocus />
      </KBarProvider>
    </div>
  );
}

aaronbushnell avatar Feb 07 '22 21:02 aaronbushnell

We'll likely want to skip the first effect run here: https://github.com/timc1/kbar/blob/805b6ad442124a3ab9ed4c4c1292ac8ce4c50a8d/src/InternalEvents.tsx#L214

Thanks @aaronbushnell!

timc1 avatar Feb 08 '22 03:02 timc1

Hey! This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Jun 08 '22 06:06 stale[bot]

Hey! This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Aug 07 '22 13:08 stale[bot]

why this issue is closed? when kbar is mounted my input call onBlur so ui is break up

do you have plan to fix this? @timc1

flex-hyuntae avatar Nov 04 '22 06:11 flex-hyuntae

@flex-hyuntae check out the latest release here.

timc1 avatar Nov 09 '22 20:11 timc1

@flex-hyuntae check out the latest release here.

i cannot find about focus in your link here. Can you give me the exact link?

flex-hyuntae avatar Nov 10 '22 01:11 flex-hyuntae

@flex-hyuntae That link is for the latest release; please see https://github.com/timc1/kbar/releases/tag/v0.1.0-beta.38.

timc1 avatar Nov 10 '22 02:11 timc1