useAutoHeight
unlumen-ui/hooks-use-auto-heightFreeHook
A hook that allows you to automatically adjust the height of an element based on its content.
Install it
npx shadcn@latest add https://ui.unlumen.com/r/hooks-use-auto-height.jsonSource
1"use client";23import * as React from "react";45type AutoHeightOptions = {6 includeParentBox?: boolean;7 includeSelfBox?: boolean;8};910export function useAutoHeight<T extends HTMLElement = HTMLDivElement>(11 deps: React.DependencyList = [],12 options: AutoHeightOptions = {13 includeParentBox: true,14 includeSelfBox: false,15 },16) {17 const ref = React.useRef<T | null>(null);18 const roRef = React.useRef<ResizeObserver | null>(null);19 const [height, setHeight] = React.useState(0);2021 const measure = React.useCallback(() => {22 const el = ref.current;23 if (!el) return 0;2425 const base = el.getBoundingClientRect().height || 0;2627 let extra = 0;2829 if (options.includeParentBox && el.parentElement) {30 const cs = getComputedStyle(el.parentElement);31 const paddingY =32 (parseFloat(cs.paddingTop || "0") || 0) +33 (parseFloat(cs.paddingBottom || "0") || 0);34 const borderY =35 (parseFloat(cs.borderTopWidth || "0") || 0) +36 (parseFloat(cs.borderBottomWidth || "0") || 0);37 const isBorderBox = cs.boxSizing === "border-box";38 if (isBorderBox) {39 extra += paddingY + borderY;40 }41 }4243 if (options.includeSelfBox) {44 const cs = getComputedStyle(el);45 const paddingY =46 (parseFloat(cs.paddingTop || "0") || 0) +47 (parseFloat(cs.paddingBottom || "0") || 0);48 const borderY =49 (parseFloat(cs.borderTopWidth || "0") || 0) +50 (parseFloat(cs.borderBottomWidth || "0") || 0);51 const isBorderBox = cs.boxSizing === "border-box";52 if (isBorderBox) {53 extra += paddingY + borderY;54 }55 }5657 const dpr =58 typeof window !== "undefined" ? window.devicePixelRatio || 1 : 1;59 const total = Math.ceil((base + extra) * dpr) / dpr;6061 return total;62 }, [options.includeParentBox, options.includeSelfBox]);6364 React.useLayoutEffect(() => {65 const el = ref.current;66 if (!el) return;6768 setHeight(measure());6970 if (roRef.current) {71 roRef.current.disconnect();72 roRef.current = null;73 }7475 const ro = new ResizeObserver(() => {76 const next = measure();77 requestAnimationFrame(() => setHeight(next));78 });7980 ro.observe(el);81 if (options.includeParentBox && el.parentElement) {82 ro.observe(el.parentElement);83 }8485 roRef.current = ro;8687 return () => {88 ro.disconnect();89 roRef.current = null;90 };91 // eslint-disable-next-line react-hooks/exhaustive-deps92 }, deps);9394// … truncated
Files it writes
More from Unlumen Ui
All 233 items| Component | Registry | Kind | Access | Installs | Command |
|---|---|---|---|---|---|
| Use Controlled Statehooks-use-controlled-state | Unlumen Ui | Hooks | Free | no deps | |
| useDataStatehooks-use-data-state | Unlumen Ui | Hooks | Free | no deps | |
| useIsInViewhooks-use-is-in-view | Unlumen Ui | Hooks | Free | 1 dep | |
| useMotionValueStatehooks-use-motion-value-state | Unlumen Ui | Hooks | Free | 1 dep |
