npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ink-tree-focus

v0.2.0

Published

Focus manager for Ink that follows the component tree structure

Downloads

7

Readme

Ink Tree Focus

This package for Ink manages focus similarly to Ink focus but it does by respecting the tree structure, instead the creation order.

Install

npm install ink-tree-focus

Basic Usage

Lets analyze the following basic example:

import React from 'react';
import { Box, BoxProps, render, Text, useApp, useInput } from 'ink';
import { FocusInputCapture, FocusManagerProvider, useFocusManager } from 'ink-tree-focus';

interface IFocusableItemProps extends BoxProps {
  children: React.ReactNode;
}
const FocusableItem = ({ children, ...boxProps }: IFocusableItemProps) => {
  const { focusRef, isFocused } = useFocusManager();

  return (
    <Box {...boxProps} ref={focusRef}
      borderStyle='round'
      borderColor={isFocused ? 'red' : ''}
    >
      {children}
    </Box>
  );
};

const App = () => {
  const { exit } = useApp();
  const { focusRef } = useFocusManager({ isFocusable: false });

  useInput((input, key) => {
    if (input === 'q') {
      exit();
    }
  });

  return (
    <>
      <Text>Use Tab / shift + Tap to move</Text>
      <Box ref={focusRef} flexDirection="row">
        <FocusableItem><Text>Box 1</Text></FocusableItem>
        <FocusableItem><Text>Box 2</Text></FocusableItem>
        <FocusableItem flexDirection="column">
          <Text>Box 3</Text>
          <Box flexDirection="row">
            <FocusableItem><Text>1</Text></FocusableItem>
            <FocusableItem><Text>2</Text></FocusableItem>
          </Box>
        </FocusableItem>
        <FocusableItem><Text>Box 4</Text></FocusableItem>
      </Box>
      <Text>Press q to quit</Text>
    </>
  );
};

render(
  <FocusManagerProvider>
    <FocusInputCapture>
      <App />
    </FocusInputCapture>
  </FocusManagerProvider>,
);

First of all, the library provides (essentially) three objects:

  • FocusManagerProvider: Used to provide the application the React's context provider with all the logic for the app. Must wrap the app, then you can forget about it.
  • FocusInputCapture: A predefined wrapper with the only purpose of bind tab key to focus next element and shift + tab to previous element. You can omite it and make your custom binding.
  • useFocusManager: The hook which provides all the resources to manage the focus system. That is the main of the library.

Also in the example we see the use of focusRef, used to bind the DOMElement to the internal tree of focusables, and isFocused, which tells if that DOMElement has currently the focus.

There are more concrete and useful items provide by the hook, which can be found in the API description.

Example

npm run example

API

Hook useFocusManager

Usage: const returned = useFocusManager(props) where props is optional and has the optional members:

  • isFocusable

    • Type boolean
    • Determines if this node can be focused.
  • focusRef

    • Type React.MutableRefObject<DOMElement | null>

and returned provide the following information:

  • focusId

    • Type: Symbol
    • A Symbol that identifies the focus node.
  • focusRef

    • Type: {current: Ink.DOMElement}
    • A reference (created with react's useRef) which component should reference to. That's needed to bind the DOMElement to a focusable node.
  • isFocused

    • Type: boolean
    • Determine if the refecenced element is focused or not.
  • isDescendantFocused

    • Type boolean
    • Determine if some estrictly descendant (not includes itself) element is focused.
  • activeId

    • Type Symbol | undefined
    • The focusId of the currently focused node, if any.

and the following commands:

  • setFocus

    • Type (focusId: Symbol) => void
    • Set focus to element with focus id focusId.
  • focusPrev

    • Type (focusId?: Symbol) => void
    • Focus the previous node in preorder. If focusId is provided, operation will be done with respect focusId, otherwise the activeId. If activeId is not defined, it will focus last element.
  • focusNext

    • Type (focusId?: Symbol) => void
    • Focus the next node in preorder. If focusId is provided, operation will be done with respect focusId, otherwise the activeId. If activeId is not defined, it will focus first element.
  • focusPrevChild

    • Type (focusId?: Symbol) => void
    • Focus the previous child node in preorder. If focusId is provided, operation will be done with respect focusId, otherwise the activeId. If activeId is not defined in the relevant subtree, it will focus last element.
  • focusNextChild

    • Type (focusId?: Symbol) => void
    • Focus the next child node in preorder. If focusId is provided, operation will be done with respect focusId, otherwise the activeId. If activeId is not defined in the relevant subtree, it will focus first element.
  • focusPrevSibling

    • Type (focusId?: Symbol) => void
    • Focus the previous sibling node in preorder. If focusId is provided, operation will be done with respect focusId, otherwise the activeId. If activeId is not defined, nothing will happen.
  • focusNextSibling

    • Type (focusId?: Symbol) => void
    • Focus the next sibling node in preorder. If focusId is provided, operation will be done with respect focusId, otherwise the activeId. If activeId is not defined, nothing will happen.
  • focusParent

    • Type (focusId?: Symbol) => void
    • Focus the parent node in preorder. If focusId is provided, operation will be done with respect focusId, otherwise the activeId. If activeId is not defined, nothing will happen. If no parent node exists, activeId will be set to undefined.