
Product
Announcing Socket Fix 2.0
Socket Fix 2.0 brings targeted CVE remediation, smarter upgrade planning, and broader ecosystem support to help developers get to zero alerts.
react-element-resize-events
Advanced tools
This components gives your div/element an onResize
event like so:
import { Div } from 'react-element-resize-events';
const MyComponent = ({ text }) => {
const onResize = ({ width, height, init }) => {
console.log(`width: ${width}, height: ${height}${init ? `, and this resize event was due to element being mounted` : ``}.`);
};
<Div onResize={onResize}>{text}</Div>
};
The basic idea is that only the window object has an on resize event. So I capitalize on this fact. The Div
component returns a div
which is positioned (relative positioning by default) and inside this div
the first element is an iframe
with absolute positioning and with both width and height set to 100% and visibility: hidden
. The children of the Div
are then placed next to the iframe
.
The idea is the iframe
will expand with the parent div
and since the iframe
has a window element which I attach an onresize
event to it, then "forward" the event to the user's input onResize
function.
import { ElemWithResizeEvents } from 'react-element-resize-events';
const MyComponent = ({ text }) => {
const onResize = ({ width, height, init }) => {
console.log(`width: ${width}, height: ${height}${init ? `, and this resize event was due to element being mounted` : ``}.`);
};
<ElemWithResizeEvents tagName="h1" onResize={onResize}>{text}</Div>
};
By default the dimentions returned to the onResize
function are the inner dimentions of the element (i.e. without margin/padding/border). To override this set getInnerDimentions
prop to false as bellow:
import { ElemWithResizeEvents } from 'react-element-resize-events';
const MyComponent = ({ text }) => {
const onResize = ({ width, height, init }) => {
console.log(`width: ${width}, height: ${height}${init ? `, and this resize event was due to element being mounted` : ``}.`);
};
<ElemWithResizeEvents tagName="h1" onResize={onResize} getInnerDimentions={false}>{text}</Div>
};
You can also change the default getInnerDimentions
by utilising the setDefaults
function as bellow
import { setDefaults } from 'react-element-resize-events';
setDefaults({ getInnerDimentions: false });
You can also change the default tag name used by ElemWithResizeEvents
import { setDefaults } from 'react-element-resize-events';
setDefaults({ tagName: 'span' });
FAQs
Made with create-react-library
We found that react-element-resize-events demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer 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.
Product
Socket Fix 2.0 brings targeted CVE remediation, smarter upgrade planning, and broader ecosystem support to help developers get to zero alerts.
Security News
Socket CEO Feross Aboukhadijeh joins Risky Business Weekly to unpack recent npm phishing attacks, their limited impact, and the risks if attackers get smarter.
Product
Socket’s new Tier 1 Reachability filters out up to 80% of irrelevant CVEs, so security teams can focus on the vulnerabilities that matter.