Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

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

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

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

              
                <!-- Optimized for Chrome, Safari and Firefox. Sorry IE -->
<div id="app"></div>
              
            
!

CSS

              
                $_padding-horizontal: 14px;
$_padding-horizontal__icon: 8px;
$_padding-vertical: 16px;
$_floating-label__padding-horizontal: 4px;
$_margin-vertical: 16px;
$_transition-timing-function: cubic-bezier(0.25, 0.8, 0.25, 1);
$_transition-duration: 0.4s;
$_border-width: 1px;
$_border-width__active: 2px;
$_icon-size: 24px;
$_icon-margin: 12px;

.outline-input {
  --border-radius: 4px;
  --color: rgba(0, 0, 0, 0.87);
  --color__secondary: rgba(0, 0, 0, 0.54);
  --color__disabled: rgba(0, 0, 0, 0.38);
  --color__inactive: rgba(0, 0, 0, 0.24);
  --color__border: var(--color__secondary);
  --color__focused: rgb(98, 0, 234);
  --color__error: rgb(213, 0, 0);
  --font-size: 16;
  --label-font-size__active: 12;
  --label-scale-factor: calc(var(--label-font-size__active) / var(--font-size));
  --label-scale-width: calc(
    (
        100% - #{$_padding-horizontal * 2} - #{$_floating-label__padding-horizontal}
      ) / var(--label-scale-factor)
  );
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica,
    Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";
  margin: $_margin-vertical 0;

  .field-wrapper {
    align-items: center;
    display: flex;
    flex-direction: row;
    position: relative;

    input,
    label {
      font-size: calc(var(--font-size) * 1px);
    }

    input {
      background: transparent;
      border: none;
      color: var(--color);
      padding: $_padding-vertical $_padding-horizontal;
      outline: none;
      width: 100%;

      &:-moz-ui-invalid {
        box-shadow: none !important;
      }
    }

    i {
      color: var(--color);
      margin: 0 $_icon-margin;
      pointer-events: none;
      width: $_icon-size;
    }

    label {
      color: var(--color__secondary);
      pointer-events: none;

      &.floating {
        left: $_padding-horizontal;
        line-height: calc((var(--label-font-size__active) * 1px) * 1.45);
        max-width: calc(100% - #{$_padding-horizontal * 2});
        overflow: hidden;
        position: absolute;
        text-overflow: ellipsis;
        transition-duration: $_transition-duration;
        transform-origin: 0 0;
        transition-property: color, font-size, padding, transform, max-width,
          left;
        transition-timing-function: $_transition-timing-function;
        white-space: nowrap;
      }

      &.reference {
        max-width: var(--label-scale-width);
        padding: 0 $_floating-label__padding-horizontal 0 0;
        transform: scale3d(
          var(--label-scale-factor),
          var(--label-scale-factor),
          1
        );
      }
    }

    &._leading-icon,
    &._trailing-icon {
      label {
        &.floating:not(.reference) {
          max-width: calc(
            100% - #{$_icon-size + ($_icon-margin * 2) + $_padding-horizontal}
          );
        }
      }
    }

    &._leading-icon._trailing-icon {
      label {
        &.floating:not(.reference) {
          max-width: calc(100% - #{(($_icon-size + ($_icon-margin * 2)) * 2)});
        }
      }
    }

    &._leading-icon {
      input {
        padding-left: 0;
      }

      label {
        &.floating:not(.reference) {
          left: $_icon-size + ($_icon-margin * 2);
        }
      }
    }

    &._trailing-icon {
      input {
        padding-right: 0;
      }
    }

    .borders {
      display: flex;
      flex-direction: row;
      height: 100%;
      pointer-events: none;
      position: absolute;
      width: 100%;

      .border {
        border-color: var(--color__border);
        border-style: solid;
        transition-duration: $_transition-duration;
        transition-property: border-color, width;
        transition-timing-function: $_transition-timing-function;
      }

      .middle {
        display: flex;
        flex: 1 1 auto;
        flex-direction: column;

        .top-borders {
          display: flex;
          flex-direction: row;
          justify-content: space-between;

          .border {
            border-width: $_border-width 0 0;
            flex: unset;

            &.start {
              // --label-active-width set within js
              width: calc(var(--label-active-width) / 2);
            }
          }
        }

        .border {
          flex: 1 1 auto;
          width: 100%;

          &.bottom {
            border-width: 0 0 $_border-width 0;
          }
        }
      }

      .right,
      .left {
        width: $_padding-horizontal - $_floating-label__padding-horizontal -
          $_border-width;
      }

      .left {
        border-radius: var(--border-radius) 0 0 var(--border-radius);
        border-width: $_border-width 0 $_border-width $_border-width;
      }

      .right {
        border-radius: 0 var(--border-radius) var(--border-radius) 0;
        border-width: $_border-width $_border-width $_border-width 0;
      }
    }

    &._floating-label {
      &._focused,
      &._has-value {
        .borders {
          .top-borders {
            .start {
              width: 0;
            }

            .end {
              width: calc(
                100% - var(--label-active-width, 0) - #{$_floating-label__padding-horizontal}
              );
            }
          }
        }
        label {
          &.floating {
            &:not(.reference) {
              left: $_padding-horizontal;
              max-width: var(--label-scale-width);
              padding: 0 $_floating-label__padding-horizontal 0 0;
              transform: translate3d(
                  0,
                  calc(-100% - var(--label-font-size__active) * 0.5px),
                  0
                )
                scale3d(var(--label-scale-factor), var(--label-scale-factor), 1);
            }
          }
        }
      }
    }

    &._focused {
      label.floating {
        color: var(--color__focused);
      }
      .borders {
        .top-borders {
          .border {
            border-width: $_border-width__active 0 0;
          }
        }

        .border {
          border-color: var(--color__focused);

          &.bottom {
            border-width: 0 0 $_border-width__active 0;
          }

          &.left {
            border-width: $_border-width__active 0 $_border-width__active
              $_border-width__active;
          }

          &.right {
            border-width: $_border-width__active $_border-width__active
              $_border-width__active 0;
          }
        }
      }
    }

    &._invalid {
      .borders {
        .border {
          border-color: var(--color__error);
        }
      }

      label.floating {
        color: var(--color__error);
      }
    }
  }

  .hints {
    color: var(--color__secondary);
    display: flex;
    flex-direction: row;
    font-size: 12px;
    margin: 4px 12px 0 12px;

    .messages {
      flex: 1 1 auto;
    }

    .error {
      color: var(--color__error);
    }

    .char-counter {
      margin: 0 0 0 12px;
    }
  }
}

// Dark mode

.dark {
  .outline-input {
    --color: rgb(255, 255, 255);
    --color__secondary: rgba(255, 255, 255, 0.7);
    --color__disabled: rgba(255, 255, 255, 0.3);
    --color__inactive: rgba(255, 255, 255, 0.24);
    --color__border: var(--color__disabled);
    --color__focused: rgb(179, 136, 255);
    --color__error: rgb(255, 82, 82);

    i {
      &.material-icons-two-tone {
        filter: invert(1);
      }
    }

    label {
      &.floating {
        color: var(--color__disabled);
      }
    }
  }
}

// Demo

html,
body {
  margin: 0 0;
  padding: 0 0;
}

section {
  display: flex;
  flex-direction: row;

  > div {
    flex: 1 1 50%;
    justify-content: center;
    padding: 0 32px;
  }

  &:first-child > div {
    padding-top: 16px;
  }

  &:last-child > div {
    padding-bottom: 16px;
  }
}

.dark {
  background-color: #212121;
}

.outline-input {
  max-width: 400px;
}

              
            
!

JS

              
                class OutlineInput extends React.Component {
  static defaultProps = {
    type: 'text',
  };
  
  constructor(props) {
    super(props);
    this.focusField = this.focusField.bind(this);
    this.onFocus = this.onFocus.bind(this);
    this.onBlur = this.onBlur.bind(this);
    this.updateValue = this.updateValue.bind(this);
    this.getStyleClasses = this.getStyleClasses.bind(this);
    this.placeholderLabel = this.placeholderLabel.bind(this);
    this.showHintContainer = this.showHintContainer.bind(this);
    this.setLabelWidthStyleProperty = this.setLabelWidthStyleProperty.bind(this);
    this.setError = this.setError.bind(this);
    this.state = {
      focused: false,
      value: this.props ? this.props.value : null,
      errorMessage: this.props ? this.props.errorMessage : null,
    };
    this.container = React.createRef();
    this.floatingLabel = React.createRef();
    this.inputField = React.createRef();
    window.addEventListener('resize', this.setLabelWidthStyleProperty);
  }

  componentDidMount() {
    this.setLabelWidthStyleProperty();
    this.updateValidity();
  }

  componentWillUnmount() {
    window.removeEventListener('resize', this.setLabelWidthStyleProperty);
  }

  focusField() {
    if (!this.inputField.current) {
      return;
    }
    this.inputField.current.focus();
  }

  onFocus() {
    this.setState({
      focused: true,
    });
    this.setLabelWidthStyleProperty();
  }

  onBlur() {
    this.setState({
      focused: false,
    });
  }

  setLabelWidthStyleProperty() {
    if (this.floatingLabel.current) {
      let dims = this.floatingLabel.current.getBoundingClientRect();
      this.container.current.style.setProperty('--label-active-width', `${dims.width}px`);
    } else {
      this.container.current.style.setProperty('--label-active-width', 0);
    }
  }

  placeholderLabel() {
    if (this.props.floatingLabel) {
        return this.state.focused ? this.props.placeholder : null;
    }
    return this.props.placeholder;
  }

  updateValue(e) {
    this.setState({
      value: e.target.value,
    }, () => this.updateValidity());
  }

  updateValidity() {
    if (!this.inputField.current) {
        return;
    }
    this.setState({
      valid: this.inputField.current.checkValidity(),
    });
  }

  getStyleClasses() {
    let classes = [];
    if (this.props.floatingLabel) {
      classes.push('_floating-label');
    }
    if (this.props.leadingIcon) {
      classes.push('_leading-icon');
    }
    if (this.props.trailingIcon) {
      classes.push('_trailing-icon');
    }
    if (!this.state.valid) {
      classes.push('_invalid');
    }
    if (this.state.value) {
      classes.push('_has-value');
    }
    if (this.state.focused) {
      classes.push('_focused');
    }
    return classes.join(' ');
  }

  showHintContainer() {
    return this.state.errorMessage || this.props.helperText || this.props.charCounter;
  }

  setError(errorMessage) {
    if (!this.inputField.current) {
      return;
    }
    this.inputField.current.setCustomValidity(errorMessage);
    this.setState({
      valid: false,
      errorMessage,
    });
  }
  
  render() {
    return (<div className="outline-input" ref={this.container}>
        <div className={`field-wrapper ${this.getStyleClasses()}`} onClick={this.focusField}>
          {this.props.floatingLabel ? <label className="floating">{this.props.floatingLabel}</label> : null}
          {this.props.floatingLabel ? <label ref={this.floatingLabel} className="floating reference" style={{ opacity: 0, pointerEvents: 'none', visibility: 'hidden', zIndex: -9999999999 }}>{this.props.floatingLabel}</label> : null}
          {this.props.leadingIcon ? <MaterialIcon className='leading' type="outlined" name={this.props.leadingIcon} /> : null}
          <input
            ref={this.inputField}
            type={this.props.type}
            minLength={this.props.minLength}
            maxLength={this.props.maxLength}
            min={this.props.min}
            max={this.props.max}
            placeholder={this.placeholderLabel()}
            value={this.state.value}
            onChange={this.updateValue}
            onFocus={this.onFocus}
            onBlur={this.onBlur} />
          {this.props.trailingIcon ? <MaterialIcon className='leading' type="outlined" name={this.props.trailingIcon} /> : null}
          <div className="borders">
            <div className="border left" />
            <div className="middle">
              <div className="top-borders">
                <div className="border start" />
                <div className="border end" />
              </div>
              <div className="border bottom" />
            </div>
            <div className="border right" />
          </div>
        </div>
        {this.showHintContainer() ? <div className="hints">
          {this.state.errorMessage || this.props.helperText ? <div className="messages">
            {!this.state.valid && this.state.errorMessage ? <div className="error">{this.state.errorMessage}</div> : null}
              {this.state.valid && this.props.helperText ? <div className="helper">{this.props.helperText}</div> : null}
          </div> : null}
          {this.props.maxLength ? <div className="char-counter">{this.state.value ? this.state.value.length : 0}/{this.props.maxLength}</div> : null}
        </div> : null}
      </div>);
  }
}

class MaterialIcon extends React.Component {
 
  constructor(props) {
    super(props);
    this.getType = this.getType.bind(this);
  }
  
  getType() {
    let types = {
      twoTone: 'two-tone',
      outlined: 'outlined',
      round: 'round',
      sharp: 'sharp',
    };
    return types[this.props.type] || '';
  }
  
  render() {
    return (<i className={`material-icons${this.getType() ? `-${this.getType()}` : ''} ${this.props.className}`}>{this.props.name}</i>);
  }
}

// Demo
class Application extends React.Component {
  key = 0;
  demoFields = [
    {
      key: this.key++,
      placeholder: 'Basic input',
    },
    {
      key: this.key++,
      floatingLabel: 'Floating label',
      placeholder: 'Secondary placeholder text',
    },
    {
      key: this.key++,
      floatingLabel: 'Example of a super super super super super super super super super super long label',
      placeholder: 'Write your text here',
    },
    {
      key: this.key++,
      floatingLabel: 'Initial value',
      value: 'This is pretty neato',
    },
    {
      key: this.key++,
      floatingLabel: 'This input has an error',
      invalid: true,
    },
    {
      key: this.key++,
      floatingLabel: 'Help me out bro',
      helperText: 'I will show you da wei',
      maxLength: 50,
    },
    {
      key: this.key++,
      floatingLabel: 'Number input',
      type: 'number',
      max: 100,
      helperText: 'Input will become invalid if value is greater than 100',
      errorMessage: 'Value can\'t be greater than 100',
    },
    {
      key: this.key++,
      placeholder: 'Basic input with leading icon',
      leadingIcon: 'local_pizza',
    },
    {
      key: this.key++,
      placeholder: 'Basic input with trailing icon',
      trailingIcon: 'info',
    },
    {
      key: this.key++,
      placeholder: 'Basic input with leading and trailing icons',
      leadingIcon: 'local_pizza',
      trailingIcon: 'info',
    },
    {
      key: this.key++,
      floatingLabel: 'Floating label with leading icon',
      leadingIcon: 'cake',
    },
    {
      key: this.key++,
      floatingLabel: 'Floating label with trailing icon',
      trailingIcon: 'visibility',
    },
    {
      key: this.key++,
      floatingLabel: 'Floating label with leading and trailing icons',
      leadingIcon: 'cake',
      trailingIcon: 'visibility',
    },
  ];
  invalidRefs = [];

  componentDidMount() {
    this.invalidRefs.forEach(ref => {
      ref.setError('You dun fuked up');
    });
  }

  render() {
    let demoFields = this.demoFields.map(field => (
      <OutlineInput {...field} ref={outlineInput => { field.invalid ? this.invalidRefs.push(outlineInput) : null }} />
    ));
    return (
      <React.Fragment>
        {demoFields.map(DemoField => (
          <section>
            <div>{DemoField}</div>
            <div className='dark'>{DemoField}</div>
          </section>
        ))}
      </React.Fragment>
    );
  }
}

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

Console