DeloitteDigitalUK / frontend-playbook

Battle-tested set of conventions, pragmatic approaches and best practices for Front-End Development
6 stars 3 forks source link

UI Patterns: float label pattern #21

Open vitkon opened 7 years ago

vitkon commented 7 years ago

http://bradfrost.com/blog/post/float-label-pattern/

https://css-tricks.com/float-labels-css/

@mattdell might share some implementation details, pros / cons

mattdell commented 7 years ago

I can recommend this implementation: https://github.com/fauxparse/bootstrap-floating-labels/issues/4

document.addEventListener('input', function(e) {
  if (e.target.value && e.target.parentElement.classList.contains('floating-label-form-group') && !e.target.parentElement.classList.contains('floating-label-form-group-with-value')) {
    e.target.parentElement.classList.add('floating-label-form-group-with-value');
  } else if (!e.target.value && e.target.parentElement.classList.contains('floating-label-form-group') && e.target.parentElement.classList.contains('floating-label-form-group-with-value')) {
    e.target.parentElement.classList.remove('floating-label-form-group-with-value');
  }
})

document.addEventListener('focusin', function(e) {
  if (e.target.parentElement.classList.contains('floating-label-form-group')) {
    e.target.parentElement.classList.add('floating-label-form-group-with-focus');
  }
});

document.addEventListener('focusout', function(e) {
  if (e.target.parentElement.classList.contains('floating-label-form-group')) {
    e.target.parentElement.classList.remove('floating-label-form-group-with-focus');
  }
});
vitkon commented 7 years ago

@mattdell can we add a pattern to Responsive.md and some useful links / gists?