Skip to content

Instantly share code, notes, and snippets.

@taufik-nurrohman
Last active June 7, 2021 03:30
Show Gist options
  • Select an option

  • Save taufik-nurrohman/6cd2422017f7baf1c745addcabaf3981 to your computer and use it in GitHub Desktop.

Select an option

Save taufik-nurrohman/6cd2422017f7baf1c745addcabaf3981 to your computer and use it in GitHub Desktop.

Revisions

  1. taufik-nurrohman revised this gist Jun 7, 2021. 1 changed file with 6 additions and 6 deletions.
    12 changes: 6 additions & 6 deletions accessible-custom-select-box.html
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,7 @@

    <title>Accessible Custom Select Box by Taufik Nurrohman</title>

    <!--
    <!--onsele
    The MIT License (MIT)
    @@ -628,11 +628,11 @@
    selectBoxValue = selectBox.value,
    selectBoxFake = getSelectBoxFake(selectBox),
    selectBoxFakeOptions = getSelectBoxFakeOptions(selectBoxFake);
    // if (!selectBoxValue && selectBox.options[0]) {
    // selectBoxValue = selectBox.options[0].value;
    // }
    let selectBoxFakeOption = getSelectBoxFakeOptionByValue(selectBoxFakeOptions, selectBoxValue);
    selectBoxFakeOption && trigger(selectBoxFakeOption, 'click');
    // if (!selectBoxValue && selectBox.options[0]) {
    // selectBoxValue = selectBox.options[0].value;
    // }
    let selectBoxFakeOption = getSelectBoxFakeOptionByValue(selectBoxFakeOptions, selectBoxValue);
    selectBoxFakeOption && trigger(selectBoxFakeOption, 'click');
    }

    function onSelectBoxFormReset() {
  2. taufik-nurrohman revised this gist Jun 7, 2021. 1 changed file with 109 additions and 79 deletions.
    188 changes: 109 additions & 79 deletions accessible-custom-select-box.html
    Original file line number Diff line number Diff line change
    @@ -50,9 +50,12 @@
    width: 12em;
    padding: .5em .75em;
    cursor: pointer;
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    -moz-user-select: none;
    user-select: none;
    -webkit-tap-highlight-color: rgba(0, 0, 0, 0);
    -webkit-tap-highlight-color: transparent;
    }

    .select,
    @@ -96,15 +99,16 @@
    border-bottom: 6px solid;
    }

    .select.js > span {
    .select.js > b {
    display: block;
    font: inherit;
    margin-right: 1em;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    }

    .select.js > span + span {
    .select.js > div {
    position: fixed;
    z-index: 9999;
    background: inherit;
    @@ -114,7 +118,7 @@
    display: none;
    }

    .select.js > span + span.up {
    .select.js > div.up {
    }

    .select[size] + .select.js {
    @@ -125,11 +129,11 @@
    display: none;
    }

    .select[size] + .select.js > span {
    .select[size] + .select.js > b {
    margin: 0;
    }

    .select[size] + .select.js > span + span {
    .select[size] + .select.js > div {
    display: block;
    position: relative;
    z-index: 1;
    @@ -141,7 +145,7 @@
    padding: 0;
    }

    .select.js > span + span a {
    .select.js > div a {
    display: block;
    padding: .25em .5em;
    font: inherit;
    @@ -152,18 +156,18 @@
    text-overflow: ellipsis;
    }

    .select.js > span + span a:hover,
    .select.js > span + span a.focused {
    .select.js > div a:hover,
    .select.js > div a.focused {
    background: blue;
    color: white;
    }

    .select.js > span + span span {
    .select.js > div span {
    display: block;
    padding: .25em .5em;
    }

    .select.js > span + span span[title]::before {
    .select.js > div span[title]::before {
    content: attr(title);
    display: block;
    margin: 0 0 .25em;
    @@ -173,33 +177,33 @@
    text-overflow: ellipsis;
    }

    .select.js > span + span span a {
    .select.js > div span a {
    padding-left: 1.5em;
    margin: 0 -.5em;
    }

    .select.js > span + span a.disabled {
    .select.js > div a.disabled {
    color: gray;
    cursor: not-allowed;
    }

    .select.js > span + span a.disabled:hover,
    .select.js > span + span a.disabled.focused {
    .select.js > div a.disabled:hover,
    .select.js > div a.disabled.focused {
    background: gray;
    color: white;
    }

    .select.js > span + span a.selected {
    .select.js > div a.selected {
    color: blue;
    }

    .select.js > span + span a.selected:hover,
    .select.js > span + span a.selected.focused {
    .select.js > div a.selected:hover,
    .select.js > div a.selected.focused {
    background: blue;
    color: white;
    }

    .select.js.open > span + span {
    .select.js.open > div {
    display: block;
    }

    @@ -303,6 +307,14 @@
    return W.getComputedStyle(node).getPropertyValue(property);
    }

    function getSelectBox(selectBoxFake) {
    return selectBoxFake.previousElementSibling;
    }

    function getSelectBoxFake(selectBox) {
    return selectBox.nextElementSibling;
    }

    function getSelectBoxFakeDropDown(selectBoxFake) {
    return selectBoxFake.children[1];
    }
    @@ -333,7 +345,7 @@
    selectBox.removeEventListener('input', onSelectBoxInput);
    if (selectBox.classList.contains('select-source')) {
    selectBox.classList.remove('select-source');
    let selectBoxFake = selectBox.nextElementSibling;
    let selectBoxFake = getSelectBoxFake(selectBox);
    selectBoxFake.removeEventListener('blur', onSelectBoxFakeBlur);
    selectBoxFake.removeEventListener('click', onSelectBoxFakeClick);
    selectBoxFake.removeEventListener('focus', onSelectBoxFakeFocus);
    @@ -344,11 +356,11 @@
    }

    function setSelectBoxFake(selectBox) {
    let selectBoxFake = D.createElement('span'),
    selectBoxFakeLabel = D.createElement('span'),
    let selectBoxFake = D.createElement('div'),
    selectBoxFakeLabel = D.createElement('b'),
    selectBoxValue = selectBox.value,
    selectBoxTitle = selectBox.title,
    selectBoxIndex = 0,
    selectBoxOptionIndex = 0,
    selectBoxItems = selectBox.children;
    selectBoxFake.className = selectBox.className;
    selectBoxFake.classList.add('js');
    @@ -363,21 +375,28 @@
    selectBox.parentNode.insertBefore(selectBoxFake, selectBox.nextElementSibling);
    selectBox.classList.add('select-source');
    function setSelectBoxFakeOptions(selectBoxItem, parent) {
    function onSelectBoxFakeOptionBlur() {
    this.classList.remove('focus');
    }
    function onSelectBoxFakeOptionClick(e) {
    let t = this,
    let selectBoxFakeOption = this,
    selectBoxValuePrevious = selectBoxValue;
    selectBoxValue = t._value;
    selectBoxFakeLabel.textContent = t.textContent;
    selectBoxValue = selectBoxFakeOption._value;
    selectBoxFakeLabel.textContent = selectBoxFakeOption.textContent;
    getSelectBoxFakeOptions(selectBoxFake).forEach(selectBoxFakeOption => {
    selectBoxFakeOption.classList[selectBoxValue === selectBoxFakeOption._value ? 'add' : 'remove']('selected');
    selectBoxFakeOption.classList.toggle('selected', selectBoxValue === selectBoxFakeOption._value);
    });
    e.isTrusted && selectBoxFake.focus();
    if (selectBoxValue !== selectBoxValuePrevious) {
    selectBox.value = selectBoxValue;
    trigger(selectBox, 'input');
    trigger(selectBox, 'input'); // `input` must come first
    trigger(selectBox, 'change');
    }
    e.preventDefault();
    }
    function onSelectBoxFakeFocus() {
    this.classList.add('focus');
    }
    if ('optgroup' === selectBoxItem.tagName.toLowerCase()) {
    let selectBoxFakeOptionGroup = D.createElement('span'),
    selectBoxItems = selectBoxItem.children;
    @@ -395,8 +414,10 @@
    selectBoxFakeOption.tabIndex = -1;
    selectBoxFakeOption.textContent = selectBoxOptionText;
    selectBoxFakeOption.title = selectBoxOptionText;
    selectBoxFakeOption._index = selectBoxIndex;
    selectBoxFakeOption._index = selectBoxOptionIndex;
    selectBoxFakeOption._value = selectBoxOptionValue;
    selectBoxFakeOption.dataset.index = selectBoxOptionIndex;
    selectBoxFakeOption.dataset.value = selectBoxOptionValue;
    if (selectBoxItem.hasAttribute('disabled')) {
    selectBoxFakeOption.classList.add('disabled');
    } else {
    @@ -408,7 +429,7 @@
    selectBoxFakeLabel.textContent = selectBoxOptionText;
    selectBoxFakeOption.classList.add('selected');
    }
    ++selectBoxIndex;
    ++selectBoxOptionIndex;
    }
    if (selectBox.disabled) {
    selectBoxFake.classList.add('disabled');
    @@ -421,16 +442,17 @@
    selectBoxFake.addEventListener('keydown', onSelectBoxFakeKeyDown, false);
    }
    if (selectBoxItems.length) {
    let selectBoxFakeDropDown = D.createElement('span');
    let selectBoxFakeDropDown = D.createElement('div');
    selectBoxFakeDropDown.tabIndex = -1;
    selectBoxFake.append(selectBoxFakeDropDown);
    for (let i = 0, j = selectBoxItems.length; i < j; ++i) {
    setSelectBoxFakeOptions(selectBoxItems[i], selectBoxFakeDropDown);
    }
    if (selectBox.size) {
    let selectBoxFakeOptions = getSelectBoxFakeOptions(selectBoxFake),
    selectBoxFakeOption = getSelectBoxFakeOptionByIndex(selectBoxFakeOptions, 0);
    selectBoxFakeDropDown.style.maxHeight = (selectBoxFakeOption.offsetHeight * selectBox.size) + 'px';
    selectBoxFakeOption = getSelectBoxFakeOptionByIndex(selectBoxFakeOptions, 0),
    heightMax = selectBoxFakeOption.offsetHeight * selectBox.size;
    selectBoxFakeDropDown.style.maxHeight = heightMax + 'px';
    }
    }
    selectBox.addEventListener('focus', onSelectBoxFocus, false);
    @@ -439,10 +461,14 @@
    if (selectBox.multiple) {
    // TODO
    }
    if (selectBox.size) {
    // Force open class
    selectBoxFake.classList.add('open');
    }
    }

    function setSelectBoxFakeOptionsPosition(selectBoxFake) {
    let selectBox = selectBoxFake.previousElementSibling,
    let selectBox = getSelectBox(selectBoxFake),
    selectBoxFakeDropDown = getSelectBoxFakeDropDown(selectBoxFake),
    selectBoxFakeBorderTopWidth = W.parseInt(getStyle(selectBoxFake, 'border-top-width'), 10),
    selectBoxFakeBorderBottomWidth = W.parseInt(getStyle(selectBoxFake, 'border-bottom-width'), 10);
    @@ -474,7 +500,7 @@
    top = selectBoxFakeOption.offsetTop,
    topScroll = selectBoxFakeDropDown.scrollTop;
    if (top < topScroll) {
    selectBoxFakeDropDown.scrollTop = selectBoxFakeOption.offsetTop;
    selectBoxFakeDropDown.scrollTop = top;
    } else if (top + height - heightParent > topScroll) {
    selectBoxFakeDropDown.scrollTop = top + height - heightParent;
    }
    @@ -483,123 +509,127 @@

    function onSelectBoxFakeClickOutside(e) {
    selectBoxesFake && selectBoxesFake.forEach(selectBoxFake => {
    selectBoxFake !== selectBoxFakeClicked && selectBoxFake.classList.remove('open');
    let selectBox = getSelectBox(selectBoxFake);
    if (!selectBox.size && selectBoxFake !== selectBoxFakeCurrent) {
    selectBoxFake.classList.remove('open');
    }
    });
    }

    function onSelectBoxFakeBlur(e) {
    if (!selectBoxFakeClicked) {
    if (!selectBoxFakeCurrent) {
    return;
    }
    let t = this,
    selectBox = t.previousElementSibling;
    if (!t.classList.contains('open') && selectBox.value !== selectBoxValue) {
    trigger(selectBox, 'change');
    }
    selectBoxFakeClicked = null;
    this.classList.remove('focus');
    selectBoxFakeCurrent = null;
    }

    function onSelectBoxFakeClick(e) {
    let t = this;
    t.classList.toggle('open');
    if (t.classList.contains('open')) {
    setSelectBoxFakeOptionsPosition(t);
    let selectBoxFake = this,
    selectBox = getSelectBox(selectBoxFake);
    selectBoxFakeCurrent = selectBoxFake;
    if (selectBox.size) {
    selectBoxFake.classList.add('open');
    return;
    }
    selectBoxFake.classList.toggle('open');
    if (selectBoxFake.classList.contains('open')) {
    setSelectBoxFakeOptionsPosition(selectBoxFake);
    }
    selectBoxFakeClicked = t;
    }

    function onSelectBoxFakeFocus() {
    let t = this,
    selectBox = t.previousElementSibling;
    let selectBoxFake = this,
    selectBox = getSelectBox(selectBoxFake);
    selectBoxFake.classList.add('focus');
    selectBoxValue = selectBox.value;
    selectBoxFakeClicked = t;
    selectBoxFakeCurrent = selectBoxFake;
    }

    function onSelectBoxFakeKeyDown(e) {
    let t = this,
    key = e.key,
    let key = e.key,
    keyCode = e.keyCode,
    selectBox = t.previousElementSibling,
    selectBoxIndex = selectBox.selectedIndex,
    selectBoxFakeOptions = getSelectBoxFakeOptions(t),
    selectBoxFakeOption = getSelectBoxFakeOptionByIndex(selectBoxFakeOptions, selectBoxIndex),
    open = t.classList.contains('open');
    // console.log([key, keyCode]);
    selectBoxFake = this,
    selectBox = getSelectBox(selectBoxFake),
    selectBoxOptionIndex = selectBox.selectedIndex,
    selectBoxFakeOptions = getSelectBoxFakeOptions(selectBoxFake),
    selectBoxFakeOption = getSelectBoxFakeOptionByIndex(selectBoxFakeOptions, selectBoxOptionIndex),
    open = selectBoxFake.classList.contains('open');
    if ('ArrowDown' === key || 40 === keyCode) {
    while (selectBoxFakeOption = getSelectBoxFakeOptionByIndex(selectBoxFakeOptions, ++selectBoxIndex)) {
    while (selectBoxFakeOption = getSelectBoxFakeOptionByIndex(selectBoxFakeOptions, ++selectBoxOptionIndex)) {
    if (!selectBoxFakeOption.classList.contains('disabled')) {
    break;
    }
    }
    if (selectBoxFakeOption) {
    trigger(selectBoxFakeOption, 'click');
    t.classList[open ? 'add' : 'remove']('open');
    selectBoxFake.classList.toggle('open', open);
    }
    e.preventDefault();
    } else if ('ArrowUp' === key || 38 === keyCode) {
    while (selectBoxFakeOption = getSelectBoxFakeOptionByIndex(selectBoxFakeOptions, --selectBoxIndex)) {
    while (selectBoxFakeOption = getSelectBoxFakeOptionByIndex(selectBoxFakeOptions, --selectBoxOptionIndex)) {
    if (!selectBoxFakeOption.classList.contains('disabled')) {
    break;
    }
    }
    if (selectBoxFakeOption) {
    trigger(selectBoxFakeOption, 'click');
    t.classList[open ? 'add' : 'remove']('open');
    selectBoxFake.classList.toggle('open', open);
    }
    e.preventDefault();
    } else if ('End' === key || 35 === keyCode) {
    selectBoxIndex = selectBox.options.length;
    while (selectBoxFakeOption = getSelectBoxFakeOptionByIndex(selectBoxFakeOptions, --selectBoxIndex)) {
    selectBoxOptionIndex = selectBox.options.length;
    while (selectBoxFakeOption = getSelectBoxFakeOptionByIndex(selectBoxFakeOptions, --selectBoxOptionIndex)) {
    if (!selectBoxFakeOption.classList.contains('disabled')) {
    break;
    }
    }
    if (selectBoxFakeOption) {
    trigger(selectBoxFakeOption, 'click');
    t.classList[open ? 'add' : 'remove']('open');
    selectBoxFake.classList.toggle('open', open);
    }
    e.preventDefault();
    } else if ('Enter' === key || 13 === keyCode) {
    t.classList.toggle('open');
    selectBoxFake.classList.toggle('open');
    e.preventDefault();
    } else if ('Escape' === key || 27 === keyCode) {
    t.classList.remove('open');
    !selectBox.size && selectBoxFake.classList.remove('open');
    // e.preventDefault();
    } else if ('Home' === key || 36 === keyCode) {
    selectBoxIndex = -1;
    while (selectBoxFakeOption = getSelectBoxFakeOptionByIndex(selectBoxFakeOptions, ++selectBoxIndex)) {
    selectBoxOptionIndex = -1;
    while (selectBoxFakeOption = getSelectBoxFakeOptionByIndex(selectBoxFakeOptions, ++selectBoxOptionIndex)) {
    if (!selectBoxFakeOption.classList.contains('disabled')) {
    break;
    }
    }
    if (selectBoxFakeOption) {
    trigger(selectBoxFakeOption, 'click');
    t.classList[open ? 'add' : 'remove']('open');
    selectBoxFake.classList.toggle('open', open);
    }
    e.preventDefault();
    } else if ('Tab' === key || 9 === keyCode) {
    selectBoxFakeOption && trigger(selectBoxFakeOption, 'click');
    t.classList.remove('open');
    !selectBox.size && selectBoxFake.classList.remove('open');
    // e.preventDefault();
    }
    t.classList.contains('open') && setSelectBoxFakeOptionsPosition(t);
    selectBoxFake.classList.contains('open') && setSelectBoxFakeOptionsPosition(selectBoxFake);
    }

    function onSelectBoxChange(e) {
    onSelectBoxInput.call(this, e);
    }

    function onSelectBoxFocus(e) {
    this.nextElementSibling.focus();
    getSelectBoxFake(this).focus();
    }

    function onSelectBoxInput() {
    let t = this,
    selectBoxValue = t.value,
    selectBoxFake = t.nextElementSibling,
    let selectBox = this,
    selectBoxValue = selectBox.value,
    selectBoxFake = getSelectBoxFake(selectBox),
    selectBoxFakeOptions = getSelectBoxFakeOptions(selectBoxFake);
    // if (!selectBoxValue && t.options[0]) {
    // selectBoxValue = t.options[0].value;
    // if (!selectBoxValue && selectBox.options[0]) {
    // selectBoxValue = selectBox.options[0].value;
    // }
    let selectBoxFakeOption = getSelectBoxFakeOptionByValue(selectBoxFakeOptions, selectBoxValue);
    selectBoxFakeOption && trigger(selectBoxFakeOption, 'click');
    @@ -632,7 +662,7 @@
    R = D.documentElement,
    W = window,
    selectBoxValue,
    selectBoxFakeClicked,
    selectBoxFakeCurrent,
    selectBoxes,
    selectBoxesFake;

  3. taufik-nurrohman revised this gist Jun 6, 2021. 1 changed file with 13 additions and 0 deletions.
    13 changes: 13 additions & 0 deletions accessible-custom-select-box.html
    Original file line number Diff line number Diff line change
    @@ -30,6 +30,14 @@
    -->

    <style>
    :root {
    font: normal normal 16px/1.2 serif;
    background: white;
    color: black;
    }
    </style>

    <style>
    .select {
    position: relative;
    @@ -64,6 +72,10 @@
    box-shadow: 0 0 0 3px rgba(0, 0, 255, .25);
    }

    .select *:focus {
    outline: 0;
    }

    .select.js::after {
    content: "";
    width: 0;
    @@ -410,6 +422,7 @@
    }
    if (selectBoxItems.length) {
    let selectBoxFakeDropDown = D.createElement('span');
    selectBoxFakeDropDown.tabIndex = -1;
    selectBoxFake.append(selectBoxFakeDropDown);
    for (let i = 0, j = selectBoxItems.length; i < j; ++i) {
    setSelectBoxFakeOptions(selectBoxItems[i], selectBoxFakeDropDown);
  4. taufik-nurrohman revised this gist Jun 6, 2021. 1 changed file with 279 additions and 130 deletions.
    409 changes: 279 additions & 130 deletions accessible-custom-select-box.html
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,35 @@
    <!DOCTYPE html>

    <meta charset="utf-8">

    <title>Accessible Custom Select Box by Taufik Nurrohman</title>

    <!--
    The MIT License (MIT)
    Copyright © 2021 Taufik Nurrohman <https://github.com/taufik-nurrohman>
    Permission is hereby granted, free of charge, to any person obtaining a copy
    of this software and associated documentation files (the “Software”), to deal
    in the Software without restriction, including without limitation the rights
    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    copies of the Software, and to permit persons to whom the Software is
    furnished to do so, subject to the following conditions:
    The above copyright notice and this permission notice shall be included in all
    copies or substantial portions of the Software.
    THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    SOFTWARE.
    -->

    <style>
    .select {
    position: relative;
    @@ -29,6 +59,7 @@
    }

    .select:focus {
    outline: 0;
    border-color: blue;
    box-shadow: 0 0 0 3px rgba(0, 0, 255, .25);
    }
    @@ -66,12 +97,38 @@
    z-index: 9999;
    background: inherit;
    border: inherit;
    margin: -1px 0 0;
    box-shadow: 0 1px 2px rgba(0, 0, 0, .4);
    overflow: auto;
    display: none;
    }

    .select.js > span + span.up {
    }

    .select[size] + .select.js {
    height: auto;
    }

    .select[size] + .select.js::after {
    display: none;
    }

    .select[size] + .select.js > span {
    margin: 0;
    }

    .select[size] + .select.js > span + span {
    display: block;
    position: relative;
    z-index: 1;
    background: none;
    border-width: 0;
    border-top-width: inherit;
    box-shadow: none;
    margin: .5em -.75em -.5em;
    padding: 0;
    }

    .select.js > span + span a {
    display: block;
    padding: .25em .5em;
    @@ -151,13 +208,11 @@

    <p>
    <label>
    <input checked onchange="this.checked ? setSelectBoxesFake() : letSelectBoxesFake();" type="checkbox">
    <span>Enable/Disable Custom Select Box</span>
    <input onchange="this.checked ? setSelectBoxesFake() : letSelectBoxesFake();" type="checkbox">
    <span>Enable Custom Select Box</span>
    </label>
    </p>

    <hr>

    <form method="get">
    <p>
    <input name="input-1" type="text">
    @@ -197,11 +252,24 @@
    <small>Description goes here.</small>
    </p>
    <p>
    <select class="select" disabled name="select-3">
    <option>Test 1</option>
    <option selected>Test 2</option>
    <option>Test 3</option>
    </select>
    <select class="select" disabled name="select-3">
    <option>Test 1</option>
    <option selected>Test 2</option>
    <option>Test 3</option>
    </select>
    </p>
    <p>
    <select class="select" name="select-4" size="4">
    <option>Item 1</option>
    <option>Item 2</option>
    <option>Item 3</option>
    <option>Item 4</option>
    <option>Item 5</option>
    <option>Item 6</option>
    <option>Item 7</option>
    <option>Item 8</option>
    <option>Item 9</option>
    </select>
    </p>
    <p>
    <input name="input-2" type="text">
    @@ -214,16 +282,46 @@

    <script>
    function trigger(node, eventName) {
    node.dispatchEvent(new Event(eventName));
    node.dispatchEvent(new Event(eventName, {
    cancellable: true
    }));
    }

    function getStyle(node, property) {
    return W.getComputedStyle(node).getPropertyValue(property);
    }

    function getSelectBoxFakeDropDown(selectBoxFake) {
    return selectBoxFake.children[1];
    }

    function getSelectBoxFakeLabel(selectBoxFake) {
    return selectBoxFake.children[0];
    }

    function getSelectBoxFakeOptions(selectBoxFake) {
    return [...getSelectBoxFakeDropDown(selectBoxFake).querySelectorAll('a')];
    }

    function getSelectBoxFakeOptionByIndex(selectBoxFakeOptions, index) {
    return selectBoxFakeOptions.length && selectBoxFakeOptions.find(selectBoxFakeOption => {
    return index === selectBoxFakeOption._index;
    });
    }

    function getSelectBoxFakeOptionByValue(selectBoxFakeOptions, value) {
    return selectBoxFakeOptions.length && selectBoxFakeOptions.find(selectBoxFakeOption => {
    return value === selectBoxFakeOption._value;
    });
    }

    function letSelectBoxFake(selectBox) {
    selectBox.removeEventListener('focus', onSelectBoxFocus);
    selectBox.removeEventListener('change', onSelectBoxChange);
    selectBox.removeEventListener('input', onSelectBoxInput);
    selectBox.classList.remove('select-source');
    let selectBoxFake = selectBox.nextElementSibling;
    if (selectBoxFake.classList.contains('select') && selectBoxFake.classList.contains('js')) {
    if (selectBox.classList.contains('select-source')) {
    selectBox.classList.remove('select-source');
    let selectBoxFake = selectBox.nextElementSibling;
    selectBoxFake.removeEventListener('blur', onSelectBoxFakeBlur);
    selectBoxFake.removeEventListener('click', onSelectBoxFakeClick);
    selectBoxFake.removeEventListener('focus', onSelectBoxFakeFocus);
    @@ -234,104 +332,139 @@
    }

    function setSelectBoxFake(selectBox) {
    let container = D.createElement('span'),
    value = D.createElement('span'),
    valueCurrent = selectBox.value,
    t = selectBox.title,
    index = 0,
    items = selectBox.children;
    container.className = selectBox.className;
    container.classList.add('js');
    container.append(value);
    if (selectBox.multiple) {
    // TODO
    }
    if (selectBox.size > 1) {
    // TODO
    let selectBoxFake = D.createElement('span'),
    selectBoxFakeLabel = D.createElement('span'),
    selectBoxValue = selectBox.value,
    selectBoxTitle = selectBox.title,
    selectBoxIndex = 0,
    selectBoxItems = selectBox.children;
    selectBoxFake.className = selectBox.className;
    selectBoxFake.classList.add('js');
    if (selectBoxTitle) {
    selectBoxFake.title = selectBoxTitle;
    }
    function setSelectBoxFakeOptions(item, options) {
    // if (!selectBoxValue && selectBox.options[0]) {
    // selectBoxValue = selectBox.options[0].value; // Set default value to the first option value
    // }
    selectBoxFakeLabel.textContent = '\u200c';
    selectBoxFake.append(selectBoxFakeLabel);
    selectBox.parentNode.insertBefore(selectBoxFake, selectBox.nextElementSibling);
    selectBox.classList.add('select-source');
    function setSelectBoxFakeOptions(selectBoxItem, parent) {
    function onSelectBoxFakeOptionClick(e) {
    let t = this,
    valuePrev = valueCurrent;
    value.textContent = t.textContent;
    valueCurrent = t.dataset.value;
    container.querySelectorAll('a[data-index]').forEach(option => {
    option.classList[valueCurrent === option.dataset.value ? 'add' : 'remove']('selected');
    selectBoxValuePrevious = selectBoxValue;
    selectBoxValue = t._value;
    selectBoxFakeLabel.textContent = t.textContent;
    getSelectBoxFakeOptions(selectBoxFake).forEach(selectBoxFakeOption => {
    selectBoxFakeOption.classList[selectBoxValue === selectBoxFakeOption._value ? 'add' : 'remove']('selected');
    });
    container.focus();
    if (valueCurrent !== valuePrev) {
    selectBox.value = valueCurrent;
    e.isTrusted && selectBoxFake.focus();
    if (selectBoxValue !== selectBoxValuePrevious) {
    selectBox.value = selectBoxValue;
    trigger(selectBox, 'input');
    }
    e.preventDefault();
    }
    if ('optgroup' === item.tagName.toLowerCase()) {
    let optgroup = D.createElement('span'),
    items = item.children;
    optgroup.title = item.label;
    options.append(optgroup);
    for (let i = 0, j = items.length; i < j; ++i) {
    setSelectBoxFakeOptions(items[i], optgroup);
    if ('optgroup' === selectBoxItem.tagName.toLowerCase()) {
    let selectBoxFakeOptionGroup = D.createElement('span'),
    selectBoxItems = selectBoxItem.children;
    selectBoxFakeOptionGroup.title = selectBoxItem.label;
    for (let i = 0, j = selectBoxItems.length; i < j; ++i) {
    setSelectBoxFakeOptions(selectBoxItems[i], selectBoxFakeOptionGroup);
    }
    parent.append(selectBoxFakeOptionGroup);
    return;
    }
    let option = D.createElement('a'),
    v = item.getAttribute('value'),
    t = item.textContent;
    option.tabIndex = -1;
    option.textContent = t;
    option.title = t;
    option.dataset.index = index;
    option.dataset.value = v = null !== v ? v : t;
    if (item.hasAttribute('disabled')) {
    option.classList.add('disabled');
    let selectBoxFakeOption = D.createElement('a'),
    selectBoxOptionValue = selectBoxItem.getAttribute('value'),
    selectBoxOptionText = selectBoxItem.textContent;
    selectBoxOptionValue = selectBoxOptionValue || selectBoxOptionText;
    selectBoxFakeOption.tabIndex = -1;
    selectBoxFakeOption.textContent = selectBoxOptionText;
    selectBoxFakeOption.title = selectBoxOptionText;
    selectBoxFakeOption._index = selectBoxIndex;
    selectBoxFakeOption._value = selectBoxOptionValue;
    if (selectBoxItem.hasAttribute('disabled')) {
    selectBoxFakeOption.classList.add('disabled');
    } else {
    option.addEventListener('click', onSelectBoxFakeOptionClick, false);
    selectBoxFakeOption.addEventListener('click', onSelectBoxFakeOptionClick, false);
    }
    options.append(option);
    if (v === valueCurrent) {
    value.textContent = t;
    option.classList.add('selected');
    parent.append(selectBoxFakeOption);
    if (selectBoxOptionValue === selectBoxValue) {
    selectBox.value = selectBoxValue;
    selectBoxFakeLabel.textContent = selectBoxOptionText;
    selectBoxFakeOption.classList.add('selected');
    }
    ++index;
    }
    if (t) {
    container.title = t;
    ++selectBoxIndex;
    }
    if (selectBox.disabled) {
    container.classList.add('disabled');
    selectBoxFake.classList.add('disabled');
    } else {
    container.tabIndex = 0;
    selectBoxFake.tabIndex = 0;
    selectBox.tabIndex = -1;
    container.addEventListener('blur', onSelectBoxFakeBlur, false);
    container.addEventListener('click', onSelectBoxFakeClick, false);
    container.addEventListener('focus', onSelectBoxFakeFocus, false);
    container.addEventListener('keydown', onSelectBoxFakeKeyDown, false);
    selectBoxFake.addEventListener('blur', onSelectBoxFakeBlur, false);
    selectBoxFake.addEventListener('click', onSelectBoxFakeClick, false);
    selectBoxFake.addEventListener('focus', onSelectBoxFakeFocus, false);
    selectBoxFake.addEventListener('keydown', onSelectBoxFakeKeyDown, false);
    }
    if (items.length) {
    let options = D.createElement('span');
    container.append(options);
    for (let i = 0, j = items.length; i < j; ++i) {
    setSelectBoxFakeOptions(items[i], options);
    if (selectBoxItems.length) {
    let selectBoxFakeDropDown = D.createElement('span');
    selectBoxFake.append(selectBoxFakeDropDown);
    for (let i = 0, j = selectBoxItems.length; i < j; ++i) {
    setSelectBoxFakeOptions(selectBoxItems[i], selectBoxFakeDropDown);
    }
    if (selectBox.size) {
    let selectBoxFakeOptions = getSelectBoxFakeOptions(selectBoxFake),
    selectBoxFakeOption = getSelectBoxFakeOptionByIndex(selectBoxFakeOptions, 0);
    selectBoxFakeDropDown.style.maxHeight = (selectBoxFakeOption.offsetHeight * selectBox.size) + 'px';
    }
    }
    selectBox.addEventListener('focus', onSelectBoxFocus, false);
    selectBox.addEventListener('change', onSelectBoxChange, false);
    selectBox.addEventListener('input', onSelectBoxInput, false);
    selectBox.parentNode.insertBefore(container, selectBox.nextElementSibling);
    selectBox.classList.add('select-source');
    if (selectBox.multiple) {
    // TODO
    }
    }

    function setSelectBoxFakeOptionsPosition(selectBoxFake) {
    let {height, left, top, width} = selectBoxFake.getBoundingClientRect(),
    selectBoxFakeOptions = selectBoxFake.children[1];
    selectBoxFakeOptions.style.top = (top + height) + 'px';
    selectBoxFakeOptions.style.left = left + 'px';
    selectBoxFakeOptions.style.width = width + 'px';
    selectBoxFakeOptions.style.maxHeight = (W.innerHeight - top - height) + 'px';
    let option = selectBoxFakeOptions.querySelector('a[data-index].selected');
    if (option) {
    selectBoxFakeOptions.scrollTop = (option.offsetTop + option.offsetHeight) - selectBoxFakeOptions.offsetHeight;
    let selectBox = selectBoxFake.previousElementSibling,
    selectBoxFakeDropDown = getSelectBoxFakeDropDown(selectBoxFake),
    selectBoxFakeBorderTopWidth = W.parseInt(getStyle(selectBoxFake, 'border-top-width'), 10),
    selectBoxFakeBorderBottomWidth = W.parseInt(getStyle(selectBoxFake, 'border-bottom-width'), 10);
    if (!selectBox.size) {
    let {height, left, top, width} = selectBoxFake.getBoundingClientRect(),
    heightWindow = W.innerHeight,
    heightMax = heightWindow - top - height;
    selectBoxFakeDropDown.style.top = (top + height - selectBoxFakeBorderTopWidth) + 'px';
    selectBoxFakeDropDown.style.bottom = "";
    selectBoxFakeDropDown.style.left = left + 'px';
    selectBoxFakeDropDown.style.width = width + 'px';
    selectBoxFakeDropDown.style.maxHeight = heightMax + 'px';
    if (heightMax < (heightWindow - height) / 2) {
    heightMax = top;
    selectBoxFakeDropDown.style.top = "";
    selectBoxFakeDropDown.style.bottom = (heightWindow - top - selectBoxFakeBorderBottomWidth) + 'px';
    selectBoxFakeDropDown.style.maxHeight = (heightMax + 1) + 'px';
    selectBoxFakeDropDown.classList.add('up');
    } else {
    selectBoxFakeDropDown.classList.remove('up');
    }
    }
    let selectBoxFakeOption = getSelectBoxFakeOptions(selectBoxFake).find(selectBoxFakeOption => {
    return selectBoxFakeOption.classList.contains('selected');
    });
    if (selectBoxFakeOption) {
    let height = selectBoxFakeOption.offsetHeight,
    heightParent = selectBoxFakeDropDown.offsetHeight,
    top = selectBoxFakeOption.offsetTop,
    topScroll = selectBoxFakeDropDown.scrollTop;
    if (top < topScroll) {
    selectBoxFakeDropDown.scrollTop = selectBoxFakeOption.offsetTop;
    } else if (top + height - heightParent > topScroll) {
    selectBoxFakeDropDown.scrollTop = top + height - heightParent;
    }
    }
    }

    @@ -366,48 +499,50 @@
    let t = this,
    selectBox = t.previousElementSibling;
    selectBoxValue = selectBox.value;
    selectBoxFakeClicked = t;
    }

    function onSelectBoxFakeKeyDown(e) {
    let t = this,
    key = e.key,
    keyCode = e.keyCode,
    selectBox = t.previousElementSibling,
    index = selectBox.selectedIndex,
    option = t.querySelector('a[data-index="' + index + '"]'),
    selectBoxIndex = selectBox.selectedIndex,
    selectBoxFakeOptions = getSelectBoxFakeOptions(t),
    selectBoxFakeOption = getSelectBoxFakeOptionByIndex(selectBoxFakeOptions, selectBoxIndex),
    open = t.classList.contains('open');
    // console.log([key, keyCode]);
    if ('ArrowDown' === key || 40 === keyCode) {
    while (option = t.querySelector('a[data-index="' + (++index) + '"]')) {
    if (!option.classList.contains('disabled')) {
    while (selectBoxFakeOption = getSelectBoxFakeOptionByIndex(selectBoxFakeOptions, ++selectBoxIndex)) {
    if (!selectBoxFakeOption.classList.contains('disabled')) {
    break;
    }
    }
    if (option) {
    trigger(option, 'click');
    if (selectBoxFakeOption) {
    trigger(selectBoxFakeOption, 'click');
    t.classList[open ? 'add' : 'remove']('open');
    }
    e.preventDefault();
    } else if ('ArrowUp' === key || 38 === keyCode) {
    while (option = t.querySelector('a[data-index="' + (--index) + '"]')) {
    if (!option.classList.contains('disabled')) {
    while (selectBoxFakeOption = getSelectBoxFakeOptionByIndex(selectBoxFakeOptions, --selectBoxIndex)) {
    if (!selectBoxFakeOption.classList.contains('disabled')) {
    break;
    }
    }
    if (option) {
    trigger(option, 'click');
    if (selectBoxFakeOption) {
    trigger(selectBoxFakeOption, 'click');
    t.classList[open ? 'add' : 'remove']('open');
    }
    e.preventDefault();
    } else if ('End' === key || 35 === keyCode) {
    index = selectBox.options.length;
    while (option = t.querySelector('a[data-index="' + (--index) + '"]')) {
    if (!option.classList.contains('disabled')) {
    selectBoxIndex = selectBox.options.length;
    while (selectBoxFakeOption = getSelectBoxFakeOptionByIndex(selectBoxFakeOptions, --selectBoxIndex)) {
    if (!selectBoxFakeOption.classList.contains('disabled')) {
    break;
    }
    }
    if (option) {
    trigger(option, 'click');
    if (selectBoxFakeOption) {
    trigger(selectBoxFakeOption, 'click');
    t.classList[open ? 'add' : 'remove']('open');
    }
    e.preventDefault();
    @@ -418,23 +553,23 @@
    t.classList.remove('open');
    // e.preventDefault();
    } else if ('Home' === key || 36 === keyCode) {
    index = -1;
    while (option = t.querySelector('a[data-index="' + (++index) + '"]')) {
    if (!option.classList.contains('disabled')) {
    selectBoxIndex = -1;
    while (selectBoxFakeOption = getSelectBoxFakeOptionByIndex(selectBoxFakeOptions, ++selectBoxIndex)) {
    if (!selectBoxFakeOption.classList.contains('disabled')) {
    break;
    }
    }
    if (option) {
    trigger(option, 'click');
    if (selectBoxFakeOption) {
    trigger(selectBoxFakeOption, 'click');
    t.classList[open ? 'add' : 'remove']('open');
    }
    e.preventDefault();
    } else if ('Tab' === key || 9 === keyCode) {
    option && trigger(option, 'click');
    selectBoxFakeOption && trigger(selectBoxFakeOption, 'click');
    t.classList.remove('open');
    // e.preventDefault();
    }
    setSelectBoxFakeOptionsPosition(t);
    t.classList.contains('open') && setSelectBoxFakeOptionsPosition(t);
    }

    function onSelectBoxChange(e) {
    @@ -447,26 +582,37 @@

    function onSelectBoxInput() {
    let t = this,
    selectBoxValue = t.value,
    selectBoxFake = t.nextElementSibling,
    selectBoxFakeOptions = selectBoxFake.querySelectorAll('a[data-index]');
    for (let i = 0, j = selectBoxFakeOptions.length; i < j; ++i) {
    if (t.value === selectBoxFakeOptions[i].dataset.value) {
    trigger(selectBoxFakeOptions[i], 'click');
    break;
    }
    }
    selectBoxFakeOptions = getSelectBoxFakeOptions(selectBoxFake);
    // if (!selectBoxValue && t.options[0]) {
    // selectBoxValue = t.options[0].value;
    // }
    let selectBoxFakeOption = getSelectBoxFakeOptionByValue(selectBoxFakeOptions, selectBoxValue);
    selectBoxFakeOption && trigger(selectBoxFakeOption, 'click');
    }

    function onSelectBoxFormReset() {
    W.setTimeout(() => selectBoxes.length && selectBoxes.forEach(selectBox => trigger(selectBox, 'input')), 1);
    }

    function letSelectBoxesFakeDocument() {
    function onSelectBoxWindowResize() {
    if (!selectBoxesFake) return;
    selectBoxesFake.length && selectBoxesFake.forEach(selectBoxFake => {
    if (selectBoxFake.classList.contains('open')) {
    setSelectBoxFakeOptionsPosition(selectBoxFake);
    }
    });
    }

    function letSelectBoxesFakeView() {
    D.removeEventListener('click', onSelectBoxFakeClickOutside);
    W.removeEventListener('resize', onSelectBoxWindowResize);
    }

    function setSelectBoxesFakeDocument() {
    function setSelectBoxesFakeView() {
    D.addEventListener('click', onSelectBoxFakeClickOutside, false);
    W.addEventListener('resize', onSelectBoxWindowResize, false);
    }

    let D = document,
    @@ -477,38 +623,36 @@
    selectBoxes,
    selectBoxesFake;

    // Destruct function
    function letSelectBoxesFake() {
    if (!selectBoxesFake) {
    return;
    }
    let form;
    selectBoxes.forEach((selectBox, index) => {
    if (0 === index) {
    if (form = selectBox.form) {
    form.removeEventListener('reset', onSelectBoxFormReset);
    }
    selectBoxes.forEach(selectBox => {
    if (form = selectBox.form) {
    form.removeEventListener('reset', onSelectBoxFormReset);
    }
    letSelectBoxFake(selectBox);
    });
    letSelectBoxesFakeDocument();
    letSelectBoxesFakeView();
    selectBoxesFake = null;
    }

    // Construct function
    function setSelectBoxesFake() {
    selectBoxes = D.querySelectorAll('.select');
    let form;
    selectBoxes.forEach((selectBox, index) => {
    if (0 === index) {
    if (form = selectBox.form) {
    form.addEventListener('reset', onSelectBoxFormReset, false);
    }
    selectBoxes.forEach(selectBox => {
    if (form = selectBox.form) {
    form.removeEventListener('reset', onSelectBoxFormReset); // Remove duplicate!
    form.addEventListener('reset', onSelectBoxFormReset, false);
    }
    setSelectBoxFake(selectBox);
    });
    setSelectBoxesFakeView();
    selectBoxesFake = D.querySelectorAll('.select.js');
    setSelectBoxesFakeDocument();
    }

    setSelectBoxesFake(); // Enable!
    </script>

    <script>
    @@ -521,4 +665,9 @@
    console.log('input: ' + JSON.stringify(this.value));
    });
    });

    document.forms[0].addEventListener('submit', function(e) {
    alert(new URLSearchParams(new FormData(this)) + "");
    e.preventDefault();
    });
    </script>
  5. taufik-nurrohman revised this gist Jun 5, 2021. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions accessible-custom-select-box.html
    Original file line number Diff line number Diff line change
    @@ -258,7 +258,7 @@
    container.querySelectorAll('a[data-index]').forEach(option => {
    option.classList[valueCurrent === option.dataset.value ? 'add' : 'remove']('selected');
    });
    trigger(container, 'focus');
    container.focus();
    if (valueCurrent !== valuePrev) {
    selectBox.value = valueCurrent;
    trigger(selectBox, 'input');
    @@ -442,7 +442,7 @@
    }

    function onSelectBoxFocus(e) {
    trigger(this.nextElementSibling, 'focus');
    this.nextElementSibling.focus();
    }

    function onSelectBoxInput() {
    @@ -458,7 +458,7 @@
    }

    function onSelectBoxFormReset() {
    W.setTimeout(() => selectBoxes.length && selectBoxes.forEach(selectBox => trigger(selectBox, 'change')), 10);
    W.setTimeout(() => selectBoxes.length && selectBoxes.forEach(selectBox => trigger(selectBox, 'input')), 1);
    }

    function letSelectBoxesFakeDocument() {
  6. taufik-nurrohman revised this gist Jun 5, 2021. 1 changed file with 61 additions and 14 deletions.
    75 changes: 61 additions & 14 deletions accessible-custom-select-box.html
    Original file line number Diff line number Diff line change
    @@ -22,6 +22,7 @@
    box-sizing: border-box;
    }

    .select[disabled],
    .select.disabled {
    color: gray;
    cursor: not-allowed;
    @@ -207,16 +208,25 @@
    </p>
    <p>
    <button type="submit" name="button-1" value="true">Submit</button>
    <button type="reset">Reset</button>
    </p>
    </form>

    <script>
    function trigger(node, eventName) {
    node.dispatchEvent(new Event(eventName));
    }

    function letSelectBoxFake(selectBox) {
    selectBox.removeEventListener('focus', onSelectBoxFocus);
    selectBox.removeEventListener('change', onSelectBoxChange);
    selectBox.removeEventListener('input', onSelectBoxInput);
    selectBox.classList.remove('select-source');
    let selectBoxFake = selectBox.nextElementSibling;
    if (selectBoxFake.classList.contains('select') && selectBoxFake.classList.contains('js')) {
    selectBoxFake.removeEventListener('blur', onSelectBoxFakeBlur);
    selectBoxFake.removeEventListener('click', onSelectBoxFakeClick);
    selectBoxFake.removeEventListener('focus', onSelectBoxFakeFocus);
    selectBoxFake.removeEventListener('keydown', onSelectBoxFakeKeyDown);
    selectBoxFake.textContent = "";
    selectBoxFake.remove();
    @@ -242,17 +252,16 @@
    function setSelectBoxFakeOptions(item, options) {
    function onSelectBoxFakeOptionClick(e) {
    let t = this,
    eventInput = new Event('input'),
    valuePrev = valueCurrent;
    value.textContent = t.textContent;
    valueCurrent = t.dataset.value;
    container.querySelectorAll('a[data-index]').forEach(option => {
    option.classList[valueCurrent === option.dataset.value ? 'add' : 'remove']('selected');
    });
    container.focus();
    trigger(container, 'focus');
    if (valueCurrent !== valuePrev) {
    selectBox.value = valueCurrent;
    selectBox.dispatchEvent(eventInput);
    trigger(selectBox, 'input');
    }
    e.preventDefault();
    }
    @@ -307,6 +316,8 @@
    }
    }
    selectBox.addEventListener('focus', onSelectBoxFocus, false);
    selectBox.addEventListener('change', onSelectBoxChange, false);
    selectBox.addEventListener('input', onSelectBoxInput, false);
    selectBox.parentNode.insertBefore(container, selectBox.nextElementSibling);
    selectBox.classList.add('select-source');
    }
    @@ -330,14 +341,14 @@
    });
    }

    function onSelectBoxFakeBlur() {
    function onSelectBoxFakeBlur(e) {
    if (!selectBoxFakeClicked) {
    return;
    }
    let t = this,
    selectBox = t.previousElementSibling;
    if (!t.classList.contains('open') && selectBox.value !== selectBoxValue) {
    selectBox.dispatchEvent(new Event('change'));
    trigger(selectBox, 'change');
    }
    selectBoxFakeClicked = null;
    }
    @@ -373,7 +384,7 @@
    }
    }
    if (option) {
    option.click();
    trigger(option, 'click');
    t.classList[open ? 'add' : 'remove']('open');
    }
    e.preventDefault();
    @@ -384,7 +395,7 @@
    }
    }
    if (option) {
    option.click();
    trigger(option, 'click');
    t.classList[open ? 'add' : 'remove']('open');
    }
    e.preventDefault();
    @@ -396,7 +407,7 @@
    }
    }
    if (option) {
    option.click();
    trigger(option, 'click');
    t.classList[open ? 'add' : 'remove']('open');
    }
    e.preventDefault();
    @@ -414,20 +425,40 @@
    }
    }
    if (option) {
    option.click();
    trigger(option, 'click');
    t.classList[open ? 'add' : 'remove']('open');
    }
    e.preventDefault();
    } else if ('Tab' === key || 9 === keyCode) {
    option && option.click();
    option && trigger(option, 'click');
    t.classList.remove('open');
    // e.preventDefault();
    }
    setSelectBoxFakeOptionsPosition(t);
    }

    function onSelectBoxChange(e) {
    onSelectBoxInput.call(this, e);
    }

    function onSelectBoxFocus(e) {
    this.nextElementSibling.focus();
    trigger(this.nextElementSibling, 'focus');
    }

    function onSelectBoxInput() {
    let t = this,
    selectBoxFake = t.nextElementSibling,
    selectBoxFakeOptions = selectBoxFake.querySelectorAll('a[data-index]');
    for (let i = 0, j = selectBoxFakeOptions.length; i < j; ++i) {
    if (t.value === selectBoxFakeOptions[i].dataset.value) {
    trigger(selectBoxFakeOptions[i], 'click');
    break;
    }
    }
    }

    function onSelectBoxFormReset() {
    W.setTimeout(() => selectBoxes.length && selectBoxes.forEach(selectBox => trigger(selectBox, 'change')), 10);
    }

    function letSelectBoxesFakeDocument() {
    @@ -450,13 +481,29 @@
    if (!selectBoxesFake) {
    return;
    }
    selectBoxes.forEach(selectBox => letSelectBoxFake(selectBox));
    let form;
    selectBoxes.forEach((selectBox, index) => {
    if (0 === index) {
    if (form = selectBox.form) {
    form.removeEventListener('reset', onSelectBoxFormReset);
    }
    }
    letSelectBoxFake(selectBox);
    });
    letSelectBoxesFakeDocument();
    }

    function setSelectBoxesFake() {
    selectBoxes = D.querySelectorAll('.select');
    selectBoxes.forEach(selectBox => setSelectBoxFake(selectBox));
    let form;
    selectBoxes.forEach((selectBox, index) => {
    if (0 === index) {
    if (form = selectBox.form) {
    form.addEventListener('reset', onSelectBoxFormReset, false);
    }
    }
    setSelectBoxFake(selectBox);
    });
    selectBoxesFake = D.querySelectorAll('.select.js');
    setSelectBoxesFakeDocument();
    }
    @@ -465,7 +512,7 @@
    </script>

    <script>
    // Test for native change event
    // Test for native JavaScript event(s)
    document.querySelectorAll('select').forEach(select => {
    select.addEventListener('change', function() {
    console.log('change: ' + JSON.stringify(this.value));
  7. taufik-nurrohman revised this gist Jun 5, 2021. No changes.
  8. taufik-nurrohman revised this gist Jun 5, 2021. 1 changed file with 44 additions and 6 deletions.
    50 changes: 44 additions & 6 deletions accessible-custom-select-box.html
    Original file line number Diff line number Diff line change
    @@ -241,13 +241,19 @@
    }
    function setSelectBoxFakeOptions(item, options) {
    function onSelectBoxFakeOptionClick(e) {
    let t = this;
    let t = this,
    eventInput = new Event('input'),
    valuePrev = valueCurrent;
    value.textContent = t.textContent;
    selectBox.value = valueCurrent = t.getAttribute('data-value');
    valueCurrent = t.dataset.value;
    container.querySelectorAll('a[data-index]').forEach(option => {
    option.classList[valueCurrent === option.getAttribute('data-value') ? 'add' : 'remove']('selected');
    option.classList[valueCurrent === option.dataset.value ? 'add' : 'remove']('selected');
    });
    container.focus();
    if (valueCurrent !== valuePrev) {
    selectBox.value = valueCurrent;
    selectBox.dispatchEvent(eventInput);
    }
    e.preventDefault();
    }
    if ('optgroup' === item.tagName.toLowerCase()) {
    @@ -266,8 +272,8 @@
    option.tabIndex = -1;
    option.textContent = t;
    option.title = t;
    option.setAttribute('data-index', index);
    option.setAttribute('data-value', v = null !== v ? v : t);
    option.dataset.index = index;
    option.dataset.value = v = null !== v ? v : t;
    if (item.hasAttribute('disabled')) {
    option.classList.add('disabled');
    } else {
    @@ -288,7 +294,9 @@
    } else {
    container.tabIndex = 0;
    selectBox.tabIndex = -1;
    container.addEventListener('blur', onSelectBoxFakeBlur, false);
    container.addEventListener('click', onSelectBoxFakeClick, false);
    container.addEventListener('focus', onSelectBoxFakeFocus, false);
    container.addEventListener('keydown', onSelectBoxFakeKeyDown, false);
    }
    if (items.length) {
    @@ -320,6 +328,17 @@
    selectBoxesFake && selectBoxesFake.forEach(selectBoxFake => {
    selectBoxFake !== selectBoxFakeClicked && selectBoxFake.classList.remove('open');
    });
    }

    function onSelectBoxFakeBlur() {
    if (!selectBoxFakeClicked) {
    return;
    }
    let t = this,
    selectBox = t.previousElementSibling;
    if (!t.classList.contains('open') && selectBox.value !== selectBoxValue) {
    selectBox.dispatchEvent(new Event('change'));
    }
    selectBoxFakeClicked = null;
    }

    @@ -332,6 +351,12 @@
    selectBoxFakeClicked = t;
    }

    function onSelectBoxFakeFocus() {
    let t = this,
    selectBox = t.previousElementSibling;
    selectBoxValue = selectBox.value;
    }

    function onSelectBoxFakeKeyDown(e) {
    let t = this,
    key = e.key,
    @@ -340,7 +365,7 @@
    index = selectBox.selectedIndex,
    option = t.querySelector('a[data-index="' + index + '"]'),
    open = t.classList.contains('open');
    console.log([key, keyCode]);
    // console.log([key, keyCode]);
    if ('ArrowDown' === key || 40 === keyCode) {
    while (option = t.querySelector('a[data-index="' + (++index) + '"]')) {
    if (!option.classList.contains('disabled')) {
    @@ -416,6 +441,7 @@
    let D = document,
    R = D.documentElement,
    W = window,
    selectBoxValue,
    selectBoxFakeClicked,
    selectBoxes,
    selectBoxesFake;
    @@ -436,4 +462,16 @@
    }

    setSelectBoxesFake(); // Enable!
    </script>

    <script>
    // Test for native change event
    document.querySelectorAll('select').forEach(select => {
    select.addEventListener('change', function() {
    console.log('change: ' + JSON.stringify(this.value));
    });
    select.addEventListener('input', function() {
    console.log('input: ' + JSON.stringify(this.value));
    });
    });
    </script>
  9. taufik-nurrohman revised this gist Jun 5, 2021. 1 changed file with 80 additions and 26 deletions.
    106 changes: 80 additions & 26 deletions accessible-custom-select-box.html
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,6 @@
    <style>
    .select {
    position: relative;
    box-sizing: border-box;
    background: white;
    color: black;
    border: 1px solid;
    @@ -18,6 +17,11 @@
    user-select: none;
    }

    .select,
    .select * {
    box-sizing: border-box;
    }

    .select.disabled {
    color: gray;
    cursor: not-allowed;
    @@ -57,14 +61,11 @@
    }

    .select.js > span + span {
    margin: 0;
    position: absolute;
    top: 100%;
    right: -1px;
    left: -1px;
    position: fixed;
    z-index: 9999;
    background: inherit;
    border: inherit;
    margin: -1px 0 0;
    box-shadow: 0 1px 2px rgba(0, 0, 0, .4);
    overflow: auto;
    display: none;
    @@ -181,6 +182,15 @@
    <option disabled>Disabled</option>
    </optgroup>
    <option value="3">A very very very very very very very very very very very long value.</option>
    <option>Item 1</option>
    <option>Item 2</option>
    <option>Item 3</option>
    <option>Item 4</option>
    <option>Item 5</option>
    <option>Item 6</option>
    <option>Item 7</option>
    <option>Item 8</option>
    <option>Item 9</option>
    </select>
    <br>
    <small>Description goes here.</small>
    @@ -223,11 +233,17 @@
    container.className = selectBox.className;
    container.classList.add('js');
    container.append(value);

    if (selectBox.multiple) {
    // TODO
    }
    if (selectBox.size > 1) {
    // TODO
    }
    function setSelectBoxFakeOptions(item, options) {
    function onSelectBoxFakeOptionClick(e) {
    value.textContent = this.textContent;
    selectBox.value = valueCurrent = this.getAttribute('data-value');
    let t = this;
    value.textContent = t.textContent;
    selectBox.value = valueCurrent = t.getAttribute('data-value');
    container.querySelectorAll('a[data-index]').forEach(option => {
    option.classList[valueCurrent === option.getAttribute('data-value') ? 'add' : 'remove']('selected');
    });
    @@ -288,7 +304,16 @@
    }

    function setSelectBoxFakeOptionsPosition(selectBoxFake) {
    // TODO
    let {height, left, top, width} = selectBoxFake.getBoundingClientRect(),
    selectBoxFakeOptions = selectBoxFake.children[1];
    selectBoxFakeOptions.style.top = (top + height) + 'px';
    selectBoxFakeOptions.style.left = left + 'px';
    selectBoxFakeOptions.style.width = width + 'px';
    selectBoxFakeOptions.style.maxHeight = (W.innerHeight - top - height) + 'px';
    let option = selectBoxFakeOptions.querySelector('a[data-index].selected');
    if (option) {
    selectBoxFakeOptions.scrollTop = (option.offsetTop + option.offsetHeight) - selectBoxFakeOptions.offsetHeight;
    }
    }

    function onSelectBoxFakeClickOutside(e) {
    @@ -299,52 +324,81 @@
    }

    function onSelectBoxFakeClick(e) {
    let valueCurrent = this.previousElementSibling.value;
    this.classList.toggle('open');
    if (this.classList.contains('open')) {
    setSelectBoxFakeOptionsPosition(this);
    let t = this;
    t.classList.toggle('open');
    if (t.classList.contains('open')) {
    setSelectBoxFakeOptionsPosition(t);
    }
    selectBoxFakeClicked = this;
    selectBoxFakeClicked = t;
    }

    function onSelectBoxFakeKeyDown(e) {
    let key = e.key,
    let t = this,
    key = e.key,
    keyCode = e.keyCode,
    selectBox = this.previousElementSibling,
    selectBox = t.previousElementSibling,
    index = selectBox.selectedIndex,
    option = this.querySelector('a[data-index="' + index + '"]'),
    open = this.classList.contains('open');
    // console.log([key, keyCode]);
    option = t.querySelector('a[data-index="' + index + '"]'),
    open = t.classList.contains('open');
    console.log([key, keyCode]);
    if ('ArrowDown' === key || 40 === keyCode) {
    while (option = this.querySelector('a[data-index="' + (++index) + '"]')) {
    while (option = t.querySelector('a[data-index="' + (++index) + '"]')) {
    if (!option.classList.contains('disabled')) {
    break;
    }
    }
    if (option) {
    option.click();
    this.classList[open ? 'add' : 'remove']('open');
    t.classList[open ? 'add' : 'remove']('open');
    }
    e.preventDefault();
    } else if ('ArrowUp' === key || 38 === keyCode) {
    while (option = this.querySelector('a[data-index="' + (--index) + '"]')) {
    while (option = t.querySelector('a[data-index="' + (--index) + '"]')) {
    if (!option.classList.contains('disabled')) {
    break;
    }
    }
    if (option) {
    option.click();
    t.classList[open ? 'add' : 'remove']('open');
    }
    e.preventDefault();
    } else if ('End' === key || 35 === keyCode) {
    index = selectBox.options.length;
    while (option = t.querySelector('a[data-index="' + (--index) + '"]')) {
    if (!option.classList.contains('disabled')) {
    break;
    }
    }
    if (option) {
    option.click();
    this.classList[open ? 'add' : 'remove']('open');
    t.classList[open ? 'add' : 'remove']('open');
    }
    e.preventDefault();
    } else if ('Enter' === key || 13 === keyCode) {
    this.classList.toggle('open');
    t.classList.toggle('open');
    e.preventDefault();
    } else if ('Escape' === key || 27 === keyCode) {
    t.classList.remove('open');
    // e.preventDefault();
    } else if ('Home' === key || 36 === keyCode) {
    index = -1;
    while (option = t.querySelector('a[data-index="' + (++index) + '"]')) {
    if (!option.classList.contains('disabled')) {
    break;
    }
    }
    if (option) {
    option.click();
    t.classList[open ? 'add' : 'remove']('open');
    }
    e.preventDefault();
    } else if ('Tab' === key || 9 === keyCode) {
    option && option.click();
    this.classList.remove('open');
    t.classList.remove('open');
    // e.preventDefault();
    }
    setSelectBoxFakeOptionsPosition(t);
    }

    function onSelectBoxFocus(e) {
  10. taufik-nurrohman created this gist Jun 5, 2021.
    385 changes: 385 additions & 0 deletions accessible-custom-select-box.html
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,385 @@
    <meta charset="utf-8">

    <style>
    .select {
    position: relative;
    box-sizing: border-box;
    background: white;
    color: black;
    border: 1px solid;
    font: inherit;
    display: inline-block;
    vertical-align: middle;
    width: 12em;
    padding: .5em .75em;
    cursor: pointer;
    -webkit-user-select: none;
    -moz-user-select: none;
    user-select: none;
    }

    .select.disabled {
    color: gray;
    cursor: not-allowed;
    }

    .select:focus {
    border-color: blue;
    box-shadow: 0 0 0 3px rgba(0, 0, 255, .25);
    }

    .select.js::after {
    content: "";
    width: 0;
    height: 0;
    border-top: 6px solid;
    border-right: 5px solid transparent;
    border-bottom: 0;
    border-left: 5px solid transparent;
    position: absolute;
    top: 50%;
    margin-top: -3px;
    right: .5em;
    pointer-events: none;
    }

    .select.js.open::after {
    border-top: 0;
    border-bottom: 6px solid;
    }

    .select.js > span {
    display: block;
    margin-right: 1em;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    }

    .select.js > span + span {
    margin: 0;
    position: absolute;
    top: 100%;
    right: -1px;
    left: -1px;
    z-index: 9999;
    background: inherit;
    border: inherit;
    box-shadow: 0 1px 2px rgba(0, 0, 0, .4);
    overflow: auto;
    display: none;
    }

    .select.js > span + span a {
    display: block;
    padding: .25em .5em;
    font: inherit;
    color: inherit;
    cursor: pointer;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    }

    .select.js > span + span a:hover,
    .select.js > span + span a.focused {
    background: blue;
    color: white;
    }

    .select.js > span + span span {
    display: block;
    padding: .25em .5em;
    }

    .select.js > span + span span[title]::before {
    content: attr(title);
    display: block;
    margin: 0 0 .25em;
    font-weight: bold;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    }

    .select.js > span + span span a {
    padding-left: 1.5em;
    margin: 0 -.5em;
    }

    .select.js > span + span a.disabled {
    color: gray;
    cursor: not-allowed;
    }

    .select.js > span + span a.disabled:hover,
    .select.js > span + span a.disabled.focused {
    background: gray;
    color: white;
    }

    .select.js > span + span a.selected {
    color: blue;
    }

    .select.js > span + span a.selected:hover,
    .select.js > span + span a.selected.focused {
    background: blue;
    color: white;
    }

    .select.js.open > span + span {
    display: block;
    }

    .select.select-source {
    position: fixed;
    top: -1px;
    left: -1px;
    width: 1px;
    height: 1px;
    background: none;
    border: 0;
    outline: 0;
    font-size: 0;
    overflow: hidden;
    opacity: 0;
    }
    </style>

    <p>
    <label>
    <input checked onchange="this.checked ? setSelectBoxesFake() : letSelectBoxesFake();" type="checkbox">
    <span>Enable/Disable Custom Select Box</span>
    </label>
    </p>

    <hr>

    <form method="get">
    <p>
    <input name="input-1" type="text">
    </p>
    <p>
    <select class="select" name="select-1">
    <option>Red</option>
    <option value="1">Green</option>
    <option value="2">Blue</option>
    <option disabled>Disabled</option>
    </select>
    </p>
    <p>
    <select class="select" name="select-2">
    <option value="#000">Black (not in group)</option>
    <optgroup label="Group 1">
    <option value="#fff">White (in group)</option>
    </optgroup>
    <optgroup label="Group 2">
    <option>Red</option>
    <option value="1">Green</option>
    <option value="2" selected>Blue</option>
    <option disabled>Disabled</option>
    </optgroup>
    <option value="3">A very very very very very very very very very very very long value.</option>
    </select>
    <br>
    <small>Description goes here.</small>
    </p>
    <p>
    <select class="select" disabled name="select-3">
    <option>Test 1</option>
    <option selected>Test 2</option>
    <option>Test 3</option>
    </select>
    </p>
    <p>
    <input name="input-2" type="text">
    </p>
    <p>
    <button type="submit" name="button-1" value="true">Submit</button>
    </p>
    </form>

    <script>
    function letSelectBoxFake(selectBox) {
    selectBox.removeEventListener('focus', onSelectBoxFocus);
    selectBox.classList.remove('select-source');
    let selectBoxFake = selectBox.nextElementSibling;
    if (selectBoxFake.classList.contains('select') && selectBoxFake.classList.contains('js')) {
    selectBoxFake.removeEventListener('click', onSelectBoxFakeClick);
    selectBoxFake.removeEventListener('keydown', onSelectBoxFakeKeyDown);
    selectBoxFake.textContent = "";
    selectBoxFake.remove();
    }
    }

    function setSelectBoxFake(selectBox) {
    let container = D.createElement('span'),
    value = D.createElement('span'),
    valueCurrent = selectBox.value,
    t = selectBox.title,
    index = 0,
    items = selectBox.children;
    container.className = selectBox.className;
    container.classList.add('js');
    container.append(value);

    function setSelectBoxFakeOptions(item, options) {
    function onSelectBoxFakeOptionClick(e) {
    value.textContent = this.textContent;
    selectBox.value = valueCurrent = this.getAttribute('data-value');
    container.querySelectorAll('a[data-index]').forEach(option => {
    option.classList[valueCurrent === option.getAttribute('data-value') ? 'add' : 'remove']('selected');
    });
    container.focus();
    e.preventDefault();
    }
    if ('optgroup' === item.tagName.toLowerCase()) {
    let optgroup = D.createElement('span'),
    items = item.children;
    optgroup.title = item.label;
    options.append(optgroup);
    for (let i = 0, j = items.length; i < j; ++i) {
    setSelectBoxFakeOptions(items[i], optgroup);
    }
    return;
    }
    let option = D.createElement('a'),
    v = item.getAttribute('value'),
    t = item.textContent;
    option.tabIndex = -1;
    option.textContent = t;
    option.title = t;
    option.setAttribute('data-index', index);
    option.setAttribute('data-value', v = null !== v ? v : t);
    if (item.hasAttribute('disabled')) {
    option.classList.add('disabled');
    } else {
    option.addEventListener('click', onSelectBoxFakeOptionClick, false);
    }
    options.append(option);
    if (v === valueCurrent) {
    value.textContent = t;
    option.classList.add('selected');
    }
    ++index;
    }
    if (t) {
    container.title = t;
    }
    if (selectBox.disabled) {
    container.classList.add('disabled');
    } else {
    container.tabIndex = 0;
    selectBox.tabIndex = -1;
    container.addEventListener('click', onSelectBoxFakeClick, false);
    container.addEventListener('keydown', onSelectBoxFakeKeyDown, false);
    }
    if (items.length) {
    let options = D.createElement('span');
    container.append(options);
    for (let i = 0, j = items.length; i < j; ++i) {
    setSelectBoxFakeOptions(items[i], options);
    }
    }
    selectBox.addEventListener('focus', onSelectBoxFocus, false);
    selectBox.parentNode.insertBefore(container, selectBox.nextElementSibling);
    selectBox.classList.add('select-source');
    }

    function setSelectBoxFakeOptionsPosition(selectBoxFake) {
    // TODO
    }

    function onSelectBoxFakeClickOutside(e) {
    selectBoxesFake && selectBoxesFake.forEach(selectBoxFake => {
    selectBoxFake !== selectBoxFakeClicked && selectBoxFake.classList.remove('open');
    });
    selectBoxFakeClicked = null;
    }

    function onSelectBoxFakeClick(e) {
    let valueCurrent = this.previousElementSibling.value;
    this.classList.toggle('open');
    if (this.classList.contains('open')) {
    setSelectBoxFakeOptionsPosition(this);
    }
    selectBoxFakeClicked = this;
    }

    function onSelectBoxFakeKeyDown(e) {
    let key = e.key,
    keyCode = e.keyCode,
    selectBox = this.previousElementSibling,
    index = selectBox.selectedIndex,
    option = this.querySelector('a[data-index="' + index + '"]'),
    open = this.classList.contains('open');
    // console.log([key, keyCode]);
    if ('ArrowDown' === key || 40 === keyCode) {
    while (option = this.querySelector('a[data-index="' + (++index) + '"]')) {
    if (!option.classList.contains('disabled')) {
    break;
    }
    }
    if (option) {
    option.click();
    this.classList[open ? 'add' : 'remove']('open');
    }
    e.preventDefault();
    } else if ('ArrowUp' === key || 38 === keyCode) {
    while (option = this.querySelector('a[data-index="' + (--index) + '"]')) {
    if (!option.classList.contains('disabled')) {
    break;
    }
    }
    if (option) {
    option.click();
    this.classList[open ? 'add' : 'remove']('open');
    }
    e.preventDefault();
    } else if ('Enter' === key || 13 === keyCode) {
    this.classList.toggle('open');
    e.preventDefault();
    } else if ('Tab' === key || 9 === keyCode) {
    option && option.click();
    this.classList.remove('open');
    // e.preventDefault();
    }
    }

    function onSelectBoxFocus(e) {
    this.nextElementSibling.focus();
    }

    function letSelectBoxesFakeDocument() {
    D.removeEventListener('click', onSelectBoxFakeClickOutside);
    }

    function setSelectBoxesFakeDocument() {
    D.addEventListener('click', onSelectBoxFakeClickOutside, false);
    }

    let D = document,
    R = D.documentElement,
    W = window,
    selectBoxFakeClicked,
    selectBoxes,
    selectBoxesFake;

    function letSelectBoxesFake() {
    if (!selectBoxesFake) {
    return;
    }
    selectBoxes.forEach(selectBox => letSelectBoxFake(selectBox));
    letSelectBoxesFakeDocument();
    }

    function setSelectBoxesFake() {
    selectBoxes = D.querySelectorAll('.select');
    selectBoxes.forEach(selectBox => setSelectBoxFake(selectBox));
    selectBoxesFake = D.querySelectorAll('.select.js');
    setSelectBoxesFakeDocument();
    }

    setSelectBoxesFake(); // Enable!
    </script>