I want to use apply to define some css setting on a component, and I also want to be able to overwrite it, like this:
<!-- CustomButton.svelte -->
<script>
let className = '';
export { className as class };
export let label = 'Click me!';
</script>
<button class="custom-button {className}">{label}</button>
<style lang="postcss">
.custom-button {
@apply bg-blue-400 font-bold text-white rounded-lg p-4;
}
</style>
And I want to use it like this:
<script>
import CustomButton from './CustomButton.svelte';
</script>
<div class="w-screen h-screen flex justify-center items-center">
<CustomButton class="bg-red-800" label="This is my button" />
</div>
That is, I want to be able to override my @applied settings
The problem is that the settings from thw @apply directives cannot be overriden by this line
<button class="custom-button {className}">{label}</button>
I understand that in order to do that I have to tell tailwind to generate the corresponding css in the components layer, that is, before the utilities.
If I enter the same css directive in my app.post.css
file, before the @tailwind utilities
line, or using the @layer components
directives it works ok:
/* app.post.css */
@tailwind base;
@tailwind components;
@tailwind utilities;
/* This works ok! */
@layer components {
.custom-button {
@apply bg-blue-400 font-bold text-white rounded-lg p-4;
}
}
So, what I want is some way to tell tailwind to add the .custom-button
setting I defined at my CustomButton.svelte
component to the components layer, so that it could be overriden with inline clases.
If I try to do this in my CustomButton.svelte
file
<style lang="postcss">
@layer components {
.custom-button {
@apply bg-blue-400 font-bold text-white rounded-lg p-4;
}
}
</style>
I get the following error:
9:13:34 PM [vite] Internal server error: /home/sas/devel/apps/glas-it/apps/wingback/end-user-portal/src/routes/CustomButton.svelte?svelte&type=style&lang.css:1:1: `@layer components` is used but no matching `@tailwind components` directive is present.
Plugin: vite:css
This issue is preventing me from using the @apply
directive from any component.
Is there some way to achieve this?