Issue when inserting synced pattern and then detaching
-
Hi there, I noticed an issue when using detached Gutenberg synced patterns that contain Greenshift blocks. Changing the style of detached patterns applies those changes to all other instances of that patterns on the same page. Steps to reproduce issue are:
- create a synced pattern with a Greenshift block
- insert that synced pattern again on the page (or duplicate)
- detach the block so it’s no longer synced
- edit any on the now detached pattern, e.g. apply a rotation transformation
- the result is that the style is applied also to the synced pattern as if they are still connected
The issue is coming from the fact that Greenshift assigns an element ID to each block and generates the CSS as specific to that ID, inserting it globally on the page under style with ID “greenshift-post-css-inline-css”; however when inserting synced patterns and then detaching them, the ID is not regenerated and remains the same as the ID of the block that is the block in the synced pattern.
Some possible suggestions for fixing that come to mind:
- regenerate the block ID when a block is detached from the master synced one
- regenerate the block ID whenever any property on it is changed
- use some other means of applying the CSS to each block that doesn’t rely on the element ID; perhaps you can use scoped styles as you do in other places
Perhaps there is a better way. For now my workaround is whenever I want to use a synced pattern but detach and modify that instance is to recreate the structure from scratch and copy/past style attributes. This creates the new blocks with unique IDs.
I think in general it is best to avoid automatically assigning ID to blocks because when inserting a synced pattern multiple times on the page you end up with elements with duplicate IDs which is against the HTML spec, even if it doesn’t apparently cause issues until you detach a block and change it.
Perhaps the best thing would be to instead generate a unique class rather than ID, and use that for styles. I.e. instead of
<div .... id="gspb_container-id-gsbp-e8ae152">
insert that as a class, like<div class="... gspb_container-id-gsbp-e8ae152">
and define its style for.gspb_container-id-gsbp-e8ae152
rather than#gspb_container-id-gsbp-e8ae152
. You would still need to regenerate the class whenever a block is detached or changed of course. Or if you use scoped styles, this might be another way._____
An interesting observation is that duplicating a block regenerates the ID, unless that block has come from a detached pattern. I.e. inserting a synced pattern, detaching it and then duplicating the block does not regenerate a unique ID, but duplicating a block that was never saved as a synced pattern, does recreate the ID as unique.
- You must be logged in to reply to this topic.