
Research
SANDWORM_MODE: Shai-Hulud-Style npm Worm Hijacks CI Workflows and Poisons AI Toolchains
An emerging npm supply chain attack that infects repos, steals CI secrets, and targets developer AI toolchains for further compromise.
@kaliber/use-element-size
Advanced tools
Determines the width and height in pixels of an element through ResizeObserver
Read the height and width of an element
Determines the width and height (in pixels) of an element through ResizeObserver. This is more performant and easier to use than listening to a resize event and reading the element's size through getBoundingClientRect or getComputedStyle, which run on the main thread and can trash layout. Note that the hook is only updated when the element's size changes, not when the window's size changes.
At the time of writing, ResizeObserver is not yet supported by all current browsers. There is a polyfill available through polyfill.io.
Using @kaliberjs/build, you can add the following argument to the polyfill() call.
{polyfill(['default', 'ResizeObserver'])}
Without, you can manually add the following script to your page (or include it in your build):
https://polyfill.io/v3/polyfill.min.js?features=ResizeObserver
yarn add @kaliber/use-element-size
import { useElementSize } from '@kaliber/use-element-size'
function Component() {
const elementRef = React.useRef()
const { width, height } = useElementSize(elementRef)
return <div ref={elementRef}>{width}px × {height}px</div>
}
.component {
overflow: hidden;
transition: height 0.5s;
}
import { useElementSize } from '@kaliber/use-element-size'
export function Expand({ children, expanded }) {
const innerRef = React.useRef(null)
const { height } = useElementSize(innerRef)
return (
<div
className={styles.component}
style={{ height: expanded ? height + 'px' : '0px' }}
>
<div ref={innerRef}>
{children}
</div>
</div>
)
}

This library is intended for internal use, we provide no support, use at your own risk. It does not import React, but expects it to be provided, which @kaliber/build can handle for you.
This library is not transpiled.
FAQs
Determines the width and height in pixels of an element through ResizeObserver
We found that @kaliber/use-element-size demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 10 open source maintainers collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Research
An emerging npm supply chain attack that infects repos, steals CI secrets, and targets developer AI toolchains for further compromise.

Company News
Socket is proud to join the OpenJS Foundation as a Silver Member, deepening our commitment to the long-term health and security of the JavaScript ecosystem.

Security News
npm now links to Socket's security analysis on every package page. Here's what you'll find when you click through.