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

Babel plugin to transform a custom sx prop for any component #10

Open
kripod opened this issue Mar 27, 2020 · 4 comments
Open

Babel plugin to transform a custom sx prop for any component #10

kripod opened this issue Mar 27, 2020 · 4 comments
Assignees
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@kripod
Copy link
Owner

kripod commented Mar 27, 2020

Motivation

Calling the useStyling hook is verbose, especially for small components, e.g.:

import { useStyling } from 'glaze';

export default function Component() {
  const sx = useStyling();

  return (
    <p
      className={sx({
        px: 4, // Sets padding-left and padding-right to 1rem
        color: 'white',
        bg: 'red', // Sets background to #f8485e
      })}
    >
      Hello, world!
    </p>
  );
}

Basic example

Instead, an API like below could be made possible with code transforms:

export default function Component() {
  return (
    <p
      sx={{
        px: 4, // Sets padding-left and padding-right to 1rem
        color: 'white',
        bg: 'red', // Sets background to #f8485e
      }}
    >
      Hello, world!
    </p>
  );
}

Details

Style composition (#4) should also be made available.

A custom JSX pragma is not required, nor recommended. If someone opts into using the Babel plugin, TypeScript module augmentation should be applied on React elements, extending the list of available props with sx.

@kripod kripod added enhancement New feature or request help wanted Extra attention is needed labels Mar 27, 2020
@kripod
Copy link
Owner Author

kripod commented Mar 30, 2020

For auto-importing the useStyling hook, the implementation in emotion-js/emotion#200 can be utilized. After that, every component with an element using the sx prop should start with:

const sx = useStyling();

@tatchi
Copy link
Contributor

tatchi commented Apr 1, 2020

Hey,

I would love to give a shot at this one. I've never written any babel plugin but I guess that's a cool learning opportunity 😊

Since there's an example I can base myself on I hope it's won't be too difficult :)

@kripod
Copy link
Owner Author

kripod commented Apr 1, 2020

@tatchi Thank you for trying! I’ve also never written a Babel plugin before, but this explanation and the preset for emotion’s css prop may help us through the journey.

Let me know if you have any questions!

@kripod
Copy link
Owner Author

kripod commented Apr 21, 2020

Now only the documentation and type definitions are left. I'm doing both right now 😊

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

No branches or pull requests

2 participants