Hi, I am just getting started with trying to create my first plugin and I want to create it as a panel. However all the tutorial docs and sample plugin projects seem to use a pattern of defining the HTML and CSS as a string, and inserted to an element using innerHTML. This feels clunky to me as my text editor can’t do syntax highlighting or autocomplete within a string, and doesn’t seem like good separation of concerns.
So my question is, how can I break out the HTML or at least the CSS out of main.js and reference it from another file? In the plugin docs it says “You can build panels using any method that creates an HTML DOM structure. This means you can use document.createElement, innerHTML, jQuery, React and other frameworks”. Are there example plugins using these other methods? Thanks!
It could also be possible to “import” an HTML file either by using some transpilation step (e.g., webpack) or using the storage APIs from the uxp module to import the HTML (or CSS) file’s content as a string and use it using something like
myPanelWrapper.innerHTML = importedString;
All in all, however, it is, to my knowledge, not possible to easily use HTML or CSS files for XD plugins…
I’ve been able to import an HTML file, though I will caveat this by saying that I’m not aware of it being a wide practice among XD plugin developers.
There could be good reasons why (for example, it requires more code than you might expect, and it puts you in async land because you’re loading a file), but so far, it has worked for me.
Here’s an example of getting an HTML file (in this case, markup.html):
Thanks @pklaschka I’m glad to participate in the community! I had already seen that repo but I was just wondering if there were examples that were closer to what I wanted. Maybe I’m just old fashioned I’ll check out the other methods you mentioned!
I’m probably going to go with this because it’s simpler and one less JavaScript loop.
Note that I’ve hard-coded "markup.html" (in the code I shared above a few days ago) or "ui/markup.html" (in the revised code based on Chris’s tip above) because I only have one file to load.
But if you refactor that string to be a function parameter (say, path), you’ve got yourself a nice helper function that can be reused.
Looks like you’ve got some good responses already, but as far as examples of other options go, yes! Check the plugin samples repo – we have react, jquery, vue, and vanilla samples: https://github.com/AdobeXD/plugin-samples
Also, we’re adding capabilities to the manifest to allow it to align across all apps taking UXP, and so in the future you’ll be able to use an HTML file as your entry point, instead of just a JS file. No date on that yet, but just thought you’d like to know.