Skip to content

Commit ee80d98

Browse files
committed
apply parent-hover styles on frontend, add theme compatibility
1 parent c8b8cf6 commit ee80d98

File tree

8 files changed

+405
-178
lines changed

8 files changed

+405
-178
lines changed

src/plugins/editor-device-preview-class/index.js

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,31 @@ const EditorPreviewClass = () => {
2424

2525
// Update the editor class when the preview size changes.
2626
useEffect( () => {
27-
if ( editorEl && editorEl.classList.contains( `stk-preview-device-${ deviceType.toLowerCase() }` ) === false ) {
28-
editorEl.classList.remove( 'stk-preview-device-desktop', 'stk-preview-device-tablet', 'stk-preview-device-mobile' )
29-
editorEl.classList.add( `stk-preview-device-${ deviceType.toLowerCase() }` )
27+
const themeRegex = /stk--is-\w+-theme/gm
3028

31-
// At first load of the editor, the `stk-preview-device-*` is removed, so we have to re-add it.
29+
if ( editorEl ) {
30+
// Add device class
31+
if ( editorEl && editorEl.classList.contains( `stk-preview-device-${ deviceType.toLowerCase() }` ) === false ) {
32+
editorEl.classList.remove( 'stk-preview-device-desktop', 'stk-preview-device-tablet', 'stk-preview-device-mobile' )
33+
editorEl.classList.add( `stk-preview-device-${ deviceType.toLowerCase() }` )
34+
}
35+
36+
// Add theme class
37+
if ( document.querySelector( 'body' ).className.match( themeRegex ) && ! editorEl.className.match( themeRegex ) ) {
38+
const theme = document.querySelector( 'body' ).className.match( themeRegex )[ 0 ]
39+
editorEl.classList.add( theme )
40+
}
41+
42+
// At first load of the editor, the `stk-preview-device-*` and `stk--is-*-theme` are removed, so we have to re-add it.
3243
const mo = onClassChange( editorEl, () => {
3344
if ( editorEl?.classList.contains( `stk-preview-device-${ deviceType.toLowerCase() }` ) === false ) {
3445
editorEl.classList.remove( 'stk-preview-device-desktop', 'stk-preview-device-tablet', 'stk-preview-device-mobile' )
3546
editorEl.classList.add( `stk-preview-device-${ deviceType.toLowerCase() }` )
3647
}
48+
if ( document.querySelector( 'body' ).className.match( themeRegex ) && ! editorEl.className.match( themeRegex ) ) {
49+
const theme = document.querySelector( 'body' ).className.match( themeRegex )[ 0 ]
50+
editorEl.classList.add( theme )
51+
}
3752
} )
3853

3954
return () => mo.disconnect()

src/plugins/global-settings/color-schemes/editor-loader.js

Lines changed: 1 addition & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* Internal dependencies
33
*/
4-
// import { hoverState } from '../utils'
4+
import { convertToObj, getCSS } from './utils'
55

66
/**
77
* WordPress dependencies
@@ -13,100 +13,6 @@ import { useEffect, useState } from '@wordpress/element'
1313
*/
1414
import { useBlockColorSchemes, useBlockHoverState } from '~stackable/hooks'
1515

16-
const convertToObj = colorSchemes => {
17-
const obj = {}
18-
19-
colorSchemes.forEach( scheme => {
20-
obj[ scheme.key ] = scheme.colorScheme
21-
} )
22-
23-
return obj
24-
}
25-
26-
const camelToKebab = property => {
27-
const result = property.replace( /([a-z0-9])([A-Z])/g, '$1-$2' )
28-
29-
// Convert the result to lowercase and return with '--stk-' prefix
30-
return '--stk-' + result.toLowerCase()
31-
}
32-
33-
const getInheritedValue = ( property, currentState ) => {
34-
let value = property?.[ currentState ]
35-
36-
if ( ! value && currentState === 'desktopHover' ) {
37-
value = property?.desktopParentHover
38-
}
39-
40-
if ( ! value && currentState !== 'desktop' ) {
41-
value = property?.desktop
42-
}
43-
44-
return value
45-
}
46-
47-
const isGradient = value => value?.startsWith( 'linear-' ) || value?.startsWith( 'radial-' )
48-
49-
const getCSS = ( scheme, mode = '' ) => {
50-
const states = [ 'desktop', 'desktopHover', 'desktopParentHover' ]
51-
const properties = [
52-
'backgroundColor',
53-
'headingColor',
54-
'textColor',
55-
'linkColor',
56-
'accentColor',
57-
'buttonBackgroundColor',
58-
'buttonTextColor',
59-
'buttonOutlineColor',
60-
]
61-
62-
const decls = {
63-
desktop: [],
64-
desktopHover: [],
65-
desktopParentHover: [],
66-
}
67-
68-
states.forEach( state => {
69-
const suffix = state === 'desktopHover' ? '-hover' : ''
70-
properties.forEach( property => {
71-
const varname = mode === 'background' ? 'block' : 'container'
72-
const customProperty = property === 'backgroundColor'
73-
? `--stk-${ varname }-background-color` : camelToKebab( property )
74-
if ( property === 'backgroundColor' && ! mode ) {
75-
return
76-
}
77-
78-
if ( scheme[ property ]?.[ state ] ) {
79-
decls[ state ].push( `${ customProperty }${ suffix }: ${ scheme[ property ]?.[ state ] };` )
80-
return
81-
}
82-
83-
const inheritedValue = getInheritedValue( scheme[ property ], state )
84-
if ( state === 'desktopHover' && ! scheme[ property ]?.[ state ] && inheritedValue ) {
85-
decls[ state ].push( `${ customProperty }${ suffix }: ${ inheritedValue };` )
86-
}
87-
88-
if ( property === 'buttonBackgroundColor' && isGradient( scheme[ property ]?.[ state ] ) ) {
89-
decls[ state ].push( `:where(.is-style-plain){ --stk-button-plain-text-color${ suffix }: var(--stk-button-outline-color${ suffix }); }` )
90-
}
91-
} )
92-
} )
93-
94-
if ( isGradient( scheme.buttonBackgroundColor?.desktop ) && ! scheme.buttonBackgroundColor?.desktopHover ) {
95-
decls.desktopHover.push( `:where(.is-style-plain){ --stk-button-plain-text-color-hover: var(--stk-button-outline-color-hover); }` )
96-
}
97-
98-
if ( isGradient( scheme.buttonBackgroundColor?.desktopParentHover ) && ! scheme.buttonBackgroundColor?.desktopHover ) {
99-
decls.desktopParentHover.push( `:where(.is-style-plain){ --stk-button-plain-text-color-hover: var(--stk-button-outline-color-hover); }` )
100-
}
101-
102-
if ( isGradient( scheme.buttonBackgroundColor?.desktop ) &&
103-
scheme.buttonBackgroundColor?.desktopParentHover && ! isGradient( scheme.buttonBackgroundColor?.desktopParentHover ) ) {
104-
decls.desktopParentHover.push( `:where(.is-style-plain){ --stk-button-plain-text-color: unset;--stk-button-plain-text-color-hover:unset; }` )
105-
}
106-
107-
return decls
108-
}
109-
11016
const renderGlobalStyles = (
11117
setStyles,
11218
colorSchemesArray,

0 commit comments

Comments
 (0)