Skip to content

Commit

Permalink
fix(Vivinho char): vivinho char in headings being read as a separated…
Browse files Browse the repository at this point in the history
… heading (#1209)
  • Loading branch information
atabel authored Aug 23, 2024
1 parent cae6c5c commit f0f5fb0
Showing 1 changed file with 53 additions and 20 deletions.
73 changes: 53 additions & 20 deletions src/text.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,25 +41,51 @@ const vivinhoForScreenReaders = (
</>
);

const makeVivinhoCharReadableForScreenReaders = (children: React.ReactNode): React.ReactNode => {
return React.Children.map(children, (child) => {
if (typeof child !== 'string') {
return child;
}
if (!child.includes(VIVINHO_CHAR)) {
return child;
}
return (
<>
{child.split(VIVINHO_CHAR).map((segment, idx) => (
<React.Fragment key={idx}>
{idx > 0 && vivinhoForScreenReaders}
{segment}
</React.Fragment>
))}
</>
);
});
const makeVivinhoCharReadableForScreenReaders = ({
children,
ariaLabel,
as,
}: {
children: React.ReactNode;
ariaLabel?: string;
as?: React.ComponentType<any> | string;
}) => {
// When the Text is a heading (<hx>), we set an aria-label replacing the vivinho char with "Vivo" for
// screen readers and hide the original text.
// If we used the generic solution, Safari/iOS VoiceOver would read "Vivo" as a separated heading
if (
typeof as === 'string' &&
['h1', 'h2', 'h3', 'h4', 'h5', 'h6'].includes(as) &&
typeof children === 'string' &&
children.includes(VIVINHO_CHAR)
) {
return {
ariaLabel: children.replace(new RegExp(VIVINHO_CHAR, 'g'), 'Vivo'),
children: <span aria-hidden>{children}</span>,
};
}

return {
children: React.Children.map(children, (child) => {
if (typeof child !== 'string') {
return child;
}
if (!child.includes(VIVINHO_CHAR)) {
return child;
}
return (
<>
{child.split(VIVINHO_CHAR).map((segment, idx) => (
<React.Fragment key={idx}>
{idx > 0 && vivinhoForScreenReaders}
{segment}
</React.Fragment>
))}
</>
);
}),
ariaLabel,
};
};

export interface TextPresetProps {
Expand All @@ -75,6 +101,7 @@ export interface TextPresetProps {
as?: React.ComponentType<any> | string;
role?: string;
'aria-level'?: number;
'aria-label'?: string;
dataAttributes?: DataAttributes;
forceMobileSizes?: boolean;
textShadow?: string;
Expand Down Expand Up @@ -121,6 +148,7 @@ export const Text: React.FC<TextProps> = ({
id,
role,
'aria-level': ariaLevel,
'aria-label': ariaLabel,
dataAttributes,
}) => {
const {skinName} = useTheme();
Expand Down Expand Up @@ -154,13 +182,18 @@ export const Text: React.FC<TextProps> = ({
})
: {};

if (skinName === VIVO_NEW_SKIN) {
({ariaLabel, children} = makeVivinhoCharReadableForScreenReaders({children, ariaLabel, as}));
}

return React.createElement(
as,
{
className,
id,
role,
'aria-level': ariaLevel,
'aria-label': ariaLabel,
...getPrefixedDataAttributes(dataAttributes, 'Text'),
style: {
...sizeVars,
Expand All @@ -179,7 +212,7 @@ export const Text: React.FC<TextProps> = ({
whiteSpace: as === 'pre' ? undefined : 'pre-line',
},
},
skinName === VIVO_NEW_SKIN ? makeVivinhoCharReadableForScreenReaders(children) : children
children
);
};

Expand Down

1 comment on commit f0f5fb0

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deploy preview for mistica-web ready!

✅ Preview
https://mistica-fanlh86p2-flows-projects-65bb050e.vercel.app

Built with commit f0f5fb0.
This pull request is being automatically deployed with vercel-action

Please sign in to comment.