@bbc/tv-lrud-spatial

中文文档
0.0.18发布于 2026/07/09Apache-2.0TypeScriptCommonJS零依赖构建来源已验证近 30 天下载 2.9万

Spatial navigation library

原始使用文档
文档目录14 节
  1. API
  2. Parameters
  3. Returns
  4. Focusables
  5. Ignoring Focusables
  6. Focusable Overlap
  7. Containers
  8. Block exits
  9. Focusable Containers
  10. How does it work?
  11. Developing
  12. Building
  13. Testing
  14. Contact

LRUD spatial

Move focus around a HTML document using Left, Right, Up, Down keys.

API

getNextFocus(currentFocus, keyOrKeyCode, [scope])

Parameters

  • currentFocus should be an HTMLElement that you want LRUD spatial to consider as the element you are navigating from. In simple applications, this can just be a reference to document.activeElement.
  • keyOrKeyCode should be a key string or a keyCode decimal representing the directional key pressed.
  • scope is an optional HTMLElement that you only want to look for focusable candidates inside of. Defaults to the whole page if not provided.

Returns

An HTMLElement that LRUD spatial thinks you should navigate to.

Focusables

LRUD spatial defines focusable elements as those which match any of the following CSS selectors:

  • [tabindex] (for tabindex >= 0)
  • a
  • button
  • input

Ignoring Focusables

Any potential candidate with the lrud-ignore class, or inside any parent with the lrud-ignore class, will not be considered focusable and will be skipped over. By default LRUD will not ignore candidates that have opacity: 0 or have a parent with opacity: 0, so this class can be used for that.

Focusables with a tabindex="-1" attribute will be skipped over, however any focusable inside any parent with tabindex="-1" will still be considered focusable.

Potential candidates with disabled or aria-disabled="true" attributes will not be considered focusable.

Focusable Overlap

By default, LRUD will measure to all candidates that are in the direction to move. It will also include candidates that overlap the current focus by up to 30%, allowing for e.g. a 'right' movement to include something that is above the current focus, but has half of it's size expanding to the right.

This threshold can be adjusted on a per-element basis with the data-lrud-overlap-threshold attribute, as a float from 0.0 to 1.0. An overlap of 0.0 will make a candidate only be considered if it is located entirely in the direction of movement.

Please also note that LRUD does not consider the Z Axis, which can cause surprising results with elements that are overlapped in this way, including in the case of full screen overlays on existing UIs. The above attribute can help alleviate this issue.

Containers

Focusables can be wrapped in containers. Containers can keep track of the last active focusable item within them using a data-focus attribute.

Focusables that should be tracked must have a unique ID attribute.

When a container has no previous focus state, it's first focusable element is used instead.

At this time, containers are defined as matching the CSS selectors: nav, section or .lrud-container.

Block exits

In some instances, it is desirable to prevent lrud-spatial from selecting another "best candidate" for example at the bottom of a list. To do this, a container element should add an additional data-block-exit attribute to prevent further selection in that direction. This should be a space separated list.

E.g.

<div class="lrud-container" data-block-exit="up down">...</div>

Focusable Containers

By default, LRUD only measures the distances to focusables and does not consider where their container boundaries are. Adding the attribute data-lrud-consider-container-distance to a container will include it in the distance calculations, as well as its children.

If the container is the closest out of all the possible focusables assessed, LRUD will return one of its children - even if they are not necessarily the spatially closest focusable.

The above container focus logic will still be used, and moving into a focusable container will move to its last focused child if there was one, at any level of container depth inside it.

How does it work?

To determine the next element that should be focused;

  1. Uses the key code to get the direction of movement
  2. From the currently focused DOM element, get the coordinates of the edge corresponding to the direction you are moving
  3. For all other focusable elements, get the coordinates of the opposite edge (the edge you are entering)
  4. Find the line between the exit and entry coordinates that has the shortest length

Developing

Requirements: Node.js 18

To get started, run npm ci.

Building

npm run build will emit a transpiled and minified version of the library. This is run during CI to prepare the artifact published to NPM. It can also be useful for integrating against another local project with npm link.

Testing

The test/layouts directory contains HTML designed to mirror various real-world use cases, and allow for behavioural vertification of library features such as containers and grids.

Significant new features should come with corresponding layouts that test them.

Use npm test to run the tests.

Debugging

The tests use puppeteer to spin up a headless browser. The browser loads the layouts mentioned above and runs scenarios from lrud.test.js against the unminified code from lib/.

To investigate why a test is failing, or just to hack on some changes... run npm run server. Then go to http://localhost:3005 and select a layout.

Contact

[email protected] - we aim to respond to emails within a week.

版本与发布记录

@bbc/tv-lrud-spatial 共发布 16 个版本,首次发布于 2023/07/13;过去 12 个月发布了 1 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.0.18latest2026/07/093 个月前
0.0.162025/05/281.4 年前
0.0.152025/04/161.6 年前
0.0.142025/04/161.6 年前
0.0.132025/04/161.6 年前
0.0.122024/09/062 年前

关于 @bbc/tv-lrud-spatial 的常见问题

@bbc/tv-lrud-spatial 是什么?

Spatial navigation library

如何安装 @bbc/tv-lrud-spatial?

在项目目录运行 npm install @bbc/tv-lrud-spatial,或使用 pnpm add / yarn add / bun add。

@bbc/tv-lrud-spatial 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@bbc/tv-lrud-spatial 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@bbc/tv-lrud-spatial 可以商用吗?

@bbc/tv-lrud-spatial 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@bbc/tv-lrud-spatial 的下载量有多少?

根据 npm 官方下载统计,@bbc/tv-lrud-spatial 在 2026/10/08 – 2026/11/06 这 30 天内被下载 29,385 次,日均约 980 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@bbc/tv-lrud-spatial 还在维护吗?

最新版本 0.0.18 发布于 2026/07/09(3 个月前);至今共发布 16 个版本,其中过去 12 个月发布了 1 个;首次发布于 2023/07/13。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。