<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Moti Zilberman on Medium]]></title>
        <description><![CDATA[Stories by Moti Zilberman on Medium]]></description>
        <link>https://medium.com/@motiz88?source=rss-f89041520dc------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*W0eqtsGs8yq3Nmv8JhkX1g.jpeg</url>
            <title>Stories by Moti Zilberman on Medium</title>
            <link>https://medium.com/@motiz88?source=rss-f89041520dc------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 16 Nov 2026 18:21:54 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@motiz88/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[A tiny expression compiler for React Native Animated values]]></title>
            <link>https://medium.com/@motiz88/a-tiny-expression-compiler-for-react-native-animated-values-193ddb36fda1?source=rss-f89041520dc------2</link>
            <guid isPermaLink="false">https://medium.com/p/193ddb36fda1</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[react-native]]></category>
            <category><![CDATA[domain-specific-languages]]></category>
            <category><![CDATA[babel]]></category>
            <category><![CDATA[animation]]></category>
            <dc:creator><![CDATA[Moti Zilberman]]></dc:creator>
            <pubDate>Sat, 10 Feb 2018 20:04:49 GMT</pubDate>
            <atom:updated>2018-02-10T20:04:49.511Z</atom:updated>
            <content:encoded><![CDATA[<p><strong><em>tl;dr:</em></strong><em> Get </em><em>animated.macro on </em><a href="https://github.com/motiz88/animated.macro"><em>GitHub</em></a><em>, </em><a href="https://www.npmjs.com/package/animated.macro"><em>npm</em></a><em>, </em><a href="https://snack.expo.io/rJrBx828M"><em>Snack</em></a><em>.</em></p><p>The <a href="https://facebook.github.io/react-native/docs/animations.html#animated-api">Animated</a> library, which allows animations to be set up in JS and <a href="https://facebook.github.io/react-native/blog/2017/02/14/using-native-driver-for-animated.html">executed on the native thread</a>, is a mainstay of the React Native programming model. Its expressive API lets developers compose animated effects based on time, spring physics and even real-time user interaction with distinct ease.</p><p>I’ve recently released <a href="https://github.com/motiz88/animated.macro">animated.macro</a>, a small JavaScript package that improves the ergonomics of working with animated values in React Native apps — and, more importantly, has been fun to build. 😋</p><p>In this post I’ll detail the ideas behind this package and how I went about building it.</p><h4>A brief introduction to Animated.Value</h4><p>The React Native docs include a fantastic <a href="https://facebook.github.io/react-native/docs/animations.html">guide to animations</a> which I won’t repeat here. Central to things is the <strong>Animated.Value</strong> class, which represents an opaque handle to a numeric variable. It’s opaque in the sense that its actual contents are maintained out of sight<em>, </em>often on the native thread, where JS can’t access them directly.</p><p>In lieu of direct access, there are methods for controlling, interpolating and declaratively combining values. There’s one particular set of methods that let you combine values mathematically, via addition, multiplication, division and modulo. The results of these operations can then be used anywhere the basic values can.</p><p>For instance, here’s how you add two animated values together:</p><pre>const result = Animated.add(x, y);</pre><p>And here’s a slightly more involved calculation:</p><pre>const result = Animated.modulo(<br>  Animated.add(phase, Math.PI * 2 + angle),<br>  Math.PI * 2<br>);</pre><h4>What can be improved</h4><p>On closer inspection, the above examples exhibit a bit of an ergonomic issue that arises when writing and maintaining Animated code. Fundamentally, the issue is that animated values and plain numbers are <em>similar</em>, but not interchangeable.</p><ul><li>In the first example, at least one of x and y must be an animated value instance — Animated.add() can’t add two plain numbers together. This can be an inconvenience when refactoring animation code.</li><li>In the second example, the types of phase and angle (and of Math.PI) similarly dictate the shape of the surrounding code — e.g. Animated values can’t be added with + and plain numbers can’t be added with Animated.add().</li></ul><p>Can we do better?</p><h3>Moti Zilberman on Twitter</h3><p>Just released animated.macro, a teeny-tiny expression compiler for @reactnative Animated values. 🕺🏽🔢 https://t.co/EsAn2b13tU https://t.co/zvQjobEk1M</p><p>Yes.</p><h4>Introducing animated.macro 🕺🏽🔢</h4><p>animated.macro lets you write JS-like arithmetic expressions that operate on animated values and plain numbers alike. These expressions take the form of <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals#Tagged_templates">tagged template literals</a> that evaluate to animated values.</p><p>The second example above can now be written as:</p><pre>import animated from &quot;animated.macro&quot;;</pre><pre>const result = animated`<br>  (${phase} + ${Math.PI} * 2 + ${angle}) % (${Math.PI} * 2)<br>`;</pre><ul><li>Addition, subtraction, multiplication, division and modulo are all supported, with the same syntax as JavaScript.</li><li>As are numeric (decimal) literals, unary + and -, // line comments and /* block comments */.</li><li>Variables in the local scope (along with anything else you can think of) are accessible via template placeholders.</li><li>The result of an animated expression is always an animated value; animated`1` is equivalent to new Animated.Value(1).</li></ul><h4>Using the package</h4><p><em>(You can try it now on </em><a href="https://snack.expo.io/rJrBx828M"><em>Expo Snack</em></a><em> without installing anything)</em></p><p>As its name suggests, animated.macro is packaged as a <a href="https://github.com/kentcdodds/babel-plugin-macros">Babel <em>macro</em></a>. Once you have babel-plugin-macros <a href="https://github.com/kentcdodds/babel-plugin-macros/blob/master/other/docs/user.md">set up</a> and animated.macro <a href="https://www.npmjs.com/package/animated.macro">installed from npm</a>, you simply import animated from &quot;animated.macro&quot; and begin to use it anywhere in your code.</p><p>Macros are a delightfully low-fuss way of embedding opt-in DSLs in JavaScript, for both the implementer and the user, and I recommend them wholeheartedly. (There <em>is</em> also a standalone Babel plugin included, if that’s more your thing.)</p><h4>The implementation</h4><p>Behind the scenes, animated.macro is currently based on a simple <a href="https://github.com/motiz88/animated.macro/blob/master/src/Compiler.js">hand-rolled parser</a>. This constructs an abstract syntax tree from the strings and placeholders that make up each template literal. Then each tree is traversed and the nodes all converted into their JavaScript equivalents, injecting calls to Animated as needed; this is returned to Babel which generates the final code.</p><p>As I find time and time again, Babel is quite powerful, and really shines as a toolkit for interacting with JS code. For example, I needed to safely inject temporary variables into scope to perform <a href="https://github.com/motiz88/animated.macro/blob/676f8db334f692a3041ffebfc53eaf15e145a577/src/babel/BabelCompiler.js#L50-L71">checked conversions from </a><a href="https://github.com/motiz88/animated.macro/blob/676f8db334f692a3041ffebfc53eaf15e145a577/src/babel/BabelCompiler.js#L50-L71">number to </a><a href="https://github.com/motiz88/animated.macro/blob/676f8db334f692a3041ffebfc53eaf15e145a577/src/babel/BabelCompiler.js#L50-L71">Animated.Value</a>, and it could not have been easier. The fact that the <a href="https://github.com/thejameskyle/babel-handbook/">Babel Handbook</a> has a section on <a href="https://github.com/thejameskyle/babel-handbook/blob/master/translations/en/plugin-handbook.md#pushing-a-variable-declaration-to-a-parent-scope">doing just that</a>, of course, made it extra easy (shout out to <a href="https://medium.com/u/cc2eaf4f2cd2">James Kyle</a> for that!)</p><p>Apart from the source-to-source transformer — which is definitely the way to go for any real-world use of animated.macro —the package actually includes a second implementation of that same process, that produces Animated.Value instances <a href="https://github.com/motiz88/animated.macro/blob/676f8db334f692a3041ffebfc53eaf15e145a577/src/runtime/RuntimeCompiler.js#L25-L51">directly</a>. This is primarily useful for the live demo of animated.macro on <a href="https://snack.expo.io/rJrBx828M">Expo Snack</a>; Architecting the code around the similarities and differences between the two compilation targets has been a fun exercise in abstraction.</p><h4>The future</h4><p>I’ve now pretty much implemented my initial <a href="https://gist.github.com/motiz88/3dbcdee9c227b93115b779361f58aa8a">Gist</a> outlining the idea that became animated.macro. So what’s next? (Apart from fixing any bugs that crop up… 🤞)</p><ul><li>Better error messages, surfacing exact line and column information for syntax errors.</li><li>More mathematical operations: abs, ceil, floor etc may be achievable with the use of interpolation.</li><li>Maybe we could even have support for some cases of exponentiation (** operator) using easing functions?</li><li>Finessing the DSL: Built-in support for some constants like Math.PI might be a nice touch, though not strictly necessary as ${Math.PI} already works well.</li><li>Hex literals! In case anyone wants to animate things with them? (I am only <em>half</em> joking.)</li><li>Some form of support for 2D vectors (Animated.ValueXY) would be awesome.</li><li>Further down the line, maybe a more ambitious DSL that targets Animated’s other capabilities (interpolation and sequencing come to mind). We’ll see!</li></ul><p>Please feel free to get involved on GitHub and reach out with your thoughts/ideas. 🎉</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=193ddb36fda1" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>