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

Block preview issue while using Alpine.js in Gutenberg #74

Closed
fabianwurk opened this issue Mar 2, 2021 · 6 comments
Closed

Block preview issue while using Alpine.js in Gutenberg #74

fabianwurk opened this issue Mar 2, 2021 · 6 comments

Comments

@fabianwurk
Copy link

Brandon do you know if there is an issue with using Alpine.js within blocks? I've just added a simple alpine code block to an acf composer block view file eg:

<div class="{{ $block->classes }}">

  <div x-data="{ count: 1 }">
    <p x-text="count"></p>
    <button @click="count++">Count Up</button>
    <button @click="count--">Count Down</button>
  </div>

  <div>
    <InnerBlocks />
  </div>
</div>

It no longer allows the block to preview in Gutenberg?

Screenshot 2021-03-02 at 16 41 08

@Log1x
Copy link
Owner

Log1x commented Mar 2, 2021

Alpine and React don't play nice together in the backend. If you get it working I'm interested.

@fabianwurk
Copy link
Author

Yeah, thought there may be a conflict. I'll have a deeper dig to see if I can find out more, and maybe test with Vue too. Will keep you posted.

@fabianwurk
Copy link
Author

I think JSX is the troublemaker - see this article: https://codewithhugo.com/alpine-js-react/

So, can disable it via on block:

    public $supports = [
       ............
        'jsx' => false,
    ];

But not sure what knock on effect that might have 🧐

@Log1x
Copy link
Owner

Log1x commented Mar 2, 2021

It'd prevent the use of <InnerBlocks />

@fabianwurk
Copy link
Author

Suppose its a bit of a trade-off, with using Alpine or Inner Blocks - can't imagine using both together, so this method is a viable workaround.

@dsturm
Copy link
Contributor

dsturm commented Jun 18, 2021

I would rather use the $block->preview attribute to conditionally include the Alpine directives. Furthermore you should avoid use event shorthand @click and instead use x-on:click to prevent issues coming from blade directives. So in this example:

<div class="{{ $block->classes }}">

  <div @if (!$block->preview) x-data="{ count: 1 }" @endif>
    <p @if (!$block->preview) x-text="count" @endif></p>
    <button x-on:click="count++">Count Up</button>
    <button x-on:click="count--">Count Down</button>
  </div>

  <div>
    <InnerBlocks />
  </div>
</div>

This issue was closed.
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

3 participants