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 id="app"></div>
              
            
!

CSS

              
                @import url(https://fonts.googleapis.com/css?family=Open+Sans:300,400,600);
html,
body {
  width: 100%;
  height: 100%;
  margin: 0;
  padding: 0;
  font-family: 'Open Sans', sans-serif;
  display: flex;
  justify-content: center;
  align-items: center;
}

.index {
  width: 400px
}
.index h2 {
  text-align: center;
}

// Component Style
$ocean-color: #2b90d9;
$selected-color: rgba($ocean-color, .3);
$border-color: #e5e5e5;

@keyframes blink
{
  to
  {
    visibility: hidden;
  }
}

.TextBox {
  display: flex;
  background-color: #fff;
  position: relative;
  overflow: hidden;

  .TextBox__input {
    width: 100%;
    border: 1px solid $border-color;
    border-radius: 1px;
    padding: 10px 12px;
    font-size: 14px;
    line-height: 20px;
    color: #555;
    white-space: nowrap;
    position: relative;
    transition: border linear 0.2s, box-shadow linear 0.2s;

    .TextBox__placeholder {
      position: absolute;
      color: #ddd;
    }

    span {
      display: inline-block;

      &.selected {
        background-color: $selected-color;
      }

      &.empty-char {
        width: 3px;
        margin-bottom: -5px;
        height: 20px;
      }
    }

    &.TextBox__input--hover {
      border-color: darken($border-color, 5%);
      box-shadow: 0 1px 0 rgba(0, 0, 0, 0.06);
    }

    &.TextBox__input--focused {
      border-color: $ocean-color;

      .TextBox__input--caret {
        visibility: visible;
      }
    }

    .TextBox__input--caret {
      width: 2px;
      margin-left: -2px;
      margin-bottom: -5px;
      display: inline-block;
      height: 20px;
      background: $ocean-color;
      animation: blink .7s steps(2, start) infinite;
      visibility: hidden;
    }
  }
  input {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    opacity: 0;
    padding: 10px 12px;
    font-size: 14px;
    line-height: 20px;
    display: inline-flex;
    width: 100%;
  }
}
              
            
!

JS

              
                class TextBox extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      focus: false,
      hover: false,
      selectionStart: null,
      selectionEnd: null,
      selectionDirection: 'forward'
    }
  }

  _updateCaret() {
    this.setState({
      selectionStart: this.refs.input.selectionStart,
      selectionEnd: this.refs.input.selectionEnd,
      selectionDirection: this.refs.input.selectionDirection
    })
  }

  _handleChange(e) {
    this._updateCaret()
    if (this.props.onChange) {
      this.props.onChange(e)
    }
  }

  _handleFocusChange(e) {
    this.setState({
      focus: e.type == 'focus'
    })
  }

  _handleKeyUp() {
    this._updateCaret()
  }

  _handleHover(e) {
    this.setState({
      hover: e.type == 'mouseover'
    })
  }

  render() {
    let { focus, hover, selectionStart, selectionEnd, selectionDirection } = this.state;
    let value = this.props.value
    let placeholder = this.props.placeholder

    if (selectionStart == selectionEnd && selectionStart == 0) {
      value = ' ' + value
    } else if (selectionStart == selectionEnd && selectionStart == value.length) {
      value = value + ' '
    } else if (selectionStart != selectionEnd) {
      // noting
    } else {
      value = value.slice(0, selectionStart) + ' ' + value.slice(selectionStart)
    }

    value = value.split('');
    return (
      <div className="TextBox">
        <div ref="fakeInput" className={classNames('TextBox__input', {
            'TextBox__input--focused': focus,
            'TextBox__input--hover': hover
          })}>
          {(this.props.value.length == 0 && placeholder != '') ? <div className="TextBox__placeholder">{placeholder}</div> : null}
          {value.map(function(char, i){
            // Selected mode
            if (selectionStart !== selectionEnd) {
              if (i >= selectionStart && i <= selectionEnd) {
                return (
                  <span key={i} className={classNames('selected', {
                    'empty-char': char === ' ' ? true : false
                  })}>{char}</span>
                );
              }
            }

            if (i == selectionStart) {
              return (
                <span key={i} className="TextBox__input--caret"></span>
              )
            }

            return (
              <span key={i} className={classNames({'empty-char': char === ' '})}>{char}</span>
            );
          })}
        </div>
        <input type="text" ref="input" value={this.props.value} onClick={this._updateCaret.bind(this)} onChange={this._handleChange.bind(this)} onBlur={this._handleFocusChange.bind(this)} onFocus={this._handleFocusChange.bind(this)} onMouseMove={this._updateCaret.bind(this)} onMouseOver={this._handleHover.bind(this)} onMouseOut={this._handleHover.bind(this)} onKeyUp={this._handleKeyUp.bind(this)} />
      </div>
    );
  }
}

TextBox.propTypes = {
  value: React.PropTypes.string,
  placeholder: React.PropTypes.string,
  onChange: React.PropTypes.func
};
TextBox.defaultProps = {
  value: null,
  placeholder: '',
  onChange: null
};

///
class App extends React.Component {
  constructor(props) {
    super();
    this.state = {
      value: 'Hello!'
    };
  }
  
  _handleChange(e) {
    this.setState({
      value: e.target.value
    });
  }

  render() {
    return (
      <div className="index">
        <h2>TextBox</h2>
        <TextBox placeholder="Hello World" value={this.state.value} onChange={this._handleChange.bind(this)} />
      </div>
    );
  }
}

React.render(<App />, document.getElementById('app'));
              
            
!
999px

Console