Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

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

Behavior

Save Automatically?

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

              
                <div class="wrapper">
	<header role="banner" class="header">
		<h1>A11y Patterns - Autocomplete</h1>
	</header>

	<main role="main" class="content">
		<h2 class="text-center">Variation: list with inline auto complete</h2>
		<form role="form" class="form" action="#">
		<label id="sp-label" class="combobox-label">
  		Preferred superpower
		</label>
		<div class="combobox-wrapper">
			<div role="combobox" aria-expanded="false" aria-owns="sp-listbox" aria-haspopup="listbox" id="sp-combobox">
				<input type="text" class="form-input" aria-autocomplete="both" aria-controls="sp-listbox" aria-labelledby="sp-label" id="sp-input">
				<div class="combobox-dropdown" id="sp-combobox-arrow" tabindex="-1" role="button" aria-label="Show superpower options">
					<i class="fa fa-icon fa-caret-down" aria-hidden="true"></i>
				</div>
			</div>
			<ul aria-labelledby="sp-label" role="listbox" id="sp-listbox" class="listbox hidden">
			</ul>
		</div>
		</form>
		<hr />

			<h3 class="" id="title-specs">Considerations (1 of 3):</h3>
		<ol class="list" aria-labelledby="title-specs">
			<li>when the dropdown is displayed, the suggested values that that would complete the typed characters will appear</li>
			<li>the first suggestion will appear inline and complete the suggested selection that the user hasn't typed yet</li>
			<li>the first suggestion is automatically highlighted as selected</li>
			<li>the automatically selected suggestion becomes the value of the textbox when the combobox loses focus (unless the user chooses a different suggestion)</li>
		</ol>
			<h3 class="" id="title-keyboardnav">Keyboard Navigation (2 of 3):</h3>
			<dl class="list" aria-labelledby="title-keyboardnav">
				<dt>DOWN ARROW</dt>
				<dd>if the listbox is not displayed, the listbox is opened and moves focus to the first value; subsequent keypresses will move the focus to the next item in the list.</dd>
				<dt>UP ARROW</dt>
				<dd>if the listbox is not displayed, the listbox is opened and focused is moved to the last value; subsequent keypresses will move the focus to the previous item in the list.</dd>
				<dt>ENTER</dt>
				<dd>If there is an option selected, the textbox value is set and the listbox is closed.</dd>
				<dt>ESCAPE</dt>
				<dd>Clears the textbox and closes the listbox if it's displayed.</dd>				
			</dl>
		<h3 class="" id="title-notes">Notes (3 of 3):</h3>	
			<ol aria-labelledby="title-notes">
				<li>I'm still seeing some strangeness from the use of the native and listbox, so I'll have to see if I can consistently control that. I haven't quite pinned down the precise conditions under which the observed behavior can be replicated.</li>
				<li>Clicking inside of the list will give you different results than pressing the down arrow. this might be a browser extension I have installed or a browser setting I have turned on to remember my past choices for things?</li>
		</ol>	
		

	</main>

	<footer role="contentinfo" class="footer">
		
	</footer>
</div>
              
            
!

CSS

              
                $height-header: 75px;
$height-footer: 50px;
$sans-serif: 'Roboto', sans-serif;
$black: #212121;

//set up the page
*, *:after {
	box-sizing: border-box;
}
body {
	font-family: $sans-serif;
	font-size: 16px;
	font-weight: normal;
	line-height: 1.618;
	margin: 0 auto;
}
//let's try a little CSS grid.
.header {
	grid-area: header;
	height: $height-header;
	width: 100%;
	background-color: rgb(0, 14, 65);
	color: rgb(242, 241, 239);
	display: flex;
	align-items: center;
	h1 {
		width: 100%;
		font-family: 'Merienda', serif;
		padding: 0 0.5em;
	}

}
.content {
	grid-area: content;
	position: relative;
	min-height: calc(100vh - 145px);	
	max-width: 100%;
	padding: 1em;
}
.footer {
	grid-area: footer;
	height: $height-footer;
}

.wrapper {
	display: grid;
	grid-gap: 10px;
	grid-template-columns: 1fr;
	grid-template-areas:
		"header header"
		"content content"
		"footer footer";
	min-height: 100vh;
}

// form
.form {
	border: 1px solid lightgray;
	max-width: 500px;
	margin: 0 auto;
	padding: 1em;
	
}
.hidden {
	display: none;
}

.combobox-wrapper {
	display: inline-block;
	position: relative;
	font-size: 1em;
}

.combobox-label {
	font-size: 0.9em;
	font-weight: bold;
	display: block;
}
.form-input {
	font-family: $sans-serif;
	font-size: 1em;
}

.listbox {
	background: white;
	border: 1px solid #ccc;
	font-family: $sans-serif;
	font-size: 0.9em;
	list-style: none;
	margin: 0;
	width:100%;
	padding: 0;
	position: absolute;
	top: 1.7em;
	z-index: 1;
}

.listbox .result {
	cursor: default;
	margin: 0;
	padding: 0.25em;
}
.focused {
	background-color: lightgray;
}


.combobox-wrapper input {
	font-size: inherit;
	border: 1px solid #aaa;
	border-radius: 2px;
	line-height: 1.5em;
	padding-right: 30px;
	width: 200px;
}

.combobox-dropdown {
	position: absolute;
	right: 0;
	top: 0;
	padding: 1px 8px 0;
	height: 1.6em;
	border-radius: 0 2px 2px 0;
	border: 1px solid transparent;
}


//other stuff
.text-center {
	text-align: center;
}
hr {
	margin: 1.618em;
}
dt {
	font-weight: bold;
	font-size: 0.9em;
	letter-spacing: 1px;
}
dd {
	margin-bottom: 1em;
}
              
            
!

JS

              
                /* ARIA Combobox Examples */
var SUPERPOWERS = [
  'Telekinesis',
  'Teleportation',
  'Physical Strength',
	'Flight',
	'Speed',
	'Immortality'  
];

function searchSuperpowers (searchString) {
  var results = [];

  for (var i = 0; i < SUPERPOWERS.length; i++) {
    var superpower = SUPERPOWERS[i].toLowerCase();
    if (superpower.indexOf(searchString.toLowerCase()) === 0) {
      results.push(SUPERPOWERS[i]);
    }
  }

  return results;
}

/**
 * @function onload
 * @desc Initialize the combobox examples once the page has loaded
 */
window.addEventListener('load', function () {
  var Combobox = new aria.ListboxCombobox(
    document.getElementById('sp-combobox'),
    document.getElementById('sp-input'),
    document.getElementById('sp-listbox'),
    searchSuperpowers,
    true,
    function () {
      // on show
      document.getElementById('sp-combobox-arrow')
        .setAttribute('aria-label', 'Hide superpower options');
    },
    function () {
      // on hide
      document.getElementById('sp-combobox-arrow')
        .setAttribute('aria-label', 'Show superpower options');
    }
  );

  document.getElementById('sp-combobox-arrow').addEventListener(
    'click',
    function () {
      if (Combobox.shown) {
        document.getElementById('sp-input').focus();
        Combobox.hideListbox();
      }
      else {
        document.getElementById('sp-input').focus();
        Combobox.updateResults(true);
      }
    }
  );
});
aria.ListboxCombobox = function (
  comboboxNode,
  input,
  listbox,
  searchFn,
  shouldAutoSelect,
  onShow,
  onHide
) {
  this.combobox = comboboxNode;
  this.input = input;
  this.listbox = listbox;
  this.searchFn = searchFn;
  this.shouldAutoSelect = shouldAutoSelect;
  this.onShow = onShow || function () {};
  this.onHide = onHide || function () {};
  this.activeIndex = -1;
  this.resultsCount = 0;
  this.shown = false;
  this.hasInlineAutocomplete =
    input.getAttribute('aria-autocomplete') === 'both';

  this.setupEvents();
};

aria.ListboxCombobox.prototype.setupEvents = function () {
  document.body.addEventListener('click', this.checkHide.bind(this));
  this.input.addEventListener('keyup', this.checkKey.bind(this));
  this.input.addEventListener('keydown', this.setActiveItem.bind(this));
  this.input.addEventListener('focus', this.checkShow.bind(this));
  this.input.addEventListener('blur', this.checkSelection.bind(this));
  this.listbox.addEventListener('click', this.clickItem.bind(this));
};

aria.ListboxCombobox.prototype.checkKey = function (evt) {
  var key = evt.which || evt.keyCode;

  switch (key) {
    case aria.KeyCode.UP:
    case aria.KeyCode.DOWN:
    case aria.KeyCode.ESC:
    case aria.KeyCode.RETURN:
      evt.preventDefault();
      return;
    default:
      this.updateResults(false);
  }

  if (this.hasInlineAutocomplete) {
    switch (key) {
      case aria.KeyCode.BACKSPACE:
        return;
      default:
        this.autocompleteItem();
    }
  }
};

