Skip to content

Instantly share code, notes, and snippets.

@GianlucaGuarini
Last active December 3, 2015 15:03
Show Gist options
  • Save GianlucaGuarini/c05446d4967ff3a271ce to your computer and use it in GitHub Desktop.
Save GianlucaGuarini/c05446d4967ff3a271ce to your computer and use it in GitHub Desktop.
my small modernizr
/**
* Check if any css property is supported
* @param { String } property - css property to test for example 'column-count'
* @param { String } value - check whether a value is to a css property is supported
* @returns { String|Array|Boolean } either only the property in camel case, or the property and its value
* 4 ex:
* supportCss('column-count') => 'MozColumnCount'
* supportCss('column-count', '2') => ['MozColumnCount', '2']
* supportCss('position', 'sticky') => ['position', '-webkit-sticky']
*/
export function supportsCss(property, value) {
var el = document.body || document.documentElement,
style = el.style,
cssProp,
// Tests for vendor specific prop
prefixes = ['Webkit', 'Moz', 'ms', 'O', 'Khtml']
// No css support detected
if (typeof style == 'undefined') return false
// normalize the property adding the prefixes
property = (function(prop) {
// Tests for standard prop
if (typeof style[prop] == 'string') return prop
prop = toCamel(prop.charAt(0).toUpperCase() + prop.substr(1))
for (var i = 0; i < prefixes.length; i++) {
if (typeof style[prefixes[i] + prop] == 'string') return prefixes[i] + prop
}
})(property)
// check only if the property is supported
if (typeof value == 'undefined') {
return property
} else {
// check the css value on a dummy dom element
el = document.createElement('test')
cssProp = `${toDash(property)}:`
style = el.style
prefixes = prefixes.map(str => `-${str.toLowerCase()}-`).concat([''])
// add the value prefixed
style.cssText = cssProp + prefixes.join( value + ';' + cssProp ) + value + ';'
if (!style[ property ] || style[ property ].indexOf(value) == -1) return false
else return [ property, style[ property ]]
}
return false
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment