embed icon indicating copy to clipboard operation
embed copied to clipboard

Typescript: type issue with components ref

Open TommyBacco opened this issue 1 year ago • 2 comments

It looks like there is some misalignment in the types when passing a ref making necessary to suppress the type error in order to proceed and use the components programmatically.

image

TommyBacco avatar Jun 21 '24 07:06 TommyBacco

Does Sidetab component excepts ref or not ??? Check that first , if it does try providing a type to ref as well

interface SidetabProps extends React.HTMLAttributes<HTMLDivElement> { ref ?: React.Ref<HTMLDivElement>; }

ref?: React.Ref<HTMLDivElement>;

Emkay2309 avatar Jun 22 '24 09:06 Emkay2309

The same is happening in the PopupButton and ref is in the documentation: https://github.com/Typeform/embed/tree/main/packages/embed-react#passing-a-custom-ref

The error I get is: Type '{ children: string; ref: MutableRefObject<undefined>; id: string; hidden: { user: string; }; className: string; onSubmit: () => void; }' is not assignable to type 'IntrinsicAttributes & BaseOptions & ModalWindowOptions & UrlOptions & ActionableOptions & ... 5 more ... & ButtonComponentBaseProps'. Property 'ref' does not exist on type 'IntrinsicAttributes & BaseOptions & ModalWindowOptions & UrlOptions & ActionableOptions & ... 5 more ... & ButtonComponentBaseProps'

FranRom avatar Jul 03 '24 13:07 FranRom

Hello @TommyBacco @FranRom

how do you define your ref? When i define it like this it works for me:

const ref = useRef();

There are no TS errors on props or ref: Screenshot 2024-07-16 at 08 58 27

~But it is true the ref is not typed properly, as it does not define the open method.~ ~I will look into this, if we can supply correct types on our side. In the meantime, as a workaround, I'd suggest typing as any.~

Edit: Actually we do have a GenericEmbed type that you can use on your side to type the ref, like this:

import { GenericEmbed } from "@typeform/embed-react/build/components";
const ref = useRef<GenericEmbed>();
const openPopup = () => ref.current?.open();

mathio avatar Jul 16 '24 07:07 mathio

Actually you are right. I was building my test project via vite. When I build with tsc --build the typescript did complain.

It proved to be quite challenging to type properly when we reuse React's forwardRef. I am renaming the prop instead to embedRef (since this is not actually a DOM element ref). Upcoming @typeform/embed-react version will introduce this breaking change in #657.

mathio avatar Jul 16 '24 13:07 mathio

:tada: This issue has been resolved in version @typeform/embed-v5.0.0 :tada:

The release is available on:

Your semantic-release bot :package::rocket:

typeform-ops-gha avatar Jul 17 '24 10:07 typeform-ops-gha

:tada: This issue has been resolved in version @typeform/embed-react-v4.0.0 :tada:

The release is available on:

Your semantic-release bot :package::rocket:

typeform-ops-gha avatar Jul 17 '24 10:07 typeform-ops-gha