Whether a component is authored in plain JavaScript or with React, the browser ultimately receives the same HTML. The interesting question is what each approach has to do to get there. Tabs and accordions are a useful case study because they share enough behavior to be handled by one script, yet they differ in a crucial way: a tabset is always mutually exclusive, while an accordion may show several panels at once.
The examples below start from minimal server-rendered markup, let JavaScript add the ARIA attributes and interaction, and take the same functionality into React. The markup uses id="TABS_ID" and id="ACCORDION_ID" to make the transformations visible, but the code generates unique IDs when none are supplied.

Before and after: the markup
The base DOM is deliberately sparse. Both widgets receive their role and aria-* attributes at runtime, so compare the initial structure with the enriched version.
Tabs
<div class="tabs" id="TABS_ID">
<ul class="tabs__list">
<li class="tabs__item">
Tab 1
</li>
<!-- .tabs__item -->
<li class="tabs__item">
Tab 2
</li>
<!-- .tabs__item -->
</ul>
<!-- .tabs__list -->
<div class="tabs__panel">
<p>
Tab 1 content
</p>
</div>
<!-- .tabs__panel -->
<div class="tabs__panel">
<p>
Tab 2 content
</p>
</div>
<!-- .tabs__panel -->
</div>
<!-- .tabs -->
<div class="tabs" id="TABS_ID">
<ul class="tabs__list" role="tablist">
<li
aria-controls="tabpanel_TABS_ID_0"
aria-selected="false"
class="tabs__item"
id="tab_TABS_ID_0"
role="tab"
tabindex="0"
>
Tab 1
</li>
<!-- .tabs__item -->
<li
aria-controls="tabpanel_TABS_ID_1"
aria-selected="true"
class="tabs__item"
id="tab_TABS_ID_1"
role="tab"
tabindex="0"
>
Tab 2
</li>
<!-- .tabs__item -->
</ul>
<!-- .tabs__list -->
<div
aria-hidden="true"
aria-labelledby="tab_TABS_ID_0"
class="tabs__panel"
id="tabpanel_TABS_ID_0"
role="tabpanel"
>
<p>
Tab 1 content
</p>
</div>
<!-- .tabs__panel -->
<div
aria-hidden="false"
aria-labelledby="tab_TABS_ID_1"
class="tabs__panel"
id="tabpanel_TABS_ID_1"
role="tabpanel"
>
<p>
Tab 2 content
</p>
</div>
<!-- .tabs__panel -->
</div>
<!-- .tabs -->
Accordion
<div class="accordion" id="ACCORDION_ID">
<div class="accordion__item">
Tab 1
</div>
<!-- .accordion__item -->
<div class="accordion__panel">
<p>
Tab 1 content
</p>
</div>
<!-- .accordion__panel -->
<div class="accordion__item">
Tab 2
</div>
<!-- .accordion__item -->
<div class="accordion__panel">
<p>
Tab 2 content
</p>
</div>
<!-- .accordion__panel -->
</div>
<!-- .accordion -->
<div
aria-multiselectable="true"
class="accordion"
id="ACCORDION_ID"
role="tablist"
>
<div
aria-controls="tabpanel_ACCORDION_ID_0"
aria-selected="true"
class="accordion__item"
id="tab_ACCORDION_ID_0"
role="tab"
tabindex="0"
>
<i aria-hidden="true" class="accordion__item__icon"></i>
Tab 1
</div>
<!-- .accordion__item -->
<div
aria-hidden="false"
aria-labelledby="tab_ACCORDION_ID_0"
class="accordion__panel"
id="tabpanel_ACCORDION_ID_0"
role="tabpanel"
>
<p>
Tab 1 content
</p>
</div>
<!-- .accordion__panel -->
<div
aria-controls="tabpanel_ACCORDION_ID_1"
aria-selected="false"
class="accordion__item"
id="tab_ACCORDION_ID_1"
role="tab"
tabindex="0"
>
<i aria-hidden="true" class="accordion__item__icon"></i>
Tab 2
</div>
<!-- .accordion__item -->
<div
aria-hidden="true"
aria-labelledby="tab_ACCORDION_ID_1"
class="accordion__panel"
id="tabpanel_ACCORDION_ID_1"
role="tabpanel"
>
<p>
Tab 2 content
</p>
</div>
<!-- .accordion__panel -->
</div>
<!-- .accordion -->
Vanilla JavaScript: the helpers
Two small utilities keep the main logic readable.
getDomFallback.js
Live DOM changes can be volatile, so this returns an object whose DOM properties and methods are no-ops. It removes the need for repeated typeof foo.getAttribute style checks and keeps a bad lookup from throwing and halting the rest of the page.
/*
Helper to mock DOM methods, for
when an element might not exist.
*/
const getDomFallback = () => {
return {
// Props.
children: [],
className: '',
classList: {
contains: () => false,
},
id: '',
innerHTML: '',
name: '',
nextSibling: null,
previousSibling: null,
outerHTML: '',
tagName: '',
textContent: '',
// Methods.
appendChild: () => Object.create(null),
blur: () => undefined,
click: () => undefined,
cloneNode: () => Object.create(null),
closest: () => null,
createElement: () => Object.create(null),
focus: () => undefined,
getAttribute: () => null,
hasAttribute: () => false,
insertAdjacentElement: () => Object.create(null),
insertBefore: () => Object.create(null),
querySelector: () => null,
querySelectorAll: () => [],
removeAttribute: () => undefined,
removeChild: () => Object.create(null),
replaceChild: () => Object.create(null),
setAttribute: () => undefined,
};
};
// Export.
export { getDomFallback };
unique.js
A poor man's UUID. It generates strings so page authors need not hand-assign unique IDs to every widget; the example TABS_ID and ACCORDION_ID placeholders are where these values normally land. Larger projects could npm install uuid, but two lightly edited Math.random() numbers concatenated suffice here, with no cryptographic requirement.
// ==========
// Constants.
// ==========
const BEFORE = '0.';
const AFTER = '';
// ==================
// Get unique string.
// ==================
const unique = () => {
// Get prefix.
let prefix = Math.random();
prefix = String(prefix);
prefix = prefix.replace(BEFORE, AFTER);
// Get suffix.
let suffix = Math.random();
suffix = String(suffix);
suffix = suffix.replace(BEFORE, AFTER);
// Expose string.
return `${prefix}_${suffix}`;
};
// Export.
export { unique };
Vanilla JavaScript: the core file
tablist.js carries most of the work and drives both tabs and accordion from a single file.
// Helpers.
import { getDomFallback } from './getDomFallback';
import { unique } from './unique';
// ==========
// Constants.
// ==========
// Boolean strings.
const TRUE = 'true';
const FALSE = 'false';
// ARIA strings.
const ARIA_CONTROLS = 'aria-controls';
const ARIA_LABELLEDBY = 'aria-labelledby';
const ARIA_HIDDEN = 'aria-hidden';
const ARIA_MULTISELECTABLE = 'aria-multiselectable';
const ARIA_ORIENTATION = 'aria-orientation';
const ARIA_SELECTED = 'aria-selected';
// Attribute strings.
const DATA_INDEX = 'data-index';
const HORIZONTAL = 'horizontal';
const ID = 'id';
const ROLE = 'role';
const TABINDEX = 'tabindex';
const TABLIST = 'tablist';
const VERTICAL = 'vertical';
// Event strings.
const AFTER_BEGIN = 'afterbegin';
const ARROW_LEFT = 'arrowleft';
const ARROW_RIGHT = 'arrowright';
const CLICK = 'click';
const KEYDOWN = 'keydown';
// Key strings.
const ENTER = 'enter';
const FUNCTION = 'function';
const SPACE = ' ';
// Tag strings.
const I = 'i';
const LI = 'li';
// Selector strings.
const ACCORDION_ITEM_ICON = 'accordion__item__icon';
const ACCORDION_ITEM_ICON_SELECTOR = `.${ACCORDION_ITEM_ICON}`;
const TAB = 'tab';
const TAB_SELECTOR = `[${ROLE}=${TAB}]`;
const TABPANEL = 'tabpanel';
const TABPANEL_SELECTOR = `[${ROLE}=${TABPANEL}]`;
const ACCORDION = 'accordion';
const TABLIST_CLASS_SELECTOR = '.accordion, .tabs';
const TAB_CLASS_SELECTOR = '.accordion__item, .tabs__item';
const TABPANEL_CLASS_SELECTOR = '.accordion__panel, .tabs__panel';
// ===========
// Get tab ID.
// ===========
const getTabId = (id = '', index = 0) => {
return `${TAB}_${id}_${index}`;
};
// =============
// Get panel ID.
// =============
const getPanelId = (id = '', index = 0) => {
return `${TABPANEL}_${id}_${index}`;
};
// ==============
// Click handler.
// ==============
const globalClick = (event = {}) => {
// Get target.
const { target = getDomFallback() } = event;
// Get key.
let { key = '' } = event;
key = key.toLowerCase();
// Key events.
const isArrowLeft = key === ARROW_LEFT;
const isArrowRight = key === ARROW_RIGHT;
const isArrowKey = isArrowLeft || isArrowRight;
const isTriggerKey = key === ENTER || key === SPACE;
// Get parent.
const { parentNode = getDomFallback(), tagName = '' } = target;
// Set later.
let wrapper = getDomFallback();
/*
=====
NOTE:
=====
We test for this, because the method does
not exist on `document.documentElement`.
*/
if (typeof target.closest === FUNCTION) {
// Get wrapper.
wrapper = target.closest(TABLIST_CLASS_SELECTOR) || getDomFallback();
}
// Is multi?
const isMulti = wrapper.getAttribute(ARIA_MULTISELECTABLE) === TRUE;
// Valid target?
const isValidTarget =
target.getAttribute(ROLE) === TAB && parentNode.getAttribute(ROLE) === TABLIST;
// Is `<li>`?
const isListItem = isValidTarget && tagName.toLowerCase() === LI;
// Valid event?
const isArrowEvent = isListItem && isArrowKey;
const isTriggerEvent = isValidTarget && (!key || isTriggerKey);
const isValidEvent = isArrowEvent || isTriggerEvent;
// Prevent default.
if (isValidEvent) {
event.preventDefault();
}
// ============
// Arrow event?
// ============
if (isArrowEvent) {
// Get index.
let index = target.getAttribute(DATA_INDEX);
index = parseFloat(index);
// Get list.
const list = wrapper.querySelectorAll(TAB_SELECTOR);
// Set later.
let newIndex = null;
let nextItem = null;
// Arrow left?
if (isArrowLeft) {
newIndex = index - 1;
nextItem = list[newIndex];
if (!nextItem) {
newIndex = list.length - 1;
nextItem = list[newIndex];
}
}
// Arrow right?
if (isArrowRight) {
newIndex = index + 1;
nextItem = list[newIndex];
if (!nextItem) {
newIndex = 0;
nextItem = list[newIndex];
}
}
// Fallback?
nextItem = nextItem || getDomFallback();
// Focus new item.
nextItem.click();
nextItem.focus();
}
// ==============
// Trigger event?
// ==============
if (isTriggerEvent) {
// Get panel.
const panelId = target.getAttribute(ARIA_CONTROLS);
const panel = wrapper.querySelector(`#${panelId}`) || getDomFallback();
// Get booleans.
let boolPanel = panel.getAttribute(ARIA_HIDDEN) !== TRUE;
let boolTab = target.getAttribute(ARIA_SELECTED) !== TRUE;
// List item?
if (isListItem) {
boolPanel = FALSE;
boolTab = TRUE;
}
// [aria-multiselectable="false"]
if (!isMulti) {
// Get tabs & panels.
const childTabs = wrapper.querySelectorAll(TAB_SELECTOR);
const childPanels = wrapper.querySelectorAll(TABPANEL_SELECTOR);
// Loop through tabs.
childTabs.forEach((tab = getDomFallback()) => {
tab.setAttribute(ARIA_SELECTED, FALSE);
// li[tabindex="-1"]
if (isListItem) {
tab.setAttribute(TABINDEX, -1);
}
});
// Loop through panels.
childPanels.forEach((panel = getDomFallback()) => {
panel.setAttribute(ARIA_HIDDEN, TRUE);
});
}
// Set individual tab.
target.setAttribute(ARIA_SELECTED, boolTab);
// li[tabindex="0"]
if (isListItem) {
target.setAttribute(TABINDEX, 0);
}
// Set individual panel.
panel.setAttribute(ARIA_HIDDEN, boolPanel);
}
};
// ====================
// Add ARIA attributes.
// ====================
const addAriaAttributes = () => {
// Get elements.
const allWrappers = document.querySelectorAll(TABLIST_CLASS_SELECTOR);
// Loop through.
allWrappers.forEach((wrapper = getDomFallback()) => {
// Get attributes.
const { id = '', classList } = wrapper;
const parentId = id || unique();
// Is accordion?
const isAccordion = classList.contains(ACCORDION);
// Get tabs & panels.
const childTabs = wrapper.querySelectorAll(TAB_CLASS_SELECTOR);
const childPanels = wrapper.querySelectorAll(TABPANEL_CLASS_SELECTOR);
// Add ID?
if (!wrapper.getAttribute(ID)) {
wrapper.setAttribute(ID, parentId);
}
// [aria-multiselectable="true"]
if (isAccordion && wrapper.getAttribute(ARIA_MULTISELECTABLE) !== FALSE) {
wrapper.setAttribute(ARIA_MULTISELECTABLE, TRUE);
}
// ===========================
// Loop through tabs & panels.
// ===========================
for (let index = 0; index < childTabs.length; index++) {
// Get elements.
const tab = childTabs[index] || getDomFallback();
const panel = childPanels[index] || getDomFallback();
// Get IDs.
const tabId = getTabId(parentId, index);
const panelId = getPanelId(parentId, index);
// ===================
// Add tab attributes.
// ===================
// Tab: add icon?
if (isAccordion) {
// Get icon.
let icon = tab.querySelector(ACCORDION_ITEM_ICON_SELECTOR);
// Create icon?
if (!icon) {
icon = document.createElement(I);
icon.className = ACCORDION_ITEM_ICON;
tab.insertAdjacentElement(AFTER_BEGIN, icon);
}
// [aria-hidden="true"]
icon.setAttribute(ARIA_HIDDEN, TRUE);
}
// Tab: add id?
if (!tab.getAttribute(ID)) {
tab.setAttribute(ID, tabId);
}
// Tab: add controls?
if (!tab.getAttribute(ARIA_CONTROLS)) {
tab.setAttribute(ARIA_CONTROLS, panelId);
}
// Tab: add selected?
if (!tab.getAttribute(ARIA_SELECTED)) {
const bool = !isAccordion && index === 0;
tab.setAttribute(ARIA_SELECTED, bool);
}
// Tab: add role?
if (tab.getAttribute(ROLE) !== TAB) {
tab.setAttribute(ROLE, TAB);
}
// Tab: add data index?
if (!tab.getAttribute(DATA_INDEX)) {
tab.setAttribute(DATA_INDEX, index);
}
// Tab: add tabindex?
if (!tab.getAttribute(TABINDEX)) {
if (isAccordion) {
tab.setAttribute(TABINDEX, 0);
} else {
tab.setAttribute(TABINDEX, index === 0 ? 0 : -1);
}
}
// Tab: first item?
if (index === 0) {
// Get parent.
const { parentNode = getDomFallback() } = tab;
/*
We do this here, instead of outside the loop.
The top level item isn't always the `tablist`.
The accordion UI only has `<div>`, whereas
the tabs UI has both `<div>` and `<ul>`.
*/
if (parentNode.getAttribute(ROLE) !== TABLIST) {
parentNode.setAttribute(ROLE, TABLIST);
}
// Accordion?
if (isAccordion) {
// [aria-orientation="vertical"]
if (parentNode.getAttribute(ARIA_ORIENTATION) !== VERTICAL) {
parentNode.setAttribute(ARIA_ORIENTATION, VERTICAL);
}
// Tabs?
} else {
// [aria-orientation="horizontal"]
if (parentNode.getAttribute(ARIA_ORIENTATION) !== HORIZONTAL) {
parentNode.setAttribute(ARIA_ORIENTATION, HORIZONTAL);
}
}
}
// =====================
// Add panel attributes.
// =====================
// Panel: add ID?
if (!panel.getAttribute(ID)) {
panel.setAttribute(ID, panelId);
}
// Panel: add hidden?
if (!panel.getAttribute(ARIA_HIDDEN)) {
const bool = isAccordion || index !== 0;
panel.setAttribute(ARIA_HIDDEN, bool);
}
// Panel: add labelled?
if (!panel.getAttribute(ARIA_LABELLEDBY)) {
panel.setAttribute(ARIA_LABELLEDBY, tabId);
}
// Panel: add role?
if (panel.getAttribute(ROLE) !== TABPANEL) {
panel.setAttribute(ROLE, TABPANEL);
}
// Panel: add tabindex?
if (!panel.getAttribute(TABINDEX)) {
panel.setAttribute(TABINDEX, 0);
}
}
});
};
// =====================
// Remove global events.
// =====================
const unbind = () => {
document.removeEventListener(CLICK, globalClick);
document.removeEventListener(KEYDOWN, globalClick);
};
// ==================
// Add global events.
// ==================
const init = () => {
// Add attributes.
addAriaAttributes();
// Prevent doubles.
unbind();
document.addEventListener(CLICK, globalClick);
document.addEventListener(KEYDOWN, globalClick);
};
// ==============
// Bundle object.
// ==============
const tablist = {
init,
unbind,
};
// =======
// Export.
// =======
export { tablist };
Pairing elements by ID
getTabId and getPanelId derive unique IDs for each item in a loop from the existing (or generated) parent ID. Those IDs fill the matching aria-controls="…" and aria-labelledby="…" attributes, which tell assistive technology which elements belong together, much as <label for="…"> does.
const getTabId = (id = '', index = 0) => {
return `${TAB}_${id}_${index}`;
};
const getPanelId = (id = '', index = 0) => {
return `${TABPANEL}_${id}_${index}`;
};
Delegated events: globalClick
A single handler on document listens for clicks through event bubbling, and the same listener covers the ArrowLeft, ArrowRight, Enter, and spacebar keys that an accessible UI requires.
The handler destructures target and key from the event, then parentNode and tagName from the target. It searches upward for the wrapper with class="tabs" or class="accordion", guarding with a typeof check on *.closest(…) and falling back through getDomFallback when nothing matches. It records whether the wrapper has aria-multiselectable="true" and whether the clicked tag is an <li>, both needed later.
Because the click could have landed anywhere, the handler verifies the event is relevant: the target must carry role="tab" and its parent must carry role="tablist". Focus can point elsewhere, so the check also includes the document.activeElement ancestor. The resulting booleans are combined into isArrowEvent, isTriggerEvent, and finally isValidEvent, with a second guard covering a parent whose role is temporary.
// Get target.
const { target = getDomFallback() } = event;
// Get key.
let { key = '' } = event;
key = key.toLowerCase();
// Key events.
const isArrowLeft = key === ARROW_LEFT;
const isArrowRight = key === ARROW_RIGHT;
const isArrowKey = isArrowLeft || isArrowRight;
const isTriggerKey = key === ENTER || key === SPACE;
// Get parent.
const { parentNode = getDomFallback(), tagName = '' } = target;
// Set later.
let wrapper = getDomFallback();
/*
=====
NOTE:
=====
We test for this, because the method does
not exist on `document.documentElement`.
*/
if (typeof target.closest === FUNCTION) {
// Get wrapper.
wrapper = target.closest(TABLIST_CLASS_SELECTOR) || getDomFallback();
}
// Is multi?
const isMulti = wrapper.getAttribute(ARIA_MULTISELECTABLE) === TRUE;
// Valid target?
const isValidTarget =
target.getAttribute(ROLE) === TAB && parentNode.getAttribute(ROLE) === TABLIST;
// Is `<li>`?
const isListItem = isValidTarget && tagName.toLowerCase() === LI;
// Valid event?
const isArrowEvent = isListItem && isArrowKey;
const isTriggerEvent = isValidTarget && (!key || isTriggerKey);
const isValidEvent = isArrowEvent || isTriggerEvent;
// Prevent default.
if (isValidEvent) {
event.preventDefault();
}
On a left or right arrow press, focus moves to the adjacent tab, wrapping from the last tab to the first and from the first to the last. The handler dispatches the next event directly rather than synthesizing a target. The selected panel is exposed through an aria-controls lookup between the tablist and panels. If an edge case moves focus to another element, a click is triggered on that target so the same logic runs.
if (isArrowEvent) {
// Get index.
let index = target.getAttribute(DATA_INDEX);
index = parseFloat(index);
// Get list.
const list = wrapper.querySelectorAll(TAB_SELECTOR);
// Set later.
let newIndex = null;
let nextItem = null;
// Arrow left?
if (isArrowLeft) {
newIndex = index - 1;
nextItem = list[newIndex];
if (!nextItem) {
newIndex = list.length - 1;
nextItem = list[newIndex];
}
}
// Arrow right?
if (isArrowRight) {
newIndex = index + 1;
nextItem = list[newIndex];
if (!nextItem) {
newIndex = 0;
nextItem = list[newIndex];
}
}
// Fallback?
nextItem = nextItem || getDomFallback();
// Focus new item.
nextItem.click();
nextItem.focus();
}
Once the event is validated, the handler finds the role="tabpanel" element whose id matches the tab's aria-controls. It checks whether that panel is hidden and whether the tab is selected, initially flipping both booleans as if dealing with an accordion. The earlier isListItem flag settles the ambiguity: clicking an <li> means tabs, so the panel becomes visible (aria-hidden="false") and the tab selected (aria-selected="true"). If the wrapper lacks aria-multiselectable or sets it to "false", every neighboring role="tab" and role="tabpanel" element is reset to its inactive state before the flags are applied.
if (isTriggerEvent) {
// Get panel.
const panelId = target.getAttribute(ARIA_CONTROLS);
const panel = wrapper.querySelector(`#${panelId}`) || getDomFallback();
// Get booleans.
let boolPanel = panel.getAttribute(ARIA_HIDDEN) !== TRUE;
let boolTab = target.getAttribute(ARIA_SELECTED) !== TRUE;
// List item?
if (isListItem) {
boolPanel = FALSE;
boolTab = TRUE;
}
// [aria-multiselectable="false"]
if (!isMulti) {
// Get tabs & panels.
const childTabs = wrapper.querySelectorAll(TAB_SELECTOR);
const childPanels = wrapper.querySelectorAll(TABPANEL_SELECTOR);
// Loop through tabs.
childTabs.forEach((tab = getDomFallback()) => {
tab.setAttribute(ARIA_SELECTED, FALSE);
// li[tabindex="-1"]
if (isListItem) {
tab.setAttribute(TABINDEX, -1);
}
});
// Loop through panels.
childPanels.forEach((panel = getDomFallback()) => {
panel.setAttribute(ARIA_HIDDEN, TRUE);
});
}
// Set individual tab.
target.setAttribute(ARIA_SELECTED, boolTab);
// li[tabindex="0"]
if (isListItem) {
target.setAttribute(TABINDEX, 0);
}
// Set individual panel.
panel.setAttribute(ARIA_HIDDEN, boolPanel);
}
Adding the ARIA layer
The base markup is bare, so the fields globalClick probes do not exist yet. addAriaAttributes adds them, and in doing so defines both the accessibility semantics and the widget's behavior. Keying interaction off class="…" instead would let the visual design outrun the assistive experience.
The function collects every element with class="tabs" and/or class="accordion", exits if there are none, and processes each wrapper. It reads id and classList, calls unique() when no ID exists, and sets a flag marking accordions. It then selects the descendants: class="tabs__item" or class="accordion__item" as tabs, and class="tabs__panel" or class="accordion__panel" as panels.
An accordion missing aria-multiselectable="false" is switched to true: developers who choose the accordion paradigm while tabs remain available likely expect several panels to expand and collapse, so that is the safer default.
// Get elements.
const allWrappers = document.querySelectorAll(TABLIST_CLASS_SELECTOR);
// Loop through.
allWrappers.forEach((wrapper = getDomFallback()) => {
/*
NOTE: Cut, for brevity.
*/
});
// Get attributes.
const { id = '', classList } = wrapper;
const parentId = id || unique();
// Is accordion?
const isAccordion = classList.contains(ACCORDION);
// Get tabs & panels.
const childTabs = wrapper.querySelectorAll(TAB_CLASS_SELECTOR);
const childPanels = wrapper.querySelectorAll(TABPANEL_CLASS_SELECTOR);
// Add ID?
if (!wrapper.getAttribute(ID)) {
wrapper.setAttribute(ID, parentId);
}
// [aria-multiselectable="true"]
if (isAccordion && wrapper.getAttribute(ARIA_MULTISELECTABLE) !== FALSE) {
wrapper.setAttribute(ARIA_MULTISELECTABLE, TRUE);
}
Tabs and panels are walked in a single old-school for loop rather than *.forEach, because two NodeList instances of equal length can advance with one index. Each iteration derives IDs in the form tab_WRAPPER_ID_0 or tab_GENERATED_STRING_0, and tabpanel_WRAPPER_ID_0 or tabpanel_GENERATED_STRING_0, for later association.
for (let index = 0; index < childTabs.length; index++) {
// Get elements.
const tab = childTabs[index] || getDomFallback();
const panel = childPanels[index] || getDomFallback();
// Get IDs.
const tabId = getTabId(parentId, index);
const panelId = getPanelId(parentId, index);
/*
NOTE: Cut, for brevity.
*/
}
For each tab, the loop makes sure a decorative expand/collapse icon exists and marks it aria-hidden="true". It adds an id and an aria-controls="…" pointing at the generated panel ID if either is missing. If aria-selected is absent, and we are not in an accordion, and the index is 0, the first tab is marked selected, since tabbed content always shows at least one panel while an accordion may be fully collapsed. The loop sets role="tab", stores data-index="…" for keyboard navigation, and applies tabindex="0" or tabindex="-1": every accordion trigger takes keyboard :focus, while only the current tab does. On the first iteration it climbs to parentNode and adds role="tablist", which lands on the <ul> in the tabs markup and on the outer <div> for an accordion. It also sets aria-orientation, vertical for accordions and horizontal for tabs.
// Tab: add icon?
if (isAccordion) {
// Get icon.
let icon = tab.querySelector(ACCORDION_ITEM_ICON_SELECTOR);
// Create icon?
if (!icon) {
icon = document.createElement(I);
icon.className = ACCORDION_ITEM_ICON;
tab.insertAdjacentElement(AFTER_BEGIN, icon);
}
// [aria-hidden="true"]
icon.setAttribute(ARIA_HIDDEN, TRUE);
}
// Tab: add id?
if (!tab.getAttribute(ID)) {
tab.setAttribute(ID, tabId);
}
// Tab: add controls?
if (!tab.getAttribute(ARIA_CONTROLS)) {
tab.setAttribute(ARIA_CONTROLS, panelId);
}
// Tab: add selected?
if (!tab.getAttribute(ARIA_SELECTED)) {
const bool = !isAccordion && index === 0;
tab.setAttribute(ARIA_SELECTED, bool);
}
// Tab: add role?
if (tab.getAttribute(ROLE) !== TAB) {
tab.setAttribute(ROLE, TAB);
}
// Tab: add data index?
if (!tab.getAttribute(DATA_INDEX)) {
tab.setAttribute(DATA_INDEX, index);
}
// Tab: add tabindex?
if (!tab.getAttribute(TABINDEX)) {
if (isAccordion) {
tab.setAttribute(TABINDEX, 0);
} else {
tab.setAttribute(TABINDEX, index === 0 ? 0 : -1);
}
}
// Tab: first item?
if (index === 0) {
// Get parent.
const { parentNode = getDomFallback() } = tab;
/*
We do this here, instead of outside the loop.
The top level item isn't always the `tablist`.
The accordion UI only has `<div>`, whereas
the tabs UI has both `<div>` and `<ul>`.
*/
if (parentNode.getAttribute(ROLE) !== TABLIST) {
parentNode.setAttribute(ROLE, TABLIST);
}
// Accordion?
if (isAccordion) {
// [aria-orientation="vertical"]
if (parentNode.getAttribute(ARIA_ORIENTATION) !== VERTICAL) {
parentNode.setAttribute(ARIA_ORIENTATION, VERTICAL);
}
// Tabs?
} else {
// [aria-orientation="horizontal"]
if (parentNode.getAttribute(ARIA_ORIENTATION) !== HORIZONTAL) {
parentNode.setAttribute(ARIA_ORIENTATION, HORIZONTAL);
}
}
}
Each panel then receives an id if needed, an aria-hidden value of true or false depending on context, an aria-labelledby="…" pointing back to its trigger, role="tabpanel", and tabindex="0" so it can take :focus.
// Panel: add ID?
if (!panel.getAttribute(ID)) {
panel.setAttribute(ID, panelId);
}
// Panel: add hidden?
if (!panel.getAttribute(ARIA_HIDDEN)) {
const bool = isAccordion || index !== 0;
panel.setAttribute(ARIA_HIDDEN, bool);
}
// Panel: add labelled?
if (!panel.getAttribute(ARIA_LABELLEDBY)) {
panel.setAttribute(ARIA_LABELLEDBY, tabId);
}
// Panel: add role?
if (panel.getAttribute(ROLE) !== TABPANEL) {
panel.setAttribute(ROLE, TABPANEL);
}
// Panel: add tabindex?
if (!panel.getAttribute(TABINDEX)) {
panel.setAttribute(TABINDEX, 0);
}
Lifecycle
Setup and teardown sit at the end of the file. The exported unbind removes the global listeners, either on its own via tablist.unbind() or, more usually, ahead of rebinding so listeners never double up. init calls addAriaAttributes(), then unbind(), then attaches the document-level listeners. Both methods are bundled into an object exported as tablist, so a flat HTML page calls tablist.init() when it is time to enable the widgets.
// =====================
// Remove global events.
// =====================
const unbind = () => {
document.removeEventListener(CLICK, globalClick);
document.removeEventListener(KEYDOWN, globalClick);
};
// ==================
// Add global events.
// ==================
const init = () => {
// Add attributes.
addAriaAttributes();
// Prevent doubles.
unbind();
document.addEventListener(CLICK, globalClick);
document.addEventListener(KEYDOWN, globalClick);
};
// ==============
// Bundle object.
// ==============
const tablist = {
init,
unbind,
};
// =======
// Export.
// =======
export { tablist };
The React implementation
React removes the raw DOM plumbing and event binding, handling that layer automatically. The analysis in the previous blocks still matters, though, since it defines the accessibility semantics React must reproduce.
Tabs.js
By convention the file shares the name of its export default component. It opens with the same getTabId and getPanelId helpers, since the tabs-to-panels mapping must stay accessible.

