Closed curran closed 2 months ago
idea:
export const main = (container, options) => {
const stateProperty = StateProperty(options);
const [dimensions, setDimensions] = stateProperty('dimensions');
if(!dimensions){
observeResize(container, { setDimensions });
return;
}
const { width, height } = dimensions;
export const observeResize = (container, { setDimensions }) => {
new ResizeObserver((entries) => {
const { width, height } = entries[0].contentRect;
setDimensions({ width, height });
}).observe(container);
};
const dimensions = observeResize(container, options);
if (!dimensions) return;
const { width, height } = dimensions;
import { StateField } from 'd3-rosetta';
export const observeResize = (container, options) => {
const [dimensions, setDimensions] =
StateField(options)('dimensions');
if (!dimensions) {
const resizeObserver = new ResizeObserver((entries) => {
setDimensions(entries[0].contentRect);
});
resizeObserver.observe(container);
}
return dimensions;
};
Observing the dimensions of a container is such a common need. It would be great to provide a utility that does this well.
Prior work:
https://vizhub.com/curran/responsive-axes?edit=files&file=observeResize.js
We may want to make the field custom, and leverage the
StateProperty
utility as well.