UseEventListenerDemo
guarahooks/use-event-listener-demoFreeExample
use-event-listener's hook in action.
Live preview
live · rendered by the registry
Rendered by guarahooks on their own page — this is the component running, not a screenshot of it.Install it
npx shadcn@latest add https://guarahooks.com/r/use-event-listener-demo.jsonSource
1'use client';23import React, { useRef, useState } from 'react';45import { Badge } from '@/components/ui/badge';6import { Button } from '@/components/ui/button';7import {8 Card,9 CardContent,10 CardFooter,11 CardHeader,12 CardTitle,13} from '@/components/ui/card';1415import { useEventListener } from '@/hooks/guarahooks/use-event-listener';1617export default function UseEventListenerDemo() {18 // State for window events19 const [windowMousePosition, setWindowMousePosition] = useState({20 x: 0,21 y: 0,22 });23 const [windowScroll, setWindowScroll] = useState(0);2425 // State for element-specific events26 const [clickCount, setClickCount] = useState(0);27 const [isHovering, setIsHovering] = useState(false);2829 // Reference for the element we want to track30 const buttonRef = useRef<HTMLButtonElement>(null);3132 // Track mouse position across the window33 useEventListener({34 eventName: 'mousemove',35 handler: (event) => {36 setWindowMousePosition({37 x: event.clientX,38 y: event.clientY,39 });40 },41 });4243 // Track window scroll position44 useEventListener({45 eventName: 'scroll',46 handler: () => {47 setWindowScroll(window.scrollY);48 },49 });5051 // Track clicks on a specific element52 useEventListener<'click', HTMLButtonElement>({53 eventName: 'click',54 handler: () => setClickCount((prev) => prev + 1),55 element: buttonRef,56 });5758 // Track mouse enter/leave on element59 useEventListener<'mouseenter', HTMLButtonElement>({60 eventName: 'mouseenter',61 handler: () => setIsHovering(true),62 element: buttonRef,63 });6465 useEventListener<'mouseleave', HTMLButtonElement>({66 eventName: 'mouseleave',67 handler: () => setIsHovering(false),68 element: buttonRef,69 });7071 return (72 <div className="space-y-4">73 <div className="grid grid-cols-1 gap-4 sm:grid-cols-2">74 <Card className="max-w-sm w-full">75 <CardHeader>76 <CardTitle>Window Events</CardTitle>77 </CardHeader>78 <CardContent className="space-y-2">79 <p className="font-medium">Mouse Position: </p>80 <Badge variant="secondary">81 X: {windowMousePosition.x}, Y: {windowMousePosition.y}82 </Badge>83 <p className="font-medium">Scroll Position: </p>84 <Badge variant="secondary">{windowScroll}px</Badge>85 </CardContent>86 </Card>8788 <Card className="max-w-sm w-full">89 <CardHeader>90// … truncated
What it pulls in
Other registry items
Files it writes
More from guarahooks
All 100 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| UseAbortControllerDemouse-abort-controller-demo | guarahooks | Examples | Free | no deps | |
| UseArrayStateDemouse-array-state-demo | guarahooks | Examples | Free | no deps | |
| UseAxiosDemouse-axios-demo | guarahooks | Examples | Free | no deps | |
| UseBatteryStatusDemouse-battery-status-demo | guarahooks | Examples | Free | no deps | |
| UseBetterAuthDemouse-better-auth-demo | guarahooks | Examples | Free | no deps | |
| UseClickOutsideDemouse-click-outside-demo | guarahooks | Examples | Free | no deps | |
| UseConfirmDemouse-confirm-demo | guarahooks | Examples | Free | no deps | |
| UseCookieDemouse-cookie-demo | guarahooks | Examples | Free | no deps |
