What's the correct way of using textAutoResize?

Hey everyone! So I have this code,

    (async function() 
        const fontName = 
            family: genText.font, 
            style: 'Regular' 

        await figma.loadFontAsync(fontName); 

        figText.fontName   = fontName;
        figText.fontSize   = Math.max(1, genText.size);

        figText.characters = genText.text;

        if (     genText.width  == 0
              && genText.height == 0) figText.textAutoResize = 'WIDTH_AND_HEIGHT';
        else if (genText.height == 0) figText.textAutoResize = 'HEIGHT';
        else                          figText.textAutoResize = 'NONE';

        setObjectTransform(figText, genText);
        setObjectProps    (figText, genText);


setObjectTransform() further does a resizeWithoutConstraints().

So I was trying to be clever and have width==0 && height==0 in my generated objects mean auto-size in the final Figma text object, but it’s not working. Setting .textAutoResize doesn’t work whether I do the resizeWithoutConstraints() or not.

Lol chatgpt suggested listening to the auto resize property change in the document, and sure, that’s doable, but compared to the way the rest of this API works that seems like incredible overkill.

What am I doing wrong?

height and width are very rarely 0 for any objects in Figma, including text. Auto resize wouldn’t set them to zero. So you basically are setting auto resize to none every time (meaning you turn off auto resize).

Next, resizeWithoutConstraints sets the exact bounding box of the text, therefore removing any auto resizing that was set previously, so even if you set some kind of autoresize, it would be reset to none after resizing the text box.

I would give you a way to “solve” this but I don’t understand what you are trying to do in the first place.

So if I don’t resize the object, textAutoResize should have an effect? Because in my case it doesn’t :frowning:

It should have an effect, but not in your current code, because like I mentioned, your condition will always result in none.