123

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <div class="preview" id="preview-old">
  <p class="title">Normal textarea behavior</p>
  <textarea spellcheck='false'>
  ## Feature detection + fallback

When using ***feature detection***, a pretty smart way to work when you aren't sure which features you can/cannot use consists in **several feature detections and consequent *fallbacks* to more basic methods** (or even creation of these methods from scratch) in case the features you want to use are not supported.

A simple example of *feature detection with fallback* may be applied to the `window.requestAnimationFrame` feature, which is not supported by all the browsers, and has several different prefixes depending on the browser you're working on. In this case, you can easily detect and *fallback* like this:

    requestAnimationFrame = 
       window.requestAnimationFrame       // Standard name
    || window.webkitRequestAnimationFrame // Fallback to -webkit (Chrome or Safari)
    || window.mozRequestAnimationFrame    // Fallback to -moz (Mozilla Firefox)
    || false;                             // Feature not supported :(

    // Same goes for cancelAnimationFrame
    cancelAnimationFrame = window.cancelAnimationFrame || window.webkitCancelAnimationFrame || window.mozCancelAnimationFrame || window.msCancelAnimationFrame || window.oCancelAnimationFrame || false;

    if (!requestAnimationFrame) {
        // Not supported? Build it by yourself!
        requestAnimationFrame = function(callback) {
            return setTimeout(callback, 0);
        }
		
		// No requestAnim. means no cancelAnim. Built that too.
        cancelAnimationFrame = function(id) {
            clearTimeout(id);
        }
    }

    // Now you can use requestAnimationFrame 
    // No matter which browser you're running
    var animationID = requestAnimationFrame(myBeautifulFunction);  
  </textarea>
</div>

<div class="preview" id="preview-new">
  <p class="title">Using textarea-devtab.js</p>
  <textarea spellcheck='false'>
  ## Feature detection + fallback

When using ***feature detection***, a pretty smart way to work when you aren't sure which features you can/cannot use consists in **several feature detections and consequent *fallbacks* to more basic methods** (or even creation of these methods from scratch) in case the features you want to use are not supported.

A simple example of *feature detection with fallback* may be applied to the `window.requestAnimationFrame` feature, which is not supported by all the browsers, and has several different prefixes depending on the browser you're working on. In this case, you can easily detect and *fallback* like this:

    requestAnimationFrame = 
       window.requestAnimationFrame       // Standard name
    || window.webkitRequestAnimationFrame // Fallback to -webkit (Chrome or Safari)
    || window.mozRequestAnimationFrame    // Fallback to -moz (Mozilla Firefox)
    || false;                             // Feature not supported :(

    // Same goes for cancelAnimationFrame
    cancelAnimationFrame = window.cancelAnimationFrame || window.webkitCancelAnimationFrame || window.mozCancelAnimationFrame || window.msCancelAnimationFrame || window.oCancelAnimationFrame || false;

    if (!requestAnimationFrame) {
        // Not supported? Build it by yourself!
        requestAnimationFrame = function(callback) {
            return setTimeout(callback, 0);
        }
		
		// No requestAnim. means no cancelAnim. Built that too.
        cancelAnimationFrame = function(id) {
            clearTimeout(id);
        }
    }

    // Now you can use requestAnimationFrame 
    // No matter which browser you're running
    var animationID = requestAnimationFrame(myBeautifulFunction);  
  </textarea>
</div>
            
          
!
            
              body {
  width: 100%;
  margin: 0;
  padding: 0;
  font-family: monospace;
}

.preview {
  position: absolute;
  display: block;
  width: 50%;
  height: 100%;
  text-align: center;
}

.preview textarea {
  position: relative;
  display: inline-block;
  width: 90%;
  height: 80%;
  resize: none;
}

.preview textarea:focus {
  outline: none;
}

.title {
  padding: 0;
  margin: 10px 0 10px 0;
  font-size: 18px;
  font-weight: bold;
}

#preview-old { right: 50%; }
#preview-new { left: 50%; }
#preview-old textarea { border: solid 2px crimson; }
#preview-new textarea { border: solid 2px green; }
#preview-old textarea:focus {  background: rgba(255, 77, 0, 0.11); }
#preview-new textarea:focus {  background: rgba(0, 255, 71, 0.11) }
            
          
!
            
              /*
 * Title: Textarea developer TAB key (beta)
 * Author: Marco Bonelli (http://stackoverflow.com/users/3889449)
 *
 * Description:
 *   Enables extended TAB functionality on textareas of the current page for indenting/dedenting single/multiple lines.
 *   The default indentation is 4 spaces ("    "); change the var TAB_CHAR if you want to use another TAB delimiter.
 *   Originally developed for Stack Overflow Markdown editor.
 *
 * Implementation:
 *   Use 'element.enableTextareaDev()' to enable the feature for all the textareas inside the element (including the element itself).
 *   Use 'element.disableTextareaDev()' to disable the previously enabled feature.
 *
 * Usage:
 *   Single line: place the cursor where you want and press [TAB] to add a tab character, or [SHIFT]+[TAB] to remove a previously inserted tab character (before the caret position).
 *   Multi line: select multiple lines of code and press [TAB] to add a tab character at the beginning of each line, or [SHIFT]+[TAB] to remove previously inserted tab characters.
 *
 */

(function() {
    function textareaDev(e) {
        var TAB_CHAR = '    ',
            t = e.target,
            ds = 0,
            s, ss, se, v;

        if (t.tagName == 'TEXTAREA' && e.keyCode == 9) {
            e.stopPropagation();
            e.preventDefault();

            ss = t.selectionStart;
            se = t.selectionEnd;
            s = t.value.substring(ss, se);
            v = t.value;

            if (~s.indexOf('\n')) {
                if (e.shiftKey) {
                    s = s.split('\n').map(function(el) {
                        if (el.substr(0,TAB_CHAR.length) == TAB_CHAR) return el.substr(4);
                        return el;
                    }).join('\n');

                    t.value = v.substr(0, ss) + s + v.substr(se);
                } else {
                    s = s.split('\n').map(function(el) { return TAB_CHAR + el }).join('\n');
                    t.value = v.substr(0, ss) + s + v.substr(se);
                }

                t.selectionStart = ss;
                t.selectionEnd = ss+s.length;
            } else {
                if (e.shiftKey) {
                    if (v.substr(ss-TAB_CHAR.length, TAB_CHAR.length) == TAB_CHAR) {
                        t.value = v.substr(0, ss-TAB_CHAR.length) + v.substr(se);
                        ds = -TAB_CHAR.length;
                    }
                } else {
                    t.value = v.substr(0, ss) + TAB_CHAR + v.substr(se);
                    ds = TAB_CHAR.length;
                }

                t.selectionStart = t.selectionEnd = ss + ds;
            }
        }
    }

    Object.defineProperties(Element.prototype, {
       enableTextareaDevTab: {
           value: function() {
               this.addEventListener('keydown', textareaDev, true);
           }
       },
       disableTextareaDevTab: {
           value: function() {
               this.removeEventListener('keydown', textareaDev);
           }
       }
    });

    document.getElementById('preview-new').enableTextareaDevTab();
})();
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.

Console