Skip to content

Commit

Permalink
♿ a11y(switch radio buttons): focus lost after selection (#1431)
Browse files Browse the repository at this point in the history
* Create PR for #1422

* fix(checkbox): keep focus on click a11y criteria

* fix(checkbox): run format

* fix(checkbox): added changelog

* fix(checkbox): added documentation

---------

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Marinkov, Magdalena <[email protected]>
  • Loading branch information
github-actions[bot] and mmarinkov authored Aug 29, 2024
1 parent a7f0d46 commit a3df225
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/strange-rules-sleep.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@baloise/ds-core': patch
---

**button**: Improving accessibility: keep focus on button after selection
18 changes: 17 additions & 1 deletion packages/core/src/components/bal-checkbox/bal-checkbox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ export class Checkbox implements ComponentInterface, FormInput<any>, Loggable, B
@State() buttonTabindex?: number
@State() ariaForm: BalAriaForm = defaultBalAriaForm

/**
* Track focus state
* If `true` checkbox needs to remain focused
*/
@State() wasFocused = false

log!: LogInstance

@Logger('bal-checkbox')
Expand Down Expand Up @@ -358,6 +364,10 @@ export class Checkbox implements ComponentInterface, FormInput<any>, Loggable, B
return
}

if (this.wasFocused) {
this.focused = true
}

if (element.nodeName !== 'INPUT' && !this.disabled && !this.readonly) {
this.toggleChecked()
this.nativeInput?.focus()
Expand All @@ -377,6 +387,7 @@ export class Checkbox implements ComponentInterface, FormInput<any>, Loggable, B

if (this.keyboardMode) {
this.focused = true
this.wasFocused = true
}
}

Expand All @@ -391,7 +402,12 @@ export class Checkbox implements ComponentInterface, FormInput<any>, Loggable, B

private onPointerDown = () => (this.keyboardMode = false)

private onKeydown = (ev: any) => (this.keyboardMode = FOCUS_KEYS.includes(ev.key))
private onKeydown = (ev: any) => {
if (!isSpaceKey(ev)) {
this.wasFocused = false
}
this.keyboardMode = FOCUS_KEYS.includes(ev.key)
}

/**
* RENDER
Expand Down

0 comments on commit a3df225

Please sign in to comment.