Select2

Overview

Select2 (v4.1.0-rc.0) replaces native <select> elements with a styled, accessible dropdown that supports search filtering and multi-select. It is initialized via the shared js/components/selects.js utility.

Standard usage

Wrap a native <select> in a .c-select container:

Handlebars
1<div class="c-select"> 2 <select name="{{properties.field_name}}"> 3 {{#each properties.options}} 4 <option value="{{this.value}}">{{this.label}}</option> 5 {{/each}} 6 </select> 7</div>

The shared utility automatically initializes Select2 on all .c-select select elements when the page loads.

Variants

SelectorBehaviorNotes
.c-select selectStandard dropdown; search hiddenDefault for all styled selects
.event-form-selectShows search field after 5 resultsEvent filter forms
.mktoForm selectAuto-wrapped in .c-select on initMarketo form fields; 1s timeout on init
.c-lang-switcher selectLanguage switcher; no searchUses custom .c-lang-switcher__dropdown CSS class
.c-provider-finder__select selectProvider finder filter; no searchProvider search pages
.c-provider-finder__select--treatment selectMulti-select with selection counterReads CYNO_DATA.filter_labels for i18n strings
.c-provider-finder__select--product selectMulti-select with selection counterReads CYNO_DATA.filter_labels for i18n strings
.c-select--treatment selectFooter multi-select with counterFooter "Find a Provider" form
.c-select--product selectFooter multi-select with counterFooter "Find a Provider" form

Importing the utility

In your block's script.js:

JavaScript
1/** 2 * CTA Provider Block 3 * 4 * Provider search form with filterable select dropdowns. 5 * 6 * @shared selects 7 * Trigger: .c-select select 8 * Behavior: Select2 styled dropdown; search hidden (minimumResultsForSearch: Infinity) 9 * 10 * @see /guidelines/javascript/selects 11 * @see /guidelines/javascript/shared-utilities 12 */ 13import '../../js/components/selects.js';

i18n strings

Multi-select variants use CYNO_DATA.filter_labels for user-facing strings. This object is injected by WordPress via wp_localize_script. Available keys:

JavaScript
1CYNO_DATA.filter_labels = { 2 any: "Any", 3 selected: "selected", 4 select_treatment_any: "Select treatment (any)", 5 select_product_any: "Select product (any)", 6};

Accessibility notes

  • Select2 generates ARIA attributes (role="listbox", aria-selected, aria-activedescendant) automatically.
  • Keyboard navigation (arrow keys, Enter, Escape) is built in.
  • Do not hide the native <select> with display: none before Select2 initializes — Select2 handles this itself.
  • The .c-select__dropdown CSS class is applied to the dropdown container to allow scoped styling.

Blocks that use selects.js

cta-provider, download, downloads-columns, downloads-list, downloads-split, events-upcoming-list-sidebar, footer, grid-three-column, grid-three-column-nav, hero-contact, hero-news, hero-search-provider, posts-latest