Issue #442Opened October 21, 2017by roytang0 reactions

Is it possible to include an external stylesheet in the grapesjs canvas?

Question

We want to create some custom components whose style changes according to an external theme css (there can be different themes), and we want those styles to appear in the canvas while editing.

Answers (3)

roytangOctober 21, 20170 reactions
Code SnippetTEXT
Please ignore. I ended up injecting the stylesheet link using javascript into Grapes' canvas' iframe

// insert external stylesheet into the grapesjs window
var iframes = Utilities_GetElementsByClass("gjs-frame", "IFRAME", document.body);
for (var i=0; i<iframes.length; i++) {
  var iframe = iframes[i];
  var doc = iframe.contentDocument;
  var element = document.createElement("link");
  element.setAttribute("rel", "stylesheet");
  element.setAttribute("type", "text/css");
  element.setAttribute("href", "/CSS/Website.css");
  doc.getElementsByTagName("head")[0].appendChild(element);
}
artfOctober 23, 20170 reactions

You can inject scripts/styles via canvas config option

const editor = grapesjs.init({
  ...
  canvas: {
    styles: ['https://...', '...'],
    scripts: ['https://...'],
  }
});
roytangOctober 23, 20170 reactions

Thanks!

Related Questions and Answers

Continue research with similar issue discussions.

Paid Plugins That Match This Issue

Curated by issue keywords and label relevance to help you ship faster.

View all plugins

Loading paid plugin recommendations...

Browse Plugin Categories

Jump directly to plugin category pages on the marketplace.