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 */
1414import { 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 - z 0 - 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-
11016const renderGlobalStyles = (
11117 setStyles ,
11218 colorSchemesArray ,
0 commit comments