aria.ListboxCombobox.prototype.updateResults = function (shouldShowAll) {
  var searchString = this.input.value;
  var results = this.searchFn(searchString);

  this.hideListbox();

  if (!shouldShowAll && !searchString) {
    results = [];
  }

  if (results.length) {
    for (var i = 0; i < results.length; i++) {
      var resultItem = document.createElement('li');
      resultItem.className = 'result';
      resultItem.setAttribute('role', 'option');
      resultItem.setAttribute('id', 'result-item-' + i);
      resultItem.innerText = results[i];
      if (this.shouldAutoSelect && i === 0) {
        resultItem.setAttribute('aria-selected', 'true');
        aria.Utils.addClass(resultItem, 'focused');
        this.activeIndex = 0;
      }
      this.listbox.appendChild(resultItem);
    }
    aria.Utils.removeClass(this.listbox, 'hidden');
    this.combobox.setAttribute('aria-expanded', 'true');
    this.resultsCount = results.length;
    this.shown = true;
    this.onShow();
  }
};

aria.ListboxCombobox.prototype.setActiveItem = function (evt) {
  var key = evt.which || evt.keyCode;
  var activeIndex = this.activeIndex;

  if (key === aria.KeyCode.ESC) {
    this.hideListbox();
    setTimeout((function () {
      // On Firefox, input does not get cleared here unless wrapped in
      // a setTimeout
      this.input.value = '';
    }).bind(this), 1);
    return;
  }

  if (this.resultsCount < 1) {
    if (this.hasInlineAutocomplete && (key === aria.KeyCode.DOWN || key === aria.KeyCode.UP)) {
      this.updateResults(true);
    }
    else {
      return;
    }
  }

  var prevActive = this.getItemAt(activeIndex);
  var activeItem;

  switch (key) {
    case aria.KeyCode.UP:
      if (activeIndex <= 0) {
        activeIndex = this.resultsCount - 1;
      }
      else {
        activeIndex--;
      }
      break;
    case aria.KeyCode.DOWN:
      if (activeIndex === -1 || activeIndex >= this.resultsCount - 1) {
        activeIndex = 0;
      }
      else {
        activeIndex++;
      }
      break;
    case aria.KeyCode.RETURN:
      activeItem = this.getItemAt(activeIndex);
      this.selectItem(activeItem);
      return;
    case aria.KeyCode.TAB:
      this.checkSelection();
      this.hideListbox();
      return;
    default:
      return;
  }

  evt.preventDefault();

  activeItem = this.getItemAt(activeIndex);
  this.activeIndex = activeIndex;

  if (prevActive) {
    aria.Utils.removeClass(prevActive, 'focused');
    prevActive.setAttribute('aria-selected', 'false');
  }

  if (activeItem) {
    this.input.setAttribute(
      'aria-activedescendant',
      'result-item-' + activeIndex
    );
    aria.Utils.addClass(activeItem, 'focused');
    activeItem.setAttribute('aria-selected', 'true');
    if (this.hasInlineAutocomplete) {
      this.input.value = activeItem.innerText;
    }
  }
  else {
    this.input.setAttribute(
      'aria-activedescendant',
      ''
    );
  }
};

aria.ListboxCombobox.prototype.getItemAt = function (index) {
  return document.getElementById('result-item-' + index);
};

aria.ListboxCombobox.prototype.clickItem = function (evt) {
  if (evt.target && evt.target.nodeName == 'LI') {
    this.selectItem(evt.target);
  }
};

aria.ListboxCombobox.prototype.selectItem = function (item) {
  if (item) {
    this.input.value = item.innerText;
    this.hideListbox();
  }
};

aria.ListboxCombobox.prototype.checkShow = function (evt) {
  this.updateResults(false);
};

aria.ListboxCombobox.prototype.checkHide = function (evt) {
  if (evt.target === this.input || this.combobox.contains(evt.target)) {
    return;
  }
  this.hideListbox();
};

aria.ListboxCombobox.prototype.hideListbox = function () {
  this.shown = false;
  this.activeIndex = -1;
  this.listbox.innerHTML = '';
  aria.Utils.addClass(this.listbox, 'hidden');
  this.combobox.setAttribute('aria-expanded', 'false');
  this.resultsCount = 0;
  this.input.setAttribute(
    'aria-activedescendant',
    ''
  );
  this.onHide();
};

aria.ListboxCombobox.prototype.checkSelection = function () {
  if (this.activeIndex < 0) {
    return;
  }
  var activeItem = this.getItemAt(this.activeIndex);
  this.selectItem(activeItem);
};

aria.ListboxCombobox.prototype.autocompleteItem = function () {
  var autocompletedItem = this.listbox.querySelector('.focused');
  var inputText = this.input.value;

  if (!autocompletedItem || !inputText) {
    return;
  }

  var autocomplete = autocompletedItem.innerText;
  if (inputText !== autocomplete) {
    this.input.value = autocomplete;
    this.input.setSelectionRange(inputText.length, autocomplete.length);
  }
};
              
            
!
999px

Console