Giter Site home page Giter Site logo

hyperlaunch / use-virtual-cursor Goto Github PK

View Code? Open in Web Editor NEW
2.0 1.0 0.0 13 KB

Hook for creating a keyboard controlled virtual cursor in a React app

Home Page: https://use-virtual-cursor.hyperlaunch.com

License: MIT License

TypeScript 100.00%
accessibility dpad hooks kiosks react touchscreen

use-virtual-cursor's Introduction

@hyperlaunch/use-virtual-cursor

The useVirtualCursor hook creates a virtual cursor that can be moved freely within the viewport via arrow key presses, and trigger click events on elements like buttons and links via the enter key. It's designed for environments such as touch screen kiosks, allowing D-pad navigation to enhance accessibility without requiring additional UI modifications.

Installation

Install the package via npm or yarn:

npm install @hyperlaunch/use-virtual-cursor
# or
pnpm add @hyperlaunch/use-virtual-cursor
# or
bun add @hyperlaunch/use-virtual-cursor

Usage

To use the useVirtualCursor hook, add it to your React component by attaching the cursorRef to the element you want to control, and using position to update the elements position. Configure the movement speed through the moveMultiplier, which scales the cursor's movement relative to its width for each key press.

Arguments

  • moveMultiplier (number): Adjusts the scale of movement of the cursor. The movement distance per arrow key press is calculated as a fraction of the cursor's width, allowing for adjustable responsiveness.

Returns

  • cursorRef (React.RefObject<HTMLDivElement>): A ref to be attached to the cursor element, allowing the hook to manage its position based on keyboard interactions.
  • position ({ x: number, y: number }): The current x and y coordinates of the cursor, useful for positioning the cursor element in an absolute layout.
  • canInteract (boolean): Indicates whether the cursor is currently over an interactive element like links or buttons, enabling dynamic styling changes.
  • suggestedStyles (React.CSSProperties): Position related styles to control the onscreen position of the cursor element (ie. top, left, z-index). These are suggested, so can be overridden if required.

The cursor will be positioned in the center of the screen, initially.

Example

Here’s an example of how to use useVirtualCursor in a component with Tailwind CSS for styles:

import useVirtualCursor from "@hyperlaunch/use-virtual-cursor";

function Cursor() {
	const { cursorRef, canInteract, suggestedStyles } = useVirtualCursor({
		moveMultiplier: 0.8,
	});

	const classNames = `
    pointer-events-none fixed h-8 w-8 rounded-full transition-transform duration-200
    ${
			canInteract
				? "bg-gray-500 border-2 border-white ring-1 ring-gray-400 shadow-lg origin-center scale-75"
				: "bg-gray-900/40"
		}
  `;

	return <div style={suggestedStyles} ref={cursorRef} className={classNames} />;
}

This component sets up a custom cursor that responds to keyboard inputs. It applies contextual class names if the cursor is over interactive elements, demonstrating how useVirtualCursor can be applied to enhance accessibility and interactivity in your React applications.

use-virtual-cursor's People

Contributors

chrsgrrtt avatar

Stargazers

Tom Byrer avatar Josh avatar

Watchers

 avatar

use-virtual-cursor's Issues

Test on scrolling pages

As the primary use for this has been touch screen apps - it's not been tested with conventional scrolling interfaces. Will need to do a spike with an example and ensure everything functions as expected.

Trigger hover state on interactive elements

Your able to change the look of the cursor when it's hovering a clickable element through canInteract property - but it would be nice if that would also trigger the hover state and styling of the underlying element. Would need to "unset" that when moving off of the item.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.