Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

A Search Bar
This is another DailyUI where I can’t resist coding the thing. The idea was to explore ways to animate a search bar. In this particular scenario, a circular button unrolls to create a search field. The idea was so simple and fun that I had to code it. You’ll see its very simple! Check out the demo.

The main mechanic makes use of the dasharray and dashoffset properties for SVG strokes. In this case, the whole SVG path starts from the end of the text field and comes around the circle. They are not two separate paths! Therefore it makes it easier to animate it using only the two dash properties. The circle is 36px in diameter, which makes a circumference of roughly  π36 = 113.097. This is our dasharray value. The length of the text field is exactly 200px. This is our dashoffset value. Something I had to figure out was that the dashoffset is calculated from the bounding box. not from the length of the path. Therefore, the dashoffset needs to be negative, so that the first dasharray starts at -199 (I had to give it a little room to accommodate the imprecision of my circumference) from the starting point.

Now we need some JavaScript to trigger the animation. The input field is self-containing, which means we can’t place elements inside. Therefore a simple CSS solution wasn’t possible (Prove me wrong! lol). Using .blur() and .focus(), I can remove and add classes that will let me interpolate between the two dasharray and dashoffset states. It’s not more complicated than that.

In the end, I still think my code is a little intense for a search box, but the goal was to create a neat little animation. Use it, implement it, modify it, cite me if you want, and keep coding!