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

react-use-gamepads

v0.0.6

Published

React Hooks for Better Using the Gamepad API inside React .

Downloads

5

Readme

react-use-gamepad 🕹🎮

React Hooks for Better Using the Gamepad API inside React.

🚀 Get Started

📦 Install

  • npm
 npm install --save-dev react-use-gamepads
  • yarn
 yarn add react-use-gamepads

react && react-dom version is required to be at least ^17.0.2.

🛠 Usage

Basic Example

This example get all connected Gamepads instances through React.useState and useGamepads Hooks.

import React from 'react';
import useGamepads from 'react-use-gamepads';

export default function App() {
  const [gamepads, setGamepads] = React.useState<Gamepad[]>([]);

  const onGamepadsUpdate = (gamepads: Gamepad[]) => setGamepads(gamepads);

  useGamepads({
    onGamepadsUpdate,
  });

  return (
    <div>{gamepads[0].buttons[0].pressed}</div>
  )
}

Control Buttons

This example uses the onButtonsDown to get the click event of gamepad buttons and do something.

import React from 'react';
import useGamepads from 'react-use-gamepads';
import typeof { IGamepadButtonsData } from 'react-use-gamepads';

export default function App() {

  const onButtonsDown = (data: IGamepadButtonsData) => {
    const { gamepad, index, buttons } = data;
    console.log(`Cur Gamepad: ${gamepad}, Gamepad Index: ${index}.`);
    console.log(`Cur Pressed Buttons: ${buttons.join(",")}.`);
  }

  useGamepads({
    onButtonsDown,
  });

  return (
    <div>React Use Gamepads</div>
  )
}

Control Axes

This example uses the onAxesChange to get the change event of gamepad axes and do something.

import React from 'react';
import useGamepads from 'react-use-gamepads';
import typeof { IGamepadButtonsData } from 'react-use-gamepads';

export default function App() {

 
  const onAxesChange = (data: IGamepadAxesData) => {
    const { gamepad, index, axes } = data;
    console.log(`Cur Gamepad: ${gamepad}, Gamepad Index: ${index}.`);
    console.log(`Cur Axes: 👇🏼👇🏼👇🏼👇🏼.`);
    console.log(`axes: `, axes);
    console.log(`left axes X: `, axes[0]);
    console.log(`left axes Y: `, axes[1]);
    console.log(`right axes X: `, axes[2]);
    console.log(`right axes Y: `, axes[3]);
  }

  useGamepads({
    onAxesChange,
  });

  return (
    <div>React Use Gamepads</div>
  )
}

Custom Buttons Map

In general, you don't need to use this API, but if you need to match Gamepad buttons label to Gampepad buttons index, you can use this API.

Note: If you use this property, onButtonsDown returns buttons from number[] to {key: number, value: string}[].

  • No gamepadButtonsMap Props

no props

  • With gamepadButtonsMap Props

with props

Full Examples

This is a full example.

import React from 'react';
import useGamepads from 'react-use-gamepads';
import typeof { IGamepadButtonsData, IGamepadButtonsData } from 'react-use-gamepads';

export default function App() {
   const [gamepads, setGamepads] = React.useState<Gamepad[]>([]);

  const onGamepadsUpdate = (gamepads: Gamepad[]) => setGamepads(gamepads);

  const onButtonsDown = (data: IGamepadButtonsData) => {
    const { gamepad, index, buttons } = data;
    console.log(`Cur Gamepad: ${gamepad}, Gamepad Index: ${index}.`);
    console.log(`Cur Pressed Buttons: ${buttons.join(",")}.`);
  }
 
  const onAxesChange = (data: IGamepadAxesData) => {
    const { gamepad, index, axes } = data;
    console.log(`Cur Gamepad: ${gamepad}, Gamepad Index: ${index}.`);
    console.log(`Cur Axes: 👇🏼👇🏼👇🏼👇🏼.`);
    console.log(`axes: `, axes);
    console.log(`left axes X: `, axes[0]);
    console.log(`left axes Y: `, axes[1]);
    console.log(`right axes X: `, axes[2]);
    console.log(`right axes Y: `, axes[3]);
  }

  useGamepads({
    onGamepadsUpdate,
    onButtonsDown,
    onAxesChange,
  });

  return (
    <div>React Use Gamepads</div>
  )
}

📚 Props API

useGamepads accepts the following props API:

export interface IGamepadProps {
  gamepadButtonsMap?: Record<number, string>;
  onGamepadsUpdate?: (gamepads: Gamepad[]) => void;
  onAxesChange?: (data: IGamepadAxesData) => void;
  onButtonsDown?: (data: IGamepadButtonsData) => void;
};

