| 1 |
import { cloneDeep } from 'lodash'; |
| 2 |
|
| 3 |
import * as styleUtils from '../utils/styles'; |
| 4 |
|
| 5 |
export default function useStyles(props) { |
| 6 |
const { attributes, setAttributes } = props; |
| 7 |
|
| 8 |
const ghostkitData = attributes?.ghostkit || {}; |
| 9 |
const styles = ghostkitData?.styles || {}; |
| 10 |
|
| 11 |
function getStyles() { |
| 12 |
return styleUtils.prepareStyles(styles); |
| 13 |
} |
| 14 |
|
| 15 |
/** |
| 16 |
* Get style value by property name, selected device and optional selector. |
| 17 |
* |
| 18 |
* @param {string} name - style property name. |
| 19 |
* @param {string} device - responsive device. |
| 20 |
* @param {string} selector - optional custom selector. |
| 21 |
* |
| 22 |
* @return {any} |
| 23 |
*/ |
| 24 |
function getStyle(name, device = false, selector = false) { |
| 25 |
return styleUtils.getStyle(styles, name, device, selector); |
| 26 |
} |
| 27 |
|
| 28 |
/** |
| 29 |
* Check if style exists. |
| 30 |
* |
| 31 |
* @param {string} name - style property name. |
| 32 |
* @param {string} device - responsive device. |
| 33 |
* @param {string} selector - optional custom selector. |
| 34 |
* |
| 35 |
* @return {boolean} |
| 36 |
*/ |
| 37 |
function hasStyle(name, device = false, selector = false) { |
| 38 |
return styleUtils.hasStyle(styles, name, device, selector); |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Set new styles. |
| 43 |
* |
| 44 |
* @param {string} name - style property name. |
| 45 |
* @param newStyles |
| 46 |
* @param {string} device - responsive device. |
| 47 |
* @param {string} selector - optional custom selector. |
| 48 |
*/ |
| 49 |
function setStyles(newStyles, device = false, selector = false) { |
| 50 |
const clonedGhostkitData = cloneDeep(ghostkitData); |
| 51 |
|
| 52 |
if (typeof clonedGhostkitData?.styles === 'undefined') { |
| 53 |
clonedGhostkitData.styles = {}; |
| 54 |
} |
| 55 |
|
| 56 |
const updatedStyles = styleUtils.getUpdatedStyles( |
| 57 |
clonedGhostkitData.styles, |
| 58 |
newStyles, |
| 59 |
device, |
| 60 |
selector |
| 61 |
); |
| 62 |
|
| 63 |
clonedGhostkitData.styles = updatedStyles; |
| 64 |
|
| 65 |
setAttributes({ ghostkit: clonedGhostkitData }); |
| 66 |
} |
| 67 |
|
| 68 |
function resetStyles(resetProps, withResponsive = false, selectors = ['']) { |
| 69 |
const result = styleUtils.getStylesToReset( |
| 70 |
resetProps, |
| 71 |
withResponsive, |
| 72 |
selectors |
| 73 |
); |
| 74 |
|
| 75 |
setStyles(result); |
| 76 |
} |
| 77 |
|
| 78 |
return { |
| 79 |
styles, |
| 80 |
getStyles, |
| 81 |
getStyle, |
| 82 |
hasStyle, |
| 83 |
setStyles, |
| 84 |
resetStyles, |
| 85 |
}; |
| 86 |
} |
| 87 |
|