コンテンツへスキップ

useMousePressed

カテゴリ
エクスポートサイズ
679 B
最終変更日
先週

反応型のマウス押下状態。ターゲット要素の`mousedown` `touchstart`でトリガーされ、ウィンドウの`mouseup` `mouseleave` `touchend` `touchcancel`で解除されます。

デモ

pressed: false
sourceType: null
トラッキング対象

基本的な使用方法

JavaScript
import { useMousePressed } from '@vueuse/core'

const { pressed } = useMousePressed()

タッチはデフォルトで有効になっています。マウスの変更のみを検出するには、`touch`を`false`に設定します。

JavaScript
const { pressed } = useMousePressed({ touch: false })

特定の要素での`mousedown`と`touchstart`のみをキャプチャするには、要素のrefを渡して`target`を指定できます。

Vue
<script setup>
const el = ref(null)

const { pressed } = useMousePressed({ target: el })
</script>

<template>
  <div ref="el">
    Only clicking on this element will trigger the update.
  </div>
</template>

コンポーネントの使用

この関数は、`@vueuse/components`パッケージを介してレンダーレスコンポーネントバージョンも提供します。使用方法の詳細はこちら

Vue
<template>
  <UseMousePressed v-slot="{ pressed }">
    Is Pressed: {{ pressed }}
  </UseMousePressed>
</template>

型定義

型定義を表示
TypeScript
export interface MousePressedOptions extends ConfigurableWindow {
  /**
   * Listen to `touchstart` `touchend` events
   *
   * @default true
   */
  touch?: boolean
  /**
   * Listen to `dragstart` `drop` and `dragend` events
   *
   * @default true
   */
  drag?: boolean
  /**
   * Add event listerners with the `capture` option set to `true`
   * (see [MDN](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener#capture))
   *
   * @default false
   */
  capture?: boolean
  /**
   * Initial values
   *
   * @default false
   */
  initialValue?: boolean
  /**
   * Element target to be capture the click
   */
  target?: MaybeComputedElementRef
}
/**
 * Reactive mouse pressing state.
 *
 * @see https://vueuse.dokyumento.jp/useMousePressed
 * @param options
 */
export declare function useMousePressed(options?: MousePressedOptions): {
  pressed: Ref<boolean, boolean>
  sourceType: Ref<UseMouseSourceType, UseMouseSourceType>
}
export type UseMousePressedReturn = ReturnType<typeof useMousePressed>

ソースコード

ソースコードデモドキュメント

コントリビューター

Anthony Fu
wheat
丶远方
Anthony Fu
Meet you
Chris-Robin Ennen
Jonas Schade
RAX7
ByMykel
vaakian X
MinatoHikari
Marshall Thompson
Shinigami
Alex Kozack

変更ログ

v12.0.0-beta.1 2024/11/21
0a9ed - feat!: Vue 2サポートの削除、バンドルの最適化、クリーンアップ (#4349)
v10.7.0 2023/12/5
17f97 - fix: elementパラメータの型をMaybeComputedElementRefに変更 (#3566)
v10.5.0 2023年10月7日
d5c81 - feat: captureオプションの追加 (#3392)
v10.1.0 2023年4月22日
4bb5b - feat(useMouse): カスタムイベント抽出機能のサポート (#2991)
v9.11.0 2023年1月17日
d5321 - fix(components): defineComponentをpureとしてマーク (#2623)

MITライセンスの下でリリースされています。