diff --git a/README.md b/README.md index b0fdcb9f0..0e455b918 100644 --- a/README.md +++ b/README.md @@ -209,6 +209,7 @@ average(1, 2, 3); * [`off`](#off) * [`on`](#on) * [`onUserInputChange`](#onuserinputchange-) +* [`prefix`](#prefix) * [`recordAnimationFrames`](#recordanimationframes) * [`redirect`](#redirect) * [`runAsync`](#runasync-) @@ -3442,6 +3443,36 @@ onUserInputChange(type => {
[⬆ Back to top](#table-of-contents) +### 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; +}; +``` + +
+Examples + +```js +prefix('appearance'); // 'appearance' on a supported browser, otherwise 'webkitAppearance', 'mozAppearance', 'msAppearance' or 'oAppearance' +``` + +
+ +
[⬆ Back to top](#table-of-contents) + + ### recordAnimationFrames Invokes the provided callback on each animation frame. diff --git a/docs/index.html b/docs/index.html index e0f6dce92..c30b8c672 100644 --- a/docs/index.html +++ b/docs/index.html @@ -74,7 +74,7 @@ document.getElementById('doc-drawer-checkbox').checked = false; } }, false); - }

logo 30 seconds of code Curated collection of useful JavaScript snippets that you can understand in 30 seconds or less.

 

Adapter

ary

Creates a function that accepts up to n arguments, ignoring any additional arguments.

Call the provided function, fn, with up to n arguments, using Array.slice(0,n) and the spread operator (...).

const ary = (fn, n) => (...args) => fn(...args.slice(0, n));
+      }

logo 30 seconds of code Curated collection of useful JavaScript snippets that you can understand in 30 seconds or less.

 

Adapter

ary

Creates a function that accepts up to n arguments, ignoring any additional arguments.

Call the provided function, fn, with up to n arguments, using Array.slice(0,n) and the spread operator (...).

const ary = (fn, n) => (...args) => fn(...args.slice(0, n));
 
const firstTwoMax = ary(Math.max, 2);
 [[2, 6, 'a'], [8, 4, 6], [10]].map(x => firstTwoMax(...x)); // [6, 8, 10]
 

call

Given a key and a set of arguments, call them when given a context. Primarily useful in composition.

Use a closure to call a stored key with stored arguments.

const call = (key, ...args) => context => context[key](...args);
@@ -747,6 +747,15 @@ document.bodyShow examples
onUserInputChange(type => {
   console.log('The user is now using', type, 'as an input method.');
 });
+

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.

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;
+};
+
prefix('appearance'); // 'appearance' on a supported browser, otherwise 'webkitAppearance', 'mozAppearance', 'msAppearance' or 'oAppearance'
 

recordAnimationFrames

Invokes the provided callback on each animation frame.

Use recursion. Provided that running is true, continue invoking window.requestAnimationFrame() which invokes the provided callback. Return an object with two methods start and stop to allow manual control of the recording. Omit the second argument, autoStart, to implicitly call start when the function is invoked.

const recordAnimationFrames = (callback, autoStart = true) => {
   let running = true,
     raf;
diff --git a/snippets/prefix.md b/snippets/prefix.md
index b5704f7b5..65d009c35 100644
--- a/snippets/prefix.md
+++ b/snippets/prefix.md
@@ -9,9 +9,11 @@ Use `String.charAt()` and `String.toUpperCase()` to capitalize the property, whi
 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;
-}
+  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