Alexandre Hansen
Alexandre Hansen

Reputation: 61

Floating label with selectize.js

I´m having some trouble in CSS. What iḿ trying to do is add a floating label like material design (I guess) with a multiple select input using selectize.js:

.selectize-control {
.selectize-input{
    border: none;
    border-radius: 0 !important;
    box-shadow: none !important;
    background: transparent;
    border: none;
    border-radius: 0 !important;
    box-shadow: none !important;

    display: block !important;
    width: 100% !important;
    height: 43px !important;
    font-size: 15px !important;
    background: none !important;
    font-family: "Novecento Normal" !important;
}
border-bottom: 2px solid #e3e6f0 !important;
&:focus-within {
    border-bottom: 2px solid #7a2932 !important;
    & + span {
        transform: translateY(-25px) scale(1) !important;
        color: #7a2932 !important;
        & + .border {
            transform: scaleX(1) !important;
        }
    }
  }
}

Like as shown at this JSFiddle.

The problem is: when the focus is lost, the label transform itself back, even if the input has values.

I'm pretty sure that issue is in

&:focus-within {...

But I can't resolve this.

Does anyone know how to do this?

Upvotes: 1

Views: 970

Answers (2)

Alexandre Hansen
Alexandre Hansen

Reputation: 61

Thank you Vipin, but I made it work like this codepen

(function($el) {
var $label = $el.prev();
$el.selectize({
  plugins: ['remove_button'],
  onFocus: function() {
    $label.addClass('fp-floating-label--focused');
  },
  onBlur: function() {
    $label.removeClass('fp-floating-label--focused');
  },
  onItemAdd: function(){
    $label.addClass('fp-floating-label--valued');
  },
  onDelete: function(){
    var count = $el.find('option').length
    if (count <= 1){
      $label.removeClass('fp-floating-label--valued');
      $(this).focus();
      $label.removeClass('fp-floating-label--focused');
    }
  },
  onChange: function(value) {
    value = value.trim();
    if (value) {
      $label.addClass('fp-floating-label--valued');
    } else {
      $label.removeClass('fp-floating-label--valued');
    }
  }
});

Upvotes: 0

Vipin Sarathe
Vipin Sarathe

Reputation: 31

Don't use ":focus-within" because this css not supported Internet Explorer & edge browsers. please review below link: https://developer.mozilla.org/en-US/docs/Web/CSS/:focus-within

$(function() {
    $('#select-name').selectize({
        plugins: ['remove_button']
    });
    // Add this js
    $('.selectize-control').on("blur", ".selectize-input", function () {
      if($(this).hasClass("has-items")){
        $('.selectize-control').next("span").addClass("active");
      }else{
        $('.selectize-control').next("span").removeClass("active");
      }
    });
});

use css like this

.selectize-control + span.active{ transform: translateY(-25px) scale(1) !important; color: #7a2932 !important; }

Upvotes: 3

Related Questions