PluginProbe ʕ •ᴥ•ʔ
GenerateBlocks / 2.2.0
GenerateBlocks v2.2.0
trunk 1.0 1.0.1 1.0.2 1.1.0 1.1.1 1.1.2 1.2.0 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.4.0 1.4.1 1.4.2 1.4.3 1.4.4 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.7.0 1.7.1 1.7.2 1.7.3 1.8.0 1.8.1 1.8.2 1.8.3 1.9.0 1.9.1 2.0.0 2.0.1 2.0.2 2.1.0 2.1.1 2.1.2 2.2.0 2.2.1 2.3.0
generateblocks / src / hoc / withStyles.js
generateblocks / src / hoc Last commit date
migrations 2 years ago index.js 1 year ago withButtonContainerLegacyMigration.js 3 years ago withButtonLegacyMigration.js 3 years ago withContainerLegacyMigration.js 3 years ago withDeviceType.js 3 years ago withDynamicTag.js 1 year ago withGridLegacyMigration.js 3 years ago withHeadlineLegacyMigration.js 3 years ago withHtmlAttributes.js 1 year ago withImageLegacyMigration.js 3 years ago withSetAttributes.js 3 years ago withStyles.js 1 year ago withUniqueId.js 2 years ago
withStyles.js
130 lines
1 import { useMemo } from '@wordpress/element';
2
3 import { defaultAtRules, getCss, cleanStylesObject, getPreviewWidth } from '@edge22/styles-builder';
4 import {
5 useAtRuleEffect,
6 useStyleSelectorEffect,
7 useGenerateCSSEffect,
8 useSetStyles,
9 buildChangedStylesObject,
10 getSelector,
11 Style,
12 useDecodeStyleKeys,
13 } from '@edge22/block-styles';
14
15 import { useBlockStyles } from '@hooks/useBlockStyles';
16
17 export function withStyles( WrappedComponent ) {
18 return ( ( props ) => {
19 const {
20 attributes,
21 name,
22 setAttributes,
23 isSelected,
24 clientId,
25 } = props;
26
27 const {
28 uniqueId,
29 styles,
30 css,
31 } = attributes;
32
33 const {
34 atRule,
35 deviceType,
36 setAtRule,
37 currentStyle,
38 setCurrentStyle,
39 setNestedRule,
40 } = useBlockStyles();
41
42 const setStyles = useSetStyles( props, { cleanStylesObject } );
43
44 const selector = useMemo( () => {
45 if ( ! uniqueId ) {
46 return '';
47 }
48
49 return getSelector( name, uniqueId );
50 }, [ name, uniqueId ] );
51
52 const frontendStyles = Array.isArray( styles ) ? {} : styles;
53
54 function onStyleChange( property, value = '', atRuleValue = '', nestedRuleValue = '' ) {
55 const newStyles = typeof property === 'object'
56 ? property
57 : { [ property ]: value };
58 const changedStyles = buildChangedStylesObject( newStyles, atRuleValue, nestedRuleValue );
59
60 setStyles( changedStyles );
61 }
62
63 function getStyleValue( property, atRuleValue = '', nestedRuleValue = '' ) {
64 if ( nestedRuleValue ) {
65 if ( atRuleValue ) {
66 return styles?.[ nestedRuleValue ]?.[ atRuleValue ]?.[ property ] ?? '';
67 }
68
69 return styles?.[ nestedRuleValue ]?.[ property ] ?? '';
70 } else if ( atRuleValue ) {
71 return styles?.[ atRuleValue ]?.[ property ] ?? '';
72 }
73
74 return styles?.[ property ] ?? '';
75 }
76
77 useAtRuleEffect( {
78 deviceType,
79 atRule,
80 setAtRule,
81 defaultAtRules,
82 isSelected,
83 getPreviewWidth,
84 } );
85
86 useGenerateCSSEffect( {
87 selector,
88 styles: frontendStyles,
89 setAttributes,
90 getCss,
91 getSelector,
92 isSelected,
93 blockCss: css,
94 clientId,
95 } );
96
97 useStyleSelectorEffect( {
98 isSelected,
99 currentStyle,
100 selector,
101 setCurrentStyle,
102 setNestedRule,
103 } );
104
105 useDecodeStyleKeys( {
106 styles,
107 setAttributes,
108 } );
109
110 return (
111 <>
112 <Style
113 selector={ selector }
114 getCss={ getCss }
115 styles={ frontendStyles }
116 clientId={ clientId }
117 name={ name }
118 />
119 <WrappedComponent
120 { ...props }
121 selector={ selector }
122 onStyleChange={ onStyleChange }
123 getStyleValue={ getStyleValue }
124 styles={ frontendStyles }
125 />
126 </>
127 );
128 } );
129 }
130