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(legend): improve tooltip wording #2439

Merged
merged 11 commits into from
Jun 3, 2024
46 changes: 29 additions & 17 deletions packages/charts/src/components/legend/label.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,31 +24,45 @@ interface LabelProps {

const isAppleDevice = typeof window !== 'undefined' && /Mac|iPhone|iPad/.test(window.navigator.userAgent);

const modifierKey = isAppleDevice ? '⌘ (Command)' : 'Ctrl';
const isolateSeriesMessage = 'isolate series';
const showAllSeriesMessage = 'show all series';
const showSeriesMessage = 'show series';
const hideSeriesMessage = 'hide series';
const modifierKey = isAppleDevice ? '⌘' : 'Ctrl';
const showAllSeriesMessage = 'to show all';
const showSeriesMessage = 'to show';
const hideSeriesMessage = 'to hide';

function getInteractivityTitle(isSeriesVisible: boolean, hiddenSeries: number, allSeries: number) {
if (isSeriesVisible) {
if (allSeries - hiddenSeries === 1) {
return `
Click: ${showAllSeriesMessage}
${modifierKey} + click: ${hideSeriesMessage}`;
Click ${showAllSeriesMessage}
${modifierKey} + click ${hideSeriesMessage}`;
dej611 marked this conversation as resolved.
Show resolved Hide resolved
}
if (hiddenSeries > 0) {
return `
Click: ${hideSeriesMessage}
${modifierKey} + click: ${hideSeriesMessage}`;
Click ${hideSeriesMessage}`;
}
return `
Click: ${isolateSeriesMessage}
${modifierKey} + click: ${hideSeriesMessage}`;
Click ${showSeriesMessage}
${modifierKey} + click ${hideSeriesMessage}`;
nickofthyme marked this conversation as resolved.
Show resolved Hide resolved
}
return `
Click: ${showSeriesMessage}
${modifierKey} + click: ${showSeriesMessage}`;
Click ${showSeriesMessage}`;
}

function getInteractivityAriaLabel(isSeriesVisible: boolean, hiddenSeries: number, allSeries: number) {
if (isSeriesVisible) {
if (allSeries - hiddenSeries === 1) {
return `
Click: ${showAllSeriesMessage}, ${modifierKey} + click: ${hideSeriesMessage}`;
nickofthyme marked this conversation as resolved.
Show resolved Hide resolved
}
if (hiddenSeries > 0) {
return `
Click: ${hideSeriesMessage}, ${modifierKey} + click: ${hideSeriesMessage}`;
nickofthyme marked this conversation as resolved.
Show resolved Hide resolved
}
return `
Click: ${showSeriesMessage}, ${modifierKey} + click: ${hideSeriesMessage}`;
nickofthyme marked this conversation as resolved.
Show resolved Hide resolved
}
return `
Click: ${showSeriesMessage}, ${modifierKey} + click: ${showSeriesMessage}`;
nickofthyme marked this conversation as resolved.
Show resolved Hide resolved
}

/**
Expand Down Expand Up @@ -86,8 +100,6 @@ export function Label({
const title = options.maxLines > 0 ? label : ''; // full text already visible
const clampStyles = maxLines > 1 ? { WebkitLineClamp: maxLines } : {};

const interactionsGuidanceText = getInteractivityTitle(!isSeriesHidden, hiddenSeriesCount, totalSeriesCount);

return isToggleable ? (
// This div is required to allow multiline text truncation, all ARIA requirements are still met
// https://stackoverflow.com/questions/68673034/webkit-line-clamp-does-not-apply-to-buttons
Expand All @@ -96,12 +108,12 @@ export function Label({
tabIndex={0}
dir={dir}
className={labelClassNames}
nickofthyme marked this conversation as resolved.
Show resolved Hide resolved
title={`${title}${interactionsGuidanceText}`}
title={`${title}${getInteractivityTitle(!isSeriesHidden, hiddenSeriesCount, totalSeriesCount)}`}
dej611 marked this conversation as resolved.
Show resolved Hide resolved
onClick={onClick}
onKeyDown={onKeyDown}
aria-pressed={isSeriesHidden}
style={clampStyles}
aria-label={`${label}; ${interactionsGuidanceText.replace('\n', '')}`} // put it in a single line
aria-label={`${label}; ${getInteractivityAriaLabel(!isSeriesHidden, hiddenSeriesCount, totalSeriesCount)}`}
>
{label}
</div>
Expand Down