bug819999

  • Revision slug: User:lmorchard/bug819999
  • Revision title: bug819999
  • Revision id: 337035
  • Created:
  • Creator: lmorchard
  • Is current revision? No
  • Comment

Revision Content

This is the second example that explain how to build custom form widgets.

Change states

HTML Content

<p>RED</p>

<form class="no-widget">
  <select name="myFruit" tabindex="-1">
      <option>Cherry</option>
      <option>Lemon</option>
      <option>Banana</option>
      <option>Strawberry</option>
      <option>Apple</option>
  </select>
 
  <div class="select" tabindex="0">
    <span class="value">Cherry</span>
    <ul class="optList hidden">
      <li class="option">Cherry</li>
      <li class="option">Lemon</li>
      <li class="option">Banana</li>
      <li class="option">Strawberry</li>
      <li class="option">Apple</li>
    </ul>
  </div>
</form>

CSS Content

p { color: red; }

.widget select,
.no-widget .select {
  position : absolute;
  left     : -5000em;
  height   : 0;
  overflow : hidden;
}
 
/* --------------- */
/* Required Styles */
/* --------------- */
 
.select {
  position: relative;
  display : inline-block;
}
 
.select.active,
.select:focus {
  box-shadow: 0 0 3px 1px #227755;
  outline: none;
}
 
.select .optList {
  position: absolute;
  top     : 100%;
  left    : 0;
}
 
.select .optList.hidden {
  max-height: 0;
  visibility: hidden;
}
 
/* ------------ */
/* Fancy Styles */
/* ------------ */
 
