Command Palette

Search for a command to run...

0
博客
Next

触感反馈

在移动设备上触发触感反馈。

Loading…
"use client"
 
import Image from "next/image"
 
import { Button } from "@/components/ui/button"
import { haptic } from "@/lib/haptic"
 
export function HapticDemo() {
  return (
    <div className="flex flex-col items-center gap-6 py-16 pointer-coarse:py-0">
      <Button onClick={() => haptic()}>Haptic</Button>
 
      <ScanToTryOnMobile />
    </div>
  )
}
 
function ScanToTryOnMobile() {
  return (
    <div className="flex flex-col items-center gap-6 pointer-coarse:hidden">
      <p className="text-center text-sm text-muted-foreground">
        Scan the QR code below to
        <br />
        try it out on mobile.
      </p>
 
      <Image
        className="rounded-lg border dark:border-transparent"
        src="https://assets.chanhdai.com/images/blog/haptic-qr-code.webp"
        alt="QR code"
        width={96}
        height={96}
        quality={100}
        unoptimized
      />
    </div>
  )
}

特性

  • 在 Android 上使用 Vibration API,在 iOS 上使用 checkbox 技巧。
  • 支持在 Android 设备上自定义持续时间和振动模式。
  • 触感功能灵感来自 Vercel 的移动端体验。

安装

$ pnpm dlx shadcn@latest add @ncdai/haptic

用法

import { haptic } from "@/lib/haptic"
<Button onClick={() => haptic()}>Haptic</Button>

API 参考

Prop

Type