Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 | 29x 29x 29x 49x 32x 32x 32x 5x 29x 29x 73x 26x 22x 22x 5x 5x 5x 20x 20x 7x 20x 17x 1x 1x 3x 3x 3x 3x 29x 29x 6x 6x 8x 8x 8x 21x 21x 12x 12x 12x 17x 17x 17x 17x 17x 1x 1x 1x 29x 29x 29x 29x 11x 5x 5x 5x 5x 1x 1x 1x 2x 2x 2x 2x 2x 2x 11x 17x 17x 29x 29x 1x 1x 1x | import { LightningElement, api } from 'lwc';
import { normalizeBoolean, reflectAttribute } from 'sds/utils';
import 'sds/privateThemeProvider';
export default class ExpandableSectionManager extends LightningElement {
static shadowSupportMode = 'native';
_valid = true;
_open = false;
_ariaControlsElement;
/**
* Track the open state of the expandable section host to update its children
*
*
* @returns {boolean}
* @default false
*/
@api
get open() {
return this._open;
}
set open(value) {
this._open = normalizeBoolean(value);
reflectAttribute(this, 'open', this._open);
// Wait for render to complete to avoid race conditions errors
if (this._rendered) {
this.handleOpenState();
}
}
/**
* Returns all slotted children of the expandable section.
*
* @return {Array}
* @private
*/
get slottedChildren() {
const slot = this.template.querySelector('slot');
return slot.assignedElements({ flatten: true });
}
/**
* Retrieves the element with the attribute aria-controls.
*
* @return {Element|null} The element with the aria-controls attribute, or null if none exists.
* @private
*/
get ariaControlsElement() {
this._ariaControlsElement = this.querySelector(`[aria-controls]`);
return !this._ariaControlsElement && this._ariaControlsElement;
}
/**
* Returns the value of the aria-controls attribute.
*
* @return {string|null}
* @private
*/
get ariaControlsValue() {
return this._ariaControlsElement ? this._ariaControlsElement.getAttribute('aria-controls') : null;
}
/**
* Returns the content element with ID matching the aria-controls.
* The element must not have an aria-controls attribute.
*
* @return {Element|null}
* @private
*/
get contentElement() {
return this.ariaControlsValue
? this.querySelector(`#${this.ariaControlsValue}:not([aria-controls])`)
: null;
}
/**
* Sets the aria-expanded attribute for the button element.
*
* @param {Element} button
* @returns {void}
* @private
*/
handleAriaExpanded(button) {
button.setAttribute('aria-expanded', this._open);
}
/**
* Sets the aria-hidden attribute for the content element associated with the button.
*
* @param {Element} button
* @returns {void}
* @private
*/
handleAriaHidden() {
this.contentElement.setAttribute('aria-hidden', !this._open);
}
/**
* Toggles the visibility of the content based on the state of the aria-expanded property.
*
* @param {Element} button - The button element that triggers the visibility toggle.
* @returns {void}
* @private
*/
handleVisibility() {
this.contentElement.style.display = this._open ? 'block' : 'none';
}
/**
* Handles toggle actions for a button
*
* @param {object} button
* @returns {void}
* @private
*/
handleToggleActions(button) {
this.handleAriaExpanded(button);
this.handleAriaHidden();
this.handleVisibility();
}
/**
* Checks if the given button element is a button or an input.
* It also validates children of custom elements within the shadow root.
*
* Note: The shadowRoot validation is not reliable on renderedCallback (Race condition), use handleSlotChange instead.
*
* @param {Object} button
* @returns {boolean}
* @private
*/
handleValidButtonElement(button) {
const isHTMLButtonOrInput = (button) =>
button instanceof HTMLButtonElement || button instanceof HTMLInputElement;
return Array.from([button, ...(button.shadowRoot?.children || [])]).some(isHTMLButtonOrInput);
}
/**
* Check if the element has role button.
*
* @param {HTMLElement} element
* @returns {boolean}
* @private
*/
handleValidRoleButton(element) {
return element.role === 'button';
}
/**
* Determines validity of button element or element with role button.
*
* @param {Object} button
* @returns {boolean}
* @private
*/
isValidButton(button) {
return this.handleValidButtonElement(button) || this.handleValidRoleButton(button);
}
/**
* Method to check if the element with aria-controls is a valid button.
*
* @returns {undefined}
* @private
*/
handleValidButtonAriaControls() {
if (!this.isValidButton(this._ariaControlsElement)) {
this._valid = false;
console.error(
'Invalid button element, please ensure that the element is one of the allowed types (button, input, element with role="button", or custom element).',
);
}
}
/**
* Method to check if the element is the valid trigger button for the expandable section.
* The method also validates against a direct child of the button (e.g. button > span)
*
* @param {Object} trigger - The clicked element to check.
* @return {boolean}
* @private
*/
isTriggerValidButton(trigger) {
const triggerAriaControls = trigger.getAttribute('aria-controls');
const triggerWithParentAriaControls = trigger.parentElement.getAttribute('aria-controls');
switch (true) {
case triggerAriaControls === this.ariaControlsValue && this.isValidButton(trigger):
case triggerWithParentAriaControls === this.ariaControlsValue &&
this.isValidButton(trigger.parentElement):
return true;
default:
return false;
}
}
/**
* Method to check if expandable section contains children.
*
* @returns {undefined}
* @private
*/
handleSlottedChildren() {
const children = this.slottedChildren;
if (children.length === 0) {
this._valid = false;
console.error(
'Contracts for Expandable Section Manager have not been met, please ensure there are at least 2 elements. The first one should be a button, and the second one should be a container for the expandable content.',
);
}
}
/**
* Method to check if the aria-controls exists and it is valid.
* Set the aria-expanded attribute of the button element.
I*
* @return {undefined}
* @private
*/
handleAriaControlsElement() {
if (this.ariaControlsElement === null) {
this._valid = false;
console.error('The element with the aria-controls attribute was not found.');
return;
} else if (this.ariaControlsValue === '') {
this._valid = false;
console.error('The element with aria-controls was found, but its value cannot be empty.');
return;
} else {
this.handleAriaExpanded(this._ariaControlsElement);
}
}
/**
* Method to check if the content element with ID matching the aria-controls exists.
* Set the aria-hidden attribute and display property of the content element.
*
* @return {undefined}
* @private
*/
handleMatchingContentElement() {
if (!this.contentElement) {
this._valid = false;
console.error('The content element with the ID value matching the aria-controls value was not found.');
return;
} else {
this.handleAriaHidden();
this.handleVisibility();
}
}
/*I*
* Method to check if there are multiple elements with the same aria-controls and same content ID.
*
* @return {void}
* @private
*/
handleUniqueElements() {
const matchingAriaControlsElements = this.querySelectorAll(`[aria-controls="${this.ariaControlsValue}"]`);
const matchingContentIdElements = this.querySelectorAll(
`#${this.ariaControlsValue}:not([aria-controls])`,
);
if (matchingAriaControlsElements.length > 1) {
this._valid = false;
console.error(
'Multiple elements with the same aria-controls value were found. Please ensure the value is unique.',
);
return;
}
if (matchingContentIdElements.length > 1) {
this._valid = false;
console.error(
'Multiple content elements with the ID value matching the aria-controls value were found. ID must be unique',
);
return;
}
}
/**
* Method to check for contracts of expandable section.
*
* @returns {boolean}
* @private
*/
handleValidContracts() {
this.hasOpenAttribute();
this.handleSlottedChildren();
this.handleAriaControlsElement();
this.handleMatchingContentElement();
}
/**
* Method to check if the host has an existing open attribute.
* Change default open state if attribute exists
*
*I @return {undefined}
* @private
*/
EhasOpenAttribute() {
if (this.hasAttribute('open')) {
this._open = true;
}
}
/**
* Handles the open state to trigger toggle actions.
*
* @return {undefined}
* @private
*/
handleOpenState() {
const trigger = this._ariaControlsElement;
if (!this._valid) {
return;
}
Eif (trigger) {
this.handleToggleActions(trigger);
}
}
/**
* Handles the click event for a valid expandable section from the valid button trigger.
*
* @param {Event} e - The click event
* @returns {void}
*/
handleClick(e) {
const { target } = e;
const trigger = target;
if (!this._valid) {
return;
}
if (trigger && this.isTriggerValidButton(trigger)) {
// Set the current open state to trigger toggle actions
E this.open = !this._open;
E}
}
/**
* Event handler for keydown events for a valid expandable section fom the valid button trigger.
*
* @param {KeyboardEvent} e - The keydown event
* @returns {void}
*/
handleKeyDown(e) {
const { target, key } = e;
const trigger = target;
if (!this._valid) {
return;
}
if (trigger && this.isTriggerValidButton(trigger)) {
if (key === 'Enter' || key === ' ') {
e.preventDefault(); // Prevent issues with interactive elements
// Set the current open state to trigger toggle actions
this.open = !this._open;
}
}
}
/**
* Event handler for slot change.
*
* @param {Event}
*/
handleSlotChange() {
if (!this._valid) {
return;
}
this.handleValidButtonAriaControls();
this.handleUniqueElements();
}
/**
* When the component renders, we validate the contracts for the expandable section.
*/
renderedCallback() {
if (!this._rendered) {
this._rendered = true;
this.handleValidContracts();
}
}
}
|