migrations
2 years ago
index.js
1 year ago
withButtonContainerLegacyMigration.js
2 years ago
withButtonLegacyMigration.js
2 years ago
withContainerLegacyMigration.js
2 years ago
withDeviceType.js
3 years ago
withDynamicTag.js
1 year ago
withGridLegacyMigration.js
2 years ago
withHeadlineLegacyMigration.js
2 years ago
withHtmlAttributes.js
1 year ago
withImageLegacyMigration.js
2 years ago
withSetAttributes.js
2 years ago
withStyles.js
1 year ago
withUniqueId.js
2 years ago
withStyles.js
125 lines
| 1 | import { useMemo } from '@wordpress/element'; |
| 2 | |
| 3 | import { defaultAtRules, getCss, cleanStylesObject } 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 | } = attributes; |
| 31 | |
| 32 | const { |
| 33 | atRule, |
| 34 | deviceType, |
| 35 | setAtRule, |
| 36 | currentStyle, |
| 37 | setCurrentStyle, |
| 38 | setNestedRule, |
| 39 | } = useBlockStyles(); |
| 40 | |
| 41 | const setStyles = useSetStyles( props, { cleanStylesObject } ); |
| 42 | |
| 43 | const selector = useMemo( () => { |
| 44 | if ( ! uniqueId ) { |
| 45 | return ''; |
| 46 | } |
| 47 | |
| 48 | return getSelector( name, uniqueId ); |
| 49 | }, [ name, uniqueId ] ); |
| 50 | |
| 51 | const frontendStyles = Array.isArray( styles ) ? {} : styles; |
| 52 | |
| 53 | function onStyleChange( property, value = '', atRuleValue = '', nestedRuleValue = '' ) { |
| 54 | const newStyles = typeof property === 'object' |
| 55 | ? property |
| 56 | : { [ property ]: value }; |
| 57 | const changedStyles = buildChangedStylesObject( newStyles, atRuleValue, nestedRuleValue ); |
| 58 | |
| 59 | setStyles( changedStyles ); |
| 60 | } |
| 61 | |
| 62 | function getStyleValue( property, atRuleValue = '', nestedRuleValue = '' ) { |
| 63 | if ( nestedRuleValue ) { |
| 64 | if ( atRuleValue ) { |
| 65 | return styles?.[ nestedRuleValue ]?.[ atRuleValue ]?.[ property ] ?? ''; |
| 66 | } |
| 67 | |
| 68 | return styles?.[ nestedRuleValue ]?.[ property ] ?? ''; |
| 69 | } else if ( atRuleValue ) { |
| 70 | return styles?.[ atRuleValue ]?.[ property ] ?? ''; |
| 71 | } |
| 72 | |
| 73 | return styles?.[ property ] ?? ''; |
| 74 | } |
| 75 | |
| 76 | useAtRuleEffect( { |
| 77 | deviceType, |
| 78 | atRule, |
| 79 | setAtRule, |
| 80 | defaultAtRules, |
| 81 | isSelected, |
| 82 | } ); |
| 83 | |
| 84 | useGenerateCSSEffect( { |
| 85 | selector, |
| 86 | styles: frontendStyles, |
| 87 | setAttributes, |
| 88 | getCss, |
| 89 | getSelector, |
| 90 | } ); |
| 91 | |
| 92 | useStyleSelectorEffect( { |
| 93 | isSelected, |
| 94 | currentStyle, |
| 95 | selector, |
| 96 | setCurrentStyle, |
| 97 | setNestedRule, |
| 98 | } ); |
| 99 | |
| 100 | useDecodeStyleKeys( { |
| 101 | styles, |
| 102 | setAttributes, |
| 103 | } ); |
| 104 | |
| 105 | return ( |
| 106 | <> |
| 107 | <Style |
| 108 | selector={ selector } |
| 109 | getCss={ getCss } |
| 110 | styles={ frontendStyles } |
| 111 | clientId={ clientId } |
| 112 | name={ name } |
| 113 | /> |
| 114 | <WrappedComponent |
| 115 | { ...props } |
| 116 | selector={ selector } |
| 117 | onStyleChange={ onStyleChange } |
| 118 | getStyleValue={ getStyleValue } |
| 119 | styles={ frontendStyles } |
| 120 | /> |
| 121 | </> |
| 122 | ); |
| 123 | } ); |
| 124 | } |
| 125 |