{"id":13850375,"url":"https://github.com/Swizec/useDimensions","last_synced_at":"2025-07-12T22:30:22.789Z","repository":{"id":34297195,"uuid":"175452361","full_name":"Swizec/useDimensions","owner":"Swizec","description":"A React Hook to measure DOM nodes","archived":false,"fork":false,"pushed_at":"2022-12-09T15:41:31.000Z","size":912,"stargazers_count":593,"open_issues_count":38,"forks_count":43,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-07-05T15:14:31.767Z","etag":null,"topics":["hooks","react","reactjs"],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Swizec.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-03-13T15:49:31.000Z","updated_at":"2025-02-18T08:18:47.000Z","dependencies_parsed_at":"2022-07-05T15:31:32.604Z","dependency_job_id":null,"html_url":"https://github.com/Swizec/useDimensions","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"purl":"pkg:github/Swizec/useDimensions","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Swizec%2FuseDimensions","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Swizec%2FuseDimensions/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Swizec%2FuseDimensions/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Swizec%2FuseDimensions/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Swizec","download_url":"https://codeload.github.com/Swizec/useDimensions/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Swizec%2FuseDimensions/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":264918078,"owners_count":23683356,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["hooks","react","reactjs"],"created_at":"2024-08-04T20:01:08.851Z","updated_at":"2025-07-12T22:30:22.356Z","avatar_url":"https://github.com/Swizec.png","language":"TypeScript","funding_links":[],"categories":["TypeScript"],"sub_categories":[],"readme":"# useDimensions - a React Hook to measure DOM nodes\n\n[![Travis][build-badge]][build]\n[![npm package][npm-badge]][npm]\n[![Coveralls][coveralls-badge]][coveralls]\n\n[build-badge]: https://img.shields.io/travis/user/repo/master.png?style=flat-square\n[build]: https://travis-ci.org/user/repo\n[npm-badge]: https://img.shields.io/npm/v/npm-package.png?style=flat-square\n[npm]: https://www.npmjs.org/package/npm-package\n[coveralls-badge]: https://img.shields.io/coveralls/user/repo/master.png?style=flat-square\n[coveralls]: https://coveralls.io/github/user/repo\n\n## Demo\n\n[👉 check out demo page to see useDimensions in action](https://usedimensions.now.sh/)\n\n## Backstory :P\n\nThe other day I wanted to measure some DOM nodes. This is useful when you have to align items, or respond to browser width, or ... lots of reasons okay.\n\nI had to align a curvy line with elements that aren't under my control. This little stepper component uses flexbox to evenly space circles, CSS layouting aligns the title, and you see where this is going.\n\n[![](https://s3.amazonaws.com/techletter.app/screenshot-1552494217818.png)](https://twitter.com/Swizec/status/1105494223011241984)\n\nSVG in the background detects position of itself, positions of the title and circle, and uses those to define the `start` and `end` line of my curve. 👌\n\nMany ways you can do this.\n\n@lavrton linked to [a list of existing NPM packages](https://www.npmjs.com/search?q=hook%20size) that sort of do it. @mcalus shared how [he uses `react-sizeme`](https://github.com/mcalus3/open-fraksl/blob/master/src/components/DomainComponents/DrawingComponents/FractalStage.tsx) to get it done.\n\nAll great, but I wanted something even simpler. I also didn't know about them and kind of just wanted to make my own.\n\nHere's an approach I found works great\n\n## useDimensions hook\n\n![useDimensions source](https://i.imgur.com/Tm1Bmdw.png)\n\nYep that's it. It really is that simple.\n\n👉 [GitHub link](https://github.com/Swizec/useDimensions)\n\n-   `useRef` creates a React.ref, lets you access the DOM\n-   `useState` gives you place to store/read the result\n-   `useLayoutEffect` runs before browser paint but after all is known\n-   `getClientBoundingRect()` measures a DOM node. Width, height, x, y, etc\n-   `toJSON` turns a DOMRect object into a plain object so you can destructure\n\nHere's how to use it in your project 👇\n\nFirst, add `useDimensions` to your project\n\n```\n$ yarn add react-use-dimensions\nor\n$ npm install --save react-use-dimensions\n```\n\nUsing it in a component looks like this\n\n```javascript\nimport React from \"react\";\nimport useDimensions from \"react-use-dimensions\";\n\nconst MyComponent = () =\u003e {\n    const [ref, { x, y, width }] = useDimensions();\n\n    return \u003cdiv ref={ref}\u003eThis is the element you'll measure\u003c/div\u003e;\n};\n```\n\n`useDimensions` returns a 2-element array. First the `ref`, second the dimensions.\n\nThis is so multiple `useDimensions` hooks in the same component don't step on each others' toes. Create as many refs and measurement objects as you'd like.\n\n```javascript\nconst MyComponent = () =\u003e {\n    const [stepRef, stepSize] = useDimensions();\n    const [titleRef, titleSize] = useDimensions();\n\n    console.log(\"Step is at X: \", stepSize.x);\n    console.log(\"Title is\", titleSize.width, \"wide\");\n\n    return (\n        \u003cdiv\u003e\n            \u003cdiv ref={stepRef}\u003eThis is a step\u003c/div\u003e\n            \u003ch1 ref={titleRef}\u003eThe title\u003c/h1\u003e\n        \u003c/div\u003e\n    );\n};\n```\n\n## Disabling live updates\n\nBy default `useDimensions` live updates its measurements on every scroll and resize event. This can lead to a lot of re-rendering, if you aren't careful.\n\nI recommend feeding the dimensions you care about into your list of `useEffect` triggers. That is the best way to ensure good performance.\n\nIf however, you don't want that and know you just need to measure once, `useDimensions` supports an optional `liveMeasure` argument.\n\n```javascript\nconst MyComponent = () =\u003e {\n    const [stepRef, stepSize] = useDimensions({ liveMeasure: false });\n\n    // useDimensions never updates and won't trigger re-renders\n    console.log(\"Step is at X: \", stepSize.x);\n\n    return (\n        \u003cdiv\u003e\n            \u003cdiv ref={stepRef}\u003eThis is a step\u003c/div\u003e\n        \u003c/div\u003e\n    );\n};\n```\n\n# License\n\nMIT License of course.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FSwizec%2FuseDimensions","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FSwizec%2FuseDimensions","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FSwizec%2FuseDimensions/lists"}