{"id":21241182,"url":"https://github.com/icyjoseph/useelementresize","last_synced_at":"2025-03-15T03:44:46.265Z","repository":{"id":37743136,"uuid":"175969962","full_name":"icyJoseph/useElementResize","owner":"icyJoseph","description":"Detect Element Resize using React Hooks + Mutation Observer + detectElementResize","archived":false,"fork":false,"pushed_at":"2023-03-04T03:21:38.000Z","size":693,"stargazers_count":2,"open_issues_count":4,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-21T19:46:45.397Z","etag":null,"topics":["autosize","detect-element-resize","element-resize","element-resize-event","mutation-ob","react","react-hooks"],"latest_commit_sha":null,"homepage":"https://super-clouds.surge.sh","language":"JavaScript","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/icyJoseph.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-03-16T12:35:27.000Z","updated_at":"2022-12-15T02:10:49.000Z","dependencies_parsed_at":"2024-12-18T06:32:59.334Z","dependency_job_id":null,"html_url":"https://github.com/icyJoseph/useElementResize","commit_stats":{"total_commits":51,"total_committers":2,"mean_commits":25.5,"dds":"0.019607843137254943","last_synced_commit":"dbf4dadf7f91c27c76db4c5b79b89f9bf81702c3"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/icyJoseph%2FuseElementResize","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/icyJoseph%2FuseElementResize/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/icyJoseph%2FuseElementResize/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/icyJoseph%2FuseElementResize/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/icyJoseph","download_url":"https://codeload.github.com/icyJoseph/useElementResize/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243681005,"owners_count":20330155,"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":["autosize","detect-element-resize","element-resize","element-resize-event","mutation-ob","react","react-hooks"],"created_at":"2024-11-21T00:54:54.279Z","updated_at":"2025-03-15T03:44:46.240Z","avatar_url":"https://github.com/icyJoseph.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# useElementResize\n\n[![npm](https://img.shields.io/npm/dm/use-element-resize.svg)]()\n\n\nThis package provides two abstractions to detect element resize events. It depends on React-Hooks!\n\nThe first abstraction uses [javascript-detect-element-resize](https://github.com/sdecima/javascript-detect-element-resize), and it can be invoked as `useDetectElementResize`.\n\nThe second abstraction is a very simple implementation of mutability observers. We create an observer and connect it to the desired element.\n\nBoth of these, can take an `onResize` callback, which is of course, called every time a resize event happens. They also take `onExit`, which is called when the component unmounts.\n\nBoth of the abstractions return `width` and `height` when invoked, and consume their callbacks with `width` and `height`. When using `onExit` on `useMutabilityObserver`, the left over records are passed to the callback.\n\n\u003e These abstractions detect element resize! Not the same as attaching a listener to the resize event.\n\nThese abstractions can be used to replace the `AutoSizer` provided by [react-virtualized](https://github.com/bvaughn/react-virtualized), which implements [javascript-detect-element-resize](https://github.com/sdecima/javascript-detect-element-resize), through a `React Class Component`. Instead you can use these hooks and pass `width` and `height` down to, for example, the [MultiGrid](https://github.com/bvaughn/react-virtualized/blob/master/docs/MultiGrid.md) component.\n\n\u003e To use, just pass the `id` of the element you wish to observe!\n\n## Demo\n\nIn the example provided, one can toggle the resize listeners off, this is delayed 2 seconds. During this time, you can spam resize events, and see that the component is unmounted cleanly without any callbacks, or attempts to set React state left.\n\nAs with any hook, you simply invoke it at the top of your function component and now you have access to variable `widht`, `height`, `obsWidth`, `obsHeight`.\n\nThis demo allows you to add divs with `1` as text content. These add as a `flex-column` and will stretch the `root` div, far beyond the height of the window. You can play around with `+1` and `-1` to see how the hooks respond to height changes.\n\n```jsx\nimport React from \"react\";\nimport {\n  useMutabilityObserver,\n  useDetectElementResize\n} from \"use-element-resize\";\n\nfunction ElementResize() {\n  const target = { id: \"root\" };\n  const [width, height] = useDetectElementResize(target);\n  const [obsWidth, obsHeight] = useMutabilityObserver(target);\n\n  return (\n    \u003cdiv className=\"resize-results\"\u003e\n      \u003cspan\u003eRoot div dimensions:\u003c/span\u003e\n      \u003cdiv\u003e\n        \u003ccode\u003euseDetectElementResize\u003c/code\u003e\n        \u003cdiv\u003e\n          \u003cspan className=\"text-muted\"\u003e\n            Width: \u003cspan className=\"text-success\"\u003e{width}\u003c/span\u003e px\n          \u003c/span\u003e\n          {\" - \"}\n          \u003cspan className=\"text-muted\"\u003e\n            Height: \u003cspan className=\"text-danger\"\u003e{height}\u003c/span\u003e px\n          \u003c/span\u003e\n        \u003c/div\u003e\n      \u003c/div\u003e\n      \u003cdiv\u003e\n        \u003ccode\u003euseMutabilityObserver\u003c/code\u003e\n        \u003cdiv\u003e\n          \u003cspan className=\"text-muted\"\u003e\n            obsWidth: \u003cspan className=\"text-success\"\u003e{obsWidth}\u003c/span\u003e px\n          \u003c/span\u003e\n          {\" - \"}\n          \u003cspan className=\"text-muted\"\u003e\n            obsHeight: \u003cspan className=\"text-danger\"\u003e{obsHeight}\u003c/span\u003e px\n          \u003c/span\u003e\n        \u003c/div\u003e\n      \u003c/div\u003e\n    \u003c/div\u003e\n  );\n}\n```\n\n## License\n\nThe MIT License (MIT)\n\nCopyright (c) 2019 Joseph Chamochumbi\n\nPermission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ficyjoseph%2Fuseelementresize","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ficyjoseph%2Fuseelementresize","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ficyjoseph%2Fuseelementresize/lists"}