.select {
  font-size   : 0.625em; /* 10px */
  font-family : Verdana, Arial, sans-serif;
 
  -moz-box-sizing : border-box;
  box-sizing : border-box;
 
  padding : 0.1em 2.5em 0.2em 0.5em; /* 1px 25px 2px 5px */
  width   : 10em; /* 100px */
 
  border        : 0.2em solid #000; /* 2px */
  border-radius : 0.4em; /* 4px */
 
  box-shadow : 0 0.1em 0.2em rgba(0,0,0,.45); /* 0 1px 2px */
 
  background : #F0F0F0;
  background : -webkit-linear-gradient(90deg, #E3E3E3, #fcfcfc 50%, #f0f0f0);
  background : linear-gradient(0deg, #E3E3E3, #fcfcfc 50%, #f0f0f0);
}
 
.select .value {
  display  : inline-block;
  width    : 100%;
  overflow : hidden;
 
  white-space   : nowrap;
  text-overflow : ellipsis;
  vertical-align: top;
}
 
.select:after {
  content : "v";
  position: absolute;
  z-index : 1;
  height  : 100%;
  width   : 2em; /* 20px */
  top     : 0;
  right   : 0;
 
  padding-top : .1em;
 
  -moz-box-sizing : border-box;
  box-sizing : border-box;
 
  text-align : center;
 
  border-left  : .2em solid #000;
  border-radius: 0 .1em .1em 0;
 
  background-color : #000;
  color : #FFF;
}
 
.select .optList {
  z-index : 2;
 
  list-style: none;
  margin : 0;
  padding: 0;
 
  background: #f0f0f0;
  border: .2em solid #000;
  border-top-width : .1em;
  border-radius: 0 0 .4em .4em;
 
  box-shadow: 0 .2em .4em rgba(0,0,0,.4);
 
  -moz-box-sizing : border-box;
  box-sizing : border-box;
 
  min-width : 100%;
  max-height: 10em; /* 100px */
  overflow-y: auto;
  overflow-x: hidden;
}
 
.select .option {
  padding: .2em .3em;
}
 
.select .highlight {
  background: #000;
  color: #FFFFFF;
}

JavaScript Content

// ------- //
// HELPERS //
// ------- //

NodeList.prototype.forEach = function (callback) {
  Array.prototype.forEach.call(this, callback);
}

// -------------------- //
// Function definitions //
// -------------------- //

function deactivateSelect(select) {
  if (!select.classList.contains('active')) return;

  var optList = select.querySelector('.optList');

  optList.classList.add('hidden');
  select.classList.remove('active');
}

function activeSelect(select, selectList) {
  if (select.classList.contains('active')) return;

  selectList.forEach(deactivateSelect);
  select.classList.add('active');
};

function toggleOptList(select, show) {
  var optList = select.querySelector('.optList');

  optList.classList.toggle('hidden');
}

function highlightOption(select, option) {
  var optionList = select.querySelectorAll('.option');

  optionList.forEach(function (other) {
    other.classList.remove('highlight');
  });

  option.classList.add('highlight');
};

// ------------- //
// Event binding //
// ------------- //

window.addEventListener("load", function () {
  var form = document.querySelector('form');
 
  form.classList.remove("no-widget");
  form.classList.add("widget");
});

window.addEventListener('load', function () {
  var selectList = document.querySelectorAll('.select');

  selectList.forEach(function (select) {
    var optionList = select.querySelectorAll('.option');

    optionList.forEach(function (option) {
      option.addEventListener('mouseover', function () {
        highlightOption(select, option);
      });
    });

    select.addEventListener('click', function (event) {
      toggleOptList(select);
    },  false);

    select.addEventListener('focus', function (event) {
      activeSelect(select, selectList);
    });

    select.addEventListener('blur', function (event) {
      deactivateSelect(select);
    });
  });
});

Result

{{ EmbedLiveSample('Change_states') }}

 

Revision Source

<p>This is the second example that explain <a href="https://developer.mozilla.org/en-US/docs/HTML/Forms/How_to_build_custom_form_widgets" title="/en-US/docs/HTML/Forms/How_to_build_custom_form_widgets">how to build custom form widgets</a>.</p>
<h2 id="Change_states" name="Change_states">Change states</h2>
<h3 id="HTML_Content">HTML Content</h3>
<pre class="brush: html">
&lt;p&gt;RED&lt;/p&gt;

&lt;form class="no-widget"&gt;
&nbsp; &lt;select name="myFruit" tabindex="-1"&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;option&gt;Cherry&lt;/option&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;option&gt;Lemon&lt;/option&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;option&gt;Banana&lt;/option&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;option&gt;Strawberry&lt;/option&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;option&gt;Apple&lt;/option&gt;
&nbsp; &lt;/select&gt;
&nbsp;
&nbsp; &lt;div class="select" tabindex="0"&gt;
&nbsp;&nbsp;&nbsp; &lt;span class="value"&gt;Cherry&lt;/span&gt;
&nbsp;&nbsp;&nbsp; &lt;ul class="optList hidden"&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;li class="option"&gt;Cherry&lt;/li&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;li class="option"&gt;Lemon&lt;/li&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;li class="option"&gt;Banana&lt;/li&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;li class="option"&gt;Strawberry&lt;/li&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;li class="option"&gt;Apple&lt;/li&gt;
&nbsp;&nbsp;&nbsp; &lt;/ul&gt;
&nbsp; &lt;/div&gt;
&lt;/form&gt;</pre>
<h3 id="CSS_Content">CSS Content</h3>
<pre class="brush: css">
p { color: red; }

.widget select,
.no-widget .select {
&nbsp; position : absolute;
&nbsp; left&nbsp;&nbsp;&nbsp;&nbsp; : -5000em;
&nbsp; height&nbsp;&nbsp; : 0;
&nbsp; overflow : hidden;
}
&nbsp;
/* --------------- */
/* Required Styles */
/* --------------- */
&nbsp;
.select {
&nbsp; position: relative;
&nbsp; display : inline-block;
}
&nbsp;
.select.active,
.select:focus {
&nbsp; box-shadow: 0 0 3px 1px #227755;
&nbsp; outline: none;
}
&nbsp;
.select .optList {
&nbsp; position: absolute;
&nbsp; top&nbsp;&nbsp;&nbsp;&nbsp; : 100%;
&nbsp; left&nbsp;&nbsp;&nbsp; : 0;
}
&nbsp;
.select .optList.hidden {
&nbsp; max-height: 0;
&nbsp; visibility: hidden;
}
&nbsp;
/* ------------ */
/* Fancy Styles */
/* ------------ */
&nbsp;
.select {
&nbsp; font-size&nbsp;&nbsp; : 0.625em; /* 10px */
&nbsp; font-family : Verdana, Arial, sans-serif;
&nbsp;
&nbsp; -moz-box-sizing : border-box;
&nbsp; box-sizing : border-box;
&nbsp;
&nbsp; padding : 0.1em 2.5em 0.2em 0.5em; /* 1px 25px 2px 5px */
&nbsp; width&nbsp;&nbsp; : 10em; /* 100px */
&nbsp;
&nbsp; border&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 0.2em solid #000; /* 2px */
&nbsp; border-radius : 0.4em; /* 4px */
&nbsp;
&nbsp; box-shadow : 0 0.1em 0.2em rgba(0,0,0,.45); /* 0 1px 2px */
&nbsp;
&nbsp; background : #F0F0F0;
&nbsp; background : -webkit-linear-gradient(90deg, #E3E3E3, #fcfcfc 50%, #f0f0f0);
&nbsp; background : linear-gradient(0deg, #E3E3E3, #fcfcfc 50%, #f0f0f0);
}
&nbsp;
.select .value {
&nbsp; display&nbsp; : inline-block;
&nbsp; width&nbsp;&nbsp;&nbsp; : 100%;
&nbsp; overflow : hidden;
&nbsp;
&nbsp; white-space&nbsp;&nbsp; : nowrap;
&nbsp; text-overflow : ellipsis;
&nbsp; vertical-align: top;
}
&nbsp;
.select:after {
&nbsp; content : "v";
&nbsp; position: absolute;
&nbsp; z-index : 1;
&nbsp; height&nbsp; : 100%;
&nbsp; width&nbsp;&nbsp; : 2em; /* 20px */
&nbsp; top&nbsp;&nbsp;&nbsp;&nbsp; : 0;
&nbsp; right&nbsp;&nbsp; : 0;
&nbsp;
&nbsp; padding-top : .1em;
&nbsp;
&nbsp; -moz-box-sizing : border-box;
&nbsp; box-sizing : border-box;
&nbsp;
&nbsp; text-align : center;
&nbsp;
&nbsp; border-left&nbsp; : .2em solid #000;
&nbsp; border-radius: 0 .1em .1em 0;
&nbsp;
&nbsp; background-color : #000;
&nbsp; color : #FFF;
}
&nbsp;
.select .optList {
&nbsp; z-index : 2;
&nbsp;
&nbsp; list-style: none;
&nbsp; margin : 0;
&nbsp; padding: 0;
&nbsp;
&nbsp; background: #f0f0f0;
&nbsp; border: .2em solid #000;
&nbsp; border-top-width : .1em;
&nbsp; border-radius: 0 0 .4em .4em;
&nbsp;
&nbsp; box-shadow: 0 .2em .4em rgba(0,0,0,.4);
&nbsp;
&nbsp; -moz-box-sizing : border-box;
&nbsp; box-sizing : border-box;
&nbsp;
&nbsp; min-width : 100%;
&nbsp; max-height: 10em; /* 100px */
&nbsp; overflow-y: auto;
&nbsp; overflow-x: hidden;
}
&nbsp;
.select .option {
&nbsp; padding: .2em .3em;
}
&nbsp;
.select .highlight {
&nbsp; background: #000;
&nbsp; color: #FFFFFF;
}

</pre>
<h3 id="JavaScript_Content">JavaScript Content</h3>
<pre class="brush: js">
// ------- //
// HELPERS //
// ------- //

NodeList.prototype.forEach = function (callback) {
  Array.prototype.forEach.call(this, callback);
}

// -------------------- //
// Function definitions //
// -------------------- //

function deactivateSelect(select) {
  if (!select.classList.contains('active')) return;

  var optList = select.querySelector('.optList');

  optList.classList.add('hidden');
  select.classList.remove('active');
}

function activeSelect(select, selectList) {
  if (select.classList.contains('active')) return;

  selectList.forEach(deactivateSelect);
  select.classList.add('active');
};

function toggleOptList(select, show) {
  var optList = select.querySelector('.optList');

  optList.classList.toggle('hidden');
}

function highlightOption(select, option) {
  var optionList = select.querySelectorAll('.option');

  optionList.forEach(function (other) {
    other.classList.remove('highlight');
  });

  option.classList.add('highlight');
};

// ------------- //
// Event binding //
// ------------- //

window.addEventListener("load", function () {
&nbsp; var form = document.querySelector('form');
&nbsp;
&nbsp; form.classList.remove("no-widget");
&nbsp; form.classList.add("widget");
});

window.addEventListener('load', function () {
  var selectList = document.querySelectorAll('.select');

  selectList.forEach(function (select) {
    var optionList = select.querySelectorAll('.option');

    optionList.forEach(function (option) {
      option.addEventListener('mouseover', function () {
        highlightOption(select, option);
      });
    });

    select.addEventListener('click', function (event) {
      toggleOptList(select);
    },  false);

    select.addEventListener('focus', function (event) {
      activeSelect(select, selectList);
    });

    select.addEventListener('blur', function (event) {
      deactivateSelect(select);
    });
  });
});</pre>
<h3 id="Result">Result</h3>
<p>{{ EmbedLiveSample('Change_states') }}</p>
<p>&nbsp;</p>
Revert to this revision