Pen Settings

HTML

CSS

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

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

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

Behavior

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.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div class="tags-wrapper">
  <span class="add-tag">
    <input type="text" class="share-with" maxSize="38" size="1"></input>
  </span>
</div>
              
            
!

CSS

              
                @import "compass/css3";

* {
  box-sizing: border-box;
  font-family: sans-serif;
  font-size: 0.9rem;
}

.tags-wrapper {
  position: relative;
  border: 1px dashed #ccc;
  border-radius: 2px;
  width: 300px;
  padding: 2px 2px 0;
}

.tag {
  position: relative;
  display: inline-block;
  background: #eee;
  border-radius: 2px;
  padding: 2px;
  border: 1px solid #ccc;
  margin-left: 2px;
  margin-bottom: 2px;
  
  &:hover, &.select {
    cursor: pointer;
    border: 1px solid #999;
  }
  
  &:focus {
    outline: 0;
  }
  
  &::after {
    content: '';
    background: #999;
    width: 10px;
    height: 15px;
    float: right;
    margin-left: 5px;
    border-radius: 2px;
  }
  
  &.author {
    background: #f5f5f5;
    color: #aaa;
    
    &::after {
      width: 0;
      height: 0
    }
  }
}

.add-tag {
  position: relative;
  display: inline-block;
  margin-bottom: 2px;
}

.share-with {
  display: inline-block;
  resize: none;
  border: 0;
  margin: 0;
  vertical-align: middle;
    
  &:active, &:focus {
    outline: 0;
  }
}

.tt-dropdown-menu {
  border: 1px solid #ccc;
  background: white;
}

.tt-suggestion {
  border-bottom: 1px solid #ccc;
  padding: 0 5px;
  
  &.tt-cursor {
    background: #eee;
  }
}
              
            
!

JS

              
                var tagsWrapper = document.querySelectorAll('.tags-wrapper');
var inputBox = document.querySelectorAll('.share-with');
var userArray = [
  {
    name: 'Alex Baldwin',
    email: 'abaldwin@demo.com'
  },
  {
    name: 'Alex Baldwin',
    email: 'abaldwin1@demo.com'
  },
  {
    name: 'Benedict Cumberbatch',
    email: 'bcumberbatch@demo.com'
  }
];

// array of selected user emails
var selectedUsers = [];

function addTag(value, isAuthor) {
  var newTag = $('<span>')
      .addClass('tag')
      .addClass(isAuthor ? 'author' : '')
      .attr('tabindex', 0)
      .html(value)
      .insertBefore($('.add-tag'));
  $(newTag).on('keydown', getKeyboardInput);
}

function selectInputBox(e) {
  $(inputBox).focus();
}

function setFocusOnTag(tag) {
  $(tag).addClass('select');
  $(tag).focus();
}

function onInputBoxFocus() {
  var tags = $('.tag');
  $.each(tags, function removeSelectClass(index, tag) {
    $(tag).removeClass('select');
  });
}

function handleKeyboardLeftArrow(e) {
  var activeTag = $('.tag.select'),
      previousTag = undefined;
  if(activeTag && activeTag.length) {
    previousTag = $(activeTag).prev();
  } else {
    previousTag = $('.tag').last();
    $(inputBox).blur();
  }

  if(previousTag.length) {
    $(activeTag).removeClass('select');
    setFocusOnTag(previousTag);
  }
}

function handleKeyboardRightArrow(e) {
  var activeTag = $('.tag.select');
  if(activeTag.length) {
    var nextElem = $(activeTag).next();
    $(activeTag).removeClass('select');
    if($(nextElem).hasClass('add-tag')) {
      selectInputBox(e);
    } else {
      setFocusOnTag(nextElem);
    }
  }
}

function handleKeyboardBackspace(e, target) {
  if($(target).hasClass('select')) {
    e.preventDefault();
    if($(target).prev().length) {
      setFocusOnTag($(target).prev());
    } else if($('.tag').length === 1) {
      selectInputBox(e);
    }
    $(target).remove();
  }
}

function handleKeyboardReturn(e, suggestion) {
  e.preventDefault();
  if(selectedUsers.indexOf(suggestion.email) === -1) {
    var value = suggestion.name;
    addTag(value);
    selectedUsers.push(suggestion.email);
    $(this).val('');
  }
}

function handleKeyboardTextInput(e, target) {
  var char = String.fromCharCode(e.keyCode).toLowerCase();
  var currentValue = target.value + char;
  setInputBoxWidth(currentValue);
}

function handleCursorChanged(e, suggestion) {
  setInputBoxWidth(suggestion.name);
}

function setInputBoxWidth(currentValue) {
  var maxCols = $('.tt-input').attr('maxSize');
  var currentCols = currentValue.length;
  if(currentCols <= maxCols) {
    $('.tt-input').attr('size', currentCols);
  }
}

$(inputBox).on('typeahead:selected', handleKeyboardReturn);
$(inputBox).on('typeahead:cursorchanged', handleCursorChanged);

function getKeyboardInput(e) {
  e.stopPropagation();
  var target = e.target;
  if(e.keyCode === 13) {
    // return
    //handleKeyboardReturn(e, target);
  } else if(e.keyCode === 8) {
    // backspace
    handleKeyboardBackspace(e, target);
  }
  else if(e.keyCode === 37) {
    // left arrow
    handleKeyboardLeftArrow(e);
  }
  else if(e.keyCode === 39) {
    // right arrow
    handleKeyboardRightArrow(e);
  } else if(e.keyCode >= 48 && e.keyCode <= 90) {
    // alphanumeric characters
    handleKeyboardTextInput(e, target);
  }
}

// taken from: https://gist.github.com/jharding/9458744#file-the-basics-js
var substringMatcher = function(users) {
  return function findMatches(q, cb) {
    var matches = [], substrRegex;
    // regex used to determine if a string contains the substring `q`
    substrRegex = new RegExp(q, 'i');
 
    $.each(users, function(i, user) {
      if (substrRegex.test(user.name)) {
        var suggestion = user.name + '\n(' + user.email + ')';
        matches.push({ suggestion: suggestion, name: user.name, email: user.email });
      }
    });
 
    cb(matches);
  };
};

addTag('John Newman (owner)', true);

tagsWrapper[0].addEventListener('click', selectInputBox);

inputBox[0].addEventListener('keydown', getKeyboardInput);

inputBox[0].addEventListener('focus', onInputBoxFocus);

$(inputBox).typeahead({
  hint: false,
  highlight: true,
  minLength: 1
},
{
  name: 'users',
  displayKey: 'suggestion',
  source: substringMatcher(userArray)
});
              
            
!
999px

Console