// =============
// Used like so…
// =============
<Tabs>
<div label="Tab 1">
<p>
Tab 1 content
</p>
</div>
<div label="Tab 2">
<p>
Tab 2 content
</p>
</div>
</Tabs>
import React, { useState } from 'react';
import PropTypes from 'prop-types';
import { v4 as uuid } from 'uuid';
import cx from 'classnames';
// Helpers.
import { getDomFallback } from '../utils';
// UI.
import Render from './Render';
// ==========
// Constants.
// ==========
const ARROW_LEFT = 'arrowleft';
const ARROW_RIGHT = 'arrowright';
const ENTER = 'enter';
const HORIZONTAL = 'horizontal';
const SPACE = ' ';
const STRING = 'string';
// Selector strings.
const TAB = 'tab';
const TAB_SELECTOR = `[role="${TAB}"]`;
const TABLIST = 'tablist';
const TABLIST_SELECTOR = `[role="${TABLIST}"]`;
const TABPANEL = 'tabpanel';
// ===========
// Get tab ID.
// ===========
const getTabId = (id = '', index = 0) => {
return `${TAB}_${id}_${index}`;
};
// =============
// Get panel ID.
// =============
const getPanelId = (id = '', index = 0) => {
return `${TABPANEL}_${id}_${index}`;
};
// ==========
// Is active?
// ==========
const getIsActive = ({ activeIndex = null, index = null, list = [] }) => {
// Index matches?
const isMatch = index === parseFloat(activeIndex);
// Is first item?
const isFirst = index === 0;
// Only first item exists?
const onlyFirstItem = list.length === 1;
// Item doesn't exist?
const badActiveItem = !list[activeIndex];
// Flag as active?
const isActive = isMatch || onlyFirstItem || (isFirst && badActiveItem);
// Expose boolean.
return !!isActive;
};
getIsActive.propTypes = {
activeIndex: PropTypes.number,
index: PropTypes.number,
list: PropTypes.array,
};
// ===============
// Focus new item.
// ===============
const focusNewItem = (target = getDomFallback(), newIndex = 0) => {
// Get tablist.
const tablist = target.closest(TABLIST_SELECTOR) || getDomFallback();
// Get list items.
const listItems = tablist.querySelectorAll(TAB_SELECTOR);
// Get new item.
const newItem = listItems[newIndex] || getDomFallback();
// Focus new item.
newItem.focus();
};
// ================
// Get `<ul>` list.
// ================
const getTabsList = ({ activeIndex = null, id = '', list = [], setActiveIndex = () => {} }) => {
// Build new list.
const newList = list.map((item = {}, index) => {
// =========
// Get data.
// =========
const { props: itemProps = {} } = item;
const { label = '' } = itemProps;
const idPanel = getPanelId(id, index);
const idTab = getTabId(id, index);
const isActive = getIsActive({ activeIndex, index, list });
// =======
// Events.
// =======
const handleClick = () => {
// Set active item.
setActiveIndex(index);
};
const handleKeyDown = (event = {}) => {
// Get target.
const { target } = event;
// Get key.
let { key = '' } = event;
key = key.toLowerCase();
// Key events.
const isArrowLeft = key === ARROW_LEFT;
const isArrowRight = key === ARROW_RIGHT;
const isArrowKey = isArrowLeft || isArrowRight;
const isTriggerKey = key === ENTER || key === SPACE;
// Valid event?
const isValidEvent = isArrowKey || isTriggerKey;
// Prevent default.
if (isValidEvent) {
event.preventDefault();
}
// ============
// Arrow event?
// ============
if (isArrowKey) {
// Set later.
let newIndex = null;
let nextItem = null;
// Arrow left?
if (isArrowLeft) {
newIndex = index - 1;
nextItem = list[newIndex];
if (!nextItem) {
newIndex = list.length - 1;
nextItem = list[newIndex];
}
}
// Arrow right?
if (isArrowRight) {
newIndex = index + 1;
nextItem = list[newIndex];
if (!nextItem) {
newIndex = 0;
nextItem = list[newIndex];
}
}
// Item exists?
if (nextItem) {
// Focus new item.
focusNewItem(target, newIndex);
// Set active item.
setActiveIndex(newIndex);
}
}
// ==============
// Trigger event?
// ==============
if (isTriggerKey) {
// Set active item.
setActiveIndex(index);
}
};
// ============
// Add to list.
// ============
return (
<li
aria-controls={idPanel}
aria-selected={isActive}
className="tabs__item"
id={idTab}
key={idTab}
role={TAB}
tabIndex={isActive ? 0 : -1}
// Events.
onClick={handleClick}
onKeyDown={handleKeyDown}
>
{label || `${index + 1}`}
</li>
);
});
// ==========
// Expose UI.
// ==========
return (
<Render if={newList.length}>
<ul aria-orientation={HORIZONTAL} className="tabs__list" role={TABLIST}>
{newList}
</ul>
</Render>
);
};
getTabsList.propTypes = {
activeIndex: PropTypes.number,
id: PropTypes.string,
list: PropTypes.array,
setActiveIndex: PropTypes.func,
};
// =================
// Get `<div>` list.
// =================
const getPanelsList = ({ activeIndex = null, id = '', list = [] }) => {
// Build new list.
const newList = list.map((item = {}, index) => {
// =========
// Get data.
// =========
const { props: itemProps = {} } = item;
const { children = '', className = null, style = null } = itemProps;
const idPanel = getPanelId(id, index);
const idTab = getTabId(id, index);
const isActive = getIsActive({ activeIndex, index, list });
// =============
// Get children.
// =============
let content = children || item;
if (typeof content === STRING) {
content = <p>{content}</p>;
}
// =================
// Build class list.
// =================
const classList = cx({
tabs__panel: true,
[String(className)]: className,
});
// ==========
// Expose UI.
// ==========
return (
<div
aria-hidden={!isActive}
aria-labelledby={idTab}
className={classList}
id={idPanel}
key={idPanel}
role={TABPANEL}
style={style}
tabIndex={0}
>
{content}
</div>
);
});
// ==========
// Expose UI.
// ==========
return newList;
};
getPanelsList.propTypes = {
activeIndex: PropTypes.number,
id: PropTypes.string,
list: PropTypes.array,
};
// ==========
// Component.
// ==========
const Tabs = ({
children = '',
className = null,
selected = 0,
style = null,
id: propsId = uuid(),
}) => {
// ===============
// Internal state.
// ===============
const [id] = useState(propsId);
const [activeIndex, setActiveIndex] = useState(selected);
// =================
// Build class list.
// =================
const classList = cx({
tabs: true,
[String(className)]: className,
});
// ===============
// Build UI lists.
// ===============
const list = Array.isArray(children) ? children : [children];
const tabsList = getTabsList({
activeIndex,
id,
list,
setActiveIndex,
});
const panelsList = getPanelsList({
activeIndex,
id,
list,
});
// ==========
// Expose UI.
// ==========
return (
<Render if={list[0]}>
<div className={classList} id={id} style={style}>
{tabsList}
{panelsList}
</div>
</Render>
);
};
Tabs.propTypes = {
children: PropTypes.node,
className: PropTypes.string,
id: PropTypes.string,
selected: PropTypes.number,
style: PropTypes.object,
};
export default Tabs;
Selecting the active tab
A tabset always shows something, so getIsActive decides whether a given index is active. It is true when the index matches activeIndex, when there is only one tab, or when it is the first tab and the activeIndex tab does not exist.
const getIsActive = ({ activeIndex = null, index = null, list = [] }) => {
// Index matches?
const isMatch = index === parseFloat(activeIndex);
// Is first item?
const isFirst = index === 0;
// Only first item exists?
const onlyFirstItem = list.length === 1;
// Item doesn't exist?
const badActiveItem = !list[activeIndex];
// Flag as active?
const isActive = isMatch || onlyFirstItem || (isFirst && badActiveItem);
// Expose boolean.
return !!isActive;
};
The tab list
getTabsList builds the clickable <li role="tab"> elements inside a <ul role="tablist">, assigns all aria-* and role attributes, and binds onClick and onKeyDown. Either event calls setActiveIndex, updating internal state. The label shown in each <li> arrives as a <div label="…"> child of <Tabs>: a concept foreign to flat HTML, but a convenient mental model, since that <div>'s children become the contents of the matching role="tabpanel".
const getTabsList = ({ activeIndex = null, id = '', list = [], setActiveIndex = () => {} }) => {
// Build new list.
const newList = list.map((item = {}, index) => {
// =========
// Get data.
// =========
const { props: itemProps = {} } = item;
const { label = '' } = itemProps;
const idPanel = getPanelId(id, index);
const idTab = getTabId(id, index);
const isActive = getIsActive({ activeIndex, index, list });
// =======
// Events.
// =======
const handleClick = () => {
// Set active item.
setActiveIndex(index);
};
const handleKeyDown = (event = {}) => {
// Get target.
const { target } = event;
// Get key.
let { key = '' } = event;
key = key.toLowerCase();
// Key events.
const isArrowLeft = key === ARROW_LEFT;
const isArrowRight = key === ARROW_RIGHT;
const isArrowKey = isArrowLeft || isArrowRight;
const isTriggerKey = key === ENTER || key === SPACE;
// Valid event?
const isValidEvent = isArrowKey || isTriggerKey;
// Prevent default.
if (isValidEvent) {
event.preventDefault();
}
// ============
// Arrow event?
// ============
if (isArrowKey) {
// Set later.
let newIndex = null;
let nextItem = null;
// Arrow left?
if (isArrowLeft) {
newIndex = index - 1;
nextItem = list[newIndex];
if (!nextItem) {
newIndex = list.length - 1;
nextItem = list[newIndex];
}
}
// Arrow right?
if (isArrowRight) {
newIndex = index + 1;
nextItem = list[newIndex];
if (!nextItem) {
newIndex = 0;
nextItem = list[newIndex];
}
}
// Item exists?
if (nextItem) {
// Focus new item.
focusNewItem(target, newIndex);
// Set active item.
setActiveIndex(newIndex);
}
}
// ==============
// Trigger event?
// ==============
if (isTriggerKey) {
// Set active item.
setActiveIndex(index);
}
};
// ============
// Add to list.
// ============
return (
<li
aria-controls={idPanel}
aria-selected={isActive}
className="tabs__item"
id={idTab}
key={idTab}
role={TAB}
tabIndex={isActive ? 0 : -1}
// Events.
onClick={handleClick}
onKeyDown={handleKeyDown}
>
{label || `${index + 1}`}
</li>
);
});
// ==========
// Expose UI.
// ==========
return (
<Render if={newList.length}>
<ul aria-orientation={HORIZONTAL} className="tabs__list" role={TABLIST}>
{newList}
</ul>
</Render>
);
};
The panel list
getPanelsList parses the top-level component's children and extracts their content. Here the accordion semantics resurface: it uses getIsActive to decide whether the panel should be hidden rather than assuming every panel but the active one is hidden. It adds the remaining aria-* and role attributes, applies any supplied className or style, and wraps loose string content in <p> tags for consistency.
const getPanelsList = ({ activeIndex = null, id = '', list = [] }) => {
// Build new list.
const newList = list.map((item = {}, index) => {
// =========
// Get data.
// =========
const { props: itemProps = {} } = item;
const { children = '', className = null, style = null } = itemProps;
const idPanel = getPanelId(id, index);
const idTab = getTabId(id, index);
const isActive = getIsActive({ activeIndex, index, list });
// =============
// Get children.
// =============
let content = children || item;
if (typeof content === STRING) {
content = <p>{content}</p>;
}
// =================
// Build class list.
// =================
const classList = cx({
tabs__panel: true,
[String(className)]: className,
});
// ==========
// Expose UI.
// ==========
return (
<div
aria-hidden={!isActive}
aria-labelledby={idTab}
className={classList}
id={idPanel}
key={idPanel}
role={TABPANEL}
style={style}
tabIndex={0}
>
{content}
</div>
);
});
// ==========
// Expose UI.
// ==========
return newList;
};
The main component
Tabs initializes an id state to cache a generated uuid() for the component's lifetime; React dislikes key attributes that change dynamically, so these must stay fixed once set. A second useState tracks the selected tab, and a setActiveIndex function is handed to each <li>. getTabsList and getPanelsList produce the UI, wrapped in <div role="tablist">. The component also accepts wrapper-level className or style, letting implementers tweak without editing the core component.
const Tabs = ({
children = '',
className = null,
selected = 0,
style = null,
id: propsId = uuid(),
}) => {
// ===============
// Internal state.
// ===============
const [id] = useState(propsId);
const [activeIndex, setActiveIndex] = useState(selected);
// =================
// Build class list.
// =================
const classList = cx({
tabs: true,
[String(className)]: className,
});
// ===============
// Build UI lists.
// ===============
const list = Array.isArray(children) ? children : [children];
const tabsList = getTabsList({
activeIndex,
id,
list,
setActiveIndex,
});
const panelsList = getPanelsList({
activeIndex,
id,
list,
});
// ==========
// Expose UI.
// ==========
return (
<Render if={list[0]}>
<div className={classList} id={id} style={style}>
{tabsList}
{panelsList}
</div>
</Render>
);
};
Accordion.js
Most of the accordion mirrors Tabs.js, matching the single-file approach on the vanilla side. The divergence is in state shape, handled by handleClick.
// =============
// Used like so…
// =============
<Accordion>
<div label="Tab 1">
<p>
Tab 1 content
</p>
</div>
<div label="Tab 2">
<p>
Tab 2 content
</p>
</div>
</Accordion>
import React, { useState } from 'react';
import PropTypes from 'prop-types';
import { v4 as uuid } from 'uuid';
import cx from 'classnames';
// UI.
import Render from './Render';
// ==========
// Constants.
// ==========
const ENTER = 'enter';
const SPACE = ' ';
const STRING = 'string';
const VERTICAL = 'vertical';
// ===========
// Get tab ID.
// ===========
const getTabId = (id = '', index = 0) => {
return `tab_${id}_${index}`;
};
// =============
// Get panel ID.
// =============
const getPanelId = (id = '', index = 0) => {
return `tabpanel_${id}_${index}`;
};
// ==============================
// Get `tab` and `tabpanel` list.
// ==============================
const getTabsAndPanelsList = ({
activeItems = {},
id = '',
isMulti = true,
list = [],
setActiveItems = () => {},
}) => {
// Build new list.
const newList = [];
// Loop through.
list.forEach((item = {}, index) => {
// =========
// Get data.
// =========
const { props: itemProps = {} } = item;
const { children = '', className = null, label = '', style = null } = itemProps;
const idPanel = getPanelId(id, index);
const idTab = getTabId(id, index);
const isActive = !!activeItems[index];
// =======
// Events.
// =======
const handleClick = (event = {}) => {
let { key = '' } = event;
key = key.toLowerCase();
// Trigger key?
const isTriggerKey = key === ENTER || key === SPACE;
// Early exit.
if (key && !isTriggerKey) {
return;
}
// Keep active items?
const state = isMulti ? activeItems : null;
// Update active item.
const newState = {
...state,
[index]: !activeItems[index],
};
// Prevent key press.
event.preventDefault();
// Set active item.
setActiveItems(newState);
};
// =============
// Get children.
// =============
let content = children || item;
if (typeof content === STRING) {
content = <p>{content}</p>;
}
// =================
// Build class list.
// =================
const classList = cx({
accordion__panel: true,
[String(className)]: className,
});
// ========
// Add tab.
// ========
newList.push(
<div
aria-controls={idPanel}
aria-selected={isActive}
className="accordion__item"
id={idTab}
key={idTab}
role="tab"
tabIndex={0}
// Events.
onClick={handleClick}
onKeyDown={handleClick}
>
<i aria-hidden="true" className="accordion__item__icon" />
{label || `${index + 1}`}
</div>
);
// ==========
// Add panel.
// ==========
newList.push(
<div
aria-hidden={!isActive}
aria-labelledby={idTab}
className={classList}
id={idPanel}
key={idPanel}
role="tabpanel"
style={style}
tabIndex={0}
>
{content}
</div>
);
});
// ==========
// Expose UI.
// ==========
return newList;
};
getTabsAndPanelsList.propTypes = {
activeItems: PropTypes.object,
id: PropTypes.string,
isMulti: PropTypes.bool,
list: PropTypes.array,
setActiveItems: PropTypes.func,
};
// ==========
// Component.
// ==========
const Accordion = ({
children = '',
className = null,
isMulti = true,
selected = {},
style = null,
id: propsId = uuid(),
}) => {
// ===============
// Internal state.
// ===============
const [id] = useState(propsId);
const [activeItems, setActiveItems] = useState(selected);
// =================
// Build class list.
// =================
const classList = cx({
accordion: true,
[String(className)]: className,
});
// ===============
// Build UI lists.
// ===============
const list = Array.isArray(children) ? children : [children];
const tabsAndPanelsList = getTabsAndPanelsList({
activeItems,
id,
isMulti,
list,
setActiveItems,
});
// ==========
// Expose UI.
// ==========
return (
<Render if={list[0]}>
<div
aria-multiselectable={isMulti}
aria-orientation={VERTICAL}
className={classList}
id={id}
role="tablist"
style={style}
>
{tabsAndPanelsList}
</div>
</Render>
);
};
Accordion.propTypes = {
children: PropTypes.node,
className: PropTypes.string,
id: PropTypes.string,
isMulti: PropTypes.bool,
selected: PropTypes.object,
style: PropTypes.object,
};
export default Accordion;
Storing state for accordions
Tabs are mutually exclusive, so one numeric index is enough. An accordion may show several panels at once or behave exclusively, so it needs a structure that covers both, which means an object. When isMulti is true, the spread syntax carries the existing activeItems into newState before the current index is flipped to its boolean opposite.
const handleClick = (event = {}) => {
let { key = '' } = event;
key = key.toLowerCase();
// Trigger key?
const isTriggerKey = key === ENTER || key === SPACE;
// Early exit.
if (key && !isTriggerKey) {
return;
}
// Keep active items?
const state = isMulti ? activeItems : null;
// Update active item.
const newState = {
...state,
[index]: !activeItems[index],
};
// Prevent key press.
event.preventDefault();
// Set active item.
setActiveItems(newState);
};
If only the first panel is open and the user clicks the second, both indexes become true and two role="tabpanel" elements are visible together.
/*
Internal representation
of `activeItems` state.
*/
{
0: true,
1: true,
}
Outside isMulti mode, meaning the wrapper carries aria-multiselectable="false", activeItems holds only one key/value pair. Spreading null in place of the current state clears everything before the active tab is recorded.
/*
Internal representation
of `activeItems` state.
*/
{
1: true,
}
Trade-offs, not winners
Compare the markup needed for the vanilla approach with the JSX that produces the same result.
HTML
<div class="tabs">
<ul class="tabs__list">
<li class="tabs__item">
Tab 1
</li>
<li class="tabs__item">
Tab 2
</li>
</ul>
<div class="tabs__panel">
<p>
Tab 1 content
</p>
</div>
<div class="tabs__panel">
<p>
Tab 2 content
</p>
</div>
</div>
JSX
<Tabs>
<div label="Tab 1">
Tab 1 content
</div>
<div label="Tab 2">
Tab 2 content
</div>
</Tabs>
Neither is strictly better. Working closer to the metal gives direct control but more tedium and a constant awareness of every tag. A framework hands functionality over for free, but can act as a black box, unless the underlying nuances are already understood. Then both realms become workable, and a framework is what it has always been underneath: JavaScript.



