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

Emotion integration #21769

Closed
gracelauren opened this issue Apr 21, 2020 · 4 comments
Closed

Emotion integration #21769

gracelauren opened this issue Apr 21, 2020 · 4 comments
Labels
[Type] Help Request Help with setup, implementation, or "How do I?" questions.

Comments

@gracelauren
Copy link

I am having trouble setting up emotion with WP and custom Gutenberg blocks. It initially renders but on the re-render I am getting an error. I was wondering if there is any specific setup for emotion with gutenberg and wp?
image

Here is my babelrc file
image

and my webpack.config
image

@ItsJonQ
Copy link

ItsJonQ commented Apr 21, 2020

Heya @gracelauren !

Hmm 🤔. Does this setup work without the Emotion babel plugins and presets?
I'm guessing it should...

(Apologies if you've tried this already. Just trying to troubleshoot)

Something that is sticking out to me, is perhaps a conflict with the wp.element.createElement pragma and Emotion's babel preset.

Taking a look... it looks like they're doing something with jsx within their preset:
https://github.com/emotion-js/emotion/blob/master/packages/babel-preset-css-prop/src/index.js#L1

That would be my guess.
I'm unsure of a potential solution though.

For some context.. Within the project, the @wordpress/components package uses Emotion. However it's very much in an experimental. The emotion APIs and build tools aren't used outside that package.

@annezazu annezazu added the [Type] Help Request Help with setup, implementation, or "How do I?" questions. label Apr 21, 2020
@chrisvanpatten
Copy link
Contributor

I think technically this would be outside the scope of the type of requests that we'd normally handle in this repo as it's more of help request.

That said… I've had success using Emotion in Gutenberg blocks but have avoided the use of the emotion Babel preset.

Without the Babel preset, you can use the className prop instead of css to set your styles. For example:

import { css, cx } from 'emotion';

const Block = ( props ) => {
    const {
        attributes: {
            isToggled,
        },
        setAttributes,
    } = props;

    const styles = {
        selector: css`
            background: red;
            color: white;
        `,
        selectorToggled: css`
            background: blue;
        `,
    };

    return (
        <div
            className={ cx(
                styles.selector,
                { [ styles.selectorToggled ]: isToggled },
            ) }
            onClick={ () => setAttributes( { isToggled: ! isToggled } ) }
        >
            Wow!
        </div>
    );
};

export default Block;

There might be limitations in this approach but it's worked really well for us. Hope this helps!

@gracelauren
Copy link
Author

gracelauren commented Apr 22, 2020

Thank you for your help, @ItsJonQ and @chrisvanpatten I will this out now
Also apologies, I had tagged a Help Request but looks like it didn't set, thanks for resetting this for me

@chrisvanpatten
Copy link
Contributor

@gracelauren No problem :) Since technically we try to discourage help requests here I'll close this out (Stack Exchange or the WordPress forums are typically better resources) but if you have questions feel free to comment on the closed issue and tag me. And if there's reason to reopen (e.g. some kind of incompatibility with the core Gutenberg code that can be addressed on our end) we can reopen as well!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Type] Help Request Help with setup, implementation, or "How do I?" questions.
Projects
None yet
Development

No branches or pull requests

4 participants