css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

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="demo-container">
  <label>Limbo's MultiLineInput Component</label>
  <div id="example-multiline-input">
    <textarea cols="40" rows="3" id="example-multiline-textarea"></textarea>
  </div>
</div>
            
          
!
            
              /* Limbo's main stylesheet is included in the Settings of this Pen */

/* Demo-only styles */
html, body {
  height: 100%;
  
}
body {
  align-items: center;
  display: flex;
  justify-content: center;
}

.demo-container {
  background-color: #fff;
  border-radius: 3px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.4);
  box-sizing: border-box;
  max-width: 50em;
  padding: 2em;
  width: 95%;
}
            
          
!
            
              /**
 * This is a copy/paste of the MultiLineInput.js module in Limbo's codebase
 * You can see a working example on the site when creating a Profile or another isolated
 * example in Limbo Pattern Library:
 * https://www.limbo.io/about/patterns/#multiline-input
 */
const defaultProps = {
  appendAfter: null,
  container: null,
  containerClasses: "",
  listClasses: "",
  textarea: null,
  items: [],
  maxItems: 3
};

const listItem = (itemContent, mode = "view") => `
  <li>
    <div class="item-view" ${mode === "edit"
      ? 'aria-hidden="true" hidden'
      : ""}>
      <span class="item-contents">${encodeHtml(itemContent)}</span>
      <div class="item-controls">
        <button type="button" class="btn-text btn-text-small btn-delete">
          Delete
        </button>
        <button type="button" class="btn-text btn-text-small btn-edit">
          Edit
        </button>
      </div>
    </div>

    <div class="item-edit" ${mode === "view"
      ? 'aria-hidden="true" hidden'
      : ""}>
      <div class="profile-projects-input">
        <textarea aria-label="Notable project" rows="2" name="projects-input"
          placeholder="ex; Built an image processing system transcoding XK images a day"
          maxlength="140">${itemContent}</textarea>
        <div class="multiline-input-counter">&nbsp;</div>
      </div>

      <div class="field-inline-btn-container">
        <button type="button" class="btn btn-small btn-save">
          Save
        </button>
      </div>
    </div>
  </li>`;

/**
 * A MultiLineInput is an input control that allows a line-by-line manipulation
 * of a specific field, separated by newlines. Its primary use presently is
 * in the Notable Projects field on the profile form.
**/
class MultiLineInput {
  constructor(props = {}) {
    this.props = $.extend({}, defaultProps, props);

    const {
      appendAfter,
      containerClasses,
      listClasses,
      maxItems,
      textarea
    } = this.props;

    // Retrieve any items from the textarea in the DOM.
    const items = textarea.val().split(/[\n\r]/).filter(p => p);
    this.props.items = items;

    // Adds the main containing element to the dom after the specified element.
    const container = $(
      `<div class="field-inline-btn ${containerClasses}"><ul class="multiline-output ${listClasses}"></ul></div>`
    );

    appendAfter.prop("hidden", true).after(container);
    this.props.container = container;

    const list = container.find("ul");
    this.props.list = list;

    // Save item when hitting the enter key
    container.on("keypress", "textarea", event => {
      if (event.which === 13) {
        event.stopPropagation();
        event.preventDefault();
        this.saveInput($(event.target));
      }
    });

    container.on("input", "textarea", event => {
      const target = $(event.target);
      const max = parseInt(target.attr("maxlength"), 10);
      const len = target.val().length;

      const counterEl = target.parent().find(".multiline-input-counter");
      counterEl.toggleClass("counter-low", max - len < 11);
      counterEl.text(max - len);
    });

    // Save item on click of 'add' button
    container.on("click", ".btn-save", event => {
      this.saveInput($(event.target));
    });

    container.on("click", ".btn-edit", this.enterEditMode.bind(this));
    container.on("dblclick", ".item-contents", this.enterEditMode.bind(this));

    // Delete item
    container.on("click", ".btn-delete", event => {
      const parentListItem = $(event.target).parents("li");
      const index = container.find("li").index(parentListItem);
      this.props.items.splice(index, 1);
      parentListItem.remove();
      this.inputValueChanged();
    });

    // Do initial rendering of the list items based on any items found in the
    // textarea on page load.
    list.append(items.map((item, i) => listItem(item)));

    // If we're under the maxItems on page load, add a blank item in edit mode.
    if (items.length < maxItems) {
      list.append(listItem("", "edit"));
    }
  }

  // Re-render a list item putting it in "edit mode".
  // This can happen when tapping the Edit button or double clicking the
  // item contents.
  enterEditMode(event) {
    const { container } = this.props;
    const parentListItem = $(event.target).parents("li");
    const text = parentListItem.find("textarea").val();
    const index = container.find("li").index(parentListItem);
    parentListItem.replaceWith(listItem(text, "edit"));
    container.find(`li:eq(${index}) textarea`).focus().trigger("input");
  }

  // Update the text of this item in props.items and re-render the list item
  // putting it in "view mode".
  saveInput(target) {
    const { container } = this.props;

    const parentListItem = target.parents("li");
    const text = parentListItem.find("textarea").val();

    if (text.length) {
      const index = container.find("li").index(parentListItem);
      this.props.items[index] = text;
      parentListItem.replaceWith(listItem(text));
      this.inputValueChanged();
    }
  }

  // An item was added, updated, or deleted.
  inputValueChanged() {
    const { items, list, maxItems, textarea } = this.props;

    const nextVal = items.join("\n");
    const prevVal = textarea.val();

    if (nextVal !== prevVal) {
      const nextLen = items.length;
      const prevLen = prevVal.split(/[\n\r]/).filter(p => p).length;

      // If the next length is greater than the previous we know we just added
      // another item. and we're still under the max number of items allowed,
      // add another blank item in edit mode.
      const addedItemUnderMax = nextLen > prevLen && nextLen < maxItems;

      // If the next length is less than the previous, we know we just deleted
      // an item and the prevLen was the max, we know we have an empty slot,
      // so add another blank item in edit mode.
      const deletedItemDroppedBelowMax =
        nextLen < prevLen && prevLen === maxItems;

      if (addedItemUnderMax || deletedItemDroppedBelowMax) {
        list.append(listItem("", "edit"));
      }

      // Update the value of the hidden textarea that contains the full string
      // of items and trigger the change event so the form saves.
      textarea.val(nextVal).trigger("change");
    }
  }
}

// NOTE: This is imported from an external module in Limbo's codebase.
/**
 * encodeHtml - Replace alligators with their html entity equivilent.
 * @param {String} s - Any string of text. Likely a block of code.
 * @return {String}
 */
const encodeHtml = s => s.replace(/</g, "&lt;").replace(/>/g, "&gt;");

// NOTE: Example instatiation of the MultiLineInput component:
new MultiLineInput({
  appendAfter: $("#example-multiline-input"),
  containerClasses: "profile-projects-field",
  listClasses: "profile-projects-output",
  textarea: $("#example-multiline-textarea"),
  maxItems: 5 // We default and use 3 on Limbo
});
            
          
!
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.
Loading ..................

Console