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 npm commands 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

See video attached

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:

  1. 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
  2. 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

  1. Navigate to xb, then click on Assets in the left sidebar
  2. Click on any of the Astro components listed
  3. Wait for the Stackblitz IDE to load and for the project to finish loading dependencies (this might take around 15/20 seconds)
  4. 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)
  5. Switch out of the Assets menu to the Layers menu (left sidebar).
  6. Now you should be able to drag the Astro components into your XB preview from the component insert menu in top bar!
  7. Try changing the props or dragging other components into the Astro component's slots!

How to edit one of the example Astro components

  1. Navigate to xb, then click on Assets in the left sidebar
  2. Click on the Astro component you want to edit
  3. 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)
  4. 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.
  5. 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)
  6. Switch out of the Assets menu and into the Layers menu and drag the component you just edited from the top bar insert menu.
  7. You should see your updated component

Proposed resolution

User interface changes

Command icon 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

hooroomoo created an issue. See original summary.

hooroomoo’s picture

Assigned: Unassigned » hooroomoo
hooroomoo’s picture

Title: Stackblitz POC » [exploratory] Stackblitz POC

xinran made their first commit to this issue’s fork.

cosmicdreams’s picture

Very interesting...

Is this an effort to make it easier to create components? https://developer.stackblitz.com/guides/user-guide/what-is-stackblitz

hooroomoo’s picture

@cosmicdreams Yup, we are exploring StackBlitz as a way for people to create their own JS components within XB

hooroomoo’s picture

Title: [exploratory] Stackblitz POC » [exploratory] StackBlitz PoC

effulgentsia made their first commit to this issue’s fork.

effulgentsia’s picture

Version: » 0.x-dev

All 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.

wim leers’s picture

Issue summary: View changes
Status: Active » Needs work
Issue tags: +Needs issue summary update
StatusFileSize
new591.74 KB

VERY COOL! 😄

I think the issue summary would benefit tremendously from an update to clarify where what the purpose of this is 🤓

hooroomoo’s picture

Issue summary: View changes
StatusFileSize
new257.08 KB

Updating screenshot

hooroomoo’s picture

Issue summary: View changes
StatusFileSize
new245.28 KB
hooroomoo’s picture

Issue summary: View changes
Status: Needs work » Active
Issue tags: -Needs issue summary update
hooroomoo’s picture

Issue summary: View changes
hooroomoo’s picture

Issue summary: View changes
effulgentsia’s picture

For this POC, we will create an SDC wrapper so our Astro components are registered as Drupal SDCs.

I 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.

effulgentsia’s picture

Issue summary: View changes

Added an "In Summary" bulleted list to the Overview.

effulgentsia’s picture

Issue summary: View changes
hooroomoo’s picture

Issue summary: View changes
StatusFileSize
new246.46 KB

Don't mind me... just updating the screenshot that has Svelte and Preact specific colors in the text...

effulgentsia’s picture

@hooroomoo, @xinran, and I met with @lauriii, and here's some UX improvement suggestions based on his feedback:

  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.
hooroomoo’s picture

Pushed 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.

effulgentsia’s picture

It would be nice to remove the step where the user has to type npm run build into 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 run npm update if they want to update their dependencies or npm 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 the dist/_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 the dist/_astro/* files from there?

hooroomoo’s picture

UX 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

brianperry’s picture

@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...

effulgentsia’s picture

Title: [exploratory] StackBlitz PoC » [exploratory] PoC of Astro island components editable via StackBlitz

Thanks for #27! Retitling to surface the Astro part in case others are looking for that as well.

hooroomoo’s picture

i 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/performance

effulgentsia’s picture

npm run build currently encounters 5 errors:

Found 5 errors in 4 files.

Errors  Files
     1  src/components/sidebar/AssetItem.tsx:12
     1  src/components/sidebar/AssetsMenu.tsx:28
     2  src/components/sidebar/PrimaryMenu.tsx:18
     1  src/components/stackblitz/StackBlitzController.tsx:54

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.

hooroomoo’s picture

hooroomoo’s picture

Assigned: hooroomoo » Unassigned
hooroomoo’s picture

Issue summary: View changes
hooroomoo’s picture

Issue summary: View changes
hooroomoo’s picture

Issue summary: View changes
hooroomoo’s picture

Will 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.")

hooroomoo’s picture

StatusFileSize
new11.6 MB
hooroomoo’s picture

Issue summary: View changes
hooroomoo’s picture

Issue summary: View changes
hooroomoo’s picture

Issue summary: View changes
hooroomoo’s picture

Issue summary: View changes

Do drupal issues not allow video tags? Wasn't able to attach the demo to the issue summary but see #39

effulgentsia’s picture

Component: Page builder » Theme builder
Category: Task » Feature request
Status: Active » Postponed
Related issues: +#3483267: [exploratory] PoC of Preact+Tailwind components editable via CodeMirror

The 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.

effulgentsia’s picture

Status: Postponed » Closed (outdated)

#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.