I am developing a custom UI component library using Svelte and each component has at least a CSS class which is the same name the component tag name. Also, I have a base component which all other components are using that. Here is the simplified codes:
Base.svelte
<script>
export let componentName
export let tag = 'div'
</script>
<svelte:element this={tag} class={componentName}>
<slot />
</svelte:element>
Foo.svelte
<script>
import Base from './Base.svelte'
</script>
<Base tag="b" componentName="Foo">
<slot />
</Base>
Bar.svelte
<script>
import Base from './Base.svelte'
</script>
<Base tag="h3" componentName="Bar">
<slot />
</Base>
App.svelte
<script>
import Foo from './Foo.svelte'
import Bar from './Bar.svelte'
</script>
<Foo>
<Bar>This is Bar 1</Bar>
<Bar>This is Bar 2</Bar>
</Foo>
<Foo>
Foo
</Foo>
In above code, I have to set the componentName
prop which actually should be a constant. Also, I should always update that in case I change the component's file name.
Is there anyway to get the current component tag name and set it in Base component?
Or, a function that returns the name similar to what we have for the component:
import { current_component } from 'svelte/internal';