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

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

Closed
aaronbushnell opened this issue Feb 7, 2022 · 7 comments · Fixed by #254
Labels
bug Something isn't working

Comments

@aaronbushnell
Copy link

aaronbushnell commented Feb 7, 2022

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>
  );
}
@timc1 timc1 added the bug Something isn't working label Feb 8, 2022
@timc1
Copy link
Owner

timc1 commented Feb 8, 2022

We'll likely want to skip the first effect run here:

React.useEffect(() => {

Thanks @aaronbushnell!

@stale stale bot added the wontfix This will not be worked on label Apr 9, 2022
@timc1 timc1 removed the wontfix This will not be worked on label Apr 9, 2022
Repository owner deleted a comment from stale bot Apr 9, 2022
@stale
Copy link

stale bot commented Jun 8, 2022

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 stale bot added the wontfix This will not be worked on label Jun 8, 2022
@timc1 timc1 removed the wontfix This will not be worked on label Jun 8, 2022
@stale
Copy link

stale bot commented Aug 7, 2022

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 stale bot added the wontfix This will not be worked on label Aug 7, 2022
@stale stale bot closed this as completed Aug 14, 2022
@flex-hyuntae
Copy link

flex-hyuntae commented Nov 4, 2022

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

@timc1 timc1 removed the wontfix This will not be worked on label Nov 4, 2022
@timc1
Copy link
Owner

timc1 commented Nov 9, 2022

@flex-hyuntae check out the latest release here.

@flex-hyuntae
Copy link

@flex-hyuntae check out the latest release here.

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

@timc1
Copy link
Owner

timc1 commented Nov 10, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants