Skip to content

Instantly share code, notes, and snippets.

@astexplorer
Last active October 29, 2018 03:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save astexplorer/a206145e34e9ec363df8c6cd5bd9de75 to your computer and use it in GitHub Desktop.
Save astexplorer/a206145e34e9ec363df8c6cd5bd9de75 to your computer and use it in GitHub Desktop.
{
"v": 2,
"parserID": "babylon7",
"toolID": "babelv7",
"settings": {
"babylon7": null
},
"versions": {
"babylon7": "7.0.0",
"babelv7": "7.0.0"
}
}
const insatiable = require('@tvui/insatiable');
const Header = insatiable(
{
textStyle: {
size: 30,
color: 'blue'
}
},
({ text, color }) => {
return {
text,
textStyle: {
color
}
};
}
);
export default function (babel) {
const { types: t } = babel;
return {
visitor: {
CallExpression(path) {
if (path.get('callee.name').node !== 'insatiable') {
return;
}
const staticStyle = path.get('arguments.0');
const calculateStyle = path.get('arguments.1');
const props = path.get('arguments.1.params.0.properties').map(p => p.node).map((n) => n.key.name);
path.replaceWithSourceString(`
(function createStyledComponent() {
const staticStyle = ${staticStyle};
const calculateStyle = ${calculateStyle};
const Component = function(props) {
return React.createElement(
'widget',
{
staticStyle: staticStyle,
style: calculateStyle(props)
},
props.children
);
};
const areEqual = function(prevProps, nextProps) {
return prevProps === nextProps || ${props.map((name) => `prevProps.${name} === nextProps.${name}`).join('&&')}
}
return React.memo(Component, areEqual);
}())
`);
}
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment