@atlaskit/css 0.11.0 → 0.12.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # @atlaskit/css
2
2
 
3
+ ## 0.12.0
4
+
5
+ ### Minor Changes
6
+
7
+ - [#177956](https://bitbucket.org/atlassian/atlassian-frontend-monorepo/pull-requests/177956)
8
+ [`124aac4a851f1`](https://bitbucket.org/atlassian/atlassian-frontend-monorepo/commits/124aac4a851f1) -
9
+ Added metric typography tokens which can be used via the CSS `font` shorthand.
10
+
11
+ ### Patch Changes
12
+
13
+ - Updated dependencies
14
+
3
15
  ## 0.11.0
4
16
 
5
17
  ### Minor Changes
@@ -96,7 +96,15 @@ function replaceXcssWithCssMap(
96
96
  let firstUsagePath: ASTPath<core.Node> | null = null;
97
97
  const styleVariables = new Set<string>();
98
98
  const variableDependencies = new Map<string, Set<string>>();
99
-
99
+ const inlineXcssUsages: Array<{
100
+ path: ASTPath<core.CallExpression>;
101
+ keyName: string;
102
+ jsxAttribute: ASTPath<core.JSXAttribute>;
103
+ componentName: string;
104
+ variableName: string;
105
+ }> = [];
106
+
107
+ // First pass: collect all xcss variable declarations
100
108
  source
101
109
  .find(j.CallExpression, {
102
110
  // get all xcss calls
@@ -137,11 +145,49 @@ function replaceXcssWithCssMap(
137
145
  variableDependencies.set(variableName, dependencies);
138
146
  }
139
147
  }
148
+ } else {
149
+ // This is an inline xcss call - check if it's inside a JSX attribute
150
+ let current = path.parentPath;
151
+ let jsxElement: ASTPath<core.JSXElement> | null = null;
152
+
153
+ while (current) {
154
+ if (
155
+ current.node.type === 'JSXAttribute' &&
156
+ current.node.name.type === 'JSXIdentifier' &&
157
+ current.node.name.name === 'xcss'
158
+ ) {
159
+ // Found inline xcss usage - now find the JSX element
160
+ let elementCurrent = current.parentPath;
161
+ while (elementCurrent) {
162
+ if (elementCurrent.node.type === 'JSXElement') {
163
+ jsxElement = elementCurrent as ASTPath<core.JSXElement>;
164
+ break;
165
+ }
166
+ elementCurrent = elementCurrent.parentPath;
167
+ }
168
+
169
+ if (jsxElement && jsxElement.node.openingElement.name.type === 'JSXIdentifier') {
170
+ const componentName = jsxElement.node.openingElement.name.name;
171
+ const variableName = `${componentName.toLowerCase()}Styles`;
172
+ const keyName = 'root';
173
+
174
+ inlineXcssUsages.push({
175
+ path,
176
+ keyName,
177
+ jsxAttribute: current as ASTPath<core.JSXAttribute>,
178
+ componentName,
179
+ variableName,
180
+ });
181
+ }
182
+ break;
183
+ }
184
+ current = current.parentPath;
185
+ }
140
186
  }
141
187
  }
142
188
  });
143
189
 
