Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Explore replacing the cx() utility with pre-defined classes #445

Closed
Greenheart opened this issue Oct 16, 2023 · 0 comments
Closed

Explore replacing the cx() utility with pre-defined classes #445

Greenheart opened this issue Oct 16, 2023 · 0 comments

Comments

@Greenheart
Copy link
Owner

Greenheart commented Oct 16, 2023

Since cx() is executed both during compile time and runtime, it adds a slight performance overhead.

One way to compile the CSS and define styles beforehand would be to use a combination of solutions:

Svelte style blocks with pre-defined classes and Post CSS @apply

Instead of writing

<p class={cx('font-bold text-sm', isRed ? 'text-red-500' : 'text-blue-500')}>text</p

we could replace the cx() helper by defining the classes at compile time.

<p class="font-bold text-sm" class:red={isRed} class:blue={!isRed}>text</p>

<style>
  .red {
    @apply text-red-500;
  }

  .blue {
    @apply text-blue-500;
  }
</style>

This would result in more code, but would remove the need for the cx utility, since all classes would be added in a nicely formatted manner, and invalid values would be filtered out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant