-
-
Notifications
You must be signed in to change notification settings - Fork 39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Enable sx
prop on any components
#37
Changes from 17 commits
4b98e48
695b502
6b8d613
2f6b209
438f2a4
ed9d466
451b6dc
cb60b51
4ccdd28
44bd1b5
6e11cd3
6c09a12
9a853d0
ede976b
c1ec425
49fab1b
f680a67
118c6be
344c70a
b3ea954
ace4f80
1259b9e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,17 @@ | ||
import Image from 'next/image'; | ||
import { styled, css } from '@pigment-css/react'; | ||
import Button from '@mui/material/Button'; | ||
import styles from './page.module.css'; | ||
|
||
declare global { | ||
namespace React { | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
interface HTMLAttributes<T> { | ||
sx?: any; | ||
} | ||
Comment on lines
+9
to
+11
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just to test that we can modify the types of JSX elements. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice, we can use the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's for POC at this point. There is one issue to fix from Pigment side which is delegating Once that's done, I will add docs for it. |
||
} | ||
} | ||
|
||
const visuallyHidden = css({ | ||
border: 0, | ||
clip: 'rect(0 0 0 0)', | ||
|
@@ -93,8 +103,16 @@ export default function Home() { | |
return ( | ||
<Main> | ||
<div className={visuallyHidden}>I am invisible</div> | ||
<Description> | ||
<p> | ||
<Description | ||
sx={{ | ||
'&::before': { | ||
content: '"🚀"', | ||
display: 'inline-block', | ||
border: '1px solid', | ||
}, | ||
}} | ||
> | ||
<p sx={{ boxShadow: '0 0 4px 0 rgba(0 0 0 / 0.12)' }}> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I ❤️ this! |
||
Get started by editing | ||
<code className={styles.code}>src/app/page.tsx</code> | ||
</p> | ||
|
@@ -117,7 +135,12 @@ export default function Home() { | |
</div> | ||
</Description> | ||
|
||
<div className={styles.center}> | ||
<div | ||
className={styles.center} | ||
sx={{ | ||
border: '1px solid', | ||
}} | ||
> | ||
<Image | ||
className={styles.logo} | ||
src="/next.svg" | ||
|
@@ -176,6 +199,18 @@ export default function Home() { | |
</h2> | ||
<p>Instantly deploy your Next.js site to a shareable URL with Vercel.</p> | ||
</a> | ||
<Button | ||
sx={{ | ||
color: 'red', | ||
backgroundColor: 'blue', | ||
'&:hover': { | ||
color: 'blue', | ||
backgroundColor: 'red', | ||
}, | ||
}} | ||
> | ||
Hello | ||
</Button> | ||
</div> | ||
</Main> | ||
); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
/processors/ | ||
/utils/ | ||
LICENSE | ||
/private-runtime/ |
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import type { Expression } from '@babel/types'; | ||
import type { NodePath } from '@babel/core'; | ||
import type { CallExpression, Expression } from '@babel/types'; | ||
import { | ||
validateParams, | ||
type Params, | ||
|
@@ -10,31 +11,7 @@ import type { IOptions } from './styled'; | |
import { processCssObject } from '../utils/processCssObject'; | ||
import { cssFnValueToVariable } from '../utils/cssFnValueToVariable'; | ||
import BaseProcessor from './base-processor'; | ||
|
||
// @TODO: Maybe figure out a better way allow imports. | ||
const allowedSxTransformImports = [`${process.env.PACKAGE_NAME}/Box`]; | ||
|
||
/** | ||
* Specifically looks for whether the sx prop should be transformed or not. | ||
* If it's a Pigment CSS styled component, the value of `argumentValue` will | ||
* be a string className starting with "." | ||
* In other cases, it explicitly checks if the import is allowed for sx transformation. | ||
*/ | ||
function allowSxTransform(argumentExpression: ExpressionValue, argumentValue?: string) { | ||
if (!argumentExpression) { | ||
return false; | ||
} | ||
if ( | ||
argumentExpression.kind === ValueType.LAZY && | ||
argumentExpression.importedFrom?.some((i) => allowedSxTransformImports.includes(i)) | ||
) { | ||
return true; | ||
} | ||
if (argumentValue && argumentValue[0] === '.') { | ||
return true; | ||
} | ||
return false; | ||
} | ||
import spreadSxProp from '../utils/spreadSxProp'; | ||
|
||
export class SxProcessor extends BaseProcessor { | ||
sxArguments: ExpressionValue[] = []; | ||
|
@@ -66,10 +43,6 @@ export class SxProcessor extends BaseProcessor { | |
} | ||
} | ||
|
||
if (!allowSxTransform(elementClassExpression, this.elementClassName)) { | ||
return; | ||
} | ||
|
||
let cssText: string = ''; | ||
if (sxStyle.kind === ValueType.CONST) { | ||
if (sxStyle.ex.type === 'StringLiteral') { | ||
|
@@ -79,16 +52,13 @@ export class SxProcessor extends BaseProcessor { | |
const styleObjOrFn = values.get(sxStyle.ex.name); | ||
cssText = this.processCss(styleObjOrFn, sxStyle); | ||
} | ||
const selector = this.elementClassName | ||
? `${this.elementClassName}${this.asSelector}` | ||
: this.asSelector; | ||
|
||
if (!cssText) { | ||
return; | ||
} | ||
|
||
const rules: Rules = { | ||
[selector]: { | ||
[this.asSelector]: { | ||
className: this.className, | ||
cssText, | ||
displayName: this.displayName, | ||
|
@@ -123,6 +93,7 @@ export class SxProcessor extends BaseProcessor { | |
if (this.artifacts.length === 0) { | ||
return; | ||
} | ||
let result = this.value; | ||
if (this.collectedVariables.length) { | ||
const varProperties: ReturnType<typeof t.objectProperty>[] = this.collectedVariables.map( | ||
([variableId, expression, isUnitLess]) => { | ||
|
@@ -156,10 +127,31 @@ export class SxProcessor extends BaseProcessor { | |
t.objectProperty(t.identifier('className'), t.stringLiteral(this.className)), | ||
t.objectProperty(t.identifier('vars'), t.objectExpression(varProperties)), | ||
]); | ||
this.replacer(obj, false); | ||
} else { | ||
this.replacer(this.value, false); | ||
result = obj; | ||
} | ||
|
||
/** | ||
* Replace the sx call with the transformed result. It works for both JSX and non-JSX calls. | ||
* | ||
* For example: | ||
* <Component sx={_sx({ color: 'red' })} /> to <Component sx={_sx('sd5jss7')} /> | ||
* <Component sx={_sx({ bgcolor: 'red', color: props.color })} /> to <Component sx={_sx({ className: 'bc1d15y', vars: { 'bc1d15y-0': [props.color, false], }})} /> | ||
*/ | ||
this.replacer((_tagPath) => { | ||
const tagPath = _tagPath as NodePath<CallExpression>; | ||
return t.callExpression(tagPath.get('callee').node, [result]); | ||
}, false); | ||
|
||
/** | ||
* Replace the sx prop with runtime sx | ||
*/ | ||
this.replacer((_tagPath) => { | ||
const tagPath = _tagPath as NodePath<CallExpression>; | ||
siriwatknp marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Casting type like this will fail So I have to create a new variable instead. |
||
|
||
spreadSxProp(tagPath); | ||
|
||
return tagPath.node; | ||
}, false); | ||
} | ||
|
||
get asSelector(): string { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,24 @@ | ||
export default function sx(styleObj) { | ||
return styleObj; | ||
export default function sx(transformedSx, { className, style }) { | ||
const sxClass = typeof transformedSx === 'string' ? transformedSx : transformedSx?.className; | ||
const sxVars = | ||
transformedSx && typeof transformedSx !== 'string' ? transformedSx.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: `${sxClass}${className ? ` ${className}` : ''}`, | ||
style: { | ||
...varStyles, | ||
...style, | ||
}, | ||
}; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ignore
sx
prop on JSX html tag.