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

feat(HelpPopover): improve a11y #106

Merged
merged 3 commits into from
Oct 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
30 changes: 15 additions & 15 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
"@gravity-ui/prettier-config": "^1.0.1",
"@gravity-ui/stylelint-config": "^2.0.0",
"@gravity-ui/tsconfig": "^1.0.0",
"@gravity-ui/uikit": "^5.7.0",
"@gravity-ui/uikit": "^5.10.0",
"@storybook/addon-essentials": "^7.1.1",
"@storybook/cli": "^7.1.1",
"@storybook/preset-scss": "^1.0.3",
Expand Down Expand Up @@ -88,7 +88,7 @@
"typescript": "^4.9.5"
},
"peerDependencies": {
"@gravity-ui/uikit": "^5.2.0",
"@gravity-ui/uikit": "^5.10.0",
"react": "^16.0.0 || ^17.0.0 || ^18.0.0",
"react-dom": "^16.0.0 || ^17.0.0 || ^18.0.0"
},
Expand Down
15 changes: 15 additions & 0 deletions src/components/HelpPopover/HelpPopover.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
@use '../variables';
@use '@gravity-ui/uikit/styles/mixins';

$block: '.#{variables.$ns}help-popover';

#{$block} {
&__button {
@include mixins.button-reset();
}

&__button:focus-visible {
outline: 2px solid var(--g-color-line-focus);
border-radius: 50%;
}
}
20 changes: 13 additions & 7 deletions src/components/HelpPopover/HelpPopover.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,25 @@ import {block} from '../utils/cn';

import {QuestionMarkIcon} from './QuestionMarkIcon';

const b = block('help-popover');
import './HelpPopover.scss';

/**
* @see {@link https://github.com/microsoft/TypeScript/issues/28339}
*/
type DistributiveOmit<T, K extends keyof T> = T extends unknown ? Omit<T, K> : never;
const b = block('help-popover');

export type HelpPopoverProps = DistributiveOmit<PopoverProps, 'children'> & QAProps;
export interface HelpPopoverProps extends Omit<PopoverProps, 'children'>, QAProps {
buttonProps?: React.ButtonHTMLAttributes<HTMLButtonElement>;
buttonRef?: React.RefObject<HTMLButtonElement>;
}

export function HelpPopover(props: HelpPopoverProps) {
return (
<Popover {...props} className={b(null, props.className)}>
<Icon data={QuestionMarkIcon} size={16} />
<button
ref={props.buttonRef}
{...props.buttonProps}
className={b('button', props.buttonProps?.className)}
>
<Icon data={QuestionMarkIcon} size={16} />
</button>
</Popover>
);
}
28 changes: 15 additions & 13 deletions src/components/HelpPopover/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,21 @@ Component to display popover with tips

### PropTypes

| Property | Type | Required | Values | Default | Description |
| :------------ | :---------- | :------- | :---------------- | :------------------ | :----------------------------------------------------------------------------------------------------------------------------------------------- |
| theme | `String` | | `info`, `special` | `info` | Appearance |
| className | `String` | | | | Control class name |
| placement | `Array` | | | [`right`, `bottom`] | Allowed popover positions |
| autoclosable | `Boolean` | | | `true` | Close popover when pointer is outside of control |
| delayClosing | `Number` | | | `300` | Timeout before closing popover (see `autoclosable`) |
| title | `String` | | | | Popover title |
| content | `ReactNode` | | | | Popover content |
| htmlContent | `String` | | | | Render HTML via `dangerouslySetInnerHTML` |
| links | `Array` | | | [] | Links below content, could be <br/> `{ text: 'Link 1', href: 'https://example.com'}` or <br/> `{ text: 'Link 2', onClick: () => onLinkClick() }` |
| tooltipButton | `Object` | | | | Render button with this value <br/> `{ text: 'Button', onClick: () => onClick() }` |
| offset | `Object` | | | `{ left: 4 }` | Control popup toggle position offset <br/> `{ top: 0, left: 0 }` |
| Property | Type | Required | Values | Default | Description |
| :------------ | :---------------------------------------------- | :------- | :---------------- | :------------------ | :----------------------------------------------------------------------------------------------------------------------------------------------- |
| theme | `String` | | `info`, `special` | `info` | Appearance |
| className | `String` | | | | Control class name |
| placement | `Array` | | | [`right`, `bottom`] | Allowed popover positions |
| autoclosable | `Boolean` | | | `true` | Close popover when pointer is outside of control |
| delayClosing | `Number` | | | `300` | Timeout before closing popover (see `autoclosable`) |
| title | `String` | | | | Popover title |
| content | `ReactNode` | | | | Popover content |
| htmlContent | `String` | | | | Render HTML via `dangerouslySetInnerHTML` |
| links | `Array` | | | [] | Links below content, could be <br/> `{ text: 'Link 1', href: 'https://example.com'}` or <br/> `{ text: 'Link 2', onClick: () => onLinkClick() }` |
| tooltipButton | `Object` | | | | Render button with this value <br/> `{ text: 'Button', onClick: () => onClick() }` |
| offset | `Object` | | | `{ left: 4 }` | Control popup toggle position offset <br/> `{ top: 0, left: 0 }` |
| buttonProps | `React.ButtonHTMLAttributes<HTMLButtonElement>` | | | | Set attributes to the underlying button element |
| buttonRef | `React.RefObject<HTMLButtonElement>` | | | | Ref to the underlying button element |

### Examples

Expand Down
48 changes: 48 additions & 0 deletions src/components/HelpPopover/__stories__/HelpPopover.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@ import React from 'react';

import type {Meta, StoryFn} from '@storybook/react';

import {cn} from '../../utils/cn';
import {HelpPopover} from '../HelpPopover';
import type {HelpPopoverProps} from '../HelpPopover';

import './HelpPopoverShowcase.scss';

const b = cn('help-popover-showcase');

export default {
title: 'Components/HelpPopover',
component: HelpPopover,
Expand All @@ -15,3 +20,46 @@ export const Default = DefaultTemplate.bind({});
Default.args = {
content: 'Some content',
};

export const Accessible: StoryFn = (args) => {
const helpPopoverWithoutActionsId = 'helpPopoverWithoutActionsId';
const helpPopoverWithActionsId = 'helpPopoverWithActionsId';
const [openPopover, setOpenPopover] = React.useState(false);
const ref = React.useRef<HTMLButtonElement>(null);
return (
<div>
<div className={b('container')}>
<span className={b('container-title')}>Without actions: </span>
<HelpPopover
{...args}
content={'Some content'}
tooltipId={helpPopoverWithoutActionsId}
aria-hidden={true}
buttonProps={{
'aria-labelledby': helpPopoverWithoutActionsId,
role: 'generic',
}}
/>
</div>
<div className={b('container')}>
<span className={b('container-title')}>With actions: </span>
<HelpPopover
{...args}
tooltipId={helpPopoverWithActionsId}
content={<a href="https://ya.ru">Some link</a>}
openOnHover={false}
onOpenChange={setOpenPopover}
focusTrap
autoFocus
restoreFocusRef={ref}
buttonProps={{
'aria-expanded': openPopover,
'aria-controls': helpPopoverWithActionsId,
'aria-label': 'More info',
}}
buttonRef={ref}
/>
</div>
</div>
);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.help-popover-showcase {
&__container {
display: flex;
margin-bottom: 10px;
}
&__container-title {
margin-right: 5px;
}
}
Loading