The problem here is that Svelte has no way of knowing that the class
property refers to a CSS class here or even where to apply this class. Something to not forget is that the following are all valid Svelte components:
<span>Hello World</span>
<span>Hello</span>
<span>World</span>
Hello World
In the first example, it should probably be on the span, but where should the class go in the second example, the first or second span ? Or maybe both ? And what about the last example where there is no DOM element at all, you cannot add a class to a textnode.
Because of this your class is marked as unused by Svelte and removed during compilation. This is inline with Svelte Single File Component philosophy where all styling for a component is included in the same file. While your construction is counter to that it is sometimes a valid approach, this is what the :global
is for.
Note that you can export a class
property from TextField and apply to the element of your choice, you would still need to mark the class as global though:
<script>
// You have to this because class is a reserved keyword in JavaScript
let className;
export { className as class };
</script>
<div>
<p>
<span class={className}>...</span>
<p>
<button>...</button>
</div>