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

Allow stylex.create nested within TS namespaces (and more?) #829

Open
nmn opened this issue Jan 2, 2025 Discussed in #827 · 2 comments
Open

Allow stylex.create nested within TS namespaces (and more?) #829

nmn opened this issue Jan 2, 2025 Discussed in #827 · 2 comments
Labels
enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed

Comments

@nmn
Copy link
Contributor

nmn commented Jan 2, 2025

Discussed in #827

Originally posted by zaydek December 30, 2024
I love using TS namespaces to create conceptual boundaries so it's easier to reason about what different chunks of my code do. Like a Button namespace so I can aggregate relevant variables and components for buttons. One thing I've noticed is that StyleX is incompatible with TS namespaces so I can't do something like namespace Foo { const styles = stylex.create({ ... }); }.

I believe what's going on is that TS namespaces are syntax sugar for objects and therefore it may be impossible to add support for this. That being said, is there a way for me to colocate styles behind something like a namespace so I can do something like the following in the future?

namespace A {
  const styles = stylex.create({
    foo: { ... },
    bar: { ... },
  });
  
  export function Foo() { return <div {...stylex.props(styles.foo)} /> }
  export function Bar() { return <div {...stylex.props(styles.bar)} /> }
}

namespace B {
  const styles = stylex.create({
    foo: { ... },
    bar: { ... },
  });
  
  export function Foo() { return <div {...stylex.props(styles.foo)} /> }
  export function Bar() { return <div {...stylex.props(styles.bar)} /> }
}

...

<A.Foo />
<B.Foo />

I know I can simply create boundary lines using files but I find namespaces allow me to do the job of several files in one file, but when it comes to StyleX APIs, it breaks this convention and I need to prefix styles, which is OK but slightly annoying.

Any ideas or recommendations?

Here is an example of some code where I'd love to have the ability to nest the styles under the namespace instead of prefixing the styles name:

Screenshot 2024-12-29 at 8 52 24 PM

@nmn nmn added enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed labels Jan 2, 2025
@zaydek
Copy link

zaydek commented Jan 4, 2025

It's worth commenting that, in case anyone works on this, it would be ideal if this works for all StyleX APIs and not just stylex.create.

I'd really enjoy being able to do something like:

export namespace Theme {
  export const Spacing = stylex.defineVars({
    ...
  })
  
  ...
}

@nonzzz
Copy link
Contributor

nonzzz commented Jan 7, 2025

Let me do it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

3 participants