Overview
This issue is to explore using StackBlitz with Astro as a way for people to create their own JS components inside of XB regardless of framework. Astro supports multiple frameworks in one project.
Astro's build step bundles interactive UI components (Astro islands) as individual files, so we can grab all the files generated from /dist and store them on the Drupal side to add them as a usable component within XB. Before it is usable in XB, we need an intermediary step to connect what we get from Astro to be an SDC. For this POC, we will create an SDC wrapper so our Astro components are registered as Drupal SDCs. In the future, once #3454519: [META] Support component types other than SDC, block, and code components is done, we'll be able to remove the SDC wrappers and implement JS components as their own first-class component types.
In summary:
- Astro's special sauce is in bundling JS components from any JS rendering framework and putting a web component wrapper around them for interoperability.
- StackBlitz's special sauce is both a nice in-browser code editor and the ability to run
npmcommands in the browser, including Astro's dev server for live previews and Astro's build step for generating the bundled assets to send to Drupal. - Experience Builder's special sauce is in providing a GUI for dragging and dropping components into slots and setting their prop values, including in the future being able to populate prop values from dynamic sources like the entity's structured data (fields), views, or remote sources.
- This POC is about exploring how this can all come together in a unified experience.
References:
StackBlitz SDK
Astro islands
Demo Video
HOW TO TEST WITH TUGBOAT (or locally)
There are four example Astro components in this MR to play with: PreactCounter, PreactTwoColumn, SvelteCounter, SvelteTwoColumn
PLEASE READ, DISCLAIMERS:
- Currently the CSS and JS aggregation needs to be turned off at this time. Tugboat is already configured to have that off, but if testing locally, turn it off at
admin/config/development/performance - Dragging astro components in the XB preview is a little broken right now for some of them. You can always drag the components around using the layers menu.
How to add one of the example Astro components to your XB page
- Navigate to xb, then click on Assets in the left sidebar
- Click on any of the Astro components listed
- Wait for the Stackblitz IDE to load and for the project to finish loading dependencies (this might take around 15/20 seconds)
- Click on the blue Get Files button in the top bar. This will also take around 15-20 seconds. Wait until an alert saying 'Files uploaded' pops up in the UI. (This bundles up the files in the background and then writes those files to /sites/default/file/xb/astro)
- Switch out of the Assets menu to the Layers menu (left sidebar).
- Now you should be able to drag the Astro components into your XB preview from the component insert menu in top bar!
- Try changing the props or dragging other components into the Astro component's slots!
How to edit one of the example Astro components
- Navigate to xb, then click on Assets in the left sidebar
- Click on the Astro component you want to edit
- Wait for the Stackblitz IDE to load if you're loading it for the first time and for the project to finish loading dependencies (this might take around 15/20 seconds)
- Make an edit to the open component file then manually save the stackblitz project with cmd + s (if on mac ) You should also see your change reflected on the right side of the Stackblitz IDE which shows a preview.
- Click on the blue Get Files button in the top bar. This will also take around 15-20 seconds. Wait until an alert saying 'Files uploaded' pops up in the UI. (This bundles up the files in the background and then writes those files to /sites/default/file/xb/astro)
- Switch out of the Assets menu and into the Layers menu and drag the component you just edited from the top bar insert menu.
- You should see your updated component
Proposed resolution
User interface changes



