Skip to content

Styling a component with forwardRef #1421

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

Open
molily opened this issue Aug 5, 2024 · 1 comment
Open

Styling a component with forwardRef #1421

molily opened this issue Aug 5, 2024 · 1 comment
Labels
bug report 🦗 Issue is probably a bug, but it needs to be checked needs: complete repro 🖥️ Issue need to have complete repro provided platform: next.js 🛠️ Issue related to next.js platform: ssr 🛠️ Issue related to SSR

Comments

@molily
Copy link

molily commented Aug 5, 2024

Environment

  • Linaria version: @linaria/core 6.2.0, @linaria/react 6.2.1
  • Next.js 14.2.5, next-with-linaria 0.7.0
  • Node.js version: 20.16.0
  • OS: macOS 14.5

Description

Hi, I'm using Linaria in a Next project via next-with-linaria.

In a file marked with 'use client', I have a component defined with forwardRef that accepts styles:

const StylableInputFwdRef = forwardRef<HTMLInputElement, StylableInputProps>(({ label, className, style }, ref) =>
  <p>
    <label>
      {label}{" "}
      <input ref={ref} className={ className } style={ style } />
    </label>
  </p>
);

I would like to style this component with Linaria:

const InputLinariaFwdRef = styled(StylableInputFwdRef)`
  border: 2px inset blue;
`;

This declaration raises an error:

next14-linaria/node_modules/@wyw-in-js/transform/lib/module.js:224
      throw new EvalError(`${e.message} in${this.callstack.join('\n| ')}\n`);
      ^

EvalError: TextEncoder is not defined in next14-linaria/node_modules/next/dist/compiled/next-server/app-page.runtime.dev.js
| next14-linaria/node_modules/next/dist/server/future/route-modules/app-page/module.compiled.js
| next14-linaria/node_modules/next/dist/server/future/route-modules/app-page/vendored/rsc/react.js
| next14-linaria/src/app/ClientComponent.tsx
    at evalFile.next (<anonymous>)

The error seems to happen in Next.js code but is re-thrown by wyw-in-js.

I'm not sure whether my problem is caused by Linaria itself, by next-with-linaria, wyw-in-js or Next.js. I'd appreciate any hints.

Is is possible to wrap a component with forwardRef? Am I fundamentally misunderstanding how Linaria works in a client component?

Thanks for your time and thank you for this project!

Reproducible Demo

Fresh Next.js project with Linaria: https://github.com/molily/next14-linaria
Relevant code is in https://github.com/molily/next14-linaria/blob/main/src/app/ClientComponent.tsx

@molily molily added bug report 🦗 Issue is probably a bug, but it needs to be checked needs: complete repro 🖥️ Issue need to have complete repro provided needs: triage 🏷 Issue needs to be checked and prioritized labels Aug 5, 2024
@github-actions github-actions bot added platform: next.js 🛠️ Issue related to next.js platform: ssr 🛠️ Issue related to SSR and removed needs: triage 🏷 Issue needs to be checked and prioritized labels Aug 5, 2024
@espretto
Copy link

Hi,

the issue seems to be a typing issue of forwardRef. Assigning the return-type of forwardRef to StylableInputFwdRef works:

- const StylableInputFwdRef = forwardRef<HTMLInputElement, StylableInputProps>(({ label, className, style }, ref) =>
+ const StylableInputFwdRef: React.ForwardRefExoticComponent<React.PropsWithoutRef<StylableInputProps> & React.RefAttributes<HTMLInputElement>> = forwardRef(({ label, className, style }, ref) =>

Note that linaria's styled already calls forwardRef on every component regardless whether it's necessary or not.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug report 🦗 Issue is probably a bug, but it needs to be checked needs: complete repro 🖥️ Issue need to have complete repro provided platform: next.js 🛠️ Issue related to next.js platform: ssr 🛠️ Issue related to SSR
Projects
None yet
Development

No branches or pull requests

2 participants