-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
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
[styled-engine][styled-engine-sc] Allow use of styled.div
, not just styled('div')
#28574
Comments
@markalfred I am pretty sure you can you use emotion's // with npm // with yarn And then check this code sandbox https://codesandbox.io/s/modern-wildflower-hijbc?file=/src/App.js |
@HyphenFrox you're totally right. I didn't think |
Please don't import anything from You can use the theme in the styled function as demonstrated at https://mui.com/system/styled/#using-the-theme. Don't forget to close the issue. |
Got it. Just want to note that this results in an error: import { styled } from "@mui/system";
const MyDiv = styled.div`
background-color: red;
`
// => TypeError: _system.styled.div is not a function I'll use |
styled.div
, not just styled('div')
styled.div
, not just styled('div')
How do I use @emotion style syntax when passing theme ? This works: const MyTestButton = styled(Button)(({ theme }) => ({ I would really prefer using standard css inside the style block ( the emotion syntax / kebab-case) vs. using the camelCase and values in single quotes. I did try enclosing the function within template literals but that does not seem to work either when passing the theme. Any pointers or a simple example would be appreciated. Thanks |
@kdgit1 If you want to use the |
Thanks for your quick response. Works like a charm ( for now; haven't tested out everything but the basics sure seem to work just fine ). So glad to be able to write css as simple clean css. |
@kdgit1 You don't need to import |
I'd like to re-open this for discussion as a new wrinkle has come up for my team. While importing from https://codesandbox.io/s/using-styled-div-in-mui-with-theme-forked-86gol?file=/src/App.tsx |
I think we should add the primitives. This would be a good starting point: index ad0e2c17f9..e5e877e655 100644
--- a/packages/mui-styled-engine-sc/src/index.js
+++ b/packages/mui-styled-engine-sc/src/index.js
@@ -34,6 +34,10 @@ export default function styled(tag, options) {
return (...styles) => stylesFactory(...styles);
}
+Object.keys(scStyled).forEach((tag) => {
+ styled[tag] = scStyled[tag];
+});
+
export { ThemeContext, keyframes, css } from 'styled-components';
export { default as StyledEngineProvider } from './StyledEngineProvider';
export { default as GlobalStyles } from './GlobalStyles';
diff --git a/packages/mui-styled-engine-sc/src/styled.test.js b/packages/mui-styled-engine-sc/src/styled.test.js
index 0b1f7957c1..de1d675664 100644
--- a/packages/mui-styled-engine-sc/src/styled.test.js
+++ b/packages/mui-styled-engine-sc/src/styled.test.js
@@ -1,5 +1,6 @@
import * as React from 'react';
import { expect } from 'chai';
+import scStyled from 'styled-components';
import { createClientRender } from 'test/utils';
import styled from '@mui/styled-engine-sc';
@@ -38,4 +39,8 @@ describe('styled', () => {
it("should not allow styled-components's APIs: .attrs", () => {
expect(typeof styled('span').attrs).to.equal('undefined');
});
+
+ it('has primitive', () => {
+ expect(styled.div).to.equal(scStyled.div);
+ });
});
diff --git a/packages/mui-styled-engine/src/index.js b/packages/mui-styled-engine/src/index.js
index 696af8f9b8..8074e6395b 100644
--- a/packages/mui-styled-engine/src/index.js
+++ b/packages/mui-styled-engine/src/index.js
@@ -25,6 +25,10 @@ export default function styled(tag, options) {
return stylesFactory;
}
+Object.keys(emStyled).forEach((tag) => {
+ styled[tag] = emStyled[tag];
+});
+
export { ThemeContext, keyframes, css } from '@emotion/react';
export { default as StyledEngineProvider } from './StyledEngineProvider';
export { default as GlobalStyles } from './GlobalStyles';
diff --git a/packages/mui-styled-engine/src/styled.test.js b/packages/mui-styled-engine/src/styled.test.js
index d7e51ee936..2fdbbcdbc3 100644
--- a/packages/mui-styled-engine/src/styled.test.js
+++ b/packages/mui-styled-engine/src/styled.test.js
@@ -1,4 +1,5 @@
import { expect } from 'chai';
+import emStyled from '@emotion/styled';
import styled from './index';
describe('styled', () => {
@@ -11,4 +12,8 @@ describe('styled', () => {
styled('span')(undefined, { color: 'red' });
}).toErrorDev('MUI: the styled("span")(...args) API requires all its args to be defined');
});
+
+ it('has primitive', () => {
+ expect(styled.div).to.equal(emStyled.div);
+ });
}); I can help if someone wants to take this up. @markalfred if you want to use direction emotion's styled() utility you need to follow https://emotion.sh/docs/typescript#define-a-theme for defining the typings for the @kdgit1 you can use the |
styled.div
, not just styled('div')
styled.div
, not just styled('div')
@mnajdova has been any progress on this? |
@h0tw4t3r I don't see why we shouldn't add the API proposed in my previous comment, feel free to grab it you want to work on it. |
@mnajdova Sure.
Those who used I'm not familiar with Can you please tell me if the patch provided above just misses the TypeScript part of it? |
@h0tw4t3r sorry for the delay, let's add the patch and some spec tests and we can see what's missing. Are you still interested to work on it? |
I'll work in the free time, yes. |
Perfect, will look out for the PR :) |
Would like to make this PR. Can you guide me? |
@geraldaddey sorry for the late response, the guidance was provided in a comment above - #28574 (comment) |
The only other issue regarding this is #22147.
Summary 💡
When using Emotion's
styled
, it's possible to use the syntax:This doesn't work when importing
styled
from@mui/materials/styles
. Instead, this is required:This is fine, but Emotion's syntax is a little nicer.
Examples 🌈
Note Emotion's
styled
documentation examples:Motivation 🔦
As a developer who's used to Emotion, I've found myself making this mistake repeatedly over the past few days. It'd be nice for MUI to have the same syntactic sugars that Emotion has.
Relevant Code
This is the code on Emotion's side that powers this feature:
https://github.com/emotion-js/emotion/blob/main/packages/styled/src/index.js
Thanks for all your hard work and efforts, v5 is looking amazing.
The text was updated successfully, but these errors were encountered: