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

Cannot use stylex in .astro files #99

Open
Haberkamp opened this issue May 19, 2024 · 1 comment
Open

Cannot use stylex in .astro files #99

Haberkamp opened this issue May 19, 2024 · 1 comment

Comments

@Haberkamp
Copy link

Hi, it's not possible to use stylex in astro files. I can only use it in react components.

---
import * as stylex from '@stylexjs/stylex';

const styles = stylex.create({
  foo: {
    color: "red"
  }
});
---

<p {...stylex.attrs(styles.foo)}>lorem</p>

This is the error I get: stylex.create() is only allowed at the root of a program.

@lilnasy
Copy link
Owner

lilnasy commented May 19, 2024

This happens because the frontmatter in .astro files, after compilation, is not the "root". This compilation is similar to svelte's, and I have described the caveat of using stylex alongside svelte here: https://github.com/lilnasy/gratelets/tree/main/packages/stylex#svelte.

To summarise, Stylex needs the styles to be written at the top-level, not in the body of a function. Astro compiles the frontmatter into a body of a function.

Astro has an undocumented escape hatch, however: you can write an exported const declaration instead of a const declaration. For example,

+export const styles = stylex.create({
-const styles = stylex.create({

This will make astro compile the stylex.create() line onto the top-level or "root" of the module, instead of compiling it into the body of the function.

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

No branches or pull requests

2 participants