0

When using Carbon Component for Svelte, is it possible to assign a component as a variable, and then pass that variable to a prop?

For example, I'm using the <Modal /> component, whose primary button exposes a primaryButtonIcon prop; I want to assign different icons depending on the state.

Something like this:

let uploadStatus: "active" | "inactive" | "finished" | "error" | undefined = "active";
let primaryButtonIcon: WHAT? = <InlineLoading status={uploadStatus} />;

<Modal primaryButtonIcon={primaryButtonIcon} />

But what should I declare the primaryButtonIcon as?


UPDATE:
When I use ...

let uploadStatus: "active" | "inactive" | "finished" | "error" | undefined = "active";
let primaryButtonIcon: typeof SvelteComponent<any> = <InlineLoading status={uploadStatus} />;

... there are errors ...

enter image description here

The error relating to uploadStatus is ...

Cannot redeclare block-scoped variable 'uploadStatus'. ts(2451)

The error relating to primaryButtonIcon is ...

Property 'prototype' is missing in type 'SvelteComponentTyped<InlineLoadingProps, { click: MouseEvent; mouseover: MouseEvent; mouseenter: MouseEvent; mouseleave: MouseEvent; success: CustomEvent<...>; }, {}>' but required in type 'typeof SvelteComponent'. ts(2741)

And the error relating to <InlineLoading /> is ...

Conversion of type 'string' to type 'InlineLoading' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first. ts(2352)

Have I declared something wrongly?

1 Answer 1

1

The type is shown right in the docs in the "Type" column, it's the base component type from svelte, so:

import { type SvelteComponent } from 'svelte';
let primaryButtonIcon: typeof SvelteComponent<any> = ...

(Though one should generally use ComponentType instead.)

But, you cannot define markup inline in Svelte. It has to be extracted to a self-contained component file.

(In Svelte 5 snippets allow referencing pieces of markup in the script.)

Sign up to request clarification or add additional context in comments.

3 Comments

I had already tried this, and it produces errors. I've updated the OP with more information.
As I said, that is invalid, you cannot just have markup in the script. You only can reference a component class, nothing else. E.g. let primaryButtonIcon: typeof SvelteComponent<any> = MyLoadingComponent. You could potentially define a function that wraps the component instantiation, returning a new component constructor, see e.g. this question.
When you wrote that "you cannot define markup inline in Svelte" I didn't appreciate that you were referring to props.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.