Skip to content

Commit b150eb9

Browse files
author
Kubit
committed
Add scrollarias to tooltip component
1 parent 078401f commit b150eb9

File tree

9 files changed

+24
-225
lines changed

9 files changed

+24
-225
lines changed
Lines changed: 1 addition & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
import { PopoverComponentType } from '@/components/popover';
2-
import { DeviceBreakpointsType } from '@/types';
3-
4-
import { getAriaDescriptorsBy, getHtmlTagForTooltip } from '../utils';
1+
import { getAriaDescriptorsBy } from '../utils';
52

63
describe('getAriaDescriptorsBy utility', () => {
74
it('should return both titleId and contentId when title and content are provided', () => {
@@ -44,41 +41,3 @@ describe('getAriaDescriptorsBy utility', () => {
4441
expect(result).toBeUndefined();
4542
});
4643
});
47-
48-
describe('getHtmlTagForTooltip utility', () => {
49-
it('should return DIALOG when mediaDevice is DESKTOP and tooltipAsModal is true', () => {
50-
const result = getHtmlTagForTooltip({
51-
mediaDevice: DeviceBreakpointsType.DESKTOP,
52-
tooltipAsModal: true,
53-
});
54-
55-
expect(result).toBe(PopoverComponentType.DIALOG);
56-
});
57-
58-
it('should return undefined when mediaDevice is not DESKTOP', () => {
59-
const result = getHtmlTagForTooltip({
60-
mediaDevice: DeviceBreakpointsType.MOBILE,
61-
tooltipAsModal: true,
62-
});
63-
64-
expect(result).toBeUndefined();
65-
});
66-
67-
it('should return undefined when tooltipAsModal is not true', () => {
68-
const result = getHtmlTagForTooltip({
69-
mediaDevice: DeviceBreakpointsType.DESKTOP,
70-
tooltipAsModal: false,
71-
});
72-
73-
expect(result).toBeUndefined();
74-
});
75-
76-
it('should return undefined when neither condition is met', () => {
77-
const result = getHtmlTagForTooltip({
78-
mediaDevice: DeviceBreakpointsType.MOBILE,
79-
tooltipAsModal: false,
80-
});
81-
82-
expect(result).toBeUndefined();
83-
});
84-
});

src/components/tooltip/__tests__/useTooltipContentScroll.test.ts

Lines changed: 0 additions & 97 deletions
This file was deleted.

src/components/tooltip/hooks/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
export * from './useTooltip';
22
export * from './useTooltipAsModal';
33
export * from './useTooltipAsModalAriaLabel';
4-
export * from './useTooltipContentScroll';

src/components/tooltip/hooks/useTooltipContentScroll.ts

Lines changed: 0 additions & 50 deletions
This file was deleted.

src/components/tooltip/stories/tooltip.stories.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ export const Tooltip: Story = {
3333
variant: Object.values(variantsObject[themeSelected].TooltipVariantType || {})[0] as string,
3434
title: { content: 'Tootltip title' },
3535
content: { content: 'Tooltip content' },
36+
contentScrollArias: {
37+
'aria-label': 'Tooltip content scroll',
38+
},
3639
children: 'Hover me',
3740
align: TooltipAlignType.TOP,
3841
triggerAsButton: {
@@ -49,6 +52,9 @@ export const TooltipSimple: Story = {
4952
variant: Object.values(variantsObject[themeSelected].TooltipVariantType || {})[0] as string,
5053
title: { content: 'Tootltip title' },
5154
content: { content: 'Tooltip content' },
55+
contentScrollArias: {
56+
'aria-label': 'Tooltip content scroll',
57+
},
5258
children: 'Hover me',
5359
triggerAsButton: {
5460
'aria-label': 'Tooltip trigger',

src/components/tooltip/tooltipControlled.tsx

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import * as React from 'react';
22

3-
import { useMediaDevice } from '@/hooks';
3+
import { useMediaDevice, useScrollDetectionWithAutoFocus } from '@/hooks';
44
import { useStyles } from '@/hooks/useStyles/useStyles';
55
import { ErrorBoundary, FallbackComponent } from '@/provider/errorBoundary';
66

77
import { TOOLTIP_STYLES } from './constants';
8-
import { useTooltipAsModal, useTooltipAsModalAriaLabel, useTooltipContentScroll } from './hooks';
8+
import { useTooltipAsModal, useTooltipAsModalAriaLabel } from './hooks';
99
import { TooltipStandAlone } from './tooltipStandAlone';
1010
import { ITooltipControlled, TooltipVariantStylesProps } from './types';
1111

@@ -25,9 +25,8 @@ const TooltipControlledComponent = <V extends string | unknown>({
2525
styleTooltipAsModal: styles.tooltipAsModal,
2626
});
2727

28-
const { contentHasScroll, contentRefHandler } = useTooltipContentScroll({
29-
tooltipRef: innerTooltipRef,
30-
});
28+
const { hasScroll: contentHasScroll, handleScrollDetection: contentRefHandler } =
29+
useScrollDetectionWithAutoFocus({ parentElementRef: innerTooltipRef });
3130

3231
React.useImperativeHandle(tooltipRef, () => {
3332
return innerTooltipRef.current as HTMLDivElement;

src/components/tooltip/tooltipStandAlone.tsx

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import {
2828
TooltipTitleStyled,
2929
} from './tooltip.styled';
3030
import { ITooltipStandAlone } from './types';
31-
import { getAriaDescriptorsBy, getHtmlTagForTooltip } from './utils';
31+
import { getAriaDescriptorsBy } from './utils';
3232

3333
const TooltipStandAlone = ({
3434
childrenAsButton = true,
@@ -63,15 +63,12 @@ const TooltipStandAlone = ({
6363
const Tooltip = (
6464
<TooltipExternalContainerStyled
6565
ref={props.tooltipRef}
66-
aria-label={props.tooltipAriaLabel}
67-
aria-labelledby={titleId}
68-
as={getHtmlTagForTooltip({
69-
mediaDevice: props.mediaDevice,
70-
tooltipAsModal: props.tooltipAsModal,
71-
})}
66+
aria-label={isDesktop ? props.tooltipAriaLabel : undefined}
67+
aria-labelledby={isDesktop ? titleId : undefined}
68+
aria-modal={isDesktop && props.tooltipAsModal ? true : undefined}
7269
data-testid={`${props.dataTestId}TooltipContent`}
7370
id={uniqueId}
74-
role={isDesktop && !props.tooltipAsModal ? ROLES.TOOLTIP : undefined}
71+
role={isDesktop ? (props.tooltipAsModal ? ROLES.DIALOG : ROLES.TOOLTIP) : undefined}
7572
styles={props.styles}
7673
onFocus={props.onTooltipFocus}
7774
onKeyDown={props.onTooltipKeyDown}
@@ -189,18 +186,19 @@ const TooltipStandAlone = ({
189186
Tooltip
190187
) : (
191188
<Popover
192-
component={!props.tooltipAsModal ? PopoverComponentType.DIV : PopoverComponentType.DIALOG}
189+
component={PopoverComponentType.DIV}
193190
dataTestId={`${props.dataTestId}Popover`}
194191
focusLastElementFocusedAfterClose={false}
195192
hasBackDrop={props.styles.showOverlay?.[props.mediaDevice]}
196193
open={props.popoverOpen}
197194
positionVariant={PopoverPositionVariantType.ABSOLUTE}
198195
preventCloseOnClickElements={[props.labelRef?.current]}
199-
role={!props.tooltipAsModal ? ROLES.TOOLTIP : undefined}
196+
role={props.tooltipAsModal ? ROLES.DIALOG : ROLES.TOOLTIP}
200197
trapFocusInsideModal={true}
201198
variant={props.styles.popoverVariant?.[props.mediaDevice]}
202199
{...props.popover}
203200
aria-label={props.popover?.['aria-label'] || props.tooltipAriaLabel}
201+
aria-labelledby={titleId}
204202
aria-modal={props.tooltipAsModal || undefined}
205203
onCloseInternally={props.onPopoverCloseInternally}
206204
>

src/components/tooltip/tooltipUnControlled.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import * as React from 'react';
22

3-
import { useMediaDevice, useSwipeDown } from '@/hooks';
3+
import { useMediaDevice, useScrollDetectionWithAutoFocus, useSwipeDown } from '@/hooks';
44
import { useStyles } from '@/hooks/useStyles/useStyles';
55
import { useTrapFocus } from '@/hooks/useTrapFocus/useTrapFocus';
66
import { ErrorBoundary, FallbackComponent } from '@/provider/errorBoundary';
77
import { DeviceBreakpointsType } from '@/types';
88
import { isKeyEnterPressed } from '@/utils';
99

1010
import { TOOLTIP_STYLES } from './constants';
11-
import { useTooltip, useTooltipAsModal, useTooltipContentScroll } from './hooks';
11+
import { useTooltip, useTooltipAsModal } from './hooks';
1212
import { TooltipStandAlone } from './tooltipStandAlone';
1313
import { ITooltipStandAlone, ITooltipUnControlled, TooltipVariantStylesProps } from './types';
1414

@@ -49,7 +49,8 @@ const TooltipUnControlledComponent = React.forwardRef(
4949
ctv,
5050
});
5151

52-
const { contentHasScroll, contentRefHandler } = useTooltipContentScroll({ tooltipRef });
52+
const { hasScroll: contentHasScroll, handleScrollDetection: contentRefHandler } =
53+
useScrollDetectionWithAutoFocus({ parentElementRef: tooltipRef });
5354

5455
// When !tooltipAsModal, in tablet/mobile, onFocus and onClick are executed at the same time
5556
// So onFocus will open the tooltip, and onClick will close it (because it was opened)
@@ -152,7 +153,7 @@ const TooltipUnControlledComponent = React.forwardRef(
152153
hideTooltip
153154
);
154155

155-
useTrapFocus({ element: tooltipRef, hasFocusTrap: tooltipAsModalValue });
156+
useTrapFocus({ ref: tooltipRef, trapFocus: open && tooltipAsModalValue });
156157

157158
// It is used TooltipStandAlone instead of TooltipControlled
158159
// The reason is that TooltipControlled only provides the styles and mediaDevice props that are used and needed in this component

src/components/tooltip/utils/tooltip.utils.ts

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
import { PopoverComponentType } from '@/components/popover';
2-
import { DeviceBreakpointsType } from '@/types';
3-
41
// build descriptive id for aria-describedby or aria-labelledby
52
export const getAriaDescriptorsBy = ({
63
title,
@@ -25,16 +22,3 @@ export const getAriaDescriptorsBy = ({
2522
}
2623
return descriptorsId.join(' ');
2724
};
28-
29-
export const getHtmlTagForTooltip = ({
30-
mediaDevice,
31-
tooltipAsModal,
32-
}: {
33-
mediaDevice: DeviceBreakpointsType;
34-
tooltipAsModal?: boolean;
35-
}): PopoverComponentType | undefined => {
36-
if (mediaDevice === DeviceBreakpointsType.DESKTOP && tooltipAsModal) {
37-
return PopoverComponentType.DIALOG;
38-
}
39-
return undefined;
40-
};

0 commit comments

Comments
 (0)