Skip to content
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

Create accessible dropdown component #13019

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
64 commits
Select commit Hold shift + click to select a range
679cca1
[POC] Add an accessible dropdown component
rak-phillip Dec 20, 2024
97276b5
Annotate poc
rak-phillip Jan 3, 2025
9c947cb
Expose focus function in RcButton
rak-phillip Jan 7, 2025
edd9ac7
Configure focus and keyboard navigation in RcDropdown
rak-phillip Jan 7, 2025
33b7195
Fix type errors in RcButton and RcDropdown
rak-phillip Jan 7, 2025
3c8a6cc
Close dropdown on activate
rak-phillip Jan 8, 2025
0c4ac90
Update styles
rak-phillip Jan 8, 2025
7b94511
Add dropdown separator component
rak-phillip Jan 8, 2025
7cad4da
Add dropdown trigger component
rak-phillip Jan 8, 2025
21170ca
Replace action menu with new component
rak-phillip Jan 8, 2025
7fedbda
Clean up unused imports and console statements
rak-phillip Jan 9, 2025
11edbc1
Move RcDropdown to Rancher Components
rak-phillip Jan 9, 2025
b4b6c69
Fix types for Rancher Components
rak-phillip Jan 9, 2025
6c9a5a9
Move RcButton to Rancher Components
rak-phillip Jan 9, 2025
69e7409
Add eslint rule to ingore unused variables that begin with `_`
rak-phillip Jan 9, 2025
5d52fa5
Move RcButton types to separate file
rak-phillip Jan 9, 2025
7a410d0
Fix focus-visible styles
rak-phillip Jan 9, 2025
3a654f6
Fix type export
rak-phillip Jan 9, 2025
27ed060
Expose `focusFirstElement()` to the dropdown trigger
rak-phillip Jan 10, 2025
5bc3470
Rename `setTrigger()` to `registerTrigger()`
rak-phillip Jan 10, 2025
cb36b9b
Build the collection of dropdown items in the `RcDropdown` root
rak-phillip Jan 10, 2025
54f5de9
Remove dropdown collection component
rak-phillip Jan 10, 2025
8283a8c
Rename popper slot to dropdownCollection
rak-phillip Jan 10, 2025
3983773
Close dropdown menu when escape is pressed and trigger has focus
rak-phillip Jan 10, 2025
4f4916a
Rename fields to dropdownContainer
rak-phillip Jan 10, 2025
929fa56
Update aria roles, states, and properties for DropdownTrigger
rak-phillip Jan 10, 2025
6bb77d7
Target popperContainer with a ref
rak-phillip Jan 15, 2025
826f5d4
Target first tabbable element in dropdown
rak-phillip Jan 15, 2025
3b24ff9
Replace user menu in header with RcDropdown component
rak-phillip Jan 15, 2025
3e457f1
Add `btn-sm` class to the button component
rak-phillip Jan 22, 2025
4798685
Remove extra padding around buttons in header
rak-phillip Jan 22, 2025
cf0df43
Allow for dropdown menu items to be disabled
rak-phillip Jan 22, 2025
3f0ecd2
Add aria-label to dropdown menu
rak-phillip Jan 22, 2025
1806eb2
Remove RcDropdownCollection from list of exports
rak-phillip Jan 22, 2025
32f630d
Update e2e tests
rak-phillip Jan 23, 2025
ec370fb
Move shared types for RcDropdown into a declaration file
rak-phillip Jan 24, 2025
fdb06a7
Move shared types for RcButton into a declaration file
rak-phillip Jan 24, 2025
5e86983
Improve type inferrence via `InstanceType` and `UseTemplateRef`
rak-phillip Jan 24, 2025
3903a84
Inline single-use functions
rak-phillip Jan 24, 2025
cd0e5d8
Group logic related to providing Dropdown context and collection
rak-phillip Jan 24, 2025
ed1bbdb
Remove unused imports
rak-phillip Jan 24, 2025
13fddf9
Fix issue with displaying user info and associated separator
rak-phillip Jan 24, 2025
2445fb5
Move `showUserMenu()` conditional to Dropdown container
rak-phillip Jan 24, 2025
8a173db
Rename `RcDropdown.d.ts` to `types.ts`
rak-phillip Jan 24, 2025
99bb47b
Update placement of dropdown menu
rak-phillip Jan 24, 2025
6d9b21a
Revert change to RcButton types
rak-phillip Jan 26, 2025
7d9ed8f
Update styles for dropdown items
rak-phillip Jan 28, 2025
b9b1004
Add aria-labels for dropdown triggers
rak-phillip Jan 28, 2025
1dc914b
Add new ghost button role
rak-phillip Jan 29, 2025
a69ad09
Fix refactor regressions
rak-phillip Jan 29, 2025
41a979c
Update styles for dropdown
rak-phillip Jan 29, 2025
827eb66
Add comments to document dropdown implementation
rak-phillip Jan 29, 2025
e5e9cac
Remove the need to provide `dropdownCollection`
rak-phillip Jan 29, 2025
1ee29a4
Focus first element regardless of click or key press
rak-phillip Jan 30, 2025
c889799
Fix border radius for dropdowns
rak-phillip Jan 30, 2025
0fd142c
Add focus styles for dropdown menu item
rak-phillip Jan 30, 2025
cea7caf
Fix hover styles
rak-phillip Jan 30, 2025
98d04e8
Set dropdownitem focus on mouseenter
rak-phillip Jan 30, 2025
000c35a
Add box shadow to dropdown menu
rak-phillip Jan 30, 2025
b4b5b72
Add doc strings with examples to new components
rak-phillip Jan 31, 2025
6ef6147
Update avatar styles
rak-phillip Jan 31, 2025
1ddd61f
Add button role to RcButton
rak-phillip Feb 3, 2025
ca3220a
Remove top/bottom padding for user info
rak-phillip Feb 5, 2025
e9b6d19
Fix regression in NamespaceFilter selected items
rak-phillip Feb 5, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions cypress/e2e/po/side-bars/page-actions.po.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export default class PageActionsPo extends ComponentPo {
* @returns {Cypress.Chainable}
*/
private static pageActionsMenu(): Cypress.Chainable {
return cy.get('body').getId('page-actions-dropdown');
richard-cox marked this conversation as resolved.
Show resolved Hide resolved
return cy.get('body').find('[dropdown-menu-collection]');
}

/**
Expand All @@ -41,7 +41,7 @@ export default class PageActionsPo extends ComponentPo {
*/
links(): Cypress.Chainable {
return PageActionsPo.open().then(() => {
PageActionsPo.pageActionsMenu().find('.user-menu-item');
PageActionsPo.pageActionsMenu().find('[dropdown-menu-item]');
});
}

Expand Down
26 changes: 4 additions & 22 deletions cypress/e2e/po/side-bars/user-menu.po.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,14 @@ export default class UserMenuPo extends ComponentPo {
*
*/
private userMenuContainer() {
return this.self().get('.user-menu');
}

/**
* Our section within the transient userMenuContainer
*/
userMenu(): Cypress.Chainable {
return this.self().getId(`user-menu-dropdown`);
return cy.get('body').find('[dropdown-menu-collection]');
}

/**
* Open the user menu
*
* Multiple clicks because sometimes just one ... isn't enough
*
*/
open(): Cypress.Chainable {
this.self().click();
this.self().click();
this.self().click();
this.self().click();
return cy.getId('nav_header_showUserMenu').should('be.visible').click();
}

/**
Expand All @@ -46,18 +33,13 @@ export default class UserMenuPo extends ComponentPo {
isOpen() {
// These should fail if `visibility: hidden` - https://docs.cypress.io/guides/core-concepts/interacting-with-elements#Visibility
this.userMenuContainer().should('be.visible');
this.userMenu().should('be.visible');
}

ensureOpen() {
// Check the user avatar icon is there
this.checkVisible();

// Yep, these are _horrible_, but flakey user avatar tests have plagued us for months and no-one has yet fixed them
// This is a temporary step until that brave, tenacious champion of e2e resolves the underlying issue.
cy.wait(1000); // eslint-disable-line cypress/no-unnecessary-waiting
this.open();
cy.wait(1000); // eslint-disable-line cypress/no-unnecessary-waiting

// Check the v-popper drop down is open, if not open it
// This isn't a pattern we want to use often, but this area has caused us lots of issues
Expand All @@ -84,15 +66,15 @@ export default class UserMenuPo extends ComponentPo {
* Check if menu is closed
*/
isClosed() {
this.userMenu().should('not.exist');
this.userMenuContainer().should('not.exist');
}

/**
* Get menu items
* @returns
*/
getMenuItems(): Cypress.Chainable {
return this.userMenu().find('li').should('be.visible').and('have.length', 4);
return this.userMenuContainer().find('[dropdown-menu-item]').should('be.visible').and('have.length', 3);
}

/**
Expand Down
8 changes: 8 additions & 0 deletions pkg/rancher-components/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ module.exports = {
'error',
{ 'ts-nocheck': false }
],
'@typescript-eslint/no-unused-vars': [
'warn',
{
argsIgnorePattern: '^_',
richard-cox marked this conversation as resolved.
Show resolved Hide resolved
varsIgnorePattern: '^_',
caughtErrorsIgnorePattern: '^_'
}
],
'vue/one-component-per-file': 'off',
'vue/no-deprecated-slot-attribute': 'off',
'vue/require-explicit-emits': 'off',
Expand Down
90 changes: 90 additions & 0 deletions pkg/rancher-components/src/components/RcButton/RcButton.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<script setup lang="ts">
richard-cox marked this conversation as resolved.
Show resolved Hide resolved
/**
* A button element used for performing actions, such as submitting forms or
* opening dialogs.
*
* Example:
*
* <rc-button primary @click="doAction">Perform an Action</rc-button>
*/
import { computed, ref, defineExpose } from 'vue';
import { ButtonRoleProps, ButtonSizeProps } from './types';

const buttonRoles: { role: keyof ButtonRoleProps, className: string }[] = [
{ role: 'primary', className: 'role-primary' },
{ role: 'secondary', className: 'role-secondary' },
{ role: 'tertiary', className: 'role-tertiary' },
{ role: 'link', className: 'role-link' },
{ role: 'ghost', className: 'role-ghost' },
];

const buttonSizes: { size: keyof ButtonSizeProps, className: string }[] = [
{ size: 'small', className: 'btn-sm' },
];

const props = defineProps<ButtonRoleProps & ButtonSizeProps>();

const buttonClass = computed(() => {
const activeRole = buttonRoles.find(({ role }) => props[role]);
const isButtonSmall = buttonSizes.some(({ size }) => props[size]);

return {
btn: true,

[activeRole?.className || 'role-primary']: true,

'btn-sm': isButtonSmall,
};
});

const RcFocusTarget = ref<HTMLElement | null>(null);

const focus = () => {
RcFocusTarget?.value?.focus();
};

defineExpose({ focus });
</script>

<template>
<button
ref="RcFocusTarget"
role="button"
:class="{ ...buttonClass, ...($attrs.class || { }) }"
>
<slot name="before">
<!-- Empty Content -->
</slot>
<slot>
<!-- Empty Content -->
</slot>
<slot name="after">
<!-- Empty Content -->
</slot>
</button>
</template>

<style lang="scss" scoped>
.role-link {
&:focus, &.focused {
outline: var(--outline-width) solid var(--border);
box-shadow: 0 0 0 var(--outline-width) var(--outline);
}
}

button {
&.role-ghost {
padding: 0;
background-color: transparent;

&:focus, &.focused {
outline: 2px solid var(--primary-keyboard-focus);
outline-offset: 0;
}

&:focus-visible {
outline: 2px solid var(--primary-keyboard-focus);
outline-offset: 0;
}
}
}</style>
2 changes: 2 additions & 0 deletions pkg/rancher-components/src/components/RcButton/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { default as RcButton } from './RcButton.vue';
export type { RcButtonType } from './types';
17 changes: 17 additions & 0 deletions pkg/rancher-components/src/components/RcButton/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// TODO: 13211 Investigate why `InstanceType<typeof RcButton>` fails prod builds
// export type RcButtonType = InstanceType<typeof RcButton>
export type RcButtonType = {
rak-phillip marked this conversation as resolved.
Show resolved Hide resolved
focus: () => void;
}

export type ButtonRoleProps = {
primary?: boolean;
secondary?: boolean;
tertiary?: boolean;
link?: boolean;
ghost?: boolean;
}

export type ButtonSizeProps = {
small?: boolean;
}
111 changes: 111 additions & 0 deletions pkg/rancher-components/src/components/RcDropdown/RcDropdown.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<script setup lang="ts">
/**
* Offers a list of choices to the user, such as a set of actions or functions.
* Opened by activating RcDropdownTrigger.
*
* Example:
*
* <rc-dropdown :aria-label="t('nav.actionMenu.label')">
* <rc-dropdown-trigger tertiary>
* <i class="icon icon-actions" />
* </rc-dropdown-trigger>
* <template #dropdownCollection>
* <rc-dropdown-item @click="performAction()">
* Action 1
* </rc-dropdown-item>
* <rc-dropdown-separator />
* <rc-dropdown-item @click="performAction()">
* Action 2
* </rc-dropdown-item>
* </template>
* </rc-dropdown>
*/
import { useTemplateRef } from 'vue';
import { useClickOutside } from '@shell/composables/useClickOutside';
import { useDropdownContext } from '@components/RcDropdown/useDropdownContext';

defineProps<{
ariaLabel?: string
}>();

const {
isMenuOpen,
showMenu,
returnFocus,
setFocus,
provideDropdownContext,
registerDropdownCollection,
} = useDropdownContext();

provideDropdownContext();

const popperContainer = useTemplateRef<HTMLElement>('popperContainer');
const dropdownTarget = useTemplateRef<HTMLElement>('dropdownTarget');

useClickOutside(dropdownTarget, () => showMenu(false));

const applyShow = () => {
registerDropdownCollection(dropdownTarget.value);
setFocus();
};

</script>

<template>
<v-dropdown
no-auto-focus
:triggers="[]"
:shown="isMenuOpen"
:auto-hide="false"
:container="popperContainer"
:placement="'bottom-end'"
@apply-show="applyShow"
>
<slot name="default">
<!--Empty slot content Trigger-->
</slot>

<template #popper>
<div
ref="dropdownTarget"
role="menu"
aria-orientation="vertical"
dropdown-menu-collection
:aria-label="ariaLabel || 'Dropdown Menu'"
>
<slot name="dropdownCollection">
<!--Empty slot content-->
</slot>
</div>
</template>
</v-dropdown>
<div
ref="popperContainer"
class="popperContainer"
@keydown.tab="showMenu(false)"
@keydown.escape="returnFocus"
>
<!--Empty container for mounting popper content-->
</div>
</template>

<style lang="scss" scoped>
.popperContainer {
display: contents;
&:deep(.v-popper__popper) {

.v-popper__wrapper {
box-shadow: 0px 6px 18px 0px rgba(0, 0, 0, 0.25), 0px 4px 10px 0px rgba(0, 0, 0, 0.15);
border-radius: var(--border-radius-lg);

.v-popper__arrow-container {
display: none;
}

.v-popper__inner {
padding: 10px 0 10px 0;
}
}
}
}
</style>
Loading