Extend TMS WEB Core with JS Libraries with Andrew: Interact.js + BigText.js
In the last two outings, we covered some big JS libraries. Bootstrap and FontAwesome are wildly popular and with good reason. And getting started using them in your TMS WEB Core project couldn’t be simpler. This time out, we’re going to explore a pair of much smaller JS libraries. And we’ll have to get a little more hands-on using JavaScript in our code to best take advantage of them. Fortunately, there are plenty of code examples to draw from. First, we’ll look at Interact.js which is used to add the ability to drag and resize HTML elements on the page. And then we’ll pair this with BigText.js which is used to automatically scale text within an element to match its size. Motivation Using standard HTML and CSS, it is possible to create elements on a page that can be resized simply by using the CSS resize property. It takes values of none, horizontal, vertical, and both. And it works as expected, adding the familiar small diagonal lines to the bottom-right corner of the element, indicating that it can be resized. It even respects standard CSS properties like min-width, max-width, min-height, max-height to limit how far the element can be resized. But that’s about the extent of the feature. And as developers, we’d like to have a few more options available. Styling options. You get the diagonal lines in the corner. And that’s exactly all that you get. There’s not even an HTML element for it. It’s just ‘there’, wherever it decides ‘there’ is. Aspect Ratio. While it is possible to force elements to have an aspect ratio, it isn’t necessarily the easiest thing to do. There’s a new aspect-ratio CSS property that may help simplify this if you happen to know that your visitors use a browser that supports it, but that’s often not the case. UI. What if you want to start your resize from a different corner? Or an edge? Not happening with the standard CSS resize. Event Handling. If your app needs to know about the change (perhaps to save the new size for later), then you have to dig into more obscure JS functionality involving Observers to get this to work. Not so fun. Dragging. The CSS resize property allows only for resizing. Not really anything readily available to deal with dragging elements. Fortunately, Interact.js addresses all of these areas quite readily. And, once setup, you can add drag and resize functionality to any of your elements simply by adding CSS classes to them! Once you’ve got an element that is resizable, the next challenge is how to deal with its contents. Often, it doesn’t matter – longer bits of text or other elements can be set to wrap automatically, and this is likely what will happen by default anyway. And even the dimensions of the elements contained within or alongside the recently dragged and/or resized element can be set to automatically resize or flow in different directions, based on almost any kind of arrangement you can envision. Sometimes, though, it would be better if the text itself could be automatically scaled to fit the new dimensions. This is where the Event Handling aspect is important, as knowing when the element has been resized can then trigger this scaling function. While it is not a […]
