Issue #3541πŸ’¬ AnsweredOpened June 15, 2021by deiga0 reactions

What is the idiomatic way to append and remove classes from components?

Quick answerby krishnaeverestengineering

@deiga we are doing something like this.

Read full answer below ↓

Question

Hey there πŸ‘‹ Love the editor! We're using traits to modify what classes a component has, but that feels a bit hacky as you have to redeclare ALL classnames for each trait selection Here a simple example: Each trait option has to have all the classes defined, because otherwise it will remove any existing classes. This feels like the wrong way to do this, but we haven't been able to figure out any other in-built way to modify classes of a component. What would be the correct way to do this?

Answers (3)

krishnaeverestengineeringβ€’ June 19, 2021

@deiga we are doing something like this.

artfβ€’ June 24, 2021

I'd attach the trait to some component property instead of the class attribute, and based on the value I'd use properly removeClass and addClass

GJSBlockβ€’ May 17, 2026

Thanks for reporting this, @deiga. Great question about [QUESTION] What is the idiomatic way to append and remove classes from components?. The recommended approach with Components is to use the event-driven API. Start here: Check the GrapesJS documentation for your specific module Look for the on(...

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

Free option

Check the open-source GrapesJS plugins on GitHub or run a quick search in our free catalog.

Browse free plugins β†’
Premium option

Premium plugins ship with support, regular updates, and production-ready features β€” save days of integration work.

Browse premium plugins β†’

Related tutorials

In-depth guides on the same topic.

All tutorials β†’

Browse Plugin Categories

Jump directly to plugin category pages on the marketplace.