Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions src/components/block-css/use-block-style-generator.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useQueryLoopInstanceId } from '~stackable/util'
import { useMemo, useRef } from '@wordpress/element'
import { useRafEffect } from '~stackable/hooks'
import { dispatch } from '@wordpress/data'
import CssSaveCompiler from './css-save-compiler'

export const useBlockCssGenerator = props => {
Expand All @@ -11,6 +12,7 @@ export const useBlockCssGenerator = props => {
context,
attributes,
blockState,
setAttributes,
} = props

// Keep the filtered block styles that we will update.
Expand Down Expand Up @@ -66,12 +68,17 @@ export const useBlockCssGenerator = props => {
}
)

// Quietly save the styles. We cannot use setAttributes here because it
// will cause the block and this hook to rerender.
// dispatch( 'core/block-editor' ).__unstableMarkNextChangeAsNotPersistent()
// setAttributes( { generatedCss: saveCss } )
attributes.generatedCss = saveCss
}, [ attributes, version ] )
// If the generated CSS is the same as the one already saved, we don't need to update it.
if ( ! setAttributes || attributes.generatedCss === saveCss ) {
return
}

// Use setAttributes to realiably update the generated CSS.
// Mutating the attributes directly will not trigger a re-render,
// but might not properly save the changes.
dispatch( 'core/block-editor' ).__unstableMarkNextChangeAsNotPersistent()
setAttributes( { generatedCss: saveCss } )
}, [ attributes, version, setAttributes ] )

return editCss
}
Loading