Skip to content
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

feat(atomic): add support for atomic using styled API #966

Merged
merged 5 commits into from
May 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/atomic/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
},
"dependencies": {
"@linaria/utils": "^3.0.0-beta.18",
"@linaria/react": "^3.0.0-beta.18",
"known-css-properties": "^0.24.0",
"postcss": "^8.3.11",
"stylis": "^3.5.4"
Expand Down
6 changes: 4 additions & 2 deletions packages/atomic/src/atomize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function hashProperty(property: string) {
return slugify(property);
}

export default function atomize(cssText: string) {
export default function atomize(cssText: string, hasPriority = false) {
stylis.set({
prefix: false,
keyframe: false,
Expand Down Expand Up @@ -91,7 +91,9 @@ export default function atomize(cssText: string) {
const className = `atm_${propertySlug}_${valueSlug}`;

const propertyPriority =
getPropertyPriority(decl.prop) + (hasAtRule ? 1 : 0);
getPropertyPriority(decl.prop) +
(hasAtRule ? 1 : 0) +
(hasPriority ? 1 : 0);
const processedCss = stylis(`.${className}`.repeat(propertyPriority), css);

atomicRules.push({
Expand Down
1 change: 1 addition & 0 deletions packages/atomic/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export { default as css } from './css';
export { default as atomize } from './atomize';
export { styled } from '@linaria/react';
export { cx } from '@linaria/utils';

export type { CSSProperties } from './CSSProperties';
2 changes: 1 addition & 1 deletion packages/atomic/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": { "paths": {}, "rootDir": "src/" },
"references": [{ "path": "../utils" }]
"references": [{ "path": "../react" }, { "path": "../utils" }]
}
213 changes: 213 additions & 0 deletions packages/babel/__tests__/__snapshots__/babel.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,177 @@ Dependencies: NA

`;

exports[`compiles atomic styled with dynamic interpolations as unique variables based on the interpolation text 1`] = `
"/* @flow */
import { styled } from '@linaria/atomic';
const Component = /*#__PURE__*/styled(\\"div\\")({
name: \\"Component\\",
vars: {
\\"1p69eoh\\": [props => props.color],
\\"l12cfn\\": [props => props.backgroundColor]
},
class: \\"atm_7l_15g95l5 atm_4b_15g95l5 atm_2d_1en4k16 c17gu1mi\\",
atomic: true
});
const Component2 = /*#__PURE__*/styled(\\"div\\")({
name: \\"Component2\\",
vars: {
\\"1p69eoh\\": [props => props.color],
\\"1n9iccq\\": [props => props.color || \\"black\\"]
},
class: \\"atm_7l_15g95l5 atm_4b_1kgl01d cccybe9\\",
atomic: true
});
console.log(Component, Component2);"
`;

exports[`compiles atomic styled with dynamic interpolations as unique variables based on the interpolation text 2`] = `

CSS:

.atm_7l_15g95l5{color:var(--1p69eoh);}
.atm_4b_15g95l5.atm_4b_15g95l5{border-color:var(--1p69eoh);}
.atm_2d_1en4k16.atm_2d_1en4k16{background-color:var(--l12cfn);}
.atm_4b_1kgl01d.atm_4b_1kgl01d{border-color:var(--1n9iccq);}

Dependencies: NA

`;

exports[`compiles atomic styled with plain css, static and dynamic interpolations 1`] = `
"/* @flow */
import { styled } from '@linaria/atomic';
const Component = /*#__PURE__*/styled(\\"div\\")({
name: \\"Component\\",
vars: {
\\"1p69eoh\\": [props => props.color]
},
class: \\"atm_7l_13q2bts atm_e2_12xxubj atm_gi_12am3vd atm_2d_15g95l5 c17gu1mi\\",
atomic: true
});
console.log(Component);"
`;

exports[`compiles atomic styled with plain css, static and dynamic interpolations 2`] = `

CSS:

.atm_7l_13q2bts{color:blue;}
.atm_e2_12xxubj{height:100px;}
.atm_gi_12am3vd{margin:50px;}
.atm_2d_15g95l5.atm_2d_15g95l5{background-color:var(--1p69eoh);}

Dependencies: NA

`;

exports[`compiles atomic styled with static css 1`] = `
"/* @flow */
import { styled } from '@linaria/atomic';
const Component = /*#__PURE__*/styled(\\"div\\")({
name: \\"Component\\",
class: \\"atm_7l_13q2bts atm_e2_12xxubj c17gu1mi\\",
atomic: true
});
console.log(Component);"
`;

exports[`compiles atomic styled with static css 2`] = `

CSS:

.atm_7l_13q2bts{color:blue;}
.atm_e2_12xxubj{height:100px;}

Dependencies: NA

`;

exports[`compiles atomic styled without colliding by property 1`] = `
"/* @flow */
import { styled } from '@linaria/atomic';
export const Component = /*#__PURE__*/styled(\\"ul\\")({
name: \\"Component\\",
class: \\"atm_9s_1txwivl atm_l8_idpfg4 c17gu1mi\\",
atomic: true
});
export const Component2 = /*#__PURE__*/styled(\\"ul\\")({
name: \\"Component2\\",
class: \\"atm_9s_1ulexfb atm_l8_idpfg4 cccybe9\\",
atomic: true
});"
`;

exports[`compiles atomic styled without colliding by property 2`] = `

CSS:

.atm_9s_1txwivl{display:flex;}
.atm_l8_idpfg4{padding:0;}
.atm_9s_1ulexfb{display:block;}

Dependencies: NA

`;

exports[`compiles atomic styled wrapping other components with extra priority 1`] = `
"/* @flow */
import { styled } from '@linaria/atomic';
const Component = /*#__PURE__*/styled(\\"div\\")({
name: \\"Component\\",
class: \\"atm_2d_13q2bts atm_e2_12xxubj c17gu1mi\\",
atomic: true
});
const ComponentCompositing = /*#__PURE__*/styled(Component)({
name: \\"ComponentCompositing\\",
class: \\"atm_26_5scuol atm_e2_1nzxncv cccybe9\\",
atomic: true
});
console.log(ComponentCompositing);"
`;

exports[`compiles atomic styled wrapping other components with extra priority 2`] = `

CSS:

.atm_2d_13q2bts.atm_2d_13q2bts{background-color:blue;}
.atm_e2_12xxubj{height:100px;}
.atm_26_5scuol.atm_26_5scuol{background:red;}
.atm_e2_1nzxncv.atm_e2_1nzxncv{height:105px;}

Dependencies: NA

`;

exports[`compiles atoms that are shared between css and styled templates 1`] = `
"/* @flow */
import { css } from '@linaria/atomic';
import { styled } from '@linaria/atomic';
const x = \\"atm_26_5scuol atm_e2_12xxubj\\";
const Component = /*#__PURE__*/styled(\\"div\\")({
name: \\"Component\\",
vars: {
\\"1p69eoh\\": [props => props.color]
},
class: \\"atm_26_5scuol atm_e2_12xxubj atm_gi_19bvopo atm_7l_15g95l5 cccybe9\\",
atomic: true
});
console.log(x, Component);"
`;

exports[`compiles atoms that are shared between css and styled templates 2`] = `

CSS:

.atm_26_5scuol{background:red;}
.atm_e2_12xxubj{height:100px;}
.atm_gi_19bvopo{margin:10px;}
.atm_7l_15g95l5{color:var(--1p69eoh);}

Dependencies: NA

`;

exports[`does not include styles if not referenced anywhere 1`] = `
"import { css } from '@linaria/core';
import { styled } from '@linaria/react';
Expand Down Expand Up @@ -673,6 +844,48 @@ Dependencies: NA

`;

exports[`transpiles renamed styled and atomic-styled imports in the same file 1`] = `
"/* @flow */
import { styled as reactStyled } from '@linaria/react';
import { styled as atomicStyled } from '@linaria/atomic';
const StyledComponent = /*#__PURE__*/reactStyled(\\"div\\")({
name: \\"StyledComponent\\",
class: \\"s17gu1mi\\"
});
const StyledComponent2 = /*#__PURE__*/reactStyled(StyledComponent)({
name: \\"StyledComponent2\\",
class: \\"sccybe9\\"
});
const AtomicComponent = /*#__PURE__*/reactStyled(\\"div\\")({
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like error. Should it be atomicStyled instead of reactStyled?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If so, I've already fixed this :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Anber good catch! sorry for missing it!

https://github.com/callstack/linaria/blob/master/packages/babel/src/evaluators/templateProcessor.ts#L331-L339

I understand this should have been replaced to use either localName?.atomicStyled or localName?.styled depending on whether styled.type === 'atomic-styled'or not.

LMK if you'd like me to open a follow up PR with the fix – many thanks!

name: \\"AtomicComponent\\",
class: \\"atm_26_5scuol aw1s2ox\\",
atomic: true
});
const AtomicComponent2 = /*#__PURE__*/reactStyled(AtomicComponent)({
name: \\"AtomicComponent2\\",
class: \\"atm_26_13q2bts a1080ijw\\",
atomic: true
});
console.log(StyledComponent, StyledComponent2, AtomicComponent, AtomicComponent2);"
`;

exports[`transpiles renamed styled and atomic-styled imports in the same file 2`] = `

CSS:

.s17gu1mi {
background: red;
}
.sccybe9 {
background: blue;
}
.atm_26_5scuol{background:red;}
.atm_26_13q2bts.atm_26_13q2bts{background:blue;}

Dependencies: NA

`;

exports[`transpiles renamed styled import 1`] = `
"import { styled as custom } from '@linaria/react';
export const Title = /*#__PURE__*/custom('h1')({
Expand Down