@shopgate/engage
Version:
Shopgate's ENGAGE library.
16 lines • 1.54 kB
JavaScript
/**
* Pass in an element and its CSS Custom Property that you want the value of.
* Optionally, you can determine what datatype you get back.
*
* @param {string} key The key of the custom property
* @param {HTMLElement} [element=document.documentElement] The target HTML element
* @param {string} [castAs='string'] Type for optional casting
* @returns {*}
*/export var getCSSCustomProp=function getCSSCustomProp(key){var element=arguments.length>1&&arguments[1]!==undefined?arguments[1]:document.documentElement;var castAs=arguments.length>2&&arguments[2]!==undefined?arguments[2]:'string';var response=getComputedStyle(element).getPropertyValue(key);// Tidy up the string if there's something to work with
if(response.length){response=response.replace(/'|"/g,'').trim();}// Convert the response into a whatever type we wanted
switch(castAs){case'number':case'int':return parseInt(response,10);case'float':return parseFloat(response,10);case'boolean':case'bool':return response==='true'||response==='1';default:return response;}};/**
* Sets the value of an CSS custom property.
* @param {string} key The key of the custom property
* @param {string} value The new value of the custom property
* @param {HTMLElement} [element=document.documentElement] The target HTML element
*/export var setCSSCustomProp=function setCSSCustomProp(key,value){var element=arguments.length>2&&arguments[2]!==undefined?arguments[2]:document.documentElement;var style=element.style;if(style.getPropertyValue(key)!==value){style.setProperty(key,value);}};