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

[nextjs] Cannot style next/link component #189

Open
Sephster opened this issue Jul 31, 2024 · 8 comments
Open

[nextjs] Cannot style next/link component #189

Sephster opened this issue Jul 31, 2024 · 8 comments
Assignees
Labels
component: link This is the name of the generic UI component, not the React module! nextjs status: waiting for maintainer These issues haven't been looked at yet by a maintainer

Comments

@Sephster
Copy link

Sephster commented Jul 31, 2024

Steps to reproduce

Reproduction repo here: https://github.com/Sephster/styled-components-bug

  1. Clone the repo
  2. run npm i
  3. run npm run dev
  4. visit the site url (usually http://localhost:3000)
  5. the page doesn't load and errors appear in the console

Current behavior

When creating a styled component from a next/link component e.g

const LinkStyle = styled(Link)`
  font-weight: bold;
`;

The page containing the component cannot render with errors in the terminal. The only workaround I've found for this is to style an a tag and use passHref and legacyBehavior for the next link e.g.

const LinkStyle = styled.a`
  font-weight: bold;
`;

return (
  <Link href={url} passHref legacyBehavior>
    <LinkStyle>Link text</LinkStyle>
  </Link>

Expected behavior

We should be able to create a styled component that uses a next/link component as its base. This is possible in the styled-components package.

Context

To style a next JS link component.

Your environment

System:
OS: Linux 6.5 Ubuntu 22.04.4 LTS 22.04.4 LTS (Jammy Jellyfish)
Binaries:
Node: 20.15.1 - ~/.nvm/versions/node/v20.15.1/bin/node
npm: 10.7.0 - ~/.nvm/versions/node/v20.15.1/bin/npm
pnpm: 7.14.0 - ~/.local/share/pnpm/pnpm
Browsers:
Chrome: Not Found
npmPackages:
@emotion/react: 11.13.0
@emotion/styled: 11.13.0
@mui/private-theming: 6.0.0-dev.20240529-082515-213b5e33ab
@mui/styled-engine: 6.0.0-dev.20240529-082515-213b5e33ab
@mui/system: 6.0.0-dev.240424162023-9968b4889d
@mui/types: 7.2.15
@mui/utils: 6.0.0-dev.20240529-082515-213b5e33ab
@types/react: ^18 => 18.3.3
react: ^18 => 18.3.1
react-dom: ^18 => 18.3.1
styled-components: ^6.1.12 => 6.1.12
typescript: ^5 => 5.5.4

Search keywords: next link

@Sephster Sephster added the status: waiting for maintainer These issues haven't been looked at yet by a maintainer label Jul 31, 2024
@zannager zannager added the component: link This is the name of the generic UI component, not the React module! label Aug 2, 2024
@Sephster
Copy link
Author

Sephster commented Aug 6, 2024

A similar issue occurs with the next/image component. It cannot be styled and results in a build error

@royporter7
Copy link

I think I'm getting the same error as the people above, for both the next/link component and the next/image.
Oddly enough these two components work in the example next.js ts project provided in the examples, however when integrated into my project, I get errors.

@DiegoAndai DiegoAndai added status: waiting for author Issue with insufficient information and removed status: waiting for maintainer These issues haven't been looked at yet by a maintainer labels Aug 27, 2024
@DiegoAndai DiegoAndai changed the title Cannot style next/link component [nextjs] Cannot style next/link component Aug 27, 2024
@DiegoAndai
Copy link
Member

Hey @Sephster, thanks for the report. I see you're using Emotion and not Pigment CSS. Did you intend to open this issue on https://github.com/mui/material-ui instead?

@DiegoAndai
Copy link
Member

@rhoiyds are you getting the error with Pigment CSS?

@DiegoAndai DiegoAndai assigned brijeshb42 and unassigned DiegoAndai Aug 27, 2024
@royporter7
Copy link

@DiegoAndai Yes I'm working with Pigment CSS, but integrated with MUI. However I've been working through a multitude of issues (bundling issue, transformLibraries issue) so I can't say for certain that I've been able to isolate this specific issue, sorry.

@Sephster
Copy link
Author

Sephster commented Aug 31, 2024

@DiegoAndai apologies the environment info is inaccurate. I think I installed emotion after testing. I can confirm this is a bug with pigment. I was using it alone. I will update the environment info for you this weekend

@github-actions github-actions bot added status: waiting for maintainer These issues haven't been looked at yet by a maintainer and removed status: waiting for author Issue with insufficient information labels Aug 31, 2024
@DiegoAndai
Copy link
Member

@brijeshb42 may I ask you to take a look into this one when you have some time?

@axeleroy
Copy link

axeleroy commented Jan 27, 2025

Hello,

I have found a way to indirectly style Link using the as prop. What I do is create a component (like Cta) and use it like that:

<Cta as={Link} href="/foo/bar">Click me</Cta>

Just make sure if you are using shouldForwardProp to not forward as

const Cta = styled("button", {
  shouldForwardProp: (propName) => propName !== "as" && propName !== "variant"
})<{ variant: "primary" | "secondary" }>({
  ...
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: link This is the name of the generic UI component, not the React module! nextjs status: waiting for maintainer These issues haven't been looked at yet by a maintainer
Projects
None yet
Development

No branches or pull requests

6 participants