JSFiddle - React, Tailwind, and code Playground

by error454

JavaScript

enyo.kind({
    name: "TextSizer",
    kind: enyo.Control,
    components: [
        {name: "myc", content: "hello"},
    ],
    constructor: function(){
        this.inherited(arguments);
        console.log("hello");                
    },
    size: function(measureClass, min, max, threshold, spaceToFit, fitVertical) {
        var fontSize;

        //This is basically a binary search algorithm
        while (max - min > threshold) {
            //calculate and apply the new font size
            fontSize = (max + min) * 0.5;
            measureClass.addStyles("font-size: " + fontSize + "px;");

            //Adjust bounds based on whether the text fits
            var dim = measureClass.getBounds().height;
            if (!fitVertical) {
                dim = measureClass.getBounds().width;
            }

            if (dim >= spaceToFit) {
                max = fontSize;
            }
            else {
                min = fontSize;
            }
        }

        return [min, max, fontSize];
    }
});

new TextSizer().write();