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: ability to add titles for the links #549

Merged
merged 2 commits into from
Sep 26, 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
3 changes: 3 additions & 0 deletions src/components/CardBase/CardBase.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export interface CardBaseProps extends AnalyticsEventsBase, CardBaseParams {
contentClassName?: string;
children: ReactElement | ReactElement[];
url?: string;
urlTitle?: string;
target?: HTMLAttributeAnchorTarget;
metrikaGoals?: MetrikaGoal;
pixelEvents?: ButtonPixel;
Expand Down Expand Up @@ -55,6 +56,7 @@ export const Layout = (props: CardBaseProps) => {
url,
target,
border = 'shadow',
urlTitle,
qa,
} = props;
const handleMetrika = useMetrika();
Expand Down Expand Up @@ -126,6 +128,7 @@ export const Layout = (props: CardBaseProps) => {
draggable={false}
onDragStart={(e) => e.preventDefault()}
onClick={onClick}
title={urlTitle}
data-qa={qa}
>
{cardContent}
Expand Down
2 changes: 2 additions & 0 deletions src/components/FileLink/FileLink.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ const FileLink = (props: WithChildren<FileLinkProps>) => {
theme = 'default',
onClick,
tabIndex,
urlTitle,
} = props;
const fileExt = getFileExt(href) as FileExtension;
const labelTheme = (FileExtensionThemes[fileExt] || 'unknown') as LabelProps['theme'];
Expand All @@ -71,6 +72,7 @@ const FileLink = (props: WithChildren<FileLinkProps>) => {
href={href}
onClick={onClick}
tabIndex={tabIndex}
title={urlTitle}
{...getLinkProps(href, hostname)}
>
{text}
Expand Down
2 changes: 2 additions & 0 deletions src/components/Link/Link.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ const LinkBlock = (props: WithChildren<LinkFullProps>) => {
children,
tabIndex,
qa,
urlTitle,
} = props;
const qaAttributes = getQaAttrubutes(qa, ['normal']);

Expand Down Expand Up @@ -105,6 +106,7 @@ const LinkBlock = (props: WithChildren<LinkFullProps>) => {
href={href}
onClick={onClick}
tabIndex={tabIndex}
title={urlTitle}
{...linkProps}
data-qa={qaAttributes.normal}
>
Expand Down
11 changes: 9 additions & 2 deletions src/components/Title/TitleItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ const TitleItem = (props: TitleItemFullProps) => {
className,
qa,
resetMargin = true,
urlTitle,
} = props;

const {hostname} = useContext(LocationContext);
Expand Down Expand Up @@ -80,13 +81,19 @@ const TitleItem = (props: TitleItemFullProps) => {
content = textMarkup;
} else if (url) {
content = (
<a className={b('link')} href={url} {...getLinkProps(url, hostname)} onClick={onClick}>
<a
className={b('link')}
href={url}
{...getLinkProps(url, hostname)}
onClick={onClick}
title={urlTitle}
>
{insideClickableContent}
</a>
);
} else if (onClick) {
content = (
<span className={b('link')} onClick={onClick}>
<span className={b('link')} onClick={onClick} title={urlTitle}>
{insideClickableContent}
</span>
);
Expand Down
6 changes: 4 additions & 2 deletions src/components/Title/__stories__/data.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,17 @@
"title": {
"text": "Lorem ipsum",
"url": "https://example.com",
"custom": "Some react node"
"custom": "Some react node",
"urlTitle": "Example website. Opens in a new window"
}
}
},
"titleLink": {
"content": {
"title": {
"text": "Lorem ipsum",
"url": "https://example.com"
"url": "https://example.com",
"urlTitle": "Example website. Opens in a new window"
}
}
},
Expand Down
3 changes: 3 additions & 0 deletions src/models/constructor-items/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ export interface MediaVideoProps extends AnalyticsEventsBase {
// links
export interface LinkProps extends AnalyticsEventsBase, Stylable, Tabbable {
url: string;
urlTitle?: string;
text?: string;
textSize?: TextSize;
theme?: LinkTheme;
Expand All @@ -175,6 +176,7 @@ export interface FileLinkProps extends ClassNameProps, Tabbable {
type?: FileLinkType;
textSize?: TextSize;
theme?: ContentTheme;
urlTitle?: string;
onClick?: () => void;
}

Expand Down Expand Up @@ -347,6 +349,7 @@ export interface TitleItemBaseProps {
text: string;
textSize?: TextSize;
url?: string;
urlTitle?: string;
custom?: string | ReactNode;
onClick?: () => void;
}
Expand Down
1 change: 1 addition & 0 deletions src/models/constructor-items/sub-blocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ export interface BackgroundCardProps
AnalyticsEventsBase,
Omit<ContentBlockProps, 'colSizes' | 'centered'> {
url?: string;
urlTitle?: string;
background?: ThemeSupporting<ImageObjectProps>;
paddingBottom?: 's' | 'm' | 'l' | 'xl';
backgroundColor?: string;
Expand Down
3 changes: 3 additions & 0 deletions src/models/navigation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export enum NavigationGithubButtonIcon {
export interface NavigationGithubButton extends Omit<NavigationItemBase, 'icon'> {
type: NavigationItemType.GithubButton;
url: string;
urlTitle?: string;
label?: string;
icon?: keyof typeof NavigationGithubButtonIcon;
size?: string;
Expand All @@ -43,6 +44,7 @@ export interface NavigationGithubButton extends Omit<NavigationItemBase, 'icon'>
export interface NavigationLinkItem extends Omit<NavigationItemBase, 'url'> {
type: NavigationItemType.Link;
url: string;
urlTitle?: string;
arrow?: boolean;
target?: string;
}
Expand All @@ -62,6 +64,7 @@ export interface NavigationSocialItem extends Omit<NavigationItemBase, 'text'> {
type: NavigationItemType.Social;
icon: ImageProps;
url: string;
urlTitle?: string;
}

export type NavigationItemModel =
Expand Down
6 changes: 4 additions & 2 deletions src/navigation/__stories__/data.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@
"type": "link",
"text": "Link with arrow",
"url": "https://example.com",
"arrow": true
"arrow": true,
"urlTitle": "External website. Opens in a new window"
},
{
"type": "link",
Expand Down Expand Up @@ -106,7 +107,8 @@
"type": "github-button",
"text": "Star",
"label": "Star @gravity-ui/page-constructor on GitHub",
"url": "https://github.com/gravity-ui/page-constructor"
"url": "https://github.com/gravity-ui/page-constructor",
"urlTitle": "Page Constructor GitHub page. Opens in a new window"
},
{
"type": "link",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export const GithubButton = ({
label,
size,
icon,
urlTitle,
}: NavigationGithubButtonProps) => {
const containerRef = useRef<HTMLSpanElement>(null);
const linkRef = useRef<HTMLAnchorElement>(null);
Expand Down Expand Up @@ -63,6 +64,7 @@ export const GithubButton = ({
<a
href={url}
ref={linkRef}
title={urlTitle}
data-show-count="true"
aria-label={label || DEFAULT_LABEL}
{...(icon && {'data-icon': NavigationGithubButtonIcon[icon]})}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type NavigationLinkProps = NavigationItemProps & NavigationLinkItem;

export const NavigationLink: React.FC<NavigationLinkProps> = (props) => {
const {hostname, Link} = useContext(LocationContext);
const {url, text, icon, arrow, target, className, iconSize, ...rest} = props;
const {url, text, icon, arrow, target, className, iconSize, urlTitle, ...rest} = props;
const linkExtraProps = getLinkProps(url, hostname, target);
const iconData = icon && getMediaImage(icon);

Expand All @@ -31,14 +31,20 @@ export const NavigationLink: React.FC<NavigationLinkProps> = (props) => {

if (linkExtraProps?.target || !Link) {
return (
<a href={url} title={text} className={classes} {...rest} {...linkExtraProps}>
<a
href={url}
title={urlTitle || text}
Copy link
Contributor

Choose a reason for hiding this comment

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

why is there a fallback to the text in this place but not in all places?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For backward compatibility I left fallback here. But I didn't do one on other places to avoid unexpected tooltips. Moreover, I expect that the link text and link title will not be the same

className={classes}
{...rest}
{...linkExtraProps}
>
{content}
</a>
);
} else {
return (
<RouterLink href={url} passHref>
<a title={text} {...rest} className={classes}>
<a title={urlTitle || text} {...rest} className={classes}>
{content}
</a>
</RouterLink>
Expand Down
4 changes: 2 additions & 2 deletions src/navigation/components/SocialIcon/SocialIcon.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ export interface NavigationSocialItemOwnProps extends NavigationSocialItem {
className?: string;
}

const SocialIcon: React.FC<NavigationSocialItemOwnProps> = ({icon, url, className}) => {
const SocialIcon: React.FC<NavigationSocialItemOwnProps> = ({icon, url, className, urlTitle}) => {
const iconData = getMediaImage(icon);
const {alt} = iconData;

return (
<a
href={url}
aria-label={alt}
title={alt}
title={urlTitle || alt}
Copy link
Contributor

Choose a reason for hiding this comment

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

Can alt of icon be the title of a link?

Copy link
Contributor Author

@Kyzyl-ool Kyzyl-ool Sep 25, 2023

Choose a reason for hiding this comment

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

Yes, but the accessible name will be read by screenreader in this order:

  1. link text
  2. image alt
  3. link title

Is icon's alt is the same as the link title, the screenreader will read two sentences twice, it is not really good. So I think they should differ

target="_blank"
rel="noopener noreferrer"
className={b(null, className)}
Expand Down
2 changes: 2 additions & 0 deletions src/sub-blocks/BackgroundCard/BackgroundCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const BackgroundCard = (props: BackgroundCardProps) => {
links,
buttons,
analyticsEvents,
urlTitle,
} = props;

const theme = useTheme();
Expand All @@ -36,6 +37,7 @@ const BackgroundCard = (props: BackgroundCardProps) => {
url={url}
border={borderType}
analyticsEvents={analyticsEvents}
urlTitle={urlTitle}
>
<CardBase.Content>
<BackgroundImage
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,5 +151,6 @@ WithUrl.args = {
...DefaultArgs,
...item,
url: data.url,
urlTitle: data.urlTitle,
})) as BackgroundCardProps[],
};
1 change: 1 addition & 0 deletions src/sub-blocks/BackgroundCard/__stories__/data.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
}
},
"url": "https://example.com",
"urlTitle": "Example website. Opens in a new window",
"paddings": {
"title": "Padding Bottom = {{padding}}"
},
Expand Down
Loading