Merge pull request #630 from Chalarangelo/prefix

Create prefix.md
This commit is contained in:
Angelos Chalaris
2018-03-12 22:29:39 +02:00
committed by GitHub
2 changed files with 20 additions and 0 deletions

19
snippets/prefix.md Normal file
View File

@ -0,0 +1,19 @@
### prefix
Returns the prefixed version (if necessary) of a CSS property that the browser supports.
Use `Array.findIndex()` on an array of vendor prefix strings to test 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 = prop => {
const capitalizedProp = prop.charAt(0).toUpperCase() + prop.slice(1);
const prefixes = ['', 'webkit', 'moz', 'ms', 'o'];
const i = prefixes.findIndex(prefix => typeof document.body.style[(prefix ? prefix + capitalizedProp : prop)] !== 'undefined');
return i !== -1 ? i === 0 ? prop : prefixes[i] + capitalizedProp : null;
}
```
```js
prefix('appearance'); // 'appearance' on a supported browser, otherwise 'webkitAppearance', 'mozAppearance', 'msAppearance' or 'oAppearance'
```

View File

@ -192,6 +192,7 @@ pipeAsyncFunctions:adapter,function,promise
pipeFunctions:adapter,function pipeFunctions:adapter,function
pluralize:string pluralize:string
powerset:math powerset:math
prefix:browser,utility
prettyBytes:utility,string,math prettyBytes:utility,string,math
primes:math,array primes:math,array
promisify:adapter,function,promise promisify:adapter,function,promise