Skip to content

Commit acd8996

Browse files
authoredJan 17, 2023
Fix react runtime ax function returning incorrect result for selectors (#1355)
* Fix react runtime ax function returning incorrect result for selectors * Bump size-limit on ax import
1 parent 527ae4c commit acd8996

File tree

4 files changed

+28
-5
lines changed

4 files changed

+28
-5
lines changed
 

‎.changeset/two-avocados-kick.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@compiled/react': patch
3+
---
4+
5+
Fix react runtime ax function returning incorrect result for selectors

‎package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@
139139
},
140140
{
141141
"path": "./packages/react/dist/browser/runtime.js",
142-
"limit": "166B",
142+
"limit": "189B",
143143
"import": "{ ax }",
144144
"ignore": [
145145
"react"

‎packages/react/src/runtime/__tests__/ax.test.ts

+20-2
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,22 @@ describe('ax', () => {
2525
expect(result).toEqual('_aaaacccc');
2626
});
2727

28+
it('should ensure the last atomic declaration of many single groups wins', () => {
29+
const result = ax(['_aaaabbbb', '_aaaacccc', '_aaaadddd', '_aaaaeeee']);
30+
31+
expect(result).toEqual('_aaaaeeee');
32+
});
33+
2834
it('should ensure the last atomic declaration of a multi group wins', () => {
29-
const result = ax(['_aaaabbbb _aaaacccc', 'foo']);
35+
const result = ax(['_aaaabbbb _aaaacccc']);
36+
37+
expect(result).toEqual('_aaaacccc');
38+
});
39+
40+
it('should ensure the last atomic declaration of many multi groups wins', () => {
41+
const result = ax(['_aaaabbbb _aaaacccc _aaaadddd _aaaaeeee']);
3042

31-
expect(result).toEqual('_aaaacccc foo');
43+
expect(result).toEqual('_aaaaeeee');
3244
});
3345

3446
it('should not remove any atomic declarations if there are no duplicate groups', () => {
@@ -49,4 +61,10 @@ describe('ax', () => {
4961

5062
expect(result).toEqual('hello_there hello_world');
5163
});
64+
65+
it('should ignore non atomic declarations when atomic declarations exist', () => {
66+
const result = ax(['hello_there', 'hello_world', '_aaaabbbb']);
67+
68+
expect(result).toEqual('hello_there hello_world _aaaabbbb');
69+
});
5270
});

‎packages/react/src/runtime/ax.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ const ATOMIC_GROUP_LENGTH = 5;
2828
* @param classes
2929
*/
3030
export default function ax(classNames: (string | undefined | false)[]): string | undefined {
31-
if (classNames.length <= 1) {
32-
// short circuit if theres no custom class names.
31+
if (classNames.length <= 1 && (!classNames[0] || classNames[0].indexOf(' ') === -1)) {
32+
// short circuit if there's no custom class names.
3333
return classNames[0] || undefined;
3434
}
3535

0 commit comments

Comments
 (0)
Please sign in to comment.