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

[core] Fix useTheme import change #134

Merged
merged 3 commits into from
Jun 10, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/pigment-css-react/src/createExtendSxProp.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* to reduce bundle size only. DO NOT use it in your codebase.
*/
function extendSxProp(props) {
return props;
return { ...props };
}
export default function createExtendSxProp() {
return extendSxProp;
Expand Down
3 changes: 3 additions & 0 deletions packages/pigment-css-react/src/processors/useTheme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ export type TemplateCallback = (params: Record<string, unknown> | undefined) =>
export class UseThemeProcessor extends BaseProcessor {
constructor(params: Params, ...args: TailProcessorParams) {
super([params[0]], ...args);
if (params.length === 1) {
throw BaseProcessor.SKIP;
}
validateParams(params, ['callee', ['call']], `Invalid use of ${this.tagSource.imported} tag.`);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { describe } from 'yargs';
import { expect } from 'chai';
import createExtendSxProp from '../../src/createExtendSxProp';

describe('createExtendSxProp', () => {
it('return the new copy of input', () => {
const original = { color: 'red' };
expect(createExtendSxProp()(original)).to.not.equal(original);
expect(createExtendSxProp()(original)).not.to.equal(original);
expect(createExtendSxProp()(original)).to.deep.equal({ color: 'red' });
Comment on lines -8 to +7
Copy link
Member

Choose a reason for hiding this comment

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

Nice, fixed in the whole codebase: mui/material-ui@6fe271e, and in MUI X.

});
});
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { useTheme } from '../zero-styled';
import { useTheme } from '@pigment-css/react';

// This is intentional to make sure Pigment leaves the reference as-is.
console.log(useTheme);

Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
console.log(useTheme);

Copy link
Member

Choose a reason for hiding this comment

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

Is the console.log intentional?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is part of the test to make sure we don't have the same issue that this PR fixes.

Copy link
Member

Choose a reason for hiding this comment

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

I see, got it. Can you add a comment that it's intentional? I am sure others will ask if they see console.log

export const Fade = React.forwardRef(function Fade(props, ref) {
const theme = useTheme();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import _default from '@pigment-css/react/theme';
import { useTheme } from '@pigment-css/react';

// This is intentional to make sure Pigment leaves the reference as-is.
console.log(useTheme);
export const Fade = React.forwardRef(function Fade(props, ref) {
const theme = _default;
return (
Expand Down
5 changes: 4 additions & 1 deletion packages/pigment-css-unplugin/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,10 @@ export const plugin = createUnplugin<PigmentOptions, true>((options) => {
if (id.endsWith('styles.css')) {
return theme ? generateTokenCss(theme) : _code;
}
if (id.includes('pigment-css-react/theme')) {
if (
id.includes('pigment-css-react/theme') ||
Copy link
Member

Choose a reason for hiding this comment

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

I wonder what's pigment-css-react/theme is for?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

At some point, it was for local testing in workspaces. We can maybe clean this up separately.

id.includes(`${process.env.RUNTIME_PACKAGE_NAME}/theme`)
) {
return generateThemeSource(theme);
}
return null;
Expand Down
4 changes: 2 additions & 2 deletions packages/pigment-css-vite-plugin/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ export function pigment(options: PigmentOptions) {
name: 'vite-mui-theme-injection-plugin',
enforce: 'pre',
resolveId(source) {
if (source === `${process.env.RUNTIME_PACKAGE_NAME}/styles.css`) {
if (source.includes(`${process.env.RUNTIME_PACKAGE_NAME}/styles.css`)) {
return VIRTUAL_CSS_FILE;
}
if (source === `${process.env.RUNTIME_PACKAGE_NAME}/theme`) {
if (source.includes(`${process.env.RUNTIME_PACKAGE_NAME}/theme`)) {
return VIRTUAL_THEME_FILE;
}
return null;
Expand Down
Loading