-
Hi there! I just started out with styled-jsx and like it a lot. However, I was noticing that the styles I defined would not always take effect, and realized that I am unable to style custom components with hyphens in the name. For example, here's a component I am trying to style: export default function Test() {
return (
<my-test className="root">
<style jsx>{`
.root {
margin: 0;
width: 100%;
height: 100%;
}
</style>
</my-test>
);
} I can name the root element something like Are custom element names supported? Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 3 replies
-
Hey Otto, this is actually less of a Next.js issue, and more of a React.js issue. You must always name your React.js components with a capital letter at the start, with no hyphens inside. The reason for this, is because in JSX, all lower-case tag names are considered to be HTML tags (for example So, for this example you should change it to: export default function Test() {
return (
<MyTest className="root">
<style jsx>{`
.root {
margin: 0;
width: 100%;
height: 100%;
}
</style>
</MyTest>
);
} Let me know if that helps :) |
Beta Was this translation helpful? Give feedback.
-
Thanks for the reply. I am actually not trying to write a new React component; I just want to use a custom HTML component for scoping styles and for clarity. So in this case, rather than using a I did some more digging and found that React treats custom components differently because it must pass So I suppose this is now more of a feature request than a help request :) It looks like styled-jsx checks for Thanks, |
Beta Was this translation helpful? Give feedback.
-
Ah I see, you want to use a web component here. Sorry i didn't understand it before. So, you are right that the current solution that you posted won't work. I tried it out and there is a solution, although it's not the most pretty. You can change your code to this: export default function Test() {
return (
<my-test class="root">
<style jsx global>{`
.root {
margin: 0;
width: 100%;
height: 100%;
}
</style>
</my-test>
);
} the only change here is that we added This means that it's added to a global stylesheet, rather than a locally scoped one. It's slightly less performant, but at least it works :) |
Beta Was this translation helpful? Give feedback.
-
I dug a bit more into the code and was able to tweak styled-jsx to support custom components. I hope it's useful; maybe it will even be merged! vercel/styled-jsx#647 |
Beta Was this translation helpful? Give feedback.
I dug a bit more into the code and was able to tweak styled-jsx to support custom components. I hope it's useful; maybe it will even be merged! vercel/styled-jsx#647