-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* more tests * enhanceSortableAccessibility can hopefully be imported better * main and module fields in package.json + version bump
- Loading branch information
Showing
17 changed files
with
241 additions
and
21 deletions.
There are no files selected for viewing
This file contains 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 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 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,9 @@ | ||
/** | ||
* 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. | ||
*/ | ||
export declare const enhanceSortableAccessibility: (tables: NodeListOf<HTMLTableElement>) => void; |
This file contains 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,68 @@ | ||
/** | ||
* 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. | ||
*/ | ||
var enhanceSortableAccessibility = function (tables) { | ||
/** | ||
* 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, default_direction) { | ||
var _a; | ||
if (default_direction === void 0) { default_direction = ''; } | ||
// Generate aria-label based on header content | ||
var header_text = element.textContent || 'element'; | ||
var current_direction = (_a = element.getAttribute('aria-sort')) !== null && _a !== void 0 ? _a : ''; | ||
var new_direction = 'descending'; | ||
if (current_direction === 'descending' || (default_direction && current_direction !== 'ascending')) { | ||
new_direction = 'ascending'; | ||
} | ||
var aria_label = "Click to sort table by ".concat(header_text, " in ").concat(new_direction, " order"); | ||
element.setAttribute('aria-label', aria_label); | ||
// element.setAttribute('title', aria_label) REMEMBER TO COMMENT OUT WHEN NOT TESTING!! | ||
} | ||
/** | ||
* Handles keyboard events on table header cells and triggers a click event when the Enter key is pressed. | ||
* @param event - The keyboard event to handle. | ||
*/ | ||
function handleKeyDown(event) { | ||
if (event.key === 'Enter') { | ||
var element = event.target; | ||
element.click(); | ||
} | ||
} | ||
// Iterate over each table in the input list | ||
tables.forEach(function (table) { | ||
var default_direction = table.classList.contains('asc') ? 'ascending' : ''; | ||
var headers = table.querySelectorAll('th'); | ||
// Iterate over each header cell in the table | ||
headers.forEach(function (header) { | ||
var element = header; | ||
// Skip if the header cell already has a tabindex attribute | ||
if (element.hasAttribute('tabindex')) | ||
return; | ||
var update = function () { | ||
updateAriaLabel(element, default_direction); | ||
}; | ||
// Add tabindex attribute and generate initial aria-label attribute | ||
element.setAttribute('tabindex', '0'); | ||
update(); | ||
// Attach click event listener to update aria-label attribute | ||
element.addEventListener('click', function () { | ||
// Add a delay to allow the new sort order to be applied | ||
setTimeout(update, 50); | ||
}); | ||
// Attach focus event listener to update aria-label attribute | ||
element.addEventListener('focus', update); | ||
// Attach keyboard event listener to trigger click event | ||
element.addEventListener('keydown', handleKeyDown); | ||
}); | ||
}); | ||
}; | ||
|
||
export { enhanceSortableAccessibility }; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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 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 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 @@ | ||
export {}; |
This file contains 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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,38 @@ | ||
/** | ||
* sortable v3.2.0 | ||
* | ||
* https://www.npmjs.com/package/sortable-tablesort | ||
* https://github.com/tofsjonas/sortable | ||
* | ||
* Makes html tables sortable, No longer ie9+ 😢 | ||
* | ||
* Styling is done in css. | ||
* | ||
* Copyleft 2017 Jonas Earendel | ||
* | ||
* This is free and unencumbered software released into the public domain. | ||
* | ||
* Anyone is free to copy, modify, publish, use, compile, sell, or | ||
* distribute this software, either in source code form or as a compiled | ||
* binary, for any purpose, commercial or non-commercial, and by any | ||
* means. | ||
* | ||
* In jurisdictions that recognize copyright laws, the author or authors | ||
* of this software dedicate any and all copyright interest in the | ||
* software to the public domain. We make this dedication for the benefit | ||
* of the public at large and to the detriment of our heirs and | ||
* successors. We intend this dedication to be an overt act of | ||
* relinquishment in perpetuity of all present and future rights to this | ||
* software under copyright law. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. | ||
* IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR | ||
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, | ||
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | ||
* OTHER DEALINGS IN THE SOFTWARE. | ||
* | ||
* For more information, please refer to <http://unlicense.org> | ||
* | ||
*/ |
This file contains 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 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 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 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 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 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