Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
terrysahaidak committed Dec 25, 2019
0 parents commit 26fdee2
Show file tree
Hide file tree
Showing 12 changed files with 4,096 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test
12 changes: 12 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"useTabs": false,
"printWidth": 70,
"tabWidth": 2,
"singleQuote": true,
"trailingComma": "all",
"jsxBracketSameLine": false,
"parser": "babel",
"noSemi": false,
"rcVerbose": true,
"arrowParens": "always"
}
24 changes: 24 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
This is free and unencumbered software released into the public domain.

Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.

In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

For more information, please refer to <https://unlicense.org>
113 changes: 113 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
# expression-node.macro

## Intro

This macro will transform a javascript block to be react native animated expression. Example:

```js
import { Animated } from 'react-native';
const { E } = Animated;
import re from 'expression-node.macro';

useOnFrameExpression(() => {
const normalizedPan = re(panY - panYOffset);

const min = (a, b) => re(a < b ? a : b);

const canSwipeMore = re(
gestureState === State.ACTIVE && scrollY <= 0,
);

return re(() => {
// memoize last panY value in order to subtract it later from the real panY
if (gestureState === State.BEGAN) {
panYOffset = scrollY;
}

// check if the user has already scrolled all the way to the top
if (canSwipeMore) {
// if so - allow user to scroll more to the top
// in order to show the refresh controls
marginTop = panY >= 0 ? min(MAX_OVERSCROLL, normalizedPan) : 0;
}
});
});
```

Will transform to:

```js
import { Animated } from 'react-native';
const { E } = Animated;
import re from 'expression-node.macro';

useOnFrameExpression(() => {
const normalizedPan = E.sub(panY, panYOffset);

const min = (a, b) => E.cond(E.lessThan(a, b), a, b);

const canSwipeMore = E.and(
E.eq(gestureState, State.ACTIVE),
E.lessOrEq(scrollY, 0),
);

return E.block(
E.block([
E.cond(
E.eq(gestureState, State.BEGAN),
E.block([E.set(panYOffset, scrollY)]),
),
E.cond(
canSwipeMore,
E.block([
E.set(
marginTop,
E.cond(
E.greaterOrEq(panY, 0),
min(MAX_OVERSCROLL, normalizedPan),
0,
),
),
]),
),
]),
);
});
```

## Installation

```sh
npm add -D terrysahaidak/expression-node.macro
```

## Usage

Make sure you have Animated.E in scope:

```js
import { Animated } from 'react-native';
const { E } = Animated;
```

Then you should be able to import `re` from the package. See example for usage cases.

## Config

Define the identifier to the reanimated config name to tell the macro what identifier holds the Reanimated value.

```js
const plugins = [
['macros',{
expressionNode: {
identifier: 'Animated'
}
}]
];
```

## License

Based on [js-to-reanimated.macro](https://github.com/kkirby/js-to-reanimated.macro) by [kkirby](https://github.com/kkirby).

MIT
27 changes: 27 additions & 0 deletions babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const presets = [
[
'@babel/env',
{
targets: {
node: 'current',
},
useBuiltIns: 'usage',
corejs: 3,
modules: false,
},
],
];

const plugins = [
[
'macros',
// {
// jsToReanimated: {
// identifier: 'E',
// },
// },
],
'@babel/plugin-proposal-class-properties',
];

module.exports = { presets, plugins };
Loading

0 comments on commit 26fdee2

Please sign in to comment.