UseResizeObserverDemo
guarahooks/use-resize-observer-demoFreeExample
use-resize-observer's hook in action.
Install it
npx shadcn@latest add https://guarahooks.com/r/use-resize-observer-demo.jsonSource
1'use client';23import React, { useRef, useState } from 'react';45import { Button } from '@/components/ui/button';6import {7 Card,8 CardContent,9 CardDescription,10 CardFooter,11 CardHeader,12 CardTitle,13} from '@/components/ui/card';1415import { useResizeObserver } from '@/hooks/guarahooks/use-resize-observer';1617export function UseResizeObserverDemo() {18 const targetRef = useRef<HTMLDivElement | null>(null);19 const [size, setSize] = useState({ width: 0, height: 0 });20 const [observing, setObserving] = useState(true);2122 const { disconnect } = useResizeObserver<HTMLDivElement>({23 target: targetRef,24 callback: (entries) => {25 const entry = entries[0];26 const { width, height } = entry.contentRect;27 setSize({ width: Math.round(width), height: Math.round(height) });28 },29 });3031 const toggleObserving = () => {32 if (observing) {33 disconnect();34 }35 setObserving((prev) => !prev);36 };3738 return (39 <Card className="max-w-sm w-full">40 <CardHeader>41 <CardTitle>Resize Observer Demo</CardTitle>42 <CardDescription>43 Observe size changes of the container in real time.44 </CardDescription>45 </CardHeader>46 <CardContent className="space-y-4">47 <div48 ref={targetRef}49 className="resize border rounded-lg p-4 max-w-full overflow-auto"50 style={{ resize: 'both' }}51 >52 <p>Drag to resize this box</p>53 </div>54 <div>55 <p>Width: {size.width}px</p>56 <p>Height: {size.height}px</p>57 </div>58 </CardContent>59 <CardFooter>60 <Button variant="outline" onClick={toggleObserving}>61 {observing ? 'Disconnect' : 'Observe'}62 </Button>63 </CardFooter>64 </Card>65 );66}
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 |