gamepadButtonsMap

A basic variable for buttons map as following:

export const INITIAL_GAMEPAD_BUTTONS_MAP: Record<number, string> = {
  0: 'A',
  1: 'B',
  2: 'X',
  3: 'Y',
  4: 'LB',
  5: 'RB',
  6: 'LT',
  7: 'RT',
  8: 'BACK',
  9: 'START',
  10: 'LS',
  11: 'RS',
  12: 'UP',
  13: 'DOWN',
  14: 'LEFT',
  15: 'RIGHT',
};

Developers can map your own buttons by rewriting overlay and other means. For examples:

  • Direct use inner variable INITIAL_GAMEPAD_BUTTONS_MAP
// Direct use inner variable
import { INITIAL_GAMEPAD_BUTTONS_MAP } from 'react-use-gampeads';

export default function App() {

  useGamepads({
    gamepadButtonsMap: INITIAL_GAMEPAD_BUTTONS_MAP,
  });

  return (
    <div>React Use Gamepads</div>
  )
}
  • Write your own variable to cover it
const MY_GAMEPAD_BUTTONS_MAP = Record<number, string> = {
  0: 'A',
  1: 'B',
  2: 'X',
  3: 'Y',
  4: 'LB',
  5: 'RB',
  6: 'LT',
  7: 'RT',
  8: 'BACK',
  9: 'START',
  10: 'LS',
  11: 'RS',
  12: 'UP',
  13: 'DOWN',
  14: 'LEFT',
  15: 'RIGHT',
  16: 'Select'
};

export default function App() {

  useGamepads({
    gamepadButtonsMap: MY_GAMEPAD_BUTTONS_MAP,
  });

  return (
    <div>React Use Gamepads</div>
  )
}

onGamepadsUpdate

Get the connected Gamepads through the callback function. Usually, it needs to be used with React.useState.

onGamepadsUpdate?: (gamepads: Gamepad[]) => void;

// How to use
import React from 'react';
import useGamepads from 'react-use-gamepads';

export default function App() {
  const [gamepads, setGamepads] = React.useState<Gamepad[]>([]);
  useGamepads({
    onGamepadsUpdate: (gps: Gamepad[]) => setGamepads(gps),
  });

  return (
    <div>React Use Gamepads</div>
  )
}

onButtonsDown

Handle the event when the buttons are pressed.

onButtonsDown?: (data: IGamepadButtonsData) => void;

// Type IGamepadAxesData
export interface IGamepadButtonsData {
  gamepad: Gamepad;
  index: number;
  buttons: number[] | { key: number, value: string }[];
}

// How to use
import React from 'react';
import useGamepads from 'react-use-gamepads';

export default function App() {
  const onButtonsDown = (data: IGamepadButtonsData) => {
    const { gamepad, index, buttons } = data;
    console.log(`Cur Gamepad: ${gamepad}, Gamepad Index: ${index}.`);
    console.log(`Cur Pressed Buttons: ${buttons.join(",")}.`);
  }
  
  useGamepads({
    onButtonsDown,
  });

  return (
    <div>React Use Gamepads</div>
  )

If developer don't pass the parameter gamepadButtonsMap, the buttons return an index arr, otherwise it returns a custom map is based on gamepadButtonsMap.

onAxesChange

Handle the event when the axes are changed.

onAxesChange?: (data: IGamepadAxesData) => void;

// Type IGamepadAxesData
export interface IGamepadButtonsData {
  gamepad: Gamepad;
  index: number;
  axes: number[];
}

// How to use
import React from 'react';
import useGamepads from 'react-use-gamepads';

export default function App() {
 const onAxesChange = (data: IGamepadAxesData) => {
    const { gamepad, index, axes } = data;
    console.log(`Cur Gamepad: ${gamepad}, Gamepad Index: ${index}.`);
    console.log(`Cur Axes: 👇🏼👇🏼👇🏼👇🏼.`);
    console.log(`axes: `, axes);
    console.log(`left axes X: `, axes[0]);
    console.log(`left axes Y: `, axes[1]);
    console.log(`right axes X: `, axes[2]);
    console.log(`right axes Y: `, axes[3]);
  }
  
  useGamepads({
    onAxesChange,
  });

  return (
    <div>React Use Gamepads</div>
  )
}

🌈 TODO

At present, the current version is available, and the corresponding events of each button and each axes may be openedin the future if necessary.