-
-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[pigment-css][react] Implement sx transform for system components (#13)
- Loading branch information
1 parent
48faf63
commit ed86fd6
Showing
18 changed files
with
309 additions
and
154 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
/processors/ | ||
/utils/ | ||
LICENSE | ||
/private-runtime/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
packages/pigment-css-react/src/private-runtime/ForwardSx.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import * as React from 'react'; | ||
import clsx from 'clsx'; | ||
|
||
function useSx(sx, className, style) { | ||
const sxClass = typeof sx === 'string' ? sx : sx?.className; | ||
const sxVars = sx && typeof sx !== 'string' ? sx.vars : undefined; | ||
const varStyles = {}; | ||
|
||
if (sxVars) { | ||
Object.entries(sxVars).forEach(([cssVariable, [value, isUnitLess]]) => { | ||
if (typeof value === 'string' || isUnitLess) { | ||
varStyles[`--${cssVariable}`] = value; | ||
} else { | ||
varStyles[`--${cssVariable}`] = `${value}px`; | ||
} | ||
}); | ||
} | ||
|
||
return { | ||
className: clsx(sxClass, className), | ||
style: { | ||
...varStyles, | ||
...style, | ||
}, | ||
}; | ||
} | ||
|
||
/* eslint-disable-next-line react/prop-types */ | ||
export const ForwardSx = React.forwardRef(({ sx, sxComponent, className, style, ...rest }, ref) => { | ||
const Component = sxComponent; | ||
return <Component ref={ref} {...rest} {...useSx(sx, className, style)} />; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './ForwardSx'; |
82 changes: 0 additions & 82 deletions
82
packages/pigment-css-react/src/utils/pre-linaria-plugin.ts
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
import { addNamed } from '@babel/helper-module-imports'; | ||
import { declare } from '@babel/helper-plugin-utils'; | ||
import { NodePath } from '@babel/core'; | ||
import * as Types from '@babel/types'; | ||
|
||
import { sxPropConverter } from './sxPropConverter'; | ||
|
||
function convertJsxMemberExpressionToMemberExpression( | ||
t: typeof Types, | ||
nodePath: NodePath<Types.JSXMemberExpression>, | ||
): Types.MemberExpression { | ||
const object = nodePath.get('object'); | ||
const property = nodePath.get('property'); | ||
|
||
if (object.isJSXMemberExpression()) { | ||
return t.memberExpression( | ||
convertJsxMemberExpressionToMemberExpression(t, object), | ||
t.identifier(property.node.name), | ||
); | ||
} | ||
return t.memberExpression( | ||
t.identifier((object.node as Types.JSXIdentifier).name), | ||
t.identifier(property.node.name), | ||
); | ||
} | ||
|
||
function wrapWithSxComponent( | ||
t: typeof Types, | ||
tagNamePath: NodePath<Types.JSXIdentifier | Types.JSXMemberExpression | Types.JSXNamespacedName>, | ||
sxComponentName: string, | ||
) { | ||
const sxComponent = addNamed( | ||
tagNamePath, | ||
sxComponentName, | ||
`${process.env.PACKAGE_NAME}/private-runtime`, | ||
); | ||
const jsxElement = tagNamePath.findParent((p) => p.isJSXElement()); | ||
if (!jsxElement?.isJSXElement()) { | ||
return; | ||
} | ||
const component = t.jsxIdentifier(sxComponent.name); | ||
|
||
const newChildren = (jsxElement.get('children') ?? []).map((child) => child.node); | ||
let sxComponentValue: Types.Identifier | Types.MemberExpression | null = null; | ||
|
||
if (tagNamePath.isJSXIdentifier()) { | ||
sxComponentValue = t.identifier(tagNamePath.node.name); | ||
} else if (tagNamePath.isJSXMemberExpression()) { | ||
sxComponentValue = convertJsxMemberExpressionToMemberExpression(t, tagNamePath); | ||
} | ||
|
||
const newElement = t.jsxElement( | ||
t.jsxOpeningElement( | ||
component, | ||
[ | ||
t.jsxAttribute( | ||
t.jsxIdentifier('sxComponent'), | ||
t.jsxExpressionContainer(sxComponentValue ?? t.nullLiteral()), | ||
), | ||
...jsxElement | ||
.get('openingElement') | ||
.get('attributes') | ||
.map((attr) => attr.node), | ||
], | ||
!newChildren.length, | ||
), | ||
newChildren.length ? t.jsxClosingElement(component) : null, | ||
newChildren, | ||
!newChildren.length, | ||
); | ||
jsxElement.replaceWith(newElement); | ||
} | ||
|
||
function replaceNodePath( | ||
expressionPath: NodePath<Types.Expression>, | ||
namePath: NodePath<Types.JSXIdentifier | Types.Identifier>, | ||
importName: string, | ||
t: typeof Types, | ||
tagNamePath: NodePath< | ||
Types.JSXIdentifier | Types.Identifier | Types.JSXMemberExpression | Types.MemberExpression | ||
>, | ||
sxComponentName: string, | ||
) { | ||
const sxIdentifier = addNamed(namePath, importName, process.env.PACKAGE_NAME as string); | ||
let wasSxTransformed = false; | ||
|
||
const wrapWithSxCall = (expPath: NodePath<Types.Expression>) => { | ||
let tagNameArg: Types.Identifier | Types.MemberExpression | null = null; | ||
if (tagNamePath.isJSXIdentifier()) { | ||
tagNameArg = t.identifier(tagNamePath.node.name); | ||
} else if (tagNamePath.isJSXMemberExpression()) { | ||
tagNameArg = convertJsxMemberExpressionToMemberExpression(t, tagNamePath); | ||
} else { | ||
tagNameArg = tagNamePath.node as Types.Identifier | Types.MemberExpression; | ||
} | ||
expPath.replaceWith(t.callExpression(sxIdentifier, [expPath.node, tagNameArg])); | ||
wasSxTransformed = true; | ||
}; | ||
|
||
sxPropConverter(expressionPath, wrapWithSxCall); | ||
|
||
if (wasSxTransformed) { | ||
if (tagNamePath.isJSXIdentifier() || tagNamePath.isJSXMemberExpression()) { | ||
wrapWithSxComponent(t, tagNamePath, sxComponentName); | ||
} | ||
} | ||
} | ||
|
||
export const babelPlugin = declare<{ | ||
propName?: string; | ||
importName?: string; | ||
sxComponentName?: string; | ||
}>((api, { propName = 'sx', importName = 'sx', sxComponentName = 'ForwardSx' }) => { | ||
api.assertVersion(7); | ||
const { types: t } = api; | ||
return { | ||
name: '@pigmentcss/sx-plugin', | ||
visitor: { | ||
JSXAttribute(path) { | ||
const namePath = path.get('name'); | ||
const openingElement = path.findParent((p) => p.isJSXOpeningElement()); | ||
if ( | ||
!openingElement || | ||
!openingElement.isJSXOpeningElement() || | ||
!namePath.isJSXIdentifier() || | ||
namePath.node.name !== propName | ||
) { | ||
return; | ||
} | ||
const tagName = openingElement.get('name'); | ||
const valuePath = path.get('value'); | ||
if (!valuePath.isJSXExpressionContainer()) { | ||
return; | ||
} | ||
const expressionPath = valuePath.get('expression'); | ||
if (!expressionPath.isExpression()) { | ||
return; | ||
} | ||
// @ts-ignore | ||
replaceNodePath(expressionPath, namePath, importName, t, tagName, sxComponentName); | ||
}, | ||
ObjectProperty(path) { | ||
// @TODO - Maybe add support for React.createElement calls as well. | ||
// Right now, it only checks for jsx(),jsxs(),jsxDEV() and jsxsDEV() calls. | ||
const keyPath = path.get('key'); | ||
if (!keyPath.isIdentifier() || keyPath.node.name !== propName) { | ||
return; | ||
} | ||
const valuePath = path.get('value'); | ||
if (!valuePath.isObjectExpression() && !valuePath.isArrowFunctionExpression()) { | ||
return; | ||
} | ||
const parentJsxCall = path.findParent((p) => p.isCallExpression()); | ||
if (!parentJsxCall || !parentJsxCall.isCallExpression()) { | ||
return; | ||
} | ||
const callee = parentJsxCall.get('callee'); | ||
if (!callee.isIdentifier() || !callee.node.name.includes('jsx')) { | ||
return; | ||
} | ||
const jsxElement = parentJsxCall.get('arguments')[0] as NodePath<Types.Identifier>; | ||
replaceNodePath(valuePath, keyPath, importName, t, jsxElement, sxComponentName); | ||
}, | ||
}, | ||
}; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
.bc1d15y { | ||
._c1d15y { | ||
margin: 0; | ||
margin-block: 1rem; | ||
padding: 0; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,10 @@ | ||
import { sx as _sx2 } from '@pigment-css/react'; | ||
import { ForwardSx as _ForwardSx } from '@pigment-css/react/private-runtime'; | ||
import Box from '@pigment-css/react/Box'; | ||
export function App() { | ||
return ( | ||
<Box as="ul" sx={'bc1d15y'}> | ||
<_ForwardSx sxComponent={Box} as="ul" sx={'_c1d15y'}> | ||
Hello Box | ||
</Box> | ||
</_ForwardSx> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.