From d235879d2715f7d787403c3a445d5de85b8394c2 Mon Sep 17 00:00:00 2001 From: atomiks Date: Thu, 8 Mar 2018 23:22:54 +1000 Subject: [PATCH] Create prefix.md --- snippets/prefix.md | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 snippets/prefix.md diff --git a/snippets/prefix.md b/snippets/prefix.md new file mode 100644 index 000000000..1de9b283c --- /dev/null +++ b/snippets/prefix.md @@ -0,0 +1,28 @@ +### prefix + +Automatically prefixes a property to the one supported by the browser. + +Use an array of vendor prefix strings and loop through them, testing if `document.body` has one +of them defined in its CSSStyleDeclaration object, otherwise return `null`. Use `String.charAt()` +and `String.toUpperCase()` to capitalize the property, which will be appended to the vendor prefix string. + +```js +const prefix = property => { + const prefixes = ['', 'webkit', 'moz', 'ms', 'o'] + const upperProp = property.charAt(0).toUpperCase() + property.slice(1) + for (let i = 0; i < prefixes.length; i++) { + const prefix = prefixes[i] + const prefixedProp = prefix ? prefix + upperProp : property + if (typeof document.body.style[prefixedProp] !== 'undefined') { + return prefixedProp + } + } + return null +} +``` + +```js +prefix('transform') +// 'transform' on a supported browser +// otherwise 'webkitTransform' or 'mozTransform' or 'msTransform' or 'oTransform' +```