0.5.2 • Published 9 months ago

ipad-cursor v0.5.2

Weekly downloads
-
License
MIT
Repository
github
Last release
9 months ago

Install

  • NPM

    npm install ipad-cursor --save
  • CDN

    Only support ESM module

    <div data-cursor="block">Block</div>
    <div data-cursor="text">text</div>
    
    <script type="module">
      import cursor from "https://unpkg.com/ipad-cursor@latest"
      cursor.initCursor()
    </script>

    See cursor.oooo.so for more details.

Usage

Basic usage

Apply data-cursor attribute to the element you want to add the effect.

  • data-cursor="text": text cursor
  • data-cursor="block": block cursor

    <div data-cursor="text">Text Cursor</div>
    <div data-cursor="block">Block Cursor</div>

    After your dom loaded, call initCursor to start the effect. You may need to call updateCursor() when dom updated.

    import { initCursor } from 'ipad-cursor'
    
    initCursor()

    ⚠️ Notice: As so far, you need to manage when to updateCursor yourself. Make sure to call updateCursor after dom updated. In the future, there maybe a better way to handle this, see Roadmap for more details.

Custom Style

You can customize the style of the cursor by Config, config can be passed to initCursor method, or use updateConfig method to update config. Every type can be configured separately.

import { initCursor, updateConfig } from 'ipad-cursor'
import type { IpadCursorConfig, IpadCursorStyle } from 'ipad-cursor'

const normalStyle: IpadCursorStyle = { background: 'red' }
const textStyle: IpadCursorStyle = { background: 'blue' }
const blockStyle: IpadCursorStyle = { background: 'green' }
const config: IpadCursorConfig = {
  normalStyle,
  textStyle,
  blockStyle,
};
initCursor(config)

Sometimes, you may want to customize the style of the cursor for a specific element, you can use data-cursor-style attribute to do this.

The value of data-cursor-style attribute is a string, split by ;, and each part is a style, split by :. For example, background:red;color:blue.

It is recommended to use customCursorStyle method to create style string.

For example, customize the style for a circle element (Like avatar).

<div 
  data-cursor="block" 
  data-cursor-style="radius: 50%" 
  style="width: 50px; height: 50px; border-radius: 50%"
/>

<script type="module">
  import cursor from "https://unpkg.com/ipad-cursor@latest"
  cursor.initCursor()
</script>

See Style for full style list.

Use in framework

  • Vue.js

    • hooks

      You can use useCursor hook to call updateCursor() automatically on mounted and unmounted.

      <script setup>
      import { useCursor } from "ipad-cursor/vue"
      
      useCursor()
      </script>
    • directive

      Register plugin globally

      // src/main.ts
      import { ipadCursorPlugin } from "ipad-cursor/vue"
      
      app.use(ipadCursorPlugin, {
        // global configurations
        blockStyle: { radius: "auto" }
      })

      Use in component

      <div v-cursor-block />
      <div v-cursor-text />
      <div v-cursor-block="{ background: 'red' }" />
  • React See App.tsx

  • Hexo See @zqqcee's Blog

Principle

When initCursor called, it will remove default cursor, and generate a fake cursor use div element. Then listen mousemove event, and move the fake cursor to the mouse position.

After init finished, it will call updateCursor method, scan element with data-cursor attribute, detect the cursor type, and add event listener to the element.

When mouse enter the element, apply styles.

API

initCursor(cfg)

see Config for more details.

Init cursor, remove default cursor, and generate a fake cursor use div element. Then listen mousemove event, and move the fake cursor to the mouse position.

updateCursor

Scan element to observe hover event, and apply styles, as well as remove unused element's event listener.

disposeCursor

Remove fake cursor, and remove all event listener, recover default cursor.

updateConfig(cfg)

Update config, see Config for more details.

customCursorStyle(style)

Create style string that can be used as data-cursor-style attribute. This method is used for better type hinting.

resetCursor

Reset cursor to default style.

Config

It is recommended to see index.d.ts in the npm package.

NameTypeDefaultDescriptionrequired
adsorptionStrengthnumber0.2The strength of adsorption effect, number between 0 and 30No
classNamestring'ipad-cursor'The class name of fake cursorNo
blockPaddingnumberautoThe padding of cursor when hover on block, set to auto will calculate automaticNo
enableAutoTextCursor(v0.2.0+)booleanfalseAuto detect text cursor, see #12No
enableLighting(v0.3.0+)booleanfalseAdd a lighting effect to block #14No
enableMouseDownEffect(v0.4.3+, Experimental)booleanfalseAdd a effect when mouse down, customize style by config mouseDownStyleNo
enableAutoUpdateCursor(v0.5.0+)booleanfalseAuto update cursor when dom updatedNo
normalStyleIpadCursorStylesee StyleThe style of normal cursor, see StyleNo
textStyleIpadCursorStylesee StyleThe style of text cursor, see StyleNo
blockStyleIpadCursorStylesee StyleThe style of block cursor, see StyleNo
mouseDownStyle(v0.4.3+, Experimental)IpadCursorStylesee StyleThe style of cursor when mouse is pressed, see StyleNo

Style

NameTypeDescriptionexample
widthMaybeSizeThe width of cursor'10px', 10, '10'
heightMaybeSizeThe height of cursor'10px', 10, '10'
radiusMaybeSize | 'auto'The border radius of cursor, if set to auto for blockStyle, it will be calculated by dom's css border-radius and config.blockPadding.'10px', 10, '10', 'auto'
backgroundstringThe background color of cursor'#fff', 'red', 'rgba(0,0,0)'
borderstringThe css border property of cursor'1px solid black'
zIndexnumberThe z-index of cursor1
scalenumberThe scale of cursor1.05
backdropBlurMaybeSizeThe backdrop-filter blur of cursor'10px', 10, '10'
backdropSaturatestringThe backdrop-filter saturate of cursor180%
durationBaseMaybeDurationTransition duration of basic properties like width, height, radius, border, background-color, if unit if not specified, ms will be used'1000', 1000, 200ms, 0.23s
durationPositionMaybeDurationTransition duration of position properties like top, left, if unit if not specified, ms will be used'1000', 1000, 200ms, 0.23s
durationBackdropFilterMaybeDurationTransition duration of backdrop-filter property, if unit if not specified, ms will be used'1000', 1000, 200ms, 0.23s

Default Style

See getDefaultConfig method in index.ts for more details.

Roadmap

  • Add Chinese document
  • API Docs
  • More examples
  • Auto detect dom update, and call updateCursor automatically

Showcase

0.5.2

9 months ago

0.5.1

9 months ago

0.4.4

10 months ago

0.4.3

10 months ago

0.4.2

10 months ago

0.4.1

10 months ago

0.4.0

10 months ago

0.3.1

10 months ago

0.3.0

10 months ago

0.2.0

10 months ago

0.1.3

10 months ago

0.1.2

10 months ago

0.1.1

10 months ago

0.1.0

10 months ago

0.0.2

10 months ago

0.0.1

10 months ago