-
Notifications
You must be signed in to change notification settings - Fork 1
feat(ngx-layout): WCAG/ARIA for drag-and-drop #270
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
IbenTesara
merged 1 commit into
master
from
feat/ngx-layout/265-studiohyperdrivengx-layout-wai-ariawcag-compliance-for-configurable-layout
Nov 25, 2024
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { Observable } from 'rxjs'; | ||
import { NgxAccessibleDragAndDropAbstractService } from '@ngx/layout'; | ||
|
||
@Injectable({ providedIn: 'root' }) | ||
export class DragAndDropService extends NgxAccessibleDragAndDropAbstractService { | ||
get currentLanguage(): string | Observable<string> { | ||
return 'nl'; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
165 changes: 165 additions & 0 deletions
165
libs/layout/src/lib/abstracts/drag-and-drop/drag-and-drop.service.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
import { filter, map, Observable, of, take, tap } from 'rxjs'; | ||
import { inject } from '@angular/core'; | ||
|
||
import { UUID } from 'angular2-uuid'; | ||
|
||
import { NgxLiveRegionService } from '../../services'; | ||
import { | ||
NgxAccessibleDragAndDropMessage, | ||
NgxAccessibleDragAndDropMessageRecord, | ||
} from '../../types'; | ||
import { NgxAccessibleDragAndDropMessageRecords } from '../../const'; | ||
import { hideElement } from '../../utils'; | ||
|
||
/** | ||
* An abstract service that is used to make drag and drop components accessible for assistive technologies | ||
*/ | ||
export abstract class NgxAccessibleDragAndDropAbstractService { | ||
/** | ||
* The live region service | ||
*/ | ||
private readonly liveRegionService: NgxLiveRegionService = inject(NgxLiveRegionService); | ||
|
||
/** | ||
* A method that passes the current language, can either be a string or an Observable | ||
*/ | ||
abstract get currentLanguage(): string | Observable<string>; | ||
|
||
/** | ||
* A custom set of messages used for the drag and drop events. | ||
* | ||
* Please check the readme for more information on what is necessary to make these messages accessible. | ||
*/ | ||
public customMessages: Record<string, NgxAccessibleDragAndDropMessageRecord>; | ||
|
||
/** | ||
* Sets a message to the live region | ||
* | ||
* @param message - The provided message | ||
*/ | ||
public setMessage(message: NgxAccessibleDragAndDropMessage): Observable<void> { | ||
// Iben: If no language was set, we early exit | ||
if (!this.currentLanguage) { | ||
console.error( | ||
'NgxAccessibleDragAndDropAbstractService: No language was provided, so no message could be set.' | ||
); | ||
|
||
return of(); | ||
} | ||
|
||
// Iben: Take the current language to fetch the message | ||
return ( | ||
typeof this.currentLanguage === 'string' | ||
? of(this.currentLanguage) | ||
: this.currentLanguage | ||
).pipe( | ||
filter(Boolean), | ||
take(1), | ||
tap((currentLanguage) => { | ||
// Iben: Fetch the necessary data | ||
const { type, data } = message; | ||
|
||
let result: string = this.messageRecord[currentLanguage][type]; | ||
|
||
// Iben: If no message was found, we early exit and throw an error | ||
if (!result) { | ||
console.error( | ||
'NgxAccessibleDragAndDropAbstractService: No message for the corresponding drag and drop event was found.' | ||
); | ||
|
||
return; | ||
} | ||
|
||
// Iben: Replace the necessary substrings | ||
if (type === 'selected' || type === 'deselected' || type === 'cancelled') { | ||
result = result.replace( | ||
'{{#item}}', | ||
data.itemLabel || `${this.messageRecord[currentLanguage].item} ${data.item}` | ||
); | ||
} else if (type === 'moved') { | ||
result = result | ||
.replace( | ||
'{{#item}}', | ||
data.itemLabel || | ||
`${this.messageRecord[currentLanguage].item} ${data.item}` | ||
) | ||
.replace( | ||
`{{#from}}`, | ||
data.fromLabel || | ||
`${this.messageRecord[currentLanguage].container} ${data.from}` | ||
) | ||
.replace( | ||
`{{#to}}`, | ||
data.toLabel || | ||
`${this.messageRecord[currentLanguage].container} ${data.to}` | ||
); | ||
} else if (type === 'reordered') { | ||
result = result | ||
.replace( | ||
'{{#item}}', | ||
data.itemLabel || | ||
`${this.messageRecord[currentLanguage].item} ${data.item}` | ||
) | ||
.replace(`{{#from}}`, data.from) | ||
.replace(`{{#to}}`, data.to); | ||
} | ||
|
||
// Iben: Update the message in the live region | ||
this.liveRegionService.setMessage(result); | ||
}), | ||
map(() => null) | ||
); | ||
} | ||
|
||
/** | ||
* Adds a description to the drag and drop host explaining how the drag and drop functions | ||
* | ||
* @param parent - The drag and drop host | ||
* @param description - An optional description used to overwrite the default description | ||
*/ | ||
public setDragAndDropDescription(parent: HTMLElement, description?: string): Observable<void> { | ||
// Iben: Create the description element and its id | ||
const element: HTMLParagraphElement = document.createElement('p'); | ||
const id: string = UUID.UUID(); | ||
|
||
// Iben: Take the current language to fetch the message | ||
return ( | ||
typeof this.currentLanguage === 'string' | ||
? of(this.currentLanguage) | ||
: this.currentLanguage | ||
).pipe( | ||
tap((language: string) => { | ||
// Iben: Get the description text | ||
const text = description || this.messageRecord[language].description; | ||
|
||
// Iben: If no description was found, we early exit and throw an error | ||
if (!text) { | ||
console.error( | ||
'NgxAccessibleDragAndDropAbstractService: No description for the drag and drop container was found.' | ||
); | ||
|
||
return; | ||
} | ||
|
||
// Iben: Set the description and id of the element | ||
element.innerText = text; | ||
element.setAttribute('id', id); | ||
|
||
// Iben: Attach the element to the parent and update the aria id | ||
parent.appendChild(element); | ||
parent.setAttribute('aria-describedby', id); | ||
|
||
// Iben: Hide element | ||
hideElement(element); | ||
}), | ||
map(() => null) | ||
); | ||
} | ||
|
||
/** | ||
* Returns the custom message record or the default when no custom record was provided | ||
*/ | ||
private get messageRecord(): Record<string, NgxAccessibleDragAndDropMessageRecord> { | ||
return this.customMessages || NgxAccessibleDragAndDropMessageRecords; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from './display-content/display-content.component'; | ||
export * from './drag-and-drop/drag-and-drop.service'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.