# usePressTap URL: /lynx/hooks/use-press-tap Source: https://github.com/daangn/seed-design/blob/dev/docs/content/lynx/hooks/use-press-tap.mdx A hook that manages press/tap interaction state of Lynx elements. 사용 가능 버전: @seed-design/lynx-react-use-press-tap@0.1.0 ## Installation - npm: npm install bun add @seed-design/lynx-react-use-press-tap - pnpm: pnpm add bun add @seed-design/lynx-react-use-press-tap - yarn: yarn add bun add @seed-design/lynx-react-use-press-tap - bun: bun add bun add @seed-design/lynx-react-use-press-tap ## Import ```ts import { usePressTap } from "@seed-design/lynx-react-use-press-tap"; ``` `usePressTap` is a standalone package that can be used without styles. You can use it without installing `@seed-design/lynx-react`. The existing `usePressTap` re-export from `@seed-design/lynx-react` is also maintained. For new code, it is recommended to import directly from the standalone package above. ## Usage ### Basic Usage ```tsx import { usePressTap } from "@seed-design/lynx-react-use-press-tap"; function TappableCard() { const { pressed, ...handlers } = usePressTap({ onTap: () => { "background only"; console.log("tapped!"); }, }); return ( {pressed ? "Pressing..." : "Tap me"} ); } ``` ### Disabled State ```tsx function DisabledButton({ disabled }: { disabled: boolean }) { const { pressed, ...handlers } = usePressTap({ disabled, onTap: () => { "background only"; console.log("won't fire when disabled"); }, }); return {pressed ? "Pressing..." : "Button"}; } ``` If `disabled` changes to `true` while pressing, `pressed` is released and tap handling on both threads is blocked. ### Main Thread Events ```tsx import { usePressTap } from "@seed-design/lynx-react-use-press-tap"; function AnimatedButton() { const mainThreadTap = () => { "main thread"; // animation logic that runs on the main thread }; const { pressed, ...handlers } = usePressTap({ onTap: () => { "background only"; console.log("tapped"); }, mainThreadOnTap: mainThreadTap, }); return {pressed ? "Pressing..." : "Animated"}; } ``` ## API ### Options | Prop | Type | Default | Description | | ----------------- | -------------------------------------------------- | ------- | ---------------------------------------------------------------------------------- | | `disabled` | `boolean` | `false` | When `true`, press state changes and the tap event are blocked. | | `onTap` | `EventHandler>` | - | Receives the Background Thread's tap event as-is. | | `mainThreadOnTap` | `IntrinsicElements["view"]["main-thread:bindtap"]` | - | A callback that requires the `"main thread"` directive. Not bound when `disabled`. | ### Return | Property | Type | Description | | --------------------- | -------------------------------------------------- | ------------------------------------------------------------------------------------------------------ | | `pressed` | `boolean` | The current press state. | | `bindtap` | `EventHandler>` | The tap event handler. | | `bindtouchstart` | `EventHandler>` | The touch start handler. | | `bindtouchend` | `EventHandler>` | The touch end handler. | | `bindtouchcancel` | `EventHandler>` | The touch cancel handler. | | `main-thread:bindtap` | `IntrinsicElements["view"]["main-thread:bindtap"]` | The main thread tap handler. Not included when `disabled` is set or `mainThreadOnTap` is not provided. | ## State and Styling Spread the return value except `pressed` onto the native element. `pressed` is a transient pressed state that turns on at touch start and releases on touch end/cancel/tap or when disabled. It differs from a toggle state that persists a selection. The hook itself does not apply any CSS class or style. In UI you build yourself, you can choose a className that matches `pressed`. Don't assume that an existing Crispy component's Recipe provides a `pressed` variant. Immediate visual feedback on the Main Thread is distinct from the `:active` or ScaleFeedback path a component uses. If you overwrite a user handler added to the same `bindtap` etc. afterward, the hook's own handling disappears. Call both the hook's handler and the user action together in a single handler, or pass tap side effects through `onTap`. ## Differences from the Web - Uses Lynx's `bindtap`·`bindtouchstart/end/cancel` instead of DOM's `onClick`·pointer events. - This hook does not provide hover/focus state, keyboard input, or accessibility attributes. Configure these separately in the consuming component. - If you provide `mainThreadOnTap`, the Main Thread handler is also returned. If you specify both callbacks, each runs on its own thread, so make sure you don't handle the same side effect twice.