Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
tofsjonas committed Oct 17, 2023
1 parent 84d3220 commit 6229a4b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 15 deletions.
17 changes: 7 additions & 10 deletions src/sortable.a11y.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,25 @@
* This is a "plugin" for the sortable package:
* https://www.npmjs.com/package/sortable-tablesort
* https://github.com/tofsjonas/sortable
*
*
* Enhances the accessibility of class="sortable" tables by adding ARIA attributes and keyboard event listeners.
* @param tables - A list of HTML table elements to enhance.
*/
const enhanceSortableAccessibility = (tables: NodeListOf<HTMLTableElement>) => {

/**
* Generates an aria-label attribute for a table header cell based on its content and current sort direction.
* @param element - The table header cell to update.
* @param default_direction - The default sort direction for the table.
*/
function updateAriaLabel(element: HTMLTableCellElement, default_direction = "") {
function updateAriaLabel(element: HTMLTableCellElement, default_direction = '') {
// Generate aria-label based on header content
const header_text = element.textContent || 'element'

let current_direction = element.getAttribute('aria-sort') ?? ''
const current_direction = element.getAttribute('aria-sort') ?? ''
let new_direction = 'descending'

if(current_direction === 'descending' ||
default_direction && current_direction !== 'ascending'
) {
new_direction='ascending'
if (current_direction === 'descending' || (default_direction && current_direction !== 'ascending')) {
new_direction = 'ascending'
}

const aria_label = `Click to sort table by ${header_text} in ${new_direction} order`
Expand Down Expand Up @@ -54,7 +51,7 @@ const enhanceSortableAccessibility = (tables: NodeListOf<HTMLTableElement>) => {

// Add tabindex attribute and generate initial aria-label attribute
element.setAttribute('tabindex', '0')
updateAriaLabel(element,default_direction)
updateAriaLabel(element, default_direction)

// Attach click event listener to update aria-label attribute
element.addEventListener('click', () => {
Expand All @@ -73,4 +70,4 @@ const enhanceSortableAccessibility = (tables: NodeListOf<HTMLTableElement>) => {
// Attach function to DOMContentLoaded event to execute when page is loaded
document.addEventListener('DOMContentLoaded', () => {
enhanceSortableAccessibility(document.querySelectorAll<HTMLTableElement>('.sortable'))
})
})
9 changes: 4 additions & 5 deletions src/sortable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,10 @@ document.addEventListener('click', function (e: MouseEvent) {

let direction = 'descending'

if(element.getAttribute('aria-sort') === 'descending'
||
(table.classList.contains(ascending_table_sort_class) && element.getAttribute('aria-sort')!=='ascending')
)
{
if (
element.getAttribute('aria-sort') === 'descending' ||
(table.classList.contains(ascending_table_sort_class) && element.getAttribute('aria-sort') !== 'ascending')
) {
direction = 'ascending'
}

Expand Down

0 comments on commit 6229a4b

Please sign in to comment.