When Style Manager is in another div, the message "Select an element before using Style Manager" is not showing
Question
Version: 0.16.34
What is the expected behavior?
When the editor is initialized, no element is selected, so the style manager should say "Select an element before using Style Manager" like it does when not using "appendTo" in the editor configuration.
What is the current behavior?
When using "appendTo" in the editor config like this :
var editor = grapesjs.init({
...
styleManager: {
appendTo: "#styles"
},
...
}
the style manager starts with all the sectors showing although no element is selected
Demo
The same behavior can be seen in the example from https://grapesjs.com/docs/getting-started.html#style-manager
Answers (4)
I used this code to solve it:
const showStylesSector = editor => {
const stylesButton = editor.Panels.getButton('panel-sidebar-header', 'show-styles');
const layersButton = editor.Panels.getButton('panel-sidebar-header', 'show-layers');
// Disable this feature if the layers manager is active.
if (!layersButton.attributes.active) {
stylesButton.set('active', 1);
}
};
editor.on('component:selected', () => {
showStylesSector(editor);
});
This is not what I want, my issue is before any element is selected so before the event "component:selected" is trigerred. I temporarily fixed it by selecting an element on editor load but it is not what I really want
editor.on('load', () => {
editor.select(editor.getWrapper().find('#entry-point')[0])
});
If you're using the StyleManager in a custom element you have to handle that state on your own.
<div class="sm-wrapper">
<div id="styles"></div><!-- here is where you append the style manager -->
<div class="sm-empty-state">
Select an element
</div>
</div>
// use `component:toggled` as `component:selected` is not triggered if the selection is empty
editor.on('component:toggled', () => {
if (editor.getSelected()) {
// show style manager, hide empty state
} else {
// show empty state, hide style manager
}
});
Thanks for reporting this, @Nielsticot.
Great question about When Style Manager is in another div, the message "Select an element before using Style Manager" is not showing. The recommended approach with ProseMirror is to use the event-driven API.
Start here:
- Check the GrapesJS documentation for your specific module
- Look for the
on()event listener method - Most operations can be achieved by listening to editor and component events
Common patterns:
// Listen for changes
editor.on('change', () => console.log('something changed'));
// Component lifecycle
editor.on('component:mount', (c) => console.log('component ready', c));
editor.on('component:update', (c) => console.log('component updated', c));
If you're still stuck:
- Share a minimal CodeSandbox reproduction
- Include what you've already tried
- Mention your GrapesJS version
- The community is here to help!
Related Questions and Answers
Continue research with similar issue discussions.
Issue #3125
linear-gradient CSS does weird things to background-image
Version: 0.16.27 Are you able to reproduce the bug from the demo? [x] Yes [ ] No What is the expected behavior? When adding background-imag...
Issue #3218
How to add new HTML element with inline styles?
I would like to add a new HTML element, with inline styles, around the rte.selection(). With the following example, the HTML is OK, but pro...
Issue #3690
appendTo not working with trait manager
I have a basic example as follow HTML <div id="gjs"> <div class="traits-container"></div> <div style="padding: 25px">Hello World!!!</div> <...
Issue #3561
FEAT: Enhanced Shadow DOM Support
What are you trying to add to GrapesJS? Better Shadow DOM support, allowing developers to render GrapesJS inside a Shadow element without h...
Paid Plugins That Match This Issue
Curated by issue keywords and label relevance to help you ship faster.
Loading paid plugin recommendations...
Check the open-source GrapesJS plugins on GitHub or run a quick search in our free catalog.
Browse free plugins βPremium plugins ship with support, regular updates, and production-ready features β save days of integration work.
Browse premium plugins βBrowse Plugin Categories
Jump directly to plugin category pages on the marketplace.