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

              
                <main>

  <p><button class="focus">Click me to set focus inside demo iframe.</button></p>

  <p>Other content above the dropdown.</p>

  <div class="simulated-select"></div>

  <p>Other content below the dropdown. <button>Some button</button></p>

  <p>Tested on Chrome, Firefox, Mac, Android.</p>

  <p><a href="https://github.com/hchiam/stylable-select" target="_blank" rel="noopener noreferrer">https://github.com/hchiam/stylable-select</a></p>

  <p><small id="activeElement"></small></p>
</main>

<style>
  html,
  body {
    height: 100%;
    margin: 0;
    padding: 0;
  }

  body {
    font-family: avenir, tahoma, monospace;
    display: flex;
    /*
    justify-content: center;
    align-items: center;
    */
    width: 200vw;
    height: 150vh;
    /* padding-left: 100vh; */
    /* padding-top: 100vh; */
  }

  main {
    width: 100%;
    margin: 1rem;
    /* text-align: right; */
  }

  button.focus {
    width: 35ch;
    padding-block: 0.5rem;
  }

  #activeElement {
    position: fixed;
    bottom: 10px;
    color: grey;
  }

  .simulated-select,
  .simulated-select-options-wrapper {
    animation: highlight 3s;
  }

  @keyframes highlight {
    from {
      background: lime;
      outline: 3px solid lime;
    }

    to {
      background: revert;
      outline: revert;
    }
  }
</style>
              
            
!

CSS

              
                .simulated-select {
  --select-height: 2rem;
  --max-items: 5;
  --selected-background: #bfd4ed;
  --selected-color: #000;
  position: relative;
  position: sticky;
  inset: 0;
  isolation: isolate;
  height: var(--select-height);
  width: fit-content;

  .simulated-select-options-end {
    /* to give the illusion that the dropdown is focused: */
    position: absolute;
    left: 0;
    top: 0;
    height: 100%;
    width: 100%;
    border-radius: 0.25rem;
    z-index: -1;
    pointer-events: none;

    /* so no need to make visually hidden: */
    // position: absolute;
    // overflow: hidden;
    // clip: rect(0 0 0 0);
    // height: 1px;
    // width: 1px;
    // margin: -1px;
    // padding: 0;
    // border: 0;
  }

  .simulated-select-options-wrapper {
    display: flex;
    flex-direction: column;
    // position: absolute; // disable position so option can go to top
    box-sizing: border-box;
    max-width: max-content;
    border-radius: 0.25rem;
    height: var(--select-height);
    max-height: calc(var(--max-items) * var(--select-height));

    > [role="option"] {
      box-sizing: border-box;
      height: var(--select-height);
      width: 100%;
      border-radius: 0;
      border: none;
      border-top: 1px solid #dee2e6;
      background: #fff;
      color: #0053b8;
      cursor: pointer;
      padding: 0.5rem;
      line-height: 1rem;
      margin-right: 2rem;
      text-align: left;
      white-space: nowrap;
      &:focus,
      &:hover,
      &[selected] {
        background: var(--selected-background);
        color: var(--selected-color);
        outline-offset: -2px;
      }
      &[selected] {
        font-weight: bold;
      }
    }
  }
}

.simulated-select-options-wrapper:not(:focus-within) {
  overflow: hidden;

  > [role="option"] {
    background: lightgrey;
    color: var(--selected-color);

    &[selected] {
      position: absolute;
      top: 0;
      background: revert;
      font-weight: revert;
    }
  }
}

.simulated-select-options-wrapper:focus,
.simulated-select-options-wrapper:focus-within {
  height: revert;
  box-shadow: rgb(0 0 0 / 50%) 0px 2px 4px 0px;
  overflow-x: hidden;
  overflow-y: auto;
}

              
            
!

JS

              
                const simulatedSelect = $(".simulated-select");
const optionsData = [
  {
    Value: "",
    Text: "Select"
  },
  {
    Text: "Apple"
  },
  {
    Text: "Banana"
  },
  {
    Text: "Coconut"
  },
  {
    Text: "Durian"
  },
  {
    Text: "Eggplant"
  },
  {
    Text: "Fig"
  },
  {
    Text: "Grape"
  },
  {
    Text: "Honeydew"
  },
  {
    Text: "Icecream"
  },
  {
    Text: "Jackfruit"
  },
  {
    Text: "Kiwi"
  },
  {
    Text: "Lemon"
  },
  {
    Text: "Mango"
  },
  {
    Text: "Nectarine"
  },
  {
    Text: "Orange"
  },
  {
    Text: "Peach"
  }
];
createStylableSelect(simulatedSelect, optionsData);

