-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Open
Enhancement
Copy link
Labels
Affects: PerformanceRelates to the speed, size, or efficiency of our codeRelates to the speed, size, or efficiency of our codeContext: JavaScriptIssue is in JavaScriptIssue is in JavaScriptNeeds: RefinementWe need to give this issue more detailWe need to give this issue more detailPackage: Combo BoxType: SpikeA short research activity to help make a decisionA short research activity to help make a decision
Description
Describe the bug
Every time the combo box list is opened, the JS rebuilds every li
element within the unordered list whether there has been a change to the results or not.
Steps to reproduce the bug
- Go to Combo Box
- Inspect HTML - At the start you see an empty
ul
element - Open the combo box drop down
li
elements are appended to the DOM- While viewing the li elements in the devTools window, close and reopen the combo box
li
elements flash because they are removed and re-appended to the DOM
Expected Behavior
li
elements should only be removed and added to the DOM when there is a change to the list or required attributes.
Related code
Code that builds the options HTML:
uswds/packages/usa-combo-box/src/index.js
Lines 409 to 439 in f0684a3
const optionHtml = options.map((option, index) => { | |
const optionId = `${listOptionBaseId}${index}`; | |
const classes = [LIST_OPTION_CLASS]; | |
let tabindex = "-1"; | |
let ariaSelected = "false"; | |
if (optionId === selectedItemId) { | |
classes.push(LIST_OPTION_SELECTED_CLASS, LIST_OPTION_FOCUSED_CLASS); | |
tabindex = "0"; | |
ariaSelected = "true"; | |
} | |
if (!selectedItemId && index === 0) { | |
classes.push(LIST_OPTION_FOCUSED_CLASS); | |
tabindex = "0"; | |
} | |
const li = document.createElement("li"); | |
li.setAttribute("aria-setsize", options.length); | |
li.setAttribute("aria-posinset", index + 1); | |
li.setAttribute("aria-selected", ariaSelected); | |
li.setAttribute("id", optionId); | |
li.setAttribute("class", classes.join(" ")); | |
li.setAttribute("tabindex", tabindex); | |
li.setAttribute("role", "option"); | |
li.setAttribute("data-value", option.value); | |
li.textContent = option.text; | |
return li; | |
}); |
Code that determines when to append the HTML:
uswds/packages/usa-combo-box/src/index.js
Lines 447 to 455 in f0684a3
if (numOptions) { | |
listEl.innerHTML = ""; | |
optionHtml.forEach((item) => | |
listEl.insertAdjacentElement("beforeend", item), | |
); | |
} else { | |
listEl.innerHTML = ""; | |
listEl.insertAdjacentElement("beforeend", noResults); | |
} |
Screenshots
Recording of this behavior:
Kapture.2024-11-08.at.15.26.38.webm
System setup
- USWDS Version:
3.9.0
- Operating System: MacOS Sonoma 14.6.1
- Browser: Arc (chromium)
Additional context
No response
Code of Conduct
- I agree to follow this project's Code of Conduct.
- I checked the current issues for duplicate bug reports.
Metadata
Metadata
Assignees
Labels
Affects: PerformanceRelates to the speed, size, or efficiency of our codeRelates to the speed, size, or efficiency of our codeContext: JavaScriptIssue is in JavaScriptIssue is in JavaScriptNeeds: RefinementWe need to give this issue more detailWe need to give this issue more detailPackage: Combo BoxType: SpikeA short research activity to help make a decisionA short research activity to help make a decision
Type
Projects
Status
Backlog