diff --git a/CHANGELOG.md b/CHANGELOG.md index 8b03512cdec..ca9dc966a0a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,16 @@ All notable changes for each version of this project will be documented in this file. +## 22.4.0 + +### New Features + +- `IgxListComponent`, `IgxListItemComponent` + - The new `selectable` input makes the list an accessible listbox (#17952): the list gets `role="listbox"`, items get `role="option"` with `aria-selected` from their `selected` state, and the list has a single tab stop on the option that was focused last, else the first selected one, else the first one. Arrow Up/Down and Home/End move between options, and Enter/Space click the focused option, so `itemClicked` fires. While `isLoading` is set, the list has `aria-busy="true"`. While the list has no options, the list itself is the tab stop and its empty or loading message is read as an option. The focused option shows an inset focus ring, also when it is selected, colored by the new `item-focus-outline-color` token of the list theme. Lists that are not `selectable` are unchanged. + - In a listbox, headers act as group names: each option points to the header above it with `aria-describedby`, so screen readers announce the group with every option, and the headers themselves are hidden from screen readers. An option that has its own `aria-describedby`, set or bound in the template, keeps it. + - **Deprecation** - The `role` input of the list has been deprecated. The role of the list is `list`, and `listbox` when the list is `selectable`, so use `selectable` to make the list a listbox instead. + - Recommended: give a listbox an `aria-label`, so it is announced when the focus moves into the list. Set `aria-multiselectable="true"` when more than one item can be selected, and avoid focusable controls inside the items. If an item needs a selection control, such as a checkbox, make it `inert` so it does not interfere with the keyboard navigation and selection, and bind it to the item's selected state. + ## 22.3.1 ### Bug Fixes diff --git a/package-lock.json b/package-lock.json index b660d19f6f8..51a6e3799f1 100644 --- a/package-lock.json +++ b/package-lock.json @@ -80,7 +80,7 @@ "igniteui-grid-lite": "~0.12.0", "igniteui-i18n-resources": "^2.0.0", "igniteui-sassdoc-theme": "^3.0.0", - "igniteui-theming": "^30.2.0", + "igniteui-theming": "^30.3.0", "igniteui-webcomponents": "^7.5.0", "lit-html": "^3.2.1", "ng-packagr": "^22.0.0", @@ -9609,13 +9609,6 @@ "ieee754": "^1.1.13" } }, - "node_modules/buffer-builder": { - "version": "0.2.0", - "resolved": "https://registry.npmjs.org/buffer-builder/-/buffer-builder-0.2.0.tgz", - "integrity": "sha512-7VPMEPuYznPSoR21NE1zvd2Xna6c/CloiZCfcMXR1Jny6PjX0N4Nsa38zcBFo/FMK+BlA+FLKbJCQ0i2yxp+Xg==", - "dev": true, - "license": "MIT/X11" - }, "node_modules/buffer-equal": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/buffer-equal/-/buffer-equal-1.0.1.tgz", @@ -14892,429 +14885,27 @@ } }, "node_modules/igniteui-theming": { - "version": "30.2.0", - "resolved": "https://registry.npmjs.org/igniteui-theming/-/igniteui-theming-30.2.0.tgz", - "integrity": "sha512-nYdHKYG1FjuzB3rpz6sLlc2sYTR4HOABB3wuWokQwZj8i/cfvoBbJrsH9JESXGwcyYuq++3ZaaFG4yWBWEZG7Q==", + "version": "30.3.0", + "resolved": "https://registry.npmjs.org/igniteui-theming/-/igniteui-theming-30.3.0.tgz", + "integrity": "sha512-wBA819JhqdDYkCUF6Y/EE1n93rfflJuYEZhd1U28zvQlRBBaM702BLoNb1pno1tziQ63BaXqHv1QMjPpn0i8iQ==", "dev": true, "license": "MIT", "dependencies": { "@modelcontextprotocol/sdk": "^1.32.1", - "sass-embedded": "~1.92.1", + "sass-embedded": "^1.97.1", "zod": "^4.6.7" }, "bin": { "igniteui-theming": "dist/mcp/index.js", "igniteui-theming-mcp": "dist/mcp/index.js" - } - }, - "node_modules/igniteui-theming/node_modules/chokidar": { - "version": "4.0.3", - "resolved": "https://registry.npmjs.org/chokidar/-/chokidar-4.0.3.tgz", - "integrity": "sha512-Qgzu8kfBvo+cA4962jnP1KkS6Dop5NS6g7R5LFYJr4b8Ub94PPQXUksCw9PvXoeXPRRddRNC5C1JQUR2SMGtnA==", - "dev": true, - "license": "MIT", - "optional": true, - "dependencies": { - "readdirp": "^4.0.1" - }, - "engines": { - "node": ">= 14.16.0" - }, - "funding": { - "url": "https://paulmillr.com/funding/" - } - }, - "node_modules/igniteui-theming/node_modules/colorjs.io": { - "version": "0.5.2", - "resolved": "https://registry.npmjs.org/colorjs.io/-/colorjs.io-0.5.2.tgz", - "integrity": "sha512-twmVoizEW7ylZSN32OgKdXRmo1qg+wT5/6C3xu5b9QsWzSFAhHLn2xd8ro0diCsKfCj1RdaTP/nrcW+vAoQPIw==", - "dev": true, - "license": "MIT" - }, - "node_modules/igniteui-theming/node_modules/readdirp": { - "version": "4.1.2", - "resolved": "https://registry.npmjs.org/readdirp/-/readdirp-4.1.2.tgz", - "integrity": "sha512-GDhwkLfywWL2s6vEjyhri+eXmfH6j1L7JE27WhqLeYzoh/A3DBaYGEj2H/HFZCn/kMfim73FXxEJTw06WtxQwg==", - "dev": true, - "license": "MIT", - "optional": true, - "engines": { - "node": ">= 14.18.0" }, - "funding": { - "type": "individual", - "url": "https://paulmillr.com/funding/" - } - }, - "node_modules/igniteui-theming/node_modules/sass": { - "version": "1.92.1", - "resolved": "https://registry.npmjs.org/sass/-/sass-1.92.1.tgz", - "integrity": "sha512-ffmsdbwqb3XeyR8jJR6KelIXARM9bFQe8A6Q3W4Klmwy5Ckd5gz7jgUNHo4UOqutU5Sk1DtKLbpDP0nLCg1xqQ==", - "dev": true, - "license": "MIT", - "optional": true, - "dependencies": { - "chokidar": "^4.0.0", - "immutable": "^5.0.2", - "source-map-js": ">=0.6.2 <2.0.0" - }, - "bin": { - "sass": "sass.js" - }, - "engines": { - "node": ">=14.0.0" - }, - "optionalDependencies": { - "@parcel/watcher": "^2.4.1" - } - }, - "node_modules/igniteui-theming/node_modules/sass-embedded": { - "version": "1.92.1", - "resolved": "https://registry.npmjs.org/sass-embedded/-/sass-embedded-1.92.1.tgz", - "integrity": "sha512-28YwLnF5atAhogt3E4hXzz/NB9dwKffyw08a7DEasLh94P7+aELkG3ENSHYCWB9QFN14hYNLfwr9ozUsPDhcDQ==", - "dev": true, - "license": "MIT", - "dependencies": { - "@bufbuild/protobuf": "^2.5.0", - "buffer-builder": "^0.2.0", - "colorjs.io": "^0.5.0", - "immutable": "^5.0.2", - "rxjs": "^7.4.0", - "supports-color": "^8.1.1", - "sync-child-process": "^1.0.2", - "varint": "^6.0.0" - }, - "bin": { - "sass": "dist/bin/sass.js" - }, - "engines": { - "node": ">=16.0.0" + "peerDependencies": { + "sass": ">=1.97.1" }, - "optionalDependencies": { - "sass-embedded-all-unknown": "1.92.1", - "sass-embedded-android-arm": "1.92.1", - "sass-embedded-android-arm64": "1.92.1", - "sass-embedded-android-riscv64": "1.92.1", - "sass-embedded-android-x64": "1.92.1", - "sass-embedded-darwin-arm64": "1.92.1", - "sass-embedded-darwin-x64": "1.92.1", - "sass-embedded-linux-arm": "1.92.1", - "sass-embedded-linux-arm64": "1.92.1", - "sass-embedded-linux-musl-arm": "1.92.1", - "sass-embedded-linux-musl-arm64": "1.92.1", - "sass-embedded-linux-musl-riscv64": "1.92.1", - "sass-embedded-linux-musl-x64": "1.92.1", - "sass-embedded-linux-riscv64": "1.92.1", - "sass-embedded-linux-x64": "1.92.1", - "sass-embedded-unknown-all": "1.92.1", - "sass-embedded-win32-arm64": "1.92.1", - "sass-embedded-win32-x64": "1.92.1" - } - }, - "node_modules/igniteui-theming/node_modules/sass-embedded-all-unknown": { - "version": "1.92.1", - "resolved": "https://registry.npmjs.org/sass-embedded-all-unknown/-/sass-embedded-all-unknown-1.92.1.tgz", - "integrity": "sha512-5t6/YZf+vhO3OY/49h8RCL6Cwo78luva0M+TnTM9gu9ASffRXAuOVLNKciSXa3loptyemDDS6IU5/dVH5w0KmA==", - "cpu": [ - "!arm", - "!arm64", - "!riscv64", - "!x64" - ], - "dev": true, - "license": "MIT", - "optional": true, - "dependencies": { - "sass": "1.92.1" - } - }, - "node_modules/igniteui-theming/node_modules/sass-embedded-android-arm": { - "version": "1.92.1", - "resolved": "https://registry.npmjs.org/sass-embedded-android-arm/-/sass-embedded-android-arm-1.92.1.tgz", - "integrity": "sha512-4EjpVVzuksERdgAd4BqeSXFnWtWN3DSRyEIUPJ7BhcS9sfDh2Gf6miI2kNTvIQLJ2XIJynDDcEQ8a1U9KwKUTQ==", - "cpu": [ - "arm" - ], - "dev": true, - "license": "MIT", - "optional": true, - "os": [ - "android" - ], - "engines": { - "node": ">=14.0.0" - } - }, - "node_modules/igniteui-theming/node_modules/sass-embedded-android-arm64": { - "version": "1.92.1", - "resolved": "https://registry.npmjs.org/sass-embedded-android-arm64/-/sass-embedded-android-arm64-1.92.1.tgz", - "integrity": "sha512-Q+UruGb7yKawHagVmVDRRKsnc4mJZvWMBnuRCu2coJo2FofyqBmXohVGXbxko97sYceA9TJTrUEx3WVKQUNCbQ==", - "cpu": [ - "arm64" - ], - "dev": true, - "license": "MIT", - "optional": true, - "os": [ - "android" - ], - "engines": { - "node": ">=14.0.0" - } - }, - "node_modules/igniteui-theming/node_modules/sass-embedded-android-riscv64": { - "version": "1.92.1", - "resolved": "https://registry.npmjs.org/sass-embedded-android-riscv64/-/sass-embedded-android-riscv64-1.92.1.tgz", - "integrity": "sha512-nCY5btLlX7W7Jc6cCL6D2Yklpiu540EJ2G08YVGu12DrAMCBzqM347CSRf2ojp1H8jyhvmLkaFwnrJWzh+6S+w==", - "cpu": [ - "riscv64" - ], - "dev": true, - "license": "MIT", - "optional": true, - "os": [ - "android" - ], - "engines": { - "node": ">=14.0.0" - } - }, - "node_modules/igniteui-theming/node_modules/sass-embedded-android-x64": { - "version": "1.92.1", - "resolved": "https://registry.npmjs.org/sass-embedded-android-x64/-/sass-embedded-android-x64-1.92.1.tgz", - "integrity": "sha512-qYWR3bftJ77aLYwYDFuzDI4dcwVVixxqQxlIQWNGkHRCexj614qGSSHemr18C2eVj3mjXAQxTQxU68U7pkGPAA==", - "cpu": [ - "x64" - ], - "dev": true, - "license": "MIT", - "optional": true, - "os": [ - "android" - ], - "engines": { - "node": ">=14.0.0" - } - }, - "node_modules/igniteui-theming/node_modules/sass-embedded-darwin-arm64": { - "version": "1.92.1", - "resolved": "https://registry.npmjs.org/sass-embedded-darwin-arm64/-/sass-embedded-darwin-arm64-1.92.1.tgz", - "integrity": "sha512-g2yQ3txjMYLKMjL2cW1xRO9nnV3ijf95NbX/QShtV6tiVUETZNWDsRMDEwBNGYY6PTE/UZerjJL1R/2xpQg6WA==", - "cpu": [ - "arm64" - ], - "dev": true, - "license": "MIT", - "optional": true, - "os": [ - "darwin" - ], - "engines": { - "node": ">=14.0.0" - } - }, - "node_modules/igniteui-theming/node_modules/sass-embedded-darwin-x64": { - "version": "1.92.1", - "resolved": "https://registry.npmjs.org/sass-embedded-darwin-x64/-/sass-embedded-darwin-x64-1.92.1.tgz", - "integrity": "sha512-eH+fgxLQhTEPjZPCgPAVuX5e514Qp/4DMAUMtlNShv4cr4TD5qOp1XlsPYR/b7uE7p2cKFkUpUn/bHNqJ2ay4A==", - "cpu": [ - "x64" - ], - "dev": true, - "license": "MIT", - "optional": true, - "os": [ - "darwin" - ], - "engines": { - "node": ">=14.0.0" - } - }, - "node_modules/igniteui-theming/node_modules/sass-embedded-linux-arm": { - "version": "1.92.1", - "resolved": "https://registry.npmjs.org/sass-embedded-linux-arm/-/sass-embedded-linux-arm-1.92.1.tgz", - "integrity": "sha512-cT3w8yoQTqrtZvWLJeutEGmawITDTY4J6oSVQjeDcPnnoPt0gOFxem8YMznraACXvahw/2+KJDH33BTNgiPo0A==", - "cpu": [ - "arm" - ], - "dev": true, - "license": "MIT", - "optional": true, - "os": [ - "linux" - ], - "engines": { - "node": ">=14.0.0" - } - }, - "node_modules/igniteui-theming/node_modules/sass-embedded-linux-arm64": { - "version": "1.92.1", - "resolved": "https://registry.npmjs.org/sass-embedded-linux-arm64/-/sass-embedded-linux-arm64-1.92.1.tgz", - "integrity": "sha512-dNmlpGeZkry1BofhAdGFBXrpM69y9LlYuNnncf+HfsOOUtj8j0q1RwS+zb5asknhKFUOAG8GCGRY1df7Rwu35g==", - "cpu": [ - "arm64" - ], - "dev": true, - "license": "MIT", - "optional": true, - "os": [ - "linux" - ], - "engines": { - "node": ">=14.0.0" - } - }, - "node_modules/igniteui-theming/node_modules/sass-embedded-linux-musl-arm": { - "version": "1.92.1", - "resolved": "https://registry.npmjs.org/sass-embedded-linux-musl-arm/-/sass-embedded-linux-musl-arm-1.92.1.tgz", - "integrity": "sha512-nPBos6lI31ef2zQhqTZhFOU7ar4impJbLIax0XsqS269YsiCwjhk11VmUloJTpFlJuKMiVXNo7dPx+katxhD/Q==", - "cpu": [ - "arm" - ], - "dev": true, - "license": "MIT", - "optional": true, - "os": [ - "linux" - ], - "engines": { - "node": ">=14.0.0" - } - }, - "node_modules/igniteui-theming/node_modules/sass-embedded-linux-musl-arm64": { - "version": "1.92.1", - "resolved": "https://registry.npmjs.org/sass-embedded-linux-musl-arm64/-/sass-embedded-linux-musl-arm64-1.92.1.tgz", - "integrity": "sha512-TfiEBkCyNzVoOhjHXUT+vZ6+p0ueDbvRw6f4jHdkvljZzXdXMby4wh7BU1odl69rgRTkSvYKhgbErRLDR/F7pQ==", - "cpu": [ - "arm64" - ], - "dev": true, - "license": "MIT", - "optional": true, - "os": [ - "linux" - ], - "engines": { - "node": ">=14.0.0" - } - }, - "node_modules/igniteui-theming/node_modules/sass-embedded-linux-musl-riscv64": { - "version": "1.92.1", - "resolved": "https://registry.npmjs.org/sass-embedded-linux-musl-riscv64/-/sass-embedded-linux-musl-riscv64-1.92.1.tgz", - "integrity": "sha512-R+RcJA4EYpJDE9JM1GgPYgZo7x94FlxZ6jPodOQkEaZ1S9kvXVCuP5X/0PXRPhu08KJOfeMsAElzfdAjUf7KJg==", - "cpu": [ - "riscv64" - ], - "dev": true, - "license": "MIT", - "optional": true, - "os": [ - "linux" - ], - "engines": { - "node": ">=14.0.0" - } - }, - "node_modules/igniteui-theming/node_modules/sass-embedded-linux-musl-x64": { - "version": "1.92.1", - "resolved": "https://registry.npmjs.org/sass-embedded-linux-musl-x64/-/sass-embedded-linux-musl-x64-1.92.1.tgz", - "integrity": "sha512-/HolYRGXJjx8nLw6oj5ZrkR7PFM7X/5kE4MYZaFMpDIPIcw3bqB2fUXLo/MYlRLsw7gBAT6hJAMBrNdKuTphfw==", - "cpu": [ - "x64" - ], - "dev": true, - "license": "MIT", - "optional": true, - "os": [ - "linux" - ], - "engines": { - "node": ">=14.0.0" - } - }, - "node_modules/igniteui-theming/node_modules/sass-embedded-linux-riscv64": { - "version": "1.92.1", - "resolved": "https://registry.npmjs.org/sass-embedded-linux-riscv64/-/sass-embedded-linux-riscv64-1.92.1.tgz", - "integrity": "sha512-b9bxe0CMsbSsLx3nrR0cq8xpIkoAC6X36o4DGMITF3m2v3KsojC7ru9X0Gz+zUFr6rwpq/0lTNzFLNu6sPNo3w==", - "cpu": [ - "riscv64" - ], - "dev": true, - "license": "MIT", - "optional": true, - "os": [ - "linux" - ], - "engines": { - "node": ">=14.0.0" - } - }, - "node_modules/igniteui-theming/node_modules/sass-embedded-linux-x64": { - "version": "1.92.1", - "resolved": "https://registry.npmjs.org/sass-embedded-linux-x64/-/sass-embedded-linux-x64-1.92.1.tgz", - "integrity": "sha512-xuiK5Jp5NldW4bvlC7AuX1Wf7o0gLZ3md/hNg+bkTvxtCDgnUHtfdo8Q+xWP11bD9QX31xXFWpmUB8UDLi6XQQ==", - "cpu": [ - "x64" - ], - "dev": true, - "license": "MIT", - "optional": true, - "os": [ - "linux" - ], - "engines": { - "node": ">=14.0.0" - } - }, - "node_modules/igniteui-theming/node_modules/sass-embedded-unknown-all": { - "version": "1.92.1", - "resolved": "https://registry.npmjs.org/sass-embedded-unknown-all/-/sass-embedded-unknown-all-1.92.1.tgz", - "integrity": "sha512-AT9oXvtNY4N+Nd0wvoWqq9A5HjdH/X3aUH4boQUtXyaJ/9DUwnQmBpP5Gtn028ZS8exOGBdobmmWAuigv0k/OA==", - "dev": true, - "license": "MIT", - "optional": true, - "os": [ - "!android", - "!darwin", - "!linux", - "!win32" - ], - "dependencies": { - "sass": "1.92.1" - } - }, - "node_modules/igniteui-theming/node_modules/sass-embedded-win32-arm64": { - "version": "1.92.1", - "resolved": "https://registry.npmjs.org/sass-embedded-win32-arm64/-/sass-embedded-win32-arm64-1.92.1.tgz", - "integrity": "sha512-KvmpQjY9yTBMtTYz4WBqetlv9bGaDW1aStcu7MSTbH7YiSybX/9fnxlCAEQv1WlIidQhcJAiyk0Eae+LGK7cIQ==", - "cpu": [ - "arm64" - ], - "dev": true, - "license": "MIT", - "optional": true, - "os": [ - "win32" - ], - "engines": { - "node": ">=14.0.0" - } - }, - "node_modules/igniteui-theming/node_modules/sass-embedded-win32-x64": { - "version": "1.92.1", - "resolved": "https://registry.npmjs.org/sass-embedded-win32-x64/-/sass-embedded-win32-x64-1.92.1.tgz", - "integrity": "sha512-B6Nz/GbH7Vkpb2TkQHsGcczWM5t+70VWopWF1x5V5yxLpA8ZzVQ7NTKKi+jDoVY2Efu6ZyzgT9n5KgG2kWliXA==", - "cpu": [ - "x64" - ], - "dev": true, - "license": "MIT", - "optional": true, - "os": [ - "win32" - ], - "engines": { - "node": ">=14.0.0" + "peerDependenciesMeta": { + "sass": { + "optional": true + } } }, "node_modules/igniteui-theming/node_modules/zod": { diff --git a/package.json b/package.json index 78aa64625dd..00c896e3252 100644 --- a/package.json +++ b/package.json @@ -136,7 +136,7 @@ "igniteui-grid-lite": "~0.12.0", "igniteui-i18n-resources": "^2.0.0", "igniteui-sassdoc-theme": "^3.0.0", - "igniteui-theming": "^30.2.0", + "igniteui-theming": "^30.3.0", "igniteui-webcomponents": "^7.5.0", "lit-html": "^3.2.1", "ng-packagr": "^22.0.0", diff --git a/projects/igniteui-angular/list/README.md b/projects/igniteui-angular/list/README.md index 80dce3a1abc..b399a0fbff4 100644 --- a/projects/igniteui-angular/list/README.md +++ b/projects/igniteui-angular/list/README.md @@ -67,6 +67,58 @@ In order to access its elements, the list provides the following: - an array with items only - an array with headers only +### Listbox and keyboard navigation +Set `selectable` on the list to make it an accessible [listbox](https://www.w3.org/WAI/ARIA/apg/patterns/listbox/): + +- The list gets `role="listbox"`. +- Items get `role="option"` with `aria-selected`, from their `selected` state. +- The list has a single tab stop. Tab focuses the option that was focused last, else the first selected option, else the first option, and the next Tab leaves the list. Headers and hidden items are skipped. +- Headers act as group names: they are hidden from screen readers, and each option points to the header above it with `aria-describedby`. A header without an `id` gets one. A screen reader reads the option content followed by the group name, e.g. "Kate, selected, 1 of 8, Employees". An option that has its own `aria-describedby`, set or bound in the template, keeps it. +- The focused option shows an inset focus ring, also when it is selected. Its color is the `item-focus-outline-color` of the list theme. +- While `isLoading` is set, the list has `aria-busy="true"`. +- While the list has no options, the list itself is the tab stop and shows the focus ring, and its empty or loading message, the default or a custom template, is read as an option. When options are added while the list has the focus, the focus moves to them. While the list has options, the loading message is hidden from screen readers. + +| Key | Action | +| :--- | :--- | +| Arrow Down / Arrow Up | Next / previous option | +| Home / End | First / last option | +| Enter / Space | Clicks the option, so `(click)` and `itemClicked` fire. Holding the key down clicks once. | + +When an option is activated with Enter or Space, the `event` of `itemClicked` is the synthetic `MouseEvent` of that click. + +```html + + @for (item of items(); track item.id) { + + {{ item.name }} + + } + +``` + +Recommendations: + +- **Name the list.** Set `aria-label`, or `aria-labelledby` pointing to a visible title. When the focus moves into the list, screen readers announce its name and role together with the focused option. Without a name, they only announce the role. Headers do not name the list. +- **Mark multi-select lists.** If more than one item can be selected, set `aria-multiselectable="true"` on the list. Without it, screen readers treat the list as single-select and may not announce additional selections. +- **Avoid interactive controls inside items.** An option should not contain buttons, links or checkboxes that can be focused. To show the selection with a checkbox, make it `inert` and bind it to the item state, so clicking it selects the item: + +```html + + @for (employee of employees; track employee.id) { + + {{ employee.name }} + + + } + +``` + +If an item really needs a button or another control for a separate action, such as deleting the item, call `$event.stopPropagation()` in the control's `(click)` handler, so using it does not select the item. + +Lists that are not `selectable` are unchanged: they are not focusable, and controls inside their items work as before. This includes a list that only sets `role="listbox"`, like the grid's Excel-style filter list, which manages the focus itself with `aria-activedescendant`. + +The keyboard navigation only moves between the rendered items. A list virtualized with `igxFor` is not scrolled to the items that are not rendered, so do not set `selectable` on it. Handle the focus and the keys of a virtualized list in the app with `aria-activedescendant`, like the grid's Excel-style filter list does. + ### Empty list template By default if there are no items in the list, the default empty list template will be displayed. @@ -120,6 +172,7 @@ public rightPanPerformed(args) { | allowRightPanning | Determines whether the right panning of an item is allowed | | emptyListTemplate | Sets a reference to a custom empty list template, otherwise default template is used | | dataLoadingTemplate | Sets a reference to a custom data loading template, otherwise default template is used | +| selectable | Whether the user selects items in the list, default _false_. Makes the list an accessible [listbox](#listbox-and-keyboard-navigation) with keyboard navigation | | panEndTriggeringThreshold | Number | Specifies the threshold after which a panning event is emitted. By default this property has a value of 0.5 which means 50% of list item's width. | ### Properties @@ -130,7 +183,7 @@ public rightPanPerformed(args) { | items | Array of items in the list | | headers | Array of headers in the list | | innerStyle | Currently used inner style depending on whether the list is empty or not | -| role | Gets the role of the list | +| role | **Deprecated**, use `selectable` instead. Gets/sets the role of the list, default _list_, _listbox_ when the list is `selectable` | ### Outputs @@ -141,7 +194,7 @@ public rightPanPerformed(args) { | panStateChange | Triggered when pan gesture is executed on list item | | leftPan | Triggered when left pan gesture is executed on list item | | rightPan | Triggered when right pan gesture is executed on list item | -| itemClicked | Triggered when a list item has been clicked | +| itemClicked | Triggered when a list item is clicked, or activated with Enter or Space | ---------- @@ -184,6 +237,7 @@ All list items implement `IListChild`. | index | The index of item in children collection | | hidden | Determines whether the item should be displayed | | isHeader | Determines whether the item should be displayed as a header, default value is _false_ | +| selected | Whether the item is selected, default _false_. In a [listbox](#listbox-and-keyboard-navigation) it sets `aria-selected` | ### Directives @@ -202,7 +256,7 @@ All list items implement `IListChild`. | :--- | :--- | | panState | Gets the item's pan state | | list | Gets the list that is associated with the item | -| role | Gets the role of the item within its respective list - _separator_ if isHeader is true and _listitem_ otherwise | +| role | Gets/sets the role of the item - _separator_ for headers; for items _option_ in a listbox, _listitem_ otherwise | | element | Gets the native element that is associated with the item | | width | Gets the width of the item | | maxLeft | Gets the maximum left position of the item | diff --git a/projects/igniteui-angular/list/src/list/list-item.component.ts b/projects/igniteui-angular/list/src/list/list-item.component.ts index e6aff5eccb0..cddd51d4c12 100644 --- a/projects/igniteui-angular/list/src/list/list-item.component.ts +++ b/projects/igniteui-angular/list/src/list/list-item.component.ts @@ -3,12 +3,15 @@ import { ChangeDetectionStrategy, Component, ElementRef, HostBinding, HostListen import { IgxListPanState, IListChild, - IgxListBaseDirective + IgxListBaseDirective, + syncAttribute } from './list.common'; import { rem, IgxTouchManager, IgxGestureEvent } from 'igniteui-angular/core'; import { NgTemplateOutlet } from '@angular/common'; +let NEXT_HEADER_ID = 0; + /** * The Ignite UI List Item component is a container intended for row items in the Ignite UI for Angular List component. * @@ -40,6 +43,12 @@ export class IgxListItemComponent implements IListChild, OnInit, OnDestroy { */ private _gestures: IgxTouchManager | null = null; + /** The `aria-describedby` the list set last, to tell it apart from one set or bound by the app, which is kept. */ + private _describedBy: string | null = null; + + /** The `id` the item generated as a header of a listbox, so it is removed when the item is no longer one. */ + private _headerId: string | null = null; + /** * Provides a reference to the template's base element shown when left panning a list item. * ```typescript @@ -258,6 +267,7 @@ export class IgxListItemComponent implements IListChild, OnInit, OnDestroy { /** * Gets/Sets the `role` attribute of the `list item`. + * Defaults to `separator` for headers, and for items to `option` in a listbox and to `listitem` otherwise. * ```typescript * let itemRole = this.listItem.role; * ``` @@ -267,7 +277,7 @@ export class IgxListItemComponent implements IListChild, OnInit, OnDestroy { @HostBinding('attr.role') @Input() public get role() { - return this._role ? this._role : this.isHeader ? 'separator' : 'listitem'; + return this._role ? this._role : this.isHeader ? 'separator' : this.list.isListbox ? 'option' : 'listitem'; } public set role(val: string) { @@ -303,6 +313,42 @@ export class IgxListItemComponent implements IListChild, OnInit, OnDestroy { this._selected = value; } + /** + * @hidden + * @internal + * Syncs the listbox attributes of the item. Options expose their selected state, only the active one is a tab stop, + * and they are described by the header of their group, unless they have their own `aria-describedby`. + * Headers are hidden from assistive technology and get an `id` when they have none, so options can point to them. + * The generated `id` is removed when the list is no longer a listbox. + * Called by the list after rendering. + * + * @param listbox Whether the list is a listbox. + * @param tabStop Whether the item is the active option. + * @param header The header of the item's group. + */ + public updateAttributes(listbox: boolean, tabStop: boolean, header: IgxListItemComponent | null) { + const option = listbox && !this.isHeader; + syncAttribute(this.element, 'tabindex', option ? (tabStop ? '0' : '-1') : null); + syncAttribute(this.element, 'aria-selected', option ? String(this.selected) : null); + syncAttribute(this.element, 'aria-hidden', listbox && this.isHeader ? 'true' : null); + if (listbox && this.isHeader) { + if (!this.element.id) { + this._headerId = `igx-list-header-${NEXT_HEADER_ID++}`; + this.element.id = this._headerId; + } + } else if (this._headerId !== null) { + if (this.element.id === this._headerId) { + this.element.removeAttribute('id'); + } + this._headerId = null; + } + const describedBy = this.element.getAttribute('aria-describedby'); + if (describedBy === null || describedBy === this._describedBy) { + this._describedBy = (option && header?.element.id) || null; + syncAttribute(this.element, 'aria-describedby', this._describedBy); + } + } + /** * Indicates whether `list item` should have header style. * ```typescript @@ -336,6 +382,12 @@ export class IgxListItemComponent implements IListChild, OnInit, OnDestroy { return false; } + /** @hidden @internal Applies the option style when the item is an option of a listbox. */ + @HostBinding('class.igx-list-item--option') + public get optionStyle(): boolean { + return !this.isHeader && this.list.isListbox; + } + /** * Returns string value which describes the display mode of the `list item`. * ```typescript diff --git a/projects/igniteui-angular/list/src/list/list.common.ts b/projects/igniteui-angular/list/src/list/list.common.ts index 7a31b42f80f..1c1c77305ad 100644 --- a/projects/igniteui-angular/list/src/list/list.common.ts +++ b/projects/igniteui-angular/list/src/list/list.common.ts @@ -25,6 +25,30 @@ export class IgxListBaseDirective { public children!: QueryList; public listItemLeftPanningTemplate!: IgxListItemLeftPanningTemplateDirective; public listItemRightPanningTemplate!: IgxListItemRightPanningTemplateDirective; + + /** + * @hidden + * @internal + * Whether the list follows the ARIA listbox pattern. + */ + public get isListbox(): boolean { + return false; + } +} + +/** + * @hidden + * Sets an attribute, or removes it with `null`, only when its value changes. + */ +export function syncAttribute(element: Element, name: string, value: string | null) { + if (element.getAttribute(name) === value) { + return; + } + if (value === null) { + element.removeAttribute(name); + } else { + element.setAttribute(name, value); + } } export enum IgxListPanState { NONE, LEFT, RIGHT } diff --git a/projects/igniteui-angular/list/src/list/list.component.html b/projects/igniteui-angular/list/src/list/list.component.html index 46bafca512b..16f5a8454fa 100644 --- a/projects/igniteui-angular/list/src/list/list.component.html +++ b/projects/igniteui-angular/list/src/list/list.component.html @@ -13,6 +13,10 @@ @if (!children || children.length === 0 || isLoading) { - - +
+ + +
} diff --git a/projects/igniteui-angular/list/src/list/list.component.spec.ts b/projects/igniteui-angular/list/src/list/list.component.spec.ts index 47ba0652a09..30c4eb497c8 100644 --- a/projects/igniteui-angular/list/src/list/list.component.spec.ts +++ b/projects/igniteui-angular/list/src/list/list.component.spec.ts @@ -1,11 +1,11 @@ import { NgZone, QueryList } from '@angular/core'; -import { TestBed } from '@angular/core/testing'; +import { ComponentFixture, TestBed } from '@angular/core/testing'; import { By } from '@angular/platform-browser'; import { IgxListItemComponent } from './list-item.component'; import { IgxListPanState } from './list.common'; import { IgxListActionDirective, IgxListComponent, IgxListLineDirective, IgxListLineSubTitleDirective, IgxListLineTitleDirective, IgxListThumbnailDirective, IListItemClickEventArgs } from './list.component'; -import { ListWithHeaderComponent, ListWithPanningComponent, EmptyListComponent, CustomEmptyListComponent, ListLoadingComponent, ListWithPanningTemplatesComponent, ListCustomLoadingComponent, ListWithIgxForAndScrollingComponent, TwoHeadersListComponent, TwoHeadersListNoPanningComponent, ListDirectivesComponent, ListWithSelectedItemComponent } from '../../../test-utils/list-components.spec'; +import { ListWithHeaderComponent, ListWithPanningComponent, EmptyListComponent, CustomEmptyListComponent, ListLoadingComponent, ListWithPanningTemplatesComponent, ListCustomLoadingComponent, ListWithIgxForAndScrollingComponent, TwoHeadersListComponent, TwoHeadersListNoPanningComponent, ListDirectivesComponent, ListWithSelectedItemComponent, ListboxComponent, GroupedListboxComponent, ListWithOwnAttributesComponent, ListWithBoundAttributesComponent, EmptyListboxComponent } from '../../../test-utils/list-components.spec'; import { nextFrame } from '../../../test-utils/ui-interactions.spec'; import { advance, useFakeClock } from '../../../test-utils/helper-utils.spec'; import { GridFunctions } from '../../../test-utils/grid-functions.spec'; @@ -31,7 +31,11 @@ describe('List', () => { ListWithPanningTemplatesComponent, ListWithIgxForAndScrollingComponent, ListDirectivesComponent, - ListWithSelectedItemComponent + ListWithSelectedItemComponent, + ListboxComponent, + GroupedListboxComponent, + ListWithOwnAttributesComponent, + ListWithBoundAttributesComponent ] }).compileComponents(); }); @@ -758,6 +762,334 @@ describe('List', () => { expect(headerItem.element.classList.contains('igx-list__item-base--selected')).toBe(false); }); + describe('Listbox', () => { + let fixture: ComponentFixture; + let list: IgxListComponent; + + const pressKey = (target: HTMLElement, key: string, repeat = false): KeyboardEvent => { + const event = new KeyboardEvent('keydown', { key, repeat, bubbles: true, cancelable: true, composed: true }); + target.dispatchEvent(event); + return event; + }; + + beforeEach(() => { + fixture = TestBed.createComponent(ListboxComponent); + list = fixture.componentInstance.list; + fixture.detectChanges(); + }); + + it('should leave a list with role listbox that is not selectable unchanged', () => { + const ownFixture = TestBed.createComponent(ListWithOwnAttributesComponent); + ownFixture.detectChanges(); + const ownList = ownFixture.componentInstance.list; + const [header, item] = ownList.children.toArray(); + + expect(ownList.element.nativeElement.getAttribute('tabindex')).toBe('0'); + expect(header.element.getAttribute('aria-hidden')).toBe('true'); + expect(item.element.getAttribute('tabindex')).toBe('-1'); + expect(item.element.getAttribute('aria-selected')).toBe('true'); + expect(item.element.classList).not.toContain('igx-list-item--option'); + + ownList.isLoading = true; + ownFixture.detectChanges(); + expect(ownList.element.nativeElement.hasAttribute('aria-busy')).toBe(false); + + const clickSpy = vi.fn(); + ownList.itemClicked.subscribe(clickSpy); + expect(pressKey(item.element, 'ArrowDown').defaultPrevented).toBe(false); + expect(pressKey(item.element, ' ').defaultPrevented).toBe(false); + expect(clickSpy).not.toHaveBeenCalled(); + }); + + it('should keep the attributes bound by the app when the list is not a listbox', () => { + const boundFixture = TestBed.createComponent(ListWithBoundAttributesComponent); + boundFixture.detectChanges(); + const boundList = boundFixture.componentInstance.list; + const [header, item] = boundList.children.toArray(); + + expect(boundList.element.nativeElement.getAttribute('tabindex')).toBe('0'); + expect(header.element.getAttribute('aria-hidden')).toBe('true'); + expect(item.element.getAttribute('tabindex')).toBe('0'); + expect(item.element.getAttribute('aria-selected')).toBe('true'); + expect(item.element.getAttribute('aria-describedby')).toBe('Note'); + + boundFixture.componentInstance.tabIndex = '-1'; + boundFixture.componentInstance.selected = 'false'; + boundFixture.detectChanges(); + expect(boundList.element.nativeElement.getAttribute('tabindex')).toBe('-1'); + expect(item.element.getAttribute('tabindex')).toBe('-1'); + expect(item.element.getAttribute('aria-selected')).toBe('false'); + }); + + it('should leave a list that is not a listbox unchanged, even when its items bind selected', () => { + const plainFixture = TestBed.createComponent(ListboxComponent); + plainFixture.componentInstance.listbox = false; + plainFixture.detectChanges(); + const plainList = plainFixture.componentInstance.list; + const listElement = plainList.element.nativeElement as HTMLElement; + + expect(listElement.getAttribute('role')).toBe('list'); + expect(listElement.hasAttribute('tabindex')).toBe(false); + plainList.headers.forEach(header => { + expect(header.element.getAttribute('role')).toBe('separator'); + expect(header.element.hasAttribute('aria-hidden')).toBe(false); + }); + plainList.items.forEach(item => { + expect(item.element.getAttribute('role')).toBe('listitem'); + expect(item.element.hasAttribute('tabindex')).toBe(false); + expect(item.element.hasAttribute('aria-selected')).toBe(false); + expect(item.element.hasAttribute('aria-describedby')).toBe(false); + }); + expect(pressKey(plainList.items[0].element, 'ArrowDown').defaultPrevented).toBe(false); + }); + + it('should be a listbox that only owns options and hidden headers, and exposes their selected state', () => { + const listElement = list.element.nativeElement as HTMLElement; + const [header, ...items] = list.children.toArray(); + const owned = Array.from(listElement.children).filter(child => child.getAttribute('aria-hidden') !== 'true'); + + expect(listElement.getAttribute('role')).toBe('listbox'); + expect(listElement.hasAttribute('tabindex')).toBe(false); + expect(owned.map(child => child.getAttribute('role'))).toEqual(['option', 'option', 'option']); + expect(header.element.hasAttribute('tabindex')).toBe(false); + expect(header.element.classList).not.toContain('igx-list-item--option'); + expect(items.every(i => i.element.classList.contains('igx-list-item--option'))).toBe(true); + expect(items.map(i => i.element.getAttribute('aria-selected'))).toEqual(['false', 'true', 'false']); + + fixture.componentInstance.selectedId = 3; + fixture.detectChanges(); + expect(items.map(i => i.element.getAttribute('aria-selected'))).toEqual(['false', 'false', 'true']); + }); + + it('should keep a single tab stop on the option focused last, else the selected one, else the first one', () => { + const items = list.items; + const tabIndexes = () => list.items.map(i => i.element.getAttribute('tabindex')); + expect(tabIndexes()).toEqual(['-1', '0', '-1']); + + fixture.componentInstance.selectedId = null; + fixture.detectChanges(); + expect(tabIndexes()).toEqual(['0', '-1', '-1']); + + items[0].element.focus(); + pressKey(items[0].element, 'End'); + fixture.detectChanges(); + expect(document.activeElement).toBe(items[2].element); + expect(tabIndexes()).toEqual(['-1', '-1', '0']); + + items[2].hidden = true; + fixture.detectChanges(); + expect(tabIndexes()).toEqual(['0', '-1', '-1']); + + fixture.componentInstance.items = fixture.componentInstance.items.slice(1); + fixture.detectChanges(); + expect(tabIndexes()).toEqual(['0', '-1']); + }); + + it('should reset the list and its items when it is no longer selectable', () => { + const header = list.children.first.element; + expect(header.id).toMatch(/^igx-list-header-\d+$/); + + fixture.componentInstance.listbox = false; + fixture.detectChanges(); + + const listElement = list.element.nativeElement as HTMLElement; + expect(listElement.getAttribute('role')).toBe('list'); + expect(header.hasAttribute('aria-hidden')).toBe(false); + expect(header.hasAttribute('id')).toBe(false); + list.items.forEach(item => { + expect(item.element.getAttribute('role')).toBe('listitem'); + expect(item.element.hasAttribute('tabindex')).toBe(false); + expect(item.element.hasAttribute('aria-selected')).toBe(false); + expect(item.element.classList).not.toContain('igx-list-item--option'); + }); + }); + + it('should keep the roles set by the app on a list that is not selectable', () => { + fixture.componentInstance.listbox = false; + list.role = 'menu'; + list.items[0].role = 'menuitem'; + fixture.detectChanges(); + + const listElement = list.element.nativeElement as HTMLElement; + expect(listElement.getAttribute('role')).toBe('menu'); + expect(listElement.hasAttribute('tabindex')).toBe(false); + expect(list.items[0].element.getAttribute('role')).toBe('menuitem'); + expect(list.items[1].element.getAttribute('role')).toBe('listitem'); + list.items.forEach(item => expect(item.element.hasAttribute('aria-selected')).toBe(false)); + expect(pressKey(list.items[1].element, 'ArrowDown').defaultPrevented).toBe(false); + + list.role = null; + fixture.detectChanges(); + expect(listElement.hasAttribute('role')).toBe(false); + }); + + it('should describe every option with the header of its group, unless it has its own aria-describedby', () => { + const groupedFixture = TestBed.createComponent(GroupedListboxComponent); + groupedFixture.detectChanges(); + const groupedList = groupedFixture.componentInstance.list; + const [employees, contractors] = groupedList.headers; + const [ungrouped, item1, item2, item3, item4] = groupedList.items; + + expect(employees.element.id).toBeTruthy(); + expect(contractors.element.id).toBeTruthy(); + expect(ungrouped.element.hasAttribute('aria-describedby')).toBe(false); + expect(item1.element.getAttribute('aria-describedby')).toBe(employees.element.id); + expect(item2.element.getAttribute('aria-describedby')).toBe('item-2-note'); + expect(item3.element.getAttribute('aria-describedby')).toBe('item-3-note'); + expect(item4.element.getAttribute('aria-describedby')).toBe(contractors.element.id); + + groupedFixture.componentInstance.note = 'item-3-other-note'; + groupedFixture.detectChanges(); + expect(item3.element.getAttribute('aria-describedby')).toBe('item-3-other-note'); + + groupedFixture.componentInstance.note = null; + groupedFixture.detectChanges(); + expect(item3.element.getAttribute('aria-describedby')).toBe(employees.element.id); + }); + + it('should be the tab stop with its empty or loading message as an option while it has no options', () => { + const emptyFixture = TestBed.createComponent(EmptyListboxComponent); + emptyFixture.detectChanges(); + const emptyList = emptyFixture.componentInstance.list; + const listElement = emptyList.element.nativeElement as HTMLElement; + const message = () => listElement.querySelector('.igx-list__message-wrapper') as HTMLElement; + const owned = () => Array.from(listElement.children).filter(child => child.getAttribute('aria-hidden') !== 'true'); + + expect(listElement.getAttribute('tabindex')).toBe('0'); + expect(listElement.classList).toContain('igx-list--focusable'); + expect(owned()).toEqual([message()]); + expect(message().getAttribute('role')).toBe('option'); + expect(message().textContent.trim()).toBe(emptyList.resourceStrings.igx_list_no_items); + + emptyFixture.componentInstance.isLoading = true; + emptyFixture.detectChanges(); + expect(listElement.getAttribute('aria-busy')).toBe('true'); + expect(owned()).toEqual([message()]); + expect(message().getAttribute('role')).toBe('option'); + expect(message().textContent.trim()).toBe('Loading data...'); + + listElement.focus(); + emptyFixture.componentInstance.items = ['Item 1', 'Item 2']; + emptyFixture.detectChanges(); + expect(listElement.hasAttribute('tabindex')).toBe(false); + expect(listElement.classList).not.toContain('igx-list--focusable'); + expect(message().hasAttribute('role')).toBe(false); + expect(message().getAttribute('aria-hidden')).toBe('true'); + expect(owned().map(child => child.getAttribute('role'))).toEqual(['option', 'option']); + expect(document.activeElement).toBe(emptyList.items[0].element); + + emptyFixture.componentInstance.isLoading = false; + emptyFixture.detectChanges(); + expect(listElement.hasAttribute('aria-busy')).toBe(false); + expect(message()).toBeNull(); + + emptyFixture.componentInstance.items = []; + emptyFixture.componentInstance.listbox = false; + emptyFixture.detectChanges(); + expect(listElement.hasAttribute('tabindex')).toBe(false); + expect(listElement.classList).not.toContain('igx-list--focusable'); + expect(message().hasAttribute('role')).toBe(false); + expect(message().hasAttribute('aria-hidden')).toBe(false); + }); + + describe('Keyboard navigation', () => { + it('should move the focus with ArrowDown and ArrowUp without wrapping around', () => { + const items = list.items; + items[0].element.focus(); + + const event = pressKey(items[0].element, 'ArrowDown'); + expect(document.activeElement).toBe(items[1].element); + expect(event.defaultPrevented).toBe(true); + + pressKey(items[1].element, 'ArrowDown'); + pressKey(items[2].element, 'ArrowDown'); + expect(document.activeElement).toBe(items[2].element); + + pressKey(items[2].element, 'ArrowUp'); + expect(document.activeElement).toBe(items[1].element); + + pressKey(items[1].element, 'ArrowUp'); + pressKey(items[0].element, 'ArrowUp'); + expect(document.activeElement).toBe(items[0].element); + }); + + it('should move the focus to the first and last item with Home and End', () => { + const items = list.items; + items[1].element.focus(); + + pressKey(items[1].element, 'End'); + expect(document.activeElement).toBe(items[2].element); + + pressKey(items[2].element, 'Home'); + expect(document.activeElement).toBe(items[0].element); + }); + + it('should follow the displayed order of virtualized items', async () => { + const forFixture = TestBed.createComponent(ListWithIgxForAndScrollingComponent); + forFixture.componentInstance.listbox = true; + forFixture.detectChanges(); + await nextFrame(); + // Scrolling down and back up reuses the views, so the children are no longer in the displayed order. + for (const index of [8, 0]) { + forFixture.componentInstance.igxFor.scrollTo(index); + await nextFrame(); + forFixture.detectChanges(); + } + + const items = forFixture.componentInstance.forOfList.items; + items[0].element.focus(); + pressKey(items[0].element, 'ArrowDown'); + expect(document.activeElement).toBe(items[1].element); + expect(items[1].index).toBe(1); + }); + + it('should skip hidden items', () => { + const items = list.items; + items[1].hidden = true; + fixture.detectChanges(); + + items[0].element.focus(); + pressKey(items[0].element, 'ArrowDown'); + expect(document.activeElement).toBe(items[2].element); + }); + + it('should activate the focused item with Enter and Space like a click', () => { + const items = list.items; + const clickedItems: IgxListItemComponent[] = []; + list.itemClicked.subscribe((args: IListItemClickEventArgs) => clickedItems.push(args.item)); + items[1].element.focus(); + + expect(pressKey(items[1].element, 'Enter').defaultPrevented).toBe(true); + expect(pressKey(items[1].element, ' ').defaultPrevented).toBe(true); + expect(pressKey(items[1].element, ' ', true).defaultPrevented).toBe(true); + expect(clickedItems).toEqual([items[1], items[1]]); + }); + + it('should not handle keys pressed in the content of an item, also inside a shadow root, or on the list', () => { + const clickSpy = vi.fn(); + list.itemClicked.subscribe(clickSpy); + const listElement = list.element.nativeElement as HTMLElement; + const option = list.items[0].element; + const title = option.querySelector('[igxListLineTitle]') as HTMLElement; + const shadowHost = document.createElement('div'); + option.appendChild(shadowHost); + const shadowRoot = shadowHost.attachShadow({ mode: 'open' }); + shadowRoot.innerHTML = ''; + const input = shadowRoot.querySelector('input')!; + + expect(pressKey(title, 'ArrowDown').defaultPrevented).toBe(false); + expect(pressKey(title, 'Enter').defaultPrevented).toBe(false); + expect(pressKey(input, 'ArrowDown').defaultPrevented).toBe(false); + expect(pressKey(input, 'Enter').defaultPrevented).toBe(false); + expect(pressKey(input, ' ').defaultPrevented).toBe(false); + expect(pressKey(listElement, ' ').defaultPrevented).toBe(false); + expect(clickSpy).not.toHaveBeenCalled(); + + expect(pressKey(option, 'ArrowDown').defaultPrevented).toBe(true); + }); + }); + }); + it('Initializes igxListThumbnail directive', () => { const fixture = TestBed.createComponent(ListDirectivesComponent); fixture.detectChanges(); diff --git a/projects/igniteui-angular/list/src/list/list.component.ts b/projects/igniteui-angular/list/src/list/list.component.ts index 7c98ebf28f6..0c1985320a4 100644 --- a/projects/igniteui-angular/list/src/list/list.component.ts +++ b/projects/igniteui-angular/list/src/list/list.component.ts @@ -7,6 +7,7 @@ import { EventEmitter, forwardRef, HostBinding, + HostListener, Input, Output, QueryList, @@ -17,7 +18,8 @@ import { inject, DestroyRef, ChangeDetectionStrategy, - ViewEncapsulation + ViewEncapsulation, + afterEveryRender } from '@angular/core'; import { IgxListItemComponent } from './list-item.component'; @@ -27,7 +29,8 @@ import { IgxEmptyListTemplateDirective, IgxListPanState, IgxListItemLeftPanningTemplateDirective, - IgxListItemRightPanningTemplateDirective + IgxListItemRightPanningTemplateDirective, + syncAttribute } from './list.common'; import { IBaseEventArgs } from 'igniteui-angular/core'; import { IListResourceStrings, ListResourceStringsEN } from 'igniteui-angular/core'; @@ -342,6 +345,24 @@ export class IgxListComponent extends IgxListBaseDirective { @Input({ transform: booleanAttribute }) public isLoading = false; + /** + * Sets/gets whether the user selects items in the list. + * + * @remarks + * Default value is `false`. When set, the list follows the ARIA listbox pattern: it has `role="listbox"`, + * its items become options that expose their `selected` state, Tab focuses the selected option, + * Arrow Up/Down and Home/End move between the options, and Enter/Space click the focused option. + * + * @example + * ```html + * + * Kate + * + * ``` + */ + @Input({ transform: booleanAttribute }) + public selectable = false; + /** * Event emitted when a left pan gesture is executed on a list item. * @@ -478,6 +499,7 @@ export class IgxListComponent extends IgxListBaseDirective { constructor() { super(); + afterEveryRender({ write: () => this.updateAttributes() }); onResourceChangeHandle(this.destroyRef, () => { this._defaultResourceStrings = getCurrentResourceStrings(ListResourceStringsEN, false); this._customResourceStrings = this._resourceStrings ? Object.assign({}, this._defaultResourceStrings, this._resourceStrings) : null!; @@ -496,11 +518,75 @@ export class IgxListComponent extends IgxListBaseDirective { return null!; } + /** Whether the list was a listbox at the last render, so its items are reset when it stops being one. */ + private _synced = false; + + /** The option that was focused last, which keeps the tab stop. */ + private _activeItem: IgxListItemComponent | null = null; + + /** The `tabindex` the list set on itself, so a `tabindex` set by the app is kept. */ + private _tabIndex: string | null = null; + + /** + * @hidden + * @internal + * Whether the list has options that are not hidden. Without them, the empty or loading message of a listbox is an option. + */ + protected get hasOptions(): boolean { + return this.ownItems.some(item => !item.isHeader && !item.hidden); + } + + /** The items of this list, without those of nested lists, in the order they are displayed. */ + private get ownItems(): IgxListItemComponent[] { + return this.sortedChildren?.filter(item => item.list === this) ?? []; + } + + /** + * Syncs the listbox attributes of the list and its items, in the order the items are displayed. + * Only the active option is a tab stop: the one focused last, else the first selected option, else the first option. + * Without options, the list itself is the tab stop, and when options are added while it has the focus, the focus moves to them. + * While loading, the listbox is marked busy. A list that is not a listbox, and has not just stopped being one, is left untouched. + */ + private updateAttributes() { + const listbox = this.isListbox; + if (!listbox && !this._synced) { + return; + } + this._synced = listbox; + const element = this.element.nativeElement as HTMLElement; + syncAttribute(element, 'aria-busy', listbox && this.isLoading ? 'true' : null); + const children = this.ownItems; + const options = children.filter(item => !item.isHeader && !item.hidden); + const tabStop = options.includes(this._activeItem!) ? this._activeItem + : options.find(item => item.selected) ?? options[0]; + const tabIndex = element.getAttribute('tabindex'); + const focused = tabIndex !== null && tabIndex === this._tabIndex + && (element.getRootNode() as Document | ShadowRoot).activeElement === element; + if (tabIndex === null || tabIndex === this._tabIndex) { + this._tabIndex = listbox && !tabStop ? '0' : null; + element.classList.toggle('igx-list--focusable', this._tabIndex !== null); + syncAttribute(element, 'tabindex', this._tabIndex); + } + let header: IgxListItemComponent | null = null; + for (const item of children) { + header = item.isHeader ? item : header; + item.updateAttributes(listbox, item === tabStop, header); + } + if (listbox && tabStop && focused) { + tabStop.element.focus(); + } + } + private _role = 'list'; /** * Gets/Sets the `role` attribute value. * + * @remarks + * Defaults to `list`, and is `listbox` when the list is `selectable`. + * + * @deprecated in version 22.4.0. Use `selectable` to make the list a listbox instead. + * * @example * ```typescript * let listRole = this.list.role; @@ -509,13 +595,88 @@ export class IgxListComponent extends IgxListBaseDirective { @HostBinding('attr.role') @Input() public get role() { - return this._role; + return this.selectable ? 'listbox' : this._role; } public set role(val: string) { this._role = val; } + /** + * @hidden + * @internal + * Whether the list follows the ARIA listbox pattern, which it does when it is `selectable`. + */ + public override get isListbox(): boolean { + return this.selectable; + } + + /** + * @hidden + * @internal + * Keeps the tab stop on the option that was focused last. + * + * @param event The focusin event of the list. + */ + @HostListener('focusin', ['$event']) + public onFocusIn(event: FocusEvent) { + const target = event.composedPath()[0]; + this._activeItem = this.ownItems.find(item => item.element === target) ?? this._activeItem; + } + + /** + * @hidden + * @internal + * Moves the focus between the options of a listbox. + * ArrowDown/ArrowUp go to the next/previous option, Home/End to the first/last one. + * Enter/Space activate the focused option like a click, so `itemClicked` and its `click` handlers fire, + * once per key press, also when the key is held down. Keys pressed in the content of an option are not handled. + * + * @param event The keydown event that bubbled up from an option. + */ + @HostListener('keydown', ['$event']) + public onKeydown(event: KeyboardEvent) { + if (!this.isListbox) { + return; + } + + const target = event.composedPath()[0]; + const items = this.ownItems.filter(item => !item.isHeader && !item.hidden); + const current = items.findIndex(item => item.element === target); + if (current === -1) { + return; + } + + if (event.key === 'Enter' || event.key === ' ') { + event.preventDefault(); + if (!event.repeat) { + items[current].element.click(); + } + return; + } + + let next: number; + switch (event.key) { + case 'ArrowDown': + next = Math.min(current + 1, items.length - 1); + break; + case 'ArrowUp': + next = Math.max(current - 1, 0); + break; + case 'Home': + next = 0; + break; + case 'End': + next = items.length - 1; + break; + default: + return; + } + + event.preventDefault(); + items[next].element.focus(); + } + /** * @hidden * @internal diff --git a/projects/igniteui-angular/list/src/list/themes/_base.scss b/projects/igniteui-angular/list/src/list/themes/_base.scss index bdf9bf454b1..078838737c3 100644 --- a/projects/igniteui-angular/list/src/list/themes/_base.scss +++ b/projects/igniteui-angular/list/src/list/themes/_base.scss @@ -42,6 +42,17 @@ $theme: digest-schema($light-list); outline-style: none; } + @include e(message-wrapper) { + display: contents; + } + + @include m(focusable) { + &:focus-visible { + outline: var(--_focus-ring-width, #{rem(1px)}) solid var-get($theme, 'item-focus-outline-color'); + outline-offset: calc(var(--_focus-ring-width, #{rem(1px)}) * -1); + } + } + @include m(empty) { justify-content: center; align-items: center; @@ -177,6 +188,17 @@ $theme: digest-schema($light-list); background: var-get($theme, 'header-background'); } + @include m(option) { + &:focus-visible { + outline: none; + + @include e(content) { + outline: var(--_focus-ring-width, #{rem(1px)}) solid var-get($theme, 'item-focus-outline-color'); + outline-offset: calc(var(--_focus-ring-width, #{rem(1px)}) * -1); + } + } + } + @include m(base) { border-radius: var-get($theme, 'item-border-radius'); border-bottom: var-get($theme, 'border-width') solid diff --git a/projects/igniteui-angular/list/src/list/themes/shared/_indigo.scss b/projects/igniteui-angular/list/src/list/themes/shared/_indigo.scss index 50ae9a13f28..80766aa2b14 100644 --- a/projects/igniteui-angular/list/src/list/themes/shared/_indigo.scss +++ b/projects/igniteui-angular/list/src/list/themes/shared/_indigo.scss @@ -3,6 +3,8 @@ @use '../../../../../core/src/core/styles/themes/scoping' as *; @include themed-block(igx-list, indigo) { + --_focus-ring-width: #{rem(2px)}; + gap: rem(4px); } diff --git a/projects/igniteui-angular/package.json b/projects/igniteui-angular/package.json index dc07f364428..4f23407bf59 100644 --- a/projects/igniteui-angular/package.json +++ b/projects/igniteui-angular/package.json @@ -75,7 +75,7 @@ "igniteui-trial-watermark": "^3.1.0", "jspdf": "^4.2.1", "lodash-es": "^4.17.21", - "igniteui-theming": "^30.2.0", + "igniteui-theming": "^30.3.0", "igniteui-i18n-core": "^2.0.0", "@igniteui/material-icons-extended": "^3.1.0" }, diff --git a/projects/igniteui-angular/test-utils/list-components.spec.ts b/projects/igniteui-angular/test-utils/list-components.spec.ts index 350c756a2f6..6c910016133 100644 --- a/projects/igniteui-angular/test-utils/list-components.spec.ts +++ b/projects/igniteui-angular/test-utils/list-components.spec.ts @@ -84,6 +84,106 @@ export class ListWithHeaderComponent extends BasicListComponent { export class ListWithSelectedItemComponent extends BasicListComponent { } +@Component({ + template: ` +
+ + Header + @for (item of items; track item.id) { + + {{ item.name }} + + } + +
`, + changeDetection: ChangeDetectionStrategy.Eager, + imports: [IgxListComponent, IgxListItemComponent, IgxListLineTitleDirective] +}) +export class ListboxComponent extends BasicListComponent { + public listbox = true; + public selectedId: number | null = 2; + public items = [ + { id: 1, name: 'Item 1' }, + { id: 2, name: 'Item 2' }, + { id: 3, name: 'Item 3' } + ]; +} + +@Component({ + template: ` +
+ + Ungrouped + Employees + Item 1 + Item 2 + Item 3 + Contractors + Item 4 + +
`, + changeDetection: ChangeDetectionStrategy.Eager, + imports: [IgxListComponent, IgxListItemComponent] +}) +export class GroupedListboxComponent extends BasicListComponent { + public note: string | null = 'item-3-note'; +} + +@Component({ + template: ` +
+ + @for (item of items; track item) { + {{ item }} + } + +

Loading data...

+
+
+
`, + changeDetection: ChangeDetectionStrategy.Eager, + imports: [IgxListComponent, IgxListItemComponent, IgxDataLoadingTemplateDirective] +}) +export class EmptyListboxComponent extends BasicListComponent { + public listbox = true; + public isLoading = false; + public items: string[] = []; +} + +@Component({ + template: ` +
+ + + Item 1 + +
`, + changeDetection: ChangeDetectionStrategy.Eager, + imports: [IgxListComponent, IgxListItemComponent] +}) +export class ListWithOwnAttributesComponent extends BasicListComponent { +} + +@Component({ + template: ` +
+ + Header + + Item 1 + + +
`, + changeDetection: ChangeDetectionStrategy.Eager, + imports: [IgxListComponent, IgxListItemComponent] +}) +export class ListWithBoundAttributesComponent extends BasicListComponent { + public tabIndex = '0'; + public hidden = 'true'; + public selected = 'true'; + public description = 'Note'; +} + @Component({ template: `
@@ -216,7 +316,7 @@ export class ListWithPanningTemplatesComponent extends ListWithPanningComponent @Component({ template: ` - +
; + public listbox = false; + public data = [ { key: 1, name: 'John' }, { key: 2, name: 'Brian' }, diff --git a/skills/igniteui-angular-components/references/data-display.md b/skills/igniteui-angular-components/references/data-display.md index 5f0e5102787..d659e50fbdd 100644 --- a/skills/igniteui-angular-components/references/data-display.md +++ b/skills/igniteui-angular-components/references/data-display.md @@ -40,6 +40,18 @@ import { IgxIconComponent } from 'igniteui-angular/icon'; Auxiliary directives for list items: `igxListThumbnail`, `igxListAction`, `igxListLine`, `igxListLineTitle`, `igxListLineSubTitle`. +For a list where the user selects items, set `selectable` on it and name it. It becomes an accessible listbox with one tab stop, arrow key navigation and Enter/Space to click the focused option. Bind `[selected]` on the items, add `aria-multiselectable="true"` when more than one item can be selected, and keep focusable controls out of the items (an `inert` checkbox bound to the state is fine). The `role` input is deprecated, use `selectable` instead. The keyboard navigation only reaches rendered items, so do not use `selectable` on a list virtualized with `igxFor`: + +```html + + @for (contact of contacts; track contact.id) { + + {{ contact.name }} + + } + +``` + ## Tree ```typescript diff --git a/src/app/list/list.sample.html b/src/app/list/list.sample.html index adf5295ae88..116c9abf5f7 100644 --- a/src/app/list/list.sample.html +++ b/src/app/list/list.sample.html @@ -2,7 +2,7 @@
Angular List
- +
deleteDelete @@ -13,10 +13,10 @@
Angular List
callDial
- Employees List + Employees List @for (employee of employeeItems; track employee) { - + @if(properties.addAvatarThumbnail) { } @@ -30,20 +30,11 @@

{{ employee.name }}

{{ employee.position }}

} @if(properties.addCheckboxAction) { - - + } @if(properties.addIconAction) { info } - @if(properties.addOutlinedButton){ - - } - @if(properties.addContainedButton) { - - }
}
@@ -75,12 +66,6 @@
WC List
@if(properties.addIconAction) { } - @if(properties.addOutlinedButton){ - Text - } - @if(properties.addContainedButton) { - Call - } } diff --git a/src/app/list/list.sample.ts b/src/app/list/list.sample.ts index fcc3d3e99d3..2464a02f4dd 100644 --- a/src/app/list/list.sample.ts +++ b/src/app/list/list.sample.ts @@ -9,10 +9,8 @@ import { FormsModule } from '@angular/forms'; import { IGX_LIST_DIRECTIVES, IgxAvatarComponent, - IgxButtonDirective, IgxCheckboxComponent, IgxIconComponent, - IgxButtonModule, } from 'igniteui-angular'; import { defineComponents, @@ -22,7 +20,6 @@ import { IgcListItemComponent, IgcIconComponent, IgcCheckboxComponent, - IgcButtonComponent, registerIconFromText, } from 'igniteui-webcomponents'; import { @@ -38,7 +35,6 @@ defineComponents( IgcAvatarComponent, IgcIconComponent, IgcCheckboxComponent, - IgcButtonComponent ); const icons = [ @@ -77,9 +73,6 @@ interface Employee { IgxIconComponent, IgxCheckboxComponent, IgxAvatarComponent, - IgxButtonModule, - IgxButtonModule, - IgxButtonDirective, ], changeDetection: ChangeDetectionStrategy.Eager }) @@ -127,20 +120,6 @@ export class ListSampleComponent { defaultValue: true } }, - addOutlinedButton: { - label: 'Add Outlined Button', - control: { - type: 'boolean', - defaultValue: false - } - }, - addContainedButton: { - label: 'Add Contained Button', - control: { - type: 'boolean', - defaultValue: false - } - }, enablePanning: { label: 'Item Panning', control: {