Skip to content

Commit dbc7d7a

Browse files
committed
fix(checkbox): trying to fix lack of label error
1 parent d82a204 commit dbc7d7a

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

core/src/components/checkbox/checkbox.scss

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,14 @@
102102
@include margin(null, null, $checkbox-item-label-margin-bottom, null);
103103
}
104104

105+
/**
106+
* The native control it must be hidden with display:none instead of aria-hidden="true"
107+
* to avoid nested interactive elements accessibility issues
108+
*/
109+
.checkbox-native-control {
110+
display: none;
111+
}
112+
105113
/**
106114
* If no label text is placed into the slot
107115
* then the element should be hidden otherwise

core/src/components/checkbox/checkbox.tsx

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -262,13 +262,21 @@ export class Checkbox implements ComponentInterface {
262262

263263
renderHiddenInput(true, el, name, checked ? value : '', disabled);
264264

265+
// Determine appropriate accessible name
266+
const hasLabelContent = el.textContent !== '';
267+
const inputLabelId = inputId + '-lbl';
268+
265269
return (
270+
/*
271+
The host must be a checkbox role to support Safari's Voice Over accessibility
272+
*/
266273
<Host
267274
role="checkbox"
268275
aria-checked={indeterminate ? 'mixed' : `${checked}`}
269276
aria-describedby={this.getHintTextID()}
270277
aria-invalid={this.getHintTextID() === this.errorTextId}
271-
aria-labelledby={inputId + '-lbl'}
278+
aria-labelledby={hasLabelContent ? inputLabelId : null}
279+
aria-label={!hasLabelContent ? inheritedAttributes['aria-label'] || 'checkbox' : null}
272280
aria-disabled={disabled ? 'true' : null}
273281
tabindex="0"
274282
onKeyDown={this.onKeyDown}
@@ -292,8 +300,7 @@ export class Checkbox implements ComponentInterface {
292300
*/}
293301
<input
294302
type="checkbox"
295-
aria-hidden="true"
296-
tabindex="-1"
303+
class="checkbox-native-control"
297304
checked={checked ? true : undefined}
298305
disabled={disabled}
299306
id={inputId}
@@ -307,10 +314,10 @@ export class Checkbox implements ComponentInterface {
307314
<div
308315
class={{
309316
'label-text-wrapper': true,
310-
'label-text-wrapper-hidden': el.textContent === '',
317+
'label-text-wrapper-hidden': !hasLabelContent,
311318
}}
312319
part="label"
313-
id={inputId + '-lbl'}
320+
id={inputLabelId}
314321
>
315322
<slot></slot>
316323
{this.renderHintText()}

0 commit comments

Comments
 (0)