Skip to content

Commit

Permalink
fix(Pagination): disabled icon button tooltip & focus state for a11y (#…
Browse files Browse the repository at this point in the history
…11275)

* fix: disabled icon btn tooltip update

* fix: focus other nav btn when disabled

Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
  • Loading branch information
jnm2377 and kodiakhq[bot] committed Apr 26, 2022
1 parent 4bdc8ed commit 532e195
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 5 deletions.
30 changes: 27 additions & 3 deletions packages/react/src/components/Pagination/next/Pagination.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import { CaretRight, CaretLeft } from '@carbon/icons-react';
import cx from 'classnames';
import PropTypes from 'prop-types';
import React, { useState } from 'react';
import React, { useState, useRef } from 'react';
import Select from '../../Select';
import SelectItem from '../../SelectItem';
import { equals } from '../../../tools/array';
Expand Down Expand Up @@ -77,6 +77,8 @@ const Pagination = React.forwardRef(function Pagination(
) {
const prefix = usePrefix();
const inputId = useFallbackId(id);
const backBtnRef = useRef(null);
const forwardBtnRef = useRef(null);
const [pageSizes, setPageSizes] = useState(() => {
return mapPageSizesToObject(controlledPageSizes);
});
Expand Down Expand Up @@ -172,21 +174,41 @@ const Pagination = React.forwardRef(function Pagination(
function incrementPage() {
const nextPage = page + 1;
setPage(nextPage);

// when the increment button reaches the last page,
// the icon button becomes disabled and the focus shifts to `main`
// this presents an a11y problem for keyboard & screen reader users
// instead, we want the focus to shift to the other pagination btn
if (nextPage === totalPages) {
backBtnRef.current.focus();
}

if (onChange) {
onChange({
page: nextPage,
pageSize,
backBtnRef,
});
}
}

function decrementPage() {
const nextPage = page - 1;
setPage(nextPage);

// when the decrement button reaches the first page,
// the icon button becomes disabled and the focus shifts to `main`
// this presents an a11y problem for keyboard & screen reader users
// instead, we want the focus to shift to the other pagination btn
if (nextPage === 1) {
forwardBtnRef.current.focus();
}

if (onChange) {
onChange({
page: nextPage,
pageSize,
forwardBtnRef,
});
}
}
Expand Down Expand Up @@ -251,7 +273,8 @@ const Pagination = React.forwardRef(function Pagination(
kind="ghost"
className={backButtonClasses}
label={backwardText}
onClick={decrementPage}>
onClick={decrementPage}
ref={backBtnRef}>
<CaretLeft />
</IconButton>
<IconButton
Expand All @@ -260,7 +283,8 @@ const Pagination = React.forwardRef(function Pagination(
kind="ghost"
className={forwardButtonClasses}
label={forwardText}
onClick={incrementPage}>
onClick={incrementPage}
ref={forwardBtnRef}>
<CaretRight />
</IconButton>
</div>
Expand Down
14 changes: 12 additions & 2 deletions packages/react/src/components/Tooltip/next/Tooltip.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@

import cx from 'classnames';
import PropTypes from 'prop-types';
import React, { useRef } from 'react';
import React, { useRef, useEffect } from 'react';
import { Popover, PopoverContent } from '../../Popover';
import { keys, match } from '../../../internal/keyboard';
import { useDelayedState } from '../../../internal/useDelayedState';
import { useId } from '../../../internal/useId';
import { useNoInteractiveChildren } from '../../../internal/useNoInteractiveChildren';
import {
useNoInteractiveChildren,
getInteractiveContent,
} from '../../../internal/useNoInteractiveChildren';
import { usePrefix } from '../../../internal/usePrefix';

function Tooltip({
Expand Down Expand Up @@ -67,6 +70,13 @@ function Tooltip({
'`label` or `description` prop'
);

useEffect(() => {
const interactiveContent = getInteractiveContent(containerRef.current);
if (!interactiveContent) {
setOpen(false);
}
});

return (
<Popover
{...rest}
Expand Down

0 comments on commit 532e195

Please sign in to comment.