| Comment | File | Size | Author |
|---|---|---|---|
| #39 | stackblitz-astro-demo.mp4 | 11.6 MB | hooroomoo |
| #33 | Screenshot 2024-10-10 at 12.00.20 PM.png | 278.29 KB | hooroomoo |
| #33 | Screenshot 2024-10-10 at 11.58.22 AM.png | 173.04 KB | hooroomoo |
| #33 | Screenshot 2024-10-10 at 11.58.06 AM.png | 158.52 KB | hooroomoo |
| #22 | Screenshot 2024-09-25 at 4.58.28 PM.png | 246.46 KB | hooroomoo |
Issue fork experience_builder-3475363
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
hooroomooComment #3
hooroomooComment #6
cosmicdreams commentedVery interesting...
Is this an effort to make it easier to create components? https://developer.stackblitz.com/guides/user-guide/what-is-stackblitz
Comment #7
hooroomoo@cosmicdreams Yup, we are exploring StackBlitz as a way for people to create their own JS components within XB
Comment #8
hooroomooComment #10
effulgentsia commentedAll I did in #9 was rebased the MR against 0.x. I hoped that would add the Tugboat link under the issue fork, but it hasn't yet.
Comment #13
wim leersVERY COOL! 😄
I think the issue summary would benefit tremendously from an update to clarify where what the purpose of this is 🤓
Comment #14
hooroomooUpdating screenshot
Comment #15
hooroomooComment #16
hooroomooComment #17
hooroomooComment #18
hooroomooComment #19
effulgentsia commentedI added a sentence after this explaining that we'll remove these SDC wrappers after #3454519: [META] Support component types other than SDC, block, and code components is done.
Comment #20
effulgentsia commentedAdded an "In Summary" bulleted list to the Overview.
Comment #21
effulgentsia commentedComment #22
hooroomooDon't mind me... just updating the screenshot that has Svelte and Preact specific colors in the text...
Comment #23
effulgentsia commented@hooroomoo, @xinran, and I met with @lauriii, and here's some UX improvement suggestions based on his feedback:
PreactCounterandSvelteCounter(or whatever theirnameis in their SDC YAML file).pages/preview/PreactCounter.astroandpages/preview/SvelteCounter.astro.Comment #24
hooroomooPushed commit that lists astro components in assets menu (#2) from comment #23.
1. Instead of the Open StackBlitz button, create the StackBlitz iframe when the user clicks on the "Assets" button in XB's left sidebar. However, create this as a hidden iframe. This way it can boot up in the background without it appearing slow to the user.
2. On XB's "Assets" tab, show PreactCounter and SvelteCounter (or whatever their name is in their SDC YAML file).
3. When one of those is clicked, make the StackBlitz iframe visible and tell it to open the corresponding component's source code file in the editor.4. Within the StackBlitz project, in addition to the index.astro file that's there, add a .astro file for each component containing a usage of that component in order to preview it. For example, pages/preview/PreactCounter.astro and pages/preview/SvelteCounter.astro.
5. For #3 above, it addition to navigating StackBlitz's code editor to the component's source file, also navigate its preview window to the component's preview page.
Comment #25
effulgentsia commentedIt would be nice to remove the step where the user has to type
npm run buildinto the terminal. In fact, if we remove this step then perhaps we could even set terminalHeight to 0 or a small number? The user would still need to be able to runnpm updateif they want to update their dependencies ornpm install ...if they want to add additional libraries, but they wouldn't need to do this every time they use the code editor, so ideally we could start the StackBlitz iframe with the terminal as out-of-the-way as possible as long as there's a way for the user to show/expand it when they do want to use it.Ok, so how can we remove the user needing to type
npm run build? Well, what if when the user clicks the "Get Files" button, instead of getting thedist/_astro/*files from the StackBlitz iframe, we instead spin up a separate WebContainer, transfer the project/source files from the StackBlitz instance to the WebContainer instance, then tell the WebContainer to run the build step, and then get thedist/_astro/*files from there?Comment #26
hooroomooUX improvements from #23 is almost all addressed besides one issue where the vm.preview.setUrl doesn't get set properly on the initial open. Will look into more tomorrow
Comment #27
brianperry@bnjmnm mentioned this Astro work in passing at DrupalCon (I was also going on about Astro,) but I wasn't sure exactly where it was bubbling up in XB work until I came across this issue. Super interesting stuff - will be following.
Have you seen Astro's experimental container API? It provides a way to render Astro components in isolation. Based on peeking at the MR I don't think it would really be needed here, but maybe it will spark some other ideas.
There is an Astro in PHP example out there, but I believe it requires node to be running on the same server. Of course, this MR provides access to web containers, so technically that gives us node. I've always wondered if some variation on this would make server rendering Astro components in Drupal possible...
Comment #28
effulgentsia commentedThanks for #27! Retitling to surface the Astro part in case others are looking for that as well.
Comment #29
hooroomooi haven't dug into this yet but the aggregation of js/css files currently has to be turned off to get our astro components to show up within the XB preview. This can be turned off at
/admin/config/development/performanceComment #32
effulgentsia commentednpm run buildcurrently encounters 5 errors:It would be great to fix those so that this can be tested in Tugboat. I'm guessing these errors weren't found during development because perhaps they're not triggered during
npm run drupaldev.Comment #33
hooroomooComment #34
hooroomooComment #35
hooroomooComment #36
hooroomooComment #37
hooroomooComment #38
hooroomooWill try to look at the dragging issue tom.
(Referring to Disclaimer #2 in the issue summary, "Dragging astro components in the XB preview is a little broken right now for some of them. You can always drag the components around using the layers menu.")
Comment #39
hooroomooComment #40
hooroomooComment #41
hooroomooComment #42
hooroomooComment #43
hooroomooDo drupal issues not allow video tags? Wasn't able to attach the demo to the issue summary but see #39
Comment #44
effulgentsia commentedThe work that's been done here, and the video in the issue summary that shows it off, is really great!
We may come back to this at some point, but for now, we're pivoting to #3483267: [exploratory] PoC of Preact+Tailwind components editable via CodeMirror.
Comment #45
effulgentsia commented#3483267: [exploratory] PoC of Preact+Tailwind components editable via CodeMirror is looking quite promising, and works without relying on any commercial service, so closing this as outdated.