144
- // find the first usage of any style variable
190
+ // find the first usage of any style variable or inline xcss
145
191
  source.find(j.Identifier).forEach((path) => {
146
192
  if (
147
193
  styleVariables.has(path.node.name) &&
@@ -156,7 +202,15 @@ function replaceXcssWithCssMap(
156
202
  }
157
203
  });
158
204
 
159
- // find all xcss function calls
205
+ // Check for inline xcss usage as well for first usage
206
+ if (
207
+ inlineXcssUsages.length > 0 &&
208
+ (!firstUsagePath || inlineXcssUsages[0].path.node.loc?.start)
209
+ ) {
210
+ firstUsagePath = inlineXcssUsages[0].path;
211
+ }
212
+
213
+ // Process variable xcss declarations
160
214
  source
161
215
  .find(j.CallExpression, {
162
216
  callee: {
@@ -202,7 +256,40 @@ function replaceXcssWithCssMap(
202
256
  }
203
257
  });
204
258
 
205
- // create new cssMap with the combined xcss object properties
259
+ // Process inline xcss usages - create separate cssMap for each
260
+ const cssMapDeclarations: core.VariableDeclaration[] = [];
261
+
262
+ inlineXcssUsages.forEach(({ path, keyName, jsxAttribute, componentName, variableName }) => {
263
+ const args = path.node.arguments;
264
+ if (args.length === 1 && args[0].type === 'ObjectExpression') {
265
+ // Create separate cssMap declaration for each inline xcss
266
+ const cssMapObject = j.objectProperty(
267
+ j.identifier(keyName),
268
+ ensureSelectorAmpersand(j, args[0]),
269
+ );
270
+
271
+ const cssMapVariableDeclaration = j.variableDeclaration('const', [
272
+ j.variableDeclarator(
273
+ j.identifier(variableName),
274
+ j.callExpression(j.identifier('cssMap'), [j.objectExpression([cssMapObject])]),
275
+ ),
276
+ ]);
277
+
278
+ cssMapDeclarations.push(cssMapVariableDeclaration);
279
+
280
+ // Update the JSX attribute to use the cssMap reference
281
+ jsxAttribute.node.value = j.jsxExpressionContainer(
282
+ j.memberExpression(j.identifier(variableName), j.identifier(keyName)),
283
+ );
284
+
285
+ // Track first xcss call if not already set
286
+ if (!firstXcssPath) {
287
+ firstXcssPath = path;
288
+ }
289
+ }
290
+ });
291
+
292
+ // create new cssMap with the combined xcss object properties (for variable declarations)
206
293
  if (cssMapProperties.length > 0) {
207
294
  const cssMapObject = j.objectExpression(cssMapProperties);
208
295
  const cssMapVariableDeclaration = j.variableDeclaration('const', [
@@ -212,44 +299,46 @@ function replaceXcssWithCssMap(
212
299
  ),
213
300
  ]);
214
301
 
215
- // insert the cssMap declaration before its first usage
216
- if (firstUsagePath) {
217
- const programBody = source.get().node.program.body;
218
- let insertIndex = 0;
219
-
220
- // find the last variable declaration that any style depends on
221
- let lastDependencyIndex = -1;
222
- for (let i = 0; i < programBody.length; i++) {
223
- const node = programBody[i];
224
- if (node.type === 'VariableDeclaration') {
225
- const varName = node.declarations[0]?.id.name;
226
- if (varName) {
227
- // check if this variable is a dependency of any style
228
- for (const [, deps] of variableDependencies.entries()) {
229
- if (deps.has(varName)) {
230
- lastDependencyIndex = i;
231
- break;
232
- }
302
+ cssMapDeclarations.unshift(cssMapVariableDeclaration);
303
+ }
304
+
305
+ // Insert all cssMap declarations
306
+ if (cssMapDeclarations.length > 0 && firstUsagePath) {
307
+ const programBody = source.get().node.program.body;
308
+ let insertIndex = 0;
309
+
310
+ // find the last variable declaration that any style depends on
311
+ let lastDependencyIndex = -1;
312
+ for (let i = 0; i < programBody.length; i++) {
313
+ const node = programBody[i];
314
+ if (node.type === 'VariableDeclaration') {
315
+ const varName = node.declarations[0]?.id.name;
316
+ if (varName) {
317
+ // check if this variable is a dependency of any style
318
+ for (const [, deps] of variableDependencies.entries()) {
319
+ if (deps.has(varName)) {
320
+ lastDependencyIndex = i;
321
+ break;
233
322
  }
234
323
  }
235
324
  }
236
325
  }
326
+ }
237
327
 
238
- // insert after the last dependency
239
- if (lastDependencyIndex !== -1) {
240
- insertIndex = lastDependencyIndex + 1;
241
- } else {
242
- // if no dependencies, find the first non-import/type declaration
243
- insertIndex = programBody.findIndex(
244
- (node: { type: string }) =>
245
- node.type !== 'ImportDeclaration' &&
246
- node.type !== 'TypeAlias' &&
247
- node.type !== 'InterfaceDeclaration',
248
- );
249
- }
250
-
251
- programBody.splice(insertIndex, 0, cssMapVariableDeclaration);
328
+ // insert after the last dependency
329
+ if (lastDependencyIndex !== -1) {
330
+ insertIndex = lastDependencyIndex + 1;
331
+ } else {
332
+ // if no dependencies, find the first non-import/type declaration
333
+ insertIndex = programBody.findIndex(
334
+ (node: { type: string }) =>
335
+ node.type !== 'ImportDeclaration' &&
336
+ node.type !== 'TypeAlias' &&
337
+ node.type !== 'InterfaceDeclaration',
338
+ );
252
339
  }
340
+
341
+ programBody.splice(insertIndex, 0, ...cssMapDeclarations);
253
342
  }
254
343
 
255
344
  // First handle backgroundColor transformation for Box components
@@ -387,19 +476,35 @@ function replaceXcssWithCssMap(
387
476
  if (value && value.type === 'JSXExpressionContainer') {
388
477
  const expression = value.expression;
389
478
  if (expression.type === 'Identifier') {
390
- // <Box xcss={buttonStyles} /> -> <Box xcss={styles.button} />
391
- expression.name = `styles.${getCssMapKey(expression.name)}`;
479
+ if (styleVariables.has(expression.name)) {
480
+ // <Box xcss={buttonStyles} /> => <Box xcss={styles.button} />
481
+ path.node.value = j.jsxExpressionContainer(
482
+ j.memberExpression(
483
+ j.identifier('styles'),
484
+ j.identifier(getCssMapKey(expression.name)),
485
+ ),
486
+ );
487
+ }
392
488
  } else if (expression.type === 'ArrayExpression') {
393
- // <Box xcss={[baseStyles, otherStyles]} /> -> <Box xcss={[styles.base, styles.other]} />
489
+ // <Box xcss={[baseStyles, otherStyles]} /> => <Box xcss={[styles.base, styles.other]} />
394
490
  expression.elements.forEach((element) => {
395
- if (element?.type === 'Identifier') {
396
- element.name = `styles.${getCssMapKey(element.name)}`;
491
+ if (element?.type === 'Identifier' && styleVariables.has(element.name)) {
492
+ const memberExpression = j.memberExpression(
493
+ j.identifier('styles'),
494
+ j.identifier(getCssMapKey(element.name)),
495
+ );
496
+ // Replace the identifier with the member expression
497
+ Object.assign(element, memberExpression);
397
498
  } else if (
398
- // <Box xcss={condition && styles} /> -> <Box xcss={condition && styles.root} />
499
+ // <Box xcss={condition && styles} /> => <Box xcss={condition && styles.root} />
399
500
  element?.type === 'LogicalExpression' &&
400
- element.right.type === 'Identifier'
501
+ element.right.type === 'Identifier' &&
502
+ styleVariables.has(element.right.name)
401
503
  ) {
402
- element.right.name = `styles.${getCssMapKey(element.right.name)}`;
504
+ element.right = j.memberExpression(
505
+ j.identifier('styles'),
506
+ j.identifier(getCssMapKey(element.right.name)),
507
+ );
403
508
  }
404
509
  });
405
510
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@atlaskit/css",
3
- "version": "0.11.0",
3
+ "version": "0.12.0",
4
4
  "description": "Style components backed by Atlassian Design System design tokens powered by Compiled CSS-in-JS.",
5
5
  "author": "Atlassian Pty Ltd",
6
6
  "license": "Apache-2.0",
@@ -49,7 +49,7 @@
49
49
  "./test-utils": "./src/test-utils/index.tsx"
50
50
  },
51
51
  "dependencies": {
52
- "@atlaskit/tokens": "^5.3.0",
52
+ "@atlaskit/tokens": "^5.4.0",
53
53
  "@babel/runtime": "^7.0.0",
54
54
  "@compiled/react": "^0.18.3"
55
55
  },
@@ -60,7 +60,7 @@
60
60
  "@af/visual-regression": "workspace:^",
61
61
  "@atlaskit/button": "^23.2.0",
62
62
  "@atlaskit/ds-lib": "^4.0.0",
63
- "@atlaskit/primitives": "^14.9.0",
63
+ "@atlaskit/primitives": "^14.10.0",
64
64
  "@emotion/react": "^11.7.1",
65
65
  "@testing-library/react": "^13.4.0",
66
66
  "@types/jscodeshift": "^0.11.0",