| 1 |
import apiFetch from '@wordpress/api-fetch'; |
| 2 |
|
| 3 |
// Add required rules here and they will be checked in Launch |
| 4 |
// previews and added to Additional CSS |
| 5 |
export const requiredCSSVars = { |
| 6 |
'--wp--preset--spacing--30': 'clamp(1.5rem, 5vw, 2rem)', |
| 7 |
'--wp--preset--spacing--40': |
| 8 |
'clamp(1.8rem, 1.8rem + ((1vw - 0.48rem) * 2.885), 3rem)', |
| 9 |
'--wp--preset--spacing--50': 'clamp(2.5rem, 8vw, 4rem)', |
| 10 |
'--wp--preset--spacing--60': 'clamp(2.5rem, 8vw, 6rem)', |
| 11 |
'--wp--preset--spacing--70': 'clamp(3.75rem, 10vw, 7rem)', |
| 12 |
'--wp--preset--spacing--80': |
| 13 |
'clamp(5rem, 5.25rem + ((1vw - 0.48rem) * 9.096), 8rem)', |
| 14 |
}; |
| 15 |
|
| 16 |
export const addGlobalCSS = async (missingCSSVars) => { |
| 17 |
const id = window.extSharedData.globalStylesPostID; |
| 18 |
const { styles, settings } = await apiFetch({ |
| 19 |
path: `/wp/v2/global-styles/${id}`, |
| 20 |
}); |
| 21 |
// If any of the rules are already in the CSS, don't add them |
| 22 |
missingCSSVars = missingCSSVars.filter( |
| 23 |
(key) => !styles?.css?.includes(`${key}:`), |
| 24 |
); |
| 25 |
if (!missingCSSVars.length) return; |
| 26 |
const missingCSSVarsString = |
| 27 |
missingCSSVars.reduce((acc, key) => { |
| 28 |
acc += `${key}: ${requiredCSSVars[key]};\n`; |
| 29 |
return acc; |
| 30 |
}, ':root {\n') + '\n}'; |
| 31 |
apiFetch({ |
| 32 |
path: `/wp/v2/global-styles/${id}`, |
| 33 |
method: 'PATCH', |
| 34 |
data: { |
| 35 |
id, |
| 36 |
settings, |
| 37 |
styles: { |
| 38 |
...styles, |
| 39 |
css: |
| 40 |
// Preserve the existing css |
| 41 |
(styles?.css ?? '') + |
| 42 |
(styles?.css ? '\n' : '') + |
| 43 |
missingCSSVarsString, |
| 44 |
}, |
| 45 |
}, |
| 46 |
}); |
| 47 |
}; |
| 48 |
|