Skip to content

Add programmatic focus option #426

@rektdeckard

Description

@rektdeckard

Excellent lib! Was thinking it would be nice to be able to programmatically focus a specific element, instead of being at the whims of the layout order. A simple, non-breaking API change could solve this quite nicely, e.g.:

const First = () => {
  // Pass an optional 'id' to useFocus
  const { isFocused } = useFocus({ id: "first" });
  return <Text>{isFocused ? "I am focused" : "I am not focused"}</Text>;
}

const Second = () => {
  // Return a 'focus' function that focuses the element with id matching the argument
  const { isFocused, focus } = useFocus();

  useInput((input) => {
      if (input === "f") focus("first");
    }, { isActive: isFocused }
  );

  return <Text>{isFocused ? "I am focused" : "I am not focused"}</Text>;
}

It looks like the machinery is already there, just not exposed in the API. WDYT?

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions