0.1.0 • Published 1 year ago

use-cursor-on-hover v0.1.0

Weekly downloads
-
License
MIT
Repository
-
Last release
1 year ago

use-cursor-on-hover

Introduction

This library is all about customizing the cursor with easy. This is still a really early concept in alpha. Nothing is yet definitive and you should NOT use this in production for now. This library has no other dependencies than react itself and polished.

Installation

Requirements

  • React 18^

This library is written with Typescript in mind, but you can use it with Javascript.

Node/npm

To install use-cursor-on-hover:

npm install use-cursor-on-hover@alpha   # npm
yarn add use-cursor-on-hover@alpha      # yarn
pnpm add use-cursor-on-hover@alpha      # pnpm

Deno

Not yet supported.

Basic usage

Overriding the default cursor by a new one:

import { Cursor, useCursorStyleOnHover } from 'use-cursor-on-hover'

function TitleHovered() {
  return <h1 ref={useCursorStyleOnHover('Effect.Fill', 'Effect.Difference')}>Hover me!</h1>
}

function App() {
  return (
    <Cursor.Provider height="40px" width="40px" color="blue">
      <Cursor.Shape.Ring />
      <Cursor.Effect.Glow />
      <TitleHovered />
    </Cursor.Provider>
  )
}

Your cursor can be customized through a Style property which can have the following types:

  • string: when already defined as a default by the library, see Cursor.Shapes and Cursor.Effects for an exhaustive list
  • CSSProperties: when you want to manually customize the cursor
  • (props) => CSSProperties: when you want to customize the cursor depending of the context

Components

Cursor.Provider

Usage

Should be called at the root level of your application.

Returns

ReactNode containing every component that should rely on a custom cursor.

Props

OptionsTypeDescription
childrenReactNodeEverything that would benefit from the custom cursor should be there id
heightCSSProperties'height'What's the expected height of your cursor
widthCSSProperties'width'What's the expected width of your cursor
colorCSSProperties'color'What's the color used when drawing your cursor
hideDefaultCursorbooleanDefine whether or not the default cursor should be hidden

Example

function CursorContainer(props: { children: ReactNode }) {
  return (
    <Cursor.Provider color="#91243E" height="25px" width="25px" hideDefaultCursor={false}>
      {children}
    </Cursor.Provider>
  )
}

Cursor.Shapes

Usage

You can find a list of default shapes provided by this library when you want to define a custom cursor. You just need to call the component in order to set the shape accordingly. Once the component is unmounted, the shape is unset from your cursor.

Available Shapes

NamerequirementsDescription
DiamondNoneWill set the shape of the cursor to ◇
MaskAn img with an svg src as childrenWill set the shape of the accordingly to the svg
RingNoneWill set the shape of the cursor to ○
SquareNoneWill set the shape of the cursor to □

Example

function CustomCursor() {
  return (
    <>
      <Cursor.Shape.Ring />
      <Cursor.Shape.Mask>
        <img src="myCustomShape.svg" alt="cursorShape" />
      </Cursor.Shape.Mask>
    </>
  )
}

Cursor.Effects

Usage

You can find a list of default effects provided by this library when you want to define a visual effect of your cursor unrelated to the shapes. You just need to call the component in order to set the effect accordingly. Once the component is unmounted, the effect is removed from your cursor.

Available Effects

NamerequirementsDescription
DifferenceNoneWill inverse the color hovered by your cursor, works well when Fill is used to
FillNoneWill fill the interior of the cursor with the color defined globally
GlowNoneWill make your cursor glow outside of its perimeter
GrowNoneWill enlarge your cursor
ZoomAn img as childrenWill make your cursor zoom the image

Example

function CustomCursorEffects() {
  return (
    <>
      <Cursor.Effects.Glow />
      <Cursor.Effects.Grow />
      <Cursor.Effects.Zoom>
        <img src="avatar.png" alt="avatar" />
      </Cursor.Effects.Zoom>
    </>
  )
}

Hooks

useCursorStyle

Usage

Should be called when you want to apply a style to your cursor globally once the component where this hooks is called is mounted. The style will be removed once the component is unmounted. This hook can take as many Style as you need. When two arguments override the same property, the latter takes precedence.

Example

function Example() {
  useCursorStyle(
    'Shapes.Ring',
    'Effects.Glow',
    ({ color }) => ({ outline: `1px solid ${color}` }),
    { padding: '15px' },
  )

  return null
}

useCursorStyleOnHover

Usage

When you want to adapt your cursor when the user is specifically hovering an element. This hook can take as many Style as you need. When two arguments override the same property, the latter takes precedence.

Example

function ImageWithZoom(props: JSX.IntrinsicElements['img']) {
  const imageRef = useCursorStyleOnHover(
    'Shapes.Ring',
    'Effects.Grow',
    'Effects.Zoom',
    ({ color }) => ({ outline: `1px solid ${color}` }),
    { padding: '15px' },
  )

  return <img {...props} ref={imageRef} />
}

useGlobalStyle

Usage

When you want to get the global style for your cursor or when you want to modify them.

Example

function CustomCursor() {
  const { width, height } = useGlobalStyle({ color: 'green' })

  return <CustomCursorShape width={width} height={height} />
}

Related

Alternatively, if you just want to modify the global style in an event, you can just call setGlobalStyle after importing it from the library.

useHideSystemCursor

Usage

When you don't want to hide the default cursor globally but just on some part of your application, you can use this hook to deactivate the cursor. It can optionaly take a target which will then ensure the default cursor is hidden only when hovering the target.

Example

function ImageWithZoom(props: JSX.IntrinsicElements['img']) {
  const imageRef = useCursorStyleOnHover('Effects.Zoom')
  useHideSystemCursor(imageRef)

  return <img {...props} ref={imageRef} />
}
0.1.0

1 year ago

0.0.4

2 years ago

0.0.3

2 years ago

0.0.2

2 years ago

0.0.1

2 years ago