跳到主要内容

useMousePressed

跟踪鼠标按下状态

Usage

实时编辑器

function Demo() {
  const [mouse, type] = useMousePressed();

  return (
    <div>
      <p>Pressed: {JSON.stringify(mouse)}</p>
      <p>SourceType: {JSON.stringify(type)}</p>
    </div>
  );
};

结果
Loading...

API

useMousePressed

Returns

readonly [boolean, UseMousePressedSourceType]: 包含以下元素的元组:

  • 鼠标是否按下。
  • 按下的事件来源。

Arguments

参数名描述类型默认值
targetdom对象BasicTarget<Element>-
options可选参数UseMousePressedOptions | undefined-

UseMousePressedOptions

参数名描述类型默认值
touch监听 touchstart 事件booleantrue
drag监听 dragStart 事件booleantrue
initialValue初始值boolean | (() => boolean)false

UseMousePressedSourceType

Type

export type UseMousePressedSourceType = 'mouse' | 'touch' | null

BasicTarget

export type BasicTarget<T extends TargetType = Element> = (() => TargetValue<T>) | TargetValue<T> | MutableRefObject<TargetValue<T>>;

TargetValue

type TargetValue<T> = T | undefined | null;

TargetType

type TargetType = HTMLElement | Element | Window | Document | EventTarget;