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

Add programmatic focus option #426

Closed
rektdeckard opened this issue Apr 5, 2021 · 1 comment
Closed

Add programmatic focus option #426

rektdeckard opened this issue Apr 5, 2021 · 1 comment

Comments

@rektdeckard
Copy link
Contributor

rektdeckard commented Apr 5, 2021

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?

rektdeckard added a commit to rektdeckard/ink that referenced this issue Apr 5, 2021
rektdeckard added a commit to rektdeckard/ink that referenced this issue Apr 5, 2021
rektdeckard added a commit to rektdeckard/ink that referenced this issue Apr 5, 2021
dustinlacewell pushed a commit to dustinlacewell/ink that referenced this issue Jul 24, 2021
@vadimdemedes
Copy link
Owner

This is published in 3.1.0. Thank you, @rektdeckard!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants