question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add new class programatically

See original GitHub issue

Hello Artf,

I want to edit the styles of components (mostly imported html) without modifying all components with the same class. I tried to add a random named class programmatically like this: ` this.editor.on(‘component:selected’, function(model) { if (model && model.length > 2) { const uuid = this.s4(); // generate new random class name

            const selector =  {
                name: uuid,
                type: 'class',
                label: uuid,
            };

            model[1].attributes.classes.push(selectorManager.get(uuid));
 let mClasees = model[1].attributes.classes;
         const uuid = this.s4();
         const selector =  {
             name: uuid,
             type: 'class',
             label: uuid,
         };
      mClasees.push(selector);

}.bind(this)); `

This add a class programmatically to the component but it seem to no let me edit it’s style.

I just want be able to change the style of a single component without having to care which class it belongs to. Event if I copy past a component, I want be able to edit each components individually. I hope that clear. If you need any further information, don’t hesitate to ask me 😃 Thanks your help!

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:10 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
artfcommented, Jan 2, 2020

@wemod123 editor.getWrapper().addClass('my-class')

1reaction
artfcommented, Feb 13, 2019

@IrinaCodixis from the https://github.com/artf/grapesjs/releases/tag/v0.14.52 we’ve added setRule/getRule methods, so to add some CSS rule programatically you should do this:

editor.CssComposer.setRule('.some-class', { color: 'red' })

// Remove
const rule = editor.CssComposer.getRule('.some-class');
editor.CssComposer.getAll().remove(rule);
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to dynamically change CSS class of an HTML tag?
You can add a CSS class based on id dynamically using classList API as follows: document.getElementById('idOfElement').
Read more >
How to dynamically create and apply CSS class in JavaScript
After adding new classes to the elements, we select the new class(test) and then apply CSS properties to it, with the help of...
Read more >
Dynamically add CSS class with JavaScript - Includehelp.com
In this post, we will learn how to dynamically add CSS class with JavaScript code. It is useful to add interactive dynamic functionality?...
Read more >
How to Create a Class Dynamically in C#? - Code Maze
In this article, we are going to learn how to create a class dynamically in C#. Thanks to the introduction of the dynamic...
Read more >
How to Dynamically Add a Class Name in Vue
Adding a dynamic class name is as simple as adding the prop :class="classname" to your component. Whatever classname evaluates to will be the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found