Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Detect when an element changes size #433

Open
Brittany-Reid opened this issue Apr 21, 2021 · 2 comments
Open

Detect when an element changes size #433

Brittany-Reid opened this issue Apr 21, 2021 · 2 comments

Comments

@Brittany-Reid
Copy link

Cool package, hoping to do some fun stuff with it.

I have the following component that measures width and prints it.

const main = () => {
    
    const [width, setWidth] = React.useState(0);

    const boxRef = React.useRef();

    React.useEffect(()=>{
        var dimensions = ink.measureElement(boxRef.current);
        setWidth(dimensions.width);
    })

    ink.useInput(()=>{

    });

    return e(ink.Box, {borderStyle:"double", width:"100%", ref:boxRef, height:4},
        e(ink.Text, {}, width)
    );
}

When I shrink the terminal width, the width value does not update. Similarly, if this component has a sibling that can change the layout and result in changing this component's width, the elements will render fine but the width value will not update. How can I tell when some change happens to the size of my element?

I have a onTerminalResize hook that solves half of this problem, but for non-terminal resize layout changes this is still an issue.

The yoga docs aren't too great so I took a look at the issues. There's a setDirtiedFunc: facebook/yoga#842
I tried this on boxRef.current.yogaNode and the function didn't exist. Not sure if dirtied state is even what I want.

And YGNodeGetHasNewLayout to detect if a node layout has been changed: facebook/yoga#681
But I couldn't find the function, so it looks like it was never added?

@dustinlacewell
Copy link

Interested in this as well.

@nclsndr
Copy link

nclsndr commented Apr 11, 2022

Did you tried using a callback ref approach?
https://reactjs.org/docs/hooks-faq.html#how-can-i-measure-a-dom-node
EDIT: nope, it gets down to the same issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants