I have this global function: function elementWidth (args, ctx) {
const element = document.getElementById(args.element); const elementWidth = Math.round(parseFloat(window.getComputedStyle(element).width));
return elementWidth;
} How can I convert it to a Toddle formula? I want the function to be encapsulated within the component, making it easily portable between projects without the need to remember to add a separate global function. Or.. Is it also possible to add this function inside a "Script" tag in the component?
Jacob Kofoed
2 months ago
Sep 21, 2024, 3:38 PM
I'm not entirely sure what you mean with portable between projects, but if you want to share a formula between projects, then I suggest creating a package. Then you can install it where needed, and maintain it in a single place. And other people can benefit from it as well ๐ Also, to get the width of an element, I suggest you use getBoundingClientRect().width rather than getComputedStyle as the computed style doesn't actually tell you the real width on the screen. It won't always take into account transforms and paddings. Also, you won't have to parse the value as it is already numerical.