2.0.0-beta.13 • Published 5 years ago

@xdproto/focus v2.0.0-beta.13

Weekly downloads
-
License
MIT
Repository
github
Last release
5 years ago

Focus

Travis build status npm version

A React library for managing focus in TV apps.

✓ Controls LRUD navigation for you – automatically
✓ Hooks-first declarative API

Motivation

Users of TV apps most often use LRUD controls to navigate within the app. Consequently, the focused element on the page is of particular importance.

It is not always the case that your runtime environment includes a built-in system for managing focus. Environments that do include such a system, like the browser, often have a simple focus system that doesn't align with the complex needs of LRUD environments.

Because of this, it is up to the application to manage its own focus state. This library helps you to do that.

Installation

Install using npm:

npm install @xdproto/focus

or yarn:

yarn add @xdproto/focus

This library has the following peer dependencies:

Table of Contents

Guides

Getting Started

Render the FocusRoot somewhere high up in your application's component tree. This is the root node of the focus tree.

import { FocusRoot } from '@xdproto/focus';

export default function App() {
  return (
    <FocusRoot>
      <AppContents />
    </FocusRoot>
  );
}

Next, use the FocusNode component to create a focusable node on the page.

import { FocusNode } from '@xdproto/focus';

export default function Profile() {
  return <FocusNode className="profile">Profile</FocusNode>;
}

This library manages moving focus between the FocusNode nodes as the user inputs LRUD commands.

Configuring this behavior is managed entirely through props of the FocusNode components. To learn more about those props, refer to the API documentation below.

FAQ

What is LRUD?

LRUD is an acronym that stands for left-right-up-down, and it refers to the directional buttons typically found on remotes. In LRUD systems, input devices usually also have some kind of "submit" button, and, less commonly, a back button.

Is this library right for me?

The limitations described below may help you to determine that.

API

This library has three named exports: FocusRoot, FocusNode, and useFocus.

<FocusRoot />

Serves as the root node of a new focus hierarchy.

All props are optional.

PropTypeDefault valueDescription
orientationstring'horizontal'Whether the children of the root node are arranged horizontally or vertically.
wrappingboolean'false'Set to true for the navigation to wrap when the user reaches the start or end of the root's children.
import { FocusRoot } from '@xdproto/focus';

export default function App() {
  return (
    <FocusRoot orientation="vertical">
      <AppContents />
    </FocusRoot>
  );
}

<FocusNode />

A Component that represents a FocusNode node in the focus tree.

All props are optional.

PropTypeDefault valueDescription
propsFromNodefunctionA function you can supply to compute additional props to apply to the element. The function is passed one argument: the focus node.
classNamestringA class name to apply to this element.
focusedClassstring"isFocused"A class name that is applied when this element is focused.
focusedExactClassstring"isFocusedExact"A class name that is applied this element is exactly focused.
disabledClassstring"focusDisabled"A class name that is applied this element is disabled.
elementTypestring|elementType'div'The React element type to render. For instance, "img" or motion.div.
focusIdstring{unique_id}A unique identifier for this node. Specify this yourself for debugging purposes, or when you will need to manually set focus to the node.
focusOnMountbooleanfalseWhether or not to focus this node when the component mounts.
orientationstring'horizontal'Whether the children of this node are arranged horizontally or vertically.
wrappingboolean'false'Set to true for the navigation to wrap when the user reaches the start or end of the children list.
disabledboolean'false'This node will not receive focus when true.
defaultChildFocusIndexnumber0The index of the child to move focus to when this element receives focused. Only applies for nodes with children.
onMountfunctionA function that is called when the element mounts. Passed the focus node as the first argument.
onUnmountfunctionA function that is called when the element unmounts. Passed the focus node as the first argument.
onFocusfunctionA function that is called when the node receives focus.
onBlurfunctionA function that is called when the node loses focus.
onKeyfunctionA function that is called when the user presses any TV remote key while this element has focus.
onArrowfunctionA function that is called when the user presses a directional button.
onLeftfunctionA function that is called when the user presses the left button.
onUpfunctionA function that is called when the user presses the up button.
onDownfunctionA function that is called when the user presses the down button.
onRightfunctionA function that is called when the user presses the right button.
onSelectfunctionA function that is called when the user pressed the select button.
onBackfunctionA function that is called when the user presses the back button.
onMovefunctionA function that is called when the focused child index of this node changes. Only called for nodes with children.
childrenReact nodeChildren of the Focus Node.
renderfunctionA render prop you may specify instead of children. It is passed one argument: the focus node.
...restanyAll other props are applied to the underlying DOM node.
import { FocusNode } from '@xdproto/focus';

export default function Profile() {
  return (
    <FocusNode
      elementType="button"
      className="profileBtn"
      onSelect={({ node }) => {
        console.log('The user just selected this profile', node);
      }}>
      Profile
    </FocusNode>
  );
}

useFocusTree()

A Hook that returns the focus tree object.

import { useFocusTree } from '@xdproto/focus';

export default function MyComponent() {
  const focusTree = useFocusTree();

  useEffect(() => {
    console.log('the current state', focusTree.getState());

    focusTree.setFocus('my-node');
  }, []);
}

useFocusNode( focusId )

A Hook that returns the focus node with ID focusId.

import { useFocusNode } from '@xdproto/focus';

export default function MyComponent() {
  const buttonFocusNode = useFocusNode('my-button');

  console.log('Is the button focused?', buttonFocusNode.isFocused);
}

useFocusHierarchy()

A Hook that returns an array representing the focus hierarchy, which are the nodes that are focused in the node tree. Each entry in the array is a focus node. The last node in the hierarchy is the node that is exactly focused.

import { useFocusHierarchy } from '@xdproto/focus';

export default function MyComponent() {
  const focusHierarchy = useFocusHierarchy('button');
  console.log(focusHierarchy);
  // => [
  //   { nodeId: 'root', ... },
  //   { nodeId: 'homePage', ... },
  //   { nodeId: 'mainNav', ... },
  // ]
}

Prior Art

Limitations

  • No support for pointer (mouse) inputs
  • No spatial navigation
2.0.0-beta.13

5 years ago

2.0.0-beta.12

5 years ago

2.0.0-beta.11

5 years ago

2.0.0-beta.10

5 years ago

2.0.0-beta.9

5 years ago

2.0.0-beta.8

5 years ago

2.0.0-beta.7

5 years ago

2.0.0-beta.6

5 years ago

2.0.0-beta.5

5 years ago

2.0.0-beta.4

5 years ago

2.0.0-beta.3

5 years ago

2.0.0-beta.2

5 years ago

2.0.0-beta.1

5 years ago

2.0.0-beta.0

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago

0.0.8

5 years ago

0.0.7

5 years ago

0.0.7-beta.2

5 years ago

0.0.7-beta.1

5 years ago

0.0.7-beta.0

5 years ago

0.0.6

5 years ago

0.0.5

5 years ago

0.0.4

5 years ago