Remove @emotion/styled dependency
See original GitHub issueHey,
If i use Box and then i apply ref forwarding on a new component called button. It would seem that in typescript when using forward ref it seems that box is set to HTMLDivElement and if i supply HTMLButtonElement we get error that it needs to be a div element, i assume this is to do with the ‘as’ prop?
Just wandered what i’m doing wrong - i even tried the typings for ForwardRef and it still did not work. TS version 4.0.2
for example:
import * as React from 'react';
import { Box, BoxProps } from 'my-lib';
export interface IButtonProps extends BoxProps {
label: string;
variant?: 'primary' | 'secondary' | 'tertiary';
children: React.ReactNode;
}
const Button = React.forwardRef<
HTMLButtonElement,
IButtonProps,
>(({ label = 'change me', variant = 'primary', ...rest }, ref) => {
return (
<Box
as="button"
ref={ref}
variant={variant}
aria-label={label}
{...rest}
/>
);
});
export { Button };
Type '((instance: HTMLButtonElement | null) => void) | MutableRefObject<HTMLButtonElement | null> | null' is not assignable to type 'string | ((instance: HTMLDivElement | null) => void) | RefObject<HTMLDivElement> | null | undefined'.
Type '(instance: HTMLButtonElement | null) => void' is not assignable to type 'string | ((instance: HTMLDivElement | null) => void) | RefObject<HTMLDivElement> | null | undefined'.
Type '(instance: HTMLButtonElement | null) => void' is not assignable to type '(instance: HTMLDivElement | null) => void'.
Types of parameters 'instance' and 'instance' are incompatible.
Type 'HTMLDivElement | null' is not assignable to type 'HTMLButtonElement | null'.
Type 'HTMLDivElement' is missing the following properties from type 'HTMLButtonElement': disabled, form, formAction, formEnctype, and 13 more.ts(2322)
index.d.ts(143, 9): The expected type comes from property 'ref' which is declared here on type 'IntrinsicAttributes & ClassAttributes<HTMLDivElement> & HTMLAttributes<HTMLDivElement> & Pick<...>
for now my fix is lame but its all i have got 😉
ref={ref as any}
looking on emotion comments and it looks like the as prop has been thought about by different source - maybe a good reference?
https://github.com/kripod/react-polymorphic-box/blob/main/src/Box.tsx
Thanks in advance 😉
Issue Analytics
- State:
- Created 3 years ago
- Comments:8 (6 by maintainers)
Top Results From Across the Web
How to remove Emotion - Next Right Now
Remove the following libraries: @emotion/core : Necessary to use emotion, with built-in css notation support. @emotion/styled : Necessary to used the styled ......
Read more >@emotion/styled - npm
styled API for emotion. Latest version: 11.10.5, last published: 2 months ago. Start using @emotion/styled in your project by running `npm i ...
Read more >@emotion/styled | Yarn - Package Manager
Fast, reliable, and secure dependency management.
Read more >Install - Emotion
styled is a way to create React components that have styles attached to them. # assuming you already have @emotion/react installed yarn add...
Read more >Module not found: Can't resolve '@emotion/react'
npm install @emotion/react npm install @emotion/styled ... try to delete the .next dir after installing the dependencies.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I’ve spent a bunch of time fiddling with it, and I’m not convinced it’s an amazing feature. TypeScript compilation times suffer drastically in large codebases using
as
prop (I even managed to get an OOM 😄). It’s a really cool gun to shoot oneself in the foot. I did it, and I’d strongly advise to usesx
prop withjsx
pragma in TypeScript.Pretty soon, with React Automatic Runtime, you won’t even need
/** @jsx jsx */
comment.Additional context:
https://blog.andrewbran.ch/polymorphic-react-components/
Papertrail: we deprecated
.withComponent
some time ago & will be fully removing it soon.