function createStylableSelect(simulatedSelect, optionsData = []) {
  fillStylableSelectHtml(simulatedSelect, optionsData); // must run before these:
  const wrapper = simulatedSelect.find(".simulated-select-options-wrapper");
  const optionsEnd = simulatedSelect.find(".simulated-select-options-end");
  const options = wrapper.find('> [role="option"]');

  simulatedSelect
    .on("click", function (e) {
      const option = $(e.target);
      if (!option.is('[role="option"]')) return;

      const previousSelection = options.filter("[selected]");
      previousSelection.removeAttr("selected").attr("aria-selected", "false");
      option.attr("selected", "selected");
      option.attr("aria-selected", "true");

      options.attr("tabindex", "0"); // enable styles related to focus for mouse users

      optionsEnd.attr("aria-label", `You selected: ${option.text()}`);

      const changedValue =
        (previousSelection.attr("value") !== option.attr("value") &&
          previousSelection.attr("value") !== undefined) ||
        (previousSelection.attr("value") === undefined &&
          option.attr("value")) ||
        (previousSelection.text() !== option.text() &&
          previousSelection.text());

      if (changedValue) optionsEnd.focus().blur();
    })
    .on("focus", function (e) {
      const isOption = $(e.target).is('[role="option"]');
      if (isOption) {
        options.attr("tabindex", "0");
      }
    })
    .on("focusout", function (e) {
      const target = $(e.target);
      const isWrapper = target.is(".simulated-select-options-wrapper");
      const isOption = target.is('[role="option"]');
      const isOptionsEnd = target.is(".simulated-select-options-end");
      if (!isWrapper && !isOption && !isOptionsEnd) {
        options.attr("tabindex", "-1"); // disable tabbing to all options
      }
    })
    .on("keyup", function (e) {
      const target = $(e.target);
      const isWrapper = target.is(".simulated-select-options-wrapper");
      const isOption = target.is('[role="option"]');
      const isOptionsEnd = target.is(".simulated-select-options-end");

      if (hitEsc(e)) {
        optionsEnd.focus();
        options.attr("tabindex", "-1");
      } else if (hitArrowUp(e) && isOption) {
        const prev = $(e.target).prev();
        if (prev.length) {
          prev.focus();
        } else {
          options.get(-1).focus();
        }
        e.stopPropagation();
      } else if (hitArrowDown(e) && isOption) {
        const next = $(e.target).next();
        if (next.length) {
          next.focus();
        } else {
          options.get(0).focus();
        }
        e.stopPropagation();
      }
      const enableDropdown = hitEnter(e) || hitSpacebar(e);
      if (enableDropdown) {
        if (isWrapper || isOptionsEnd) {
          options.attr("tabindex", "0");
          const previousSelection = options.filter("[selected]");
          if (previousSelection.length) {
            previousSelection.focus();
          } else {
            options.get(0).focus();
          }
        } else if (isOption) {
          options.attr("tabindex", "-1");
          optionsEnd.focus();
        }
      }
    });

  function fillStylableSelectHtml(simulatedSelect, optionsData) {
    simulatedSelect.append(`
      <div class="simulated-select-options-wrapper" tabindex="0" role="listbox"></div>
      <div class="simulated-select-options-end" tabindex="0" aria-label="end of options"></div>
    `);
    const wrapper = simulatedSelect.find(".simulated-select-options-wrapper");
    wrapper.append(
      optionsData
        .map((o) => {
          const value =
            typeof o.Value !== "undefined" ? `value="${o.Value || ""}"` : "";
          return `<button role="option" aria-selected="false" tabindex="-1" ${value}>${
            o.Text || ""
          }</button>`;
        })
        .join("")
    );
  }
}

function hitEsc(event) {
  return event.key === "Escape" || event.keyCode === 27 || event.which === 27;
}

function hitEnter(event) {
  var key = event.key || event.code || event.keyCode || event.which || event;
  return key === "Enter" || key === "ENTER" || key === 13;
}

function hitSpacebar(event) {
  var key = event.key || event.code || event.keyCode || event.which || event;
  return key === " " || key === "Spacebar" || key === 32;
}

function hitArrowUp(event) {
  var key = event.key || event.code || event.keyCode || event.which || event;
  return key === "ArrowUp" || key === 38;
}

function hitArrowDown(event) {
  var key = event.key || event.code || event.keyCode || event.which || event;
  return key === "ArrowDown" || key === 40;
}

///////

$("*").on("focus", function () {
  const focusedElement = document.activeElement;
  const tag = focusedElement.tagName;
  const classList = [...focusedElement.classList];
  const classString = classList.length ? "." + classList.join(".") : "";
  $("#activeElement").text(`Focused: ${tag}${classString}`);
});

              
            
!
999px

Console