diff --git a/packages/react-core/src/components/Drawer/examples/Drawer.md b/packages/react-core/src/components/Drawer/examples/Drawer.md index 1f36db46dad..afb3a2f4cc0 100644 --- a/packages/react-core/src/components/Drawer/examples/Drawer.md +++ b/packages/react-core/src/components/Drawer/examples/Drawer.md @@ -132,7 +132,9 @@ section: components ### With focus trap -Use the `focusTrap` property to enable and customize a focus trap on the ``. Enabling a focus trap with `focusTrap.enabled` will also automatically place focus on the first focusable element when the drawer panel expands, and return focus to the previously focused element when it collapses. +When a [focus trap](/accessibility/product-development-guide#trapping-focus) is enabled on an element, a user will only be able to interact with the contents of that element until the focus trap is closed or deactivated. + +To enable and customize a focus trap on a drawer panel, apply the `focusTrap` property to the `` component. Enabling a focus trap with `focusTrap.enabled` will also automatically place focus on the first focusable element when the drawer panel expands, and return focus to the previously focused element when it collapses. To customize which element receives focus when the drawer panel expands, use the `focusTrap.elementToFocusOnExpand` property. diff --git a/packages/react-integration/cypress/integration/drawer.spec.ts b/packages/react-integration/cypress/integration/drawer.spec.ts index 6fef7301dda..04171b9de83 100644 --- a/packages/react-integration/cypress/integration/drawer.spec.ts +++ b/packages/react-integration/cypress/integration/drawer.spec.ts @@ -3,28 +3,43 @@ describe('Drawer Demo Test', () => { cy.visit('http://localhost:3000/drawer-demo-nav-link'); }); + it('Verify focus is automatically handled with focus trap enabled', () => { + cy.get('#toggleFocusTrapButton').click(); + cy.get('#focusTrap-panelContent .pf-v5-c-button.pf-m-plain').should('have.focus'); + cy.get('#focusTrap-panelContent .pf-v5-c-button.pf-m-plain').click(); + cy.get('#toggleFocusTrapButton').should('have.focus'); + }); + + it('Verify focus can be customized with focus trap enabled', () => { + cy.get('#toggleCustomFocusButton').click(); + // Wait for transition animation to end + cy.wait(500); + cy.get('#customFocus-panelContent').should('have.focus'); + cy.get('#toggleCustomFocusButton').click(); + }); + it('Verify text in content', () => { const drawerContent = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus pretium est a porttitor vehicula. Quisque vel commodo urna. Morbi mattis rutrum ante, id vehicula ex accumsan ut. Morbi viverra, eros vel porttitor facilisis, eros purus aliquet erat,nec lobortis felis elit pulvinar sem. Vivamus vulputate, risus eget commodo eleifend, eros nibh porta quam, vitae lacinia leo libero at magna. Maecenas aliquam sagittis orci, et posuere nisi ultrices sit amet. Aliquam ex odio, malesuada sed posuere quis, pellentesque at mauris. Phasellus venenatis massa ex, eget pulvinar libero auctor pretium. Aliquam erat volutpat. Duis euismod justo in quam ullamcorper, in commodo massa vulputate.'; - cy.get('.pf-v5-c-drawer').contains(drawerContent); + cy.get('#basic-drawer.pf-v5-c-drawer').contains(drawerContent); }); it('Verify drawer expands and collapses', () => { - cy.get('.pf-v5-c-drawer').should('not.have.class', 'pf-m-expanded'); + cy.get('#basic-drawer.pf-v5-c-drawer').should('not.have.class', 'pf-m-expanded'); cy.get('#toggleButton').click(); - cy.get('.pf-v5-c-drawer').should('have.class', 'pf-m-expanded'); + cy.get('#basic-drawer.pf-v5-c-drawer').should('have.class', 'pf-m-expanded'); cy.get('#toggleButton').click(); }); it('Verify bottom drawer with background variant', () => { - cy.get('.pf-v5-c-drawer').should('have.class', 'pf-m-panel-bottom'); - cy.get('.pf-v5-c-drawer__panel').should('have.class', 'pf-m-light-200'); + cy.get('#basic-drawer.pf-v5-c-drawer').should('have.class', 'pf-m-panel-bottom'); + cy.get('#basic-drawer .pf-v5-c-drawer__panel').should('have.class', 'pf-m-light-200'); }); it('Verify panel widths', () => { // Large viewport - const $drawerPanel = cy.get('.pf-v5-c-drawer__panel'); + const $drawerPanel = cy.get('#basic-drawer .pf-v5-c-drawer__panel'); $drawerPanel.should('have.class', 'pf-m-width-100'); $drawerPanel.should('have.class', 'pf-m-width-50-on-lg'); $drawerPanel.should('have.class', 'pf-m-width-33-on-xl'); @@ -32,31 +47,29 @@ describe('Drawer Demo Test', () => { $drawerPanel.should('have.css', 'flex-basis', 'max(0% + 24px, min(0% + 300px, 100% + 0px))'); // Medium viewport cy.viewport(800, 660); - cy.get('.pf-v5-c-drawer__panel').should('have.css', 'flex-basis', 'max(0% + 24px, min(100% + 0px, 100% + 0px))'); + cy.get('#basic-drawer .pf-v5-c-drawer__panel').should( + 'have.css', + 'flex-basis', + 'max(0% + 24px, min(100% + 0px, 100% + 0px))' + ); // Xl viewport cy.viewport(1200, 660); - cy.get('.pf-v5-c-drawer__panel').should('have.css', 'flex-basis', 'max(0% + 24px, min(0% + 300px, 100% + 0px))'); + cy.get('#basic-drawer .pf-v5-c-drawer__panel').should( + 'have.css', + 'flex-basis', + 'max(0% + 24px, min(0% + 300px, 100% + 0px))' + ); // 2Xl viewport cy.viewport(1450, 660); - cy.get('.pf-v5-c-drawer__panel').should('have.css', 'flex-basis', 'max(0% + 24px, min(0% + 300px, 100% + 0px))'); + cy.get('#basic-drawer .pf-v5-c-drawer__panel').should( + 'have.css', + 'flex-basis', + 'max(0% + 24px, min(0% + 300px, 100% + 0px))' + ); }); it('Verify that focus gets sent to drawer', () => { cy.get('#toggleButton').click(); cy.wrap(() => cy.focused().contains('drawer-panel in demo with onExpand'), { timeout: 1000 }); }); - - it('Verify focus is automatically handled with focus trap enabled', () => { - cy.get('#toggleFocusTrapButton').click(); - cy.get('#focusTrap-panelContent .pf-v5-c-button.pf-m-plain').should('have.focus'); - cy.get('#focusTrap-panelContent .pf-v5-c-button.pf-m-plain').click(); - cy.get('#toggleFocusTrapButton').should('have.focus'); - }); - - it('Verify focus can be customized with focus trap enabled', () => { - cy.get('#toggleCustomFocusButton').click(); - // Wait for transition animation to end - cy.wait(500); - cy.get('#customFocus-panelContent').should('have.focus'); - }); }); diff --git a/packages/react-integration/demo-app-ts/src/components/demos/DrawerDemo/DrawerDemo.tsx b/packages/react-integration/demo-app-ts/src/components/demos/DrawerDemo/DrawerDemo.tsx index dd196654ff4..922f01a8383 100644 --- a/packages/react-integration/demo-app-ts/src/components/demos/DrawerDemo/DrawerDemo.tsx +++ b/packages/react-integration/demo-app-ts/src/components/demos/DrawerDemo/DrawerDemo.tsx @@ -123,7 +123,7 @@ export class DrawerDemo extends React.Component { - + drawer-section {drawerContent}