Skip to content

Commit e18b083

Browse files
committed
fix styling for block styles in style guide
1 parent 9443e52 commit e18b083

File tree

5 files changed

+94
-7
lines changed

5 files changed

+94
-7
lines changed

src/components/style-guide/editor.scss

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,10 @@ $guide-font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Ro
330330
.stk-block-timeline > .stk-inner-blocks::after {
331331
content: "" !important;
332332
}
333+
334+
.stk-block-notification h3 {
335+
color: var(--stk-container-color) !important;
336+
}
333337
}
334338

335339
.ugb-style-guide__elements__buttons .stk-button {
41.1 KB
Loading

src/components/style-guide/utils.js

Lines changed: 71 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
import heroBg from './images/hero-bg.webp'
2-
import { i18n, srcUrl } from 'stackable'
3-
import { cleanSerializedBlock, createUniqueClass } from '~stackable/util'
2+
import profile from './images/profile.webp'
3+
import {
4+
i18n, srcUrl, version as VERSION,
5+
} from 'stackable'
6+
7+
import { CssSaveCompiler } from '../block-css'
8+
import {
9+
cleanSerializedBlock,
10+
createUniqueClass,
11+
blockStyleGenerators,
12+
} from '~stackable/util'
13+
import { PLACEHOLDER_INNER_BLOCKS } from '~stackable/util/block-templates'
414

515
import { __ } from '@wordpress/i18n'
616
import { RawHTML } from '@wordpress/element'
@@ -9,7 +19,6 @@ import {
919
createBlocksFromInnerBlocksTemplate,
1020
getBlockVariations,
1121
} from '@wordpress/blocks'
12-
import { PLACEHOLDER_INNER_BLOCKS } from '~stackable/util/block-templates'
1322

1423
/* eslint-disable jsx-a11y/anchor-is-valid */
1524
export const DefaultButton = ( {
@@ -90,19 +99,51 @@ const ADDITIONAL_ATTRIBUTES = {
9099
'stackable/number-box': { text: __( '1', i18n ) },
91100
}
92101

102+
const getGeneratedCss = ( blocks, generateForInnerBlocks = false ) => {
103+
return blocks.map( block => {
104+
if ( ! block.attributes.uniqueId ) {
105+
block.attributes.uniqueId = createUniqueClass( block.clientId )
106+
}
107+
108+
const blockStyleGenerator = blockStyleGenerators[ block.name ]
109+
const attrNamesWithValues = blockStyleGenerator.getAttributesWithValues( block.attributes )
110+
const blockStyleDefs = blockStyleGenerator.getBlockStyles( attrNamesWithValues )
111+
112+
const cssCompiler = new CssSaveCompiler()
113+
const saveCss = blockStyleGenerator.generateBlockStylesForSave(
114+
cssCompiler,
115+
block.attributes,
116+
blockStyleDefs,
117+
{
118+
version: VERSION,
119+
}
120+
)
121+
122+
block.attributes.generatedCss = saveCss
123+
124+
if ( generateForInnerBlocks ) {
125+
block.innerBlocks = getGeneratedCss( block.innerBlocks, generateForInnerBlocks )
126+
}
127+
128+
return block
129+
} )
130+
}
131+
93132
export const RenderBlock = props => {
94133
const {
95134
blockName, attributes, innerBlocks, name = __( 'Default', i18n ),
96135
} = props
97136

98137
const block = createBlock( blockName, attributes, innerBlocks )
99-
block.attributes.uniqueId = createUniqueClass( block.clientId )
100-
let serialized = serialize( [ block ] )
138+
const newBlock = getGeneratedCss( [ block ] )
139+
140+
let serialized = serialize( newBlock )
101141

102142
if ( blockName === 'stackable/timeline' ) {
103143
const _block = createBlock( blockName, attributes, innerBlocks )
104144
_block.attributes.timelineIsLast = true
105-
serialized += '\n' + serialize( [ _block ] )
145+
const duplicateBlock = getGeneratedCss( [ _block ] )
146+
serialized += '\n' + serialize( duplicateBlock )
106147
}
107148

108149
return (
@@ -113,6 +154,19 @@ export const RenderBlock = props => {
113154
)
114155
}
115156

157+
const INNER_BLOCK_CALLBACKS = {
158+
'stackable/team-member': innerBlocks => {
159+
innerBlocks[ 0 ].attributes.imageExternalUrl = `${ srcUrl }/${ profile }`
160+
161+
return innerBlocks
162+
},
163+
'stackable/testimonial': innerBlocks => {
164+
innerBlocks[ 1 ].attributes.imageExternalUrl = `${ srcUrl }/${ profile }`
165+
166+
return innerBlocks
167+
},
168+
}
169+
116170
export const getPlaceholders = blockName => {
117171
let innerBlocks = []
118172
let attributes = {}
@@ -123,6 +177,14 @@ export const getPlaceholders = blockName => {
123177
innerBlocks = createBlocksFromInnerBlocksTemplate( PLACEHOLDER_INNER_BLOCKS[ blockName ] )
124178
} else if ( variations.length && variations[ 0 ].innerBlocks?.length ) {
125179
innerBlocks = createBlocksFromInnerBlocksTemplate( variations[ 0 ].innerBlocks )
180+
181+
if ( blockName in INNER_BLOCK_CALLBACKS ) {
182+
innerBlocks = INNER_BLOCK_CALLBACKS[ blockName ]( innerBlocks )
183+
}
184+
}
185+
186+
if ( innerBlocks.length ) {
187+
innerBlocks = getGeneratedCss( innerBlocks, true )
126188
}
127189

128190
if ( variations.length && variations[ 0 ].attributes ) {
@@ -136,5 +198,7 @@ export const getPlaceholders = blockName => {
136198
}
137199
}
138200

139-
return { attributes, innerBlocks }
201+
return {
202+
attributes, innerBlocks,
203+
}
140204
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const fetchBlockStyleGenerators = r => {
2+
const blockStyleGenerators = {}
3+
r.keys().forEach( key => {
4+
const styleGenerator = r( key ).default
5+
6+
// Get the folder name from the key (e.g., './feature/style.js' => 'feature')
7+
const match = key.match( /^\.\/([^/]+)\// )
8+
const blockName = match ? match[ 1 ] : null
9+
10+
if ( blockName ) {
11+
blockStyleGenerators[ `stackable/${ blockName }` ] = styleGenerator
12+
}
13+
} )
14+
15+
return blockStyleGenerators
16+
}
17+
18+
export const blockStyleGenerators = fetchBlockStyleGenerators( require.context( '../../block', true, /style\.js$/ ) )

src/util/styles/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import { applyFilters } from '@wordpress/hooks'
1515
export { default as StyleObject } from './style-object'
1616
export { useStyles, getStyles } from './style-object'
1717
export { useQueryLoopInstanceId } from './style-object'
18+
export { blockStyleGenerators } from './block-style-generators'
1819

1920
export const isDarkColor = _color => {
2021
try {

0 commit comments

Comments
 (0)