-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix the
cx()
function's class collection at runtime as it generated…
… class instances rather than strings. (#1697) * Fix the `cx()` function's class collection at runtime as it generated class instances rather than strings Migrate from `ac()` which generates `AtomicGroups` classes to `ax()` which strictly collects strings. This is because passing `<Component xcss={cx({ … })}>` around resulted in errors, `'cls.split is not a function'` trying to iterate over non-strings. * Add a test for pass-through props to properly utilize the cx function
- Loading branch information
1 parent
18024be
commit 25a4bed
Showing
4 changed files
with
104 additions
and
4 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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@compiled/react': patch | ||
--- | ||
|
||
Fix the `cx()` function's class collection at runtime as it generated class instances rather than strings |
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
95 changes: 95 additions & 0 deletions
95
packages/react/src/create-strict-api/__tests__/pass-through-xcss.test.tsx
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,95 @@ | ||
/** @jsxImportSource @compiled/react */ | ||
import { render } from '@testing-library/react'; | ||
|
||
import { cssMap, type XCSSProp, cx } from './__fixtures__/strict-api'; | ||
|
||
const styles = cssMap({ | ||
rootNative: { | ||
color: 'var(--ds-text)', | ||
background: 'var(--ds-surface)', | ||
}, | ||
rootComponent: { | ||
color: 'var(--ds-text-error)', | ||
background: 'var(--ds-surface-overlay)', | ||
}, | ||
bold: { | ||
color: 'var(--ds-text-bold)', | ||
}, | ||
sunken: { | ||
background: 'var(--ds-surface-sunken)', | ||
}, | ||
}); | ||
|
||
function ComponentPassThrough({ | ||
xcss, | ||
}: { | ||
xcss?: ReturnType<typeof XCSSProp<'background' | 'color', '&:hover'>>; | ||
}) { | ||
return <NativePassThrough xcss={cx(styles.rootComponent, xcss)} />; | ||
} | ||
|
||
function NativePassThrough({ | ||
xcss, | ||
}: { | ||
xcss?: ReturnType<typeof XCSSProp<'background' | 'color', '&:hover'>>; | ||
}) { | ||
return <button data-testid="button" className={xcss} css={styles.rootNative} />; | ||
} | ||
|
||
describe('pass-through props.xcss directly to DOM', () => { | ||
it('works with no props.xcss', () => { | ||
const { getByTestId } = render(<NativePassThrough />); | ||
|
||
expect(getByTestId('button')).toHaveCompiledCss({ | ||
color: 'var(--ds-text)', | ||
background: 'var(--ds-surface)', | ||
}); | ||
}); | ||
|
||
it('works with pass-through props.xcss', () => { | ||
const { getByTestId } = render(<NativePassThrough xcss={styles.bold} />); | ||
|
||
expect(getByTestId('button')).toHaveCompiledCss({ | ||
color: 'var(--ds-text-bold)', | ||
background: 'var(--ds-surface)', // rootNative styles | ||
}); | ||
}); | ||
|
||
it('works with pass-through multiple props.xcss via cx', () => { | ||
const { getByTestId } = render(<NativePassThrough xcss={cx(styles.bold, styles.sunken)} />); | ||
|
||
expect(getByTestId('button')).toHaveCompiledCss({ | ||
color: 'var(--ds-text-bold)', | ||
background: 'var(--ds-surface-sunken)', | ||
}); | ||
}); | ||
}); | ||
|
||
describe('pass-through props.xcss via another component', () => { | ||
it('works with no props.xcss', () => { | ||
const { getByTestId } = render(<ComponentPassThrough />); | ||
|
||
expect(getByTestId('button')).toHaveCompiledCss({ | ||
color: 'var(--ds-text-error)', | ||
background: 'var(--ds-surface-overlay)', | ||
}); | ||
}); | ||
|
||
it('works with pass-through props.xcss', () => { | ||
const { getByTestId } = render(<ComponentPassThrough xcss={styles.bold} />); | ||
|
||
expect(getByTestId('button')).toHaveCompiledCss({ | ||
color: 'var(--ds-text-bold)', | ||
background: 'var(--ds-surface-overlay)', // rootComponent styles | ||
}); | ||
}); | ||
|
||
it('works with pass-through multiple props.xcss via cx', () => { | ||
const { getByTestId } = render(<ComponentPassThrough xcss={cx(styles.bold, styles.sunken)} />); | ||
|
||
expect(getByTestId('button')).toHaveCompiledCss({ | ||
color: 'var(--ds-text-bold)', | ||
background: 'var(--ds-surface-sunken)', | ||
}); | ||
}); | ||
}); |
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