Skip to content

Commit

Permalink
feat: alt attributes and titles, fix yfm plugins bug (#655)
Browse files Browse the repository at this point in the history
* feat: alt attributes for Quote block and Author avatar
* feat: share links with labels and titles
* feat: alt attributes are not present at all by default
* fix: yfm plugins paramter bug
  • Loading branch information
Kyzyl-ool authored Oct 23, 2023
1 parent 053b469 commit 2786c81
Show file tree
Hide file tree
Showing 17 changed files with 63 additions and 41 deletions.
6 changes: 6 additions & 0 deletions src/blocks/Share/Share.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ const Share = ({items, title}: ShareBlockProps) => {
const url = getAbsolutePath(hostname, pathname);
const socialUrl = getShareLink(url, type);
const icon = icons[type];
const urlTitle = i18n(`${type}-title`);
const buttonLabel = i18n(`${type}-label`);

return (
<Button
Expand All @@ -54,6 +56,10 @@ const Share = ({items, title}: ShareBlockProps) => {
href={socialUrl}
className={b('item', {type: type.toLowerCase()})}
onClick={handleButtonClick}
title={urlTitle}
extraProps={{
'aria-label': buttonLabel,
}}
>
{icon && <Icon data={icon} size={24} className={b('icon', {type})} />}
</Button>
Expand Down
12 changes: 11 additions & 1 deletion src/blocks/Share/i18n/en.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
{
"constructor-share": "Share"
"constructor-share": "Share",
"facebook-title": "Opens in a new tab",
"twitter-title": "Opens in a new tab",
"linkedin-title": "Opens in a new tab",
"vk-title": "Opens in a new tab",
"telegram-title": "Opens in a new tab",
"facebook-label": "Facebook logo",
"twitter-label": "Twitter logo",
"linkedin-label": "LinkedIn logo",
"vk-label": "VK logo",
"telegram-label": "Telegram logo"
}
12 changes: 11 additions & 1 deletion src/blocks/Share/i18n/ru.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
{
"constructor-share": "Поделиться"
"constructor-share": "Поделиться",
"facebook-title": "Откроется в новой вкладке",
"twitter-title": "Откроется в новой вкладке",
"linkedin-title": "Откроется в новой вкладке",
"vk-title": "Откроется в новой вкладке",
"telegram-title": "Откроется в новой вкладке",
"facebook-label": "Facebook лого",
"twitter-label": "Twitter лого",
"linkedin-label": "LinkedIn лого",
"vk-label": "VK лого",
"telegram-label": "Telegram лого"
}
10 changes: 8 additions & 2 deletions src/components/Author/Author.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import React from 'react';

import {AuthorProps, AuthorType, WithChildren} from '../../models';
import {AuthorProps, AuthorType, ImageProps, WithChildren} from '../../models';
import {block} from '../../utils';
import {getMediaImage} from '../Media/Image/utils';
import {Image} from '../index';

import './Author.scss';
Expand All @@ -13,12 +14,17 @@ const Author = (props: WithChildren<AuthorProps>) => {
const {firstName, secondName, description, avatar} = author;

const name = secondName ? `${firstName} ${secondName}` : firstName;
const isAvatarJSX = React.isValidElement(avatar);
let avatarProps = {};
if (!isAvatarJSX && avatar) {
avatarProps = getMediaImage(avatar as ImageProps);
}

return (
<div className={b({type}, className)} data-qa={qa}>
{avatar && (
<div className={b('avatar', authorContainerClassName)}>
{typeof avatar === 'string' ? <Image src={avatar} /> : avatar}
{isAvatarJSX ? avatar : <Image {...avatarProps} />}
</div>
)}
<div className={b('label')}>
Expand Down
5 changes: 4 additions & 1 deletion src/components/CardBase/CardBase.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export interface CardBaseProps extends AnalyticsEventsBase, CardBaseParams {
metrikaGoals?: MetrikaGoal;
pixelEvents?: ButtonPixel;
qa?: string;
extraProps?: React.HTMLAttributes<HTMLElement>;
}

export interface CardHeaderBaseProps {
Expand Down Expand Up @@ -61,6 +62,7 @@ export const Layout = (props: CardBaseProps) => {
border = 'shadow',
urlTitle,
qa,
extraProps = {},
} = props;
const handleMetrika = useMetrika();
const handleAnalytics = useAnalytics(DefaultEventNames.CardBase, url);
Expand Down Expand Up @@ -133,14 +135,15 @@ export const Layout = (props: CardBaseProps) => {
extraProps={{
draggable: false,
onDragStart: (e: React.DragEvent<HTMLAnchorElement>) => e.preventDefault(),
...extraProps,
}}
qa={qa}
>
{cardContent}
</Link>
</RouterLink>
) : (
<div className={fullClassName} data-qa={qa}>
<div className={fullClassName} data-qa={qa} {...extraProps}>
{cardContent}
</div>
);
Expand Down
4 changes: 1 addition & 3 deletions src/components/Image/Image.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import {getQaAttrubutes} from '../../utils';
import {isCompressible} from '../../utils/imageCompress';
import ImageBase from '../ImageBase/ImageBase';

import i18n from './i18n';

export interface ImageProps extends Partial<ImageObjectProps>, Partial<ImageDeviceProps>, QAProps {
style?: CSSProperties;
className?: string;
Expand Down Expand Up @@ -49,7 +47,7 @@ const Image = (props: ImageProps) => {
const projectSettings = useContext(ProjectSettingsContext);
const {
src: imageSrc,
alt = i18n('img-alt'),
alt,
disableCompress,
tablet,
desktop,
Expand Down
5 changes: 2 additions & 3 deletions src/components/Image/__tests__/Image.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
} from '../../../../test-utils/shared/common';
import {testSourceProps} from '../../../../test-utils/shared/image';
import Image, {ImageProps} from '../Image';
import i18n from '../i18n';

const qaId = 'image-component';

Expand Down Expand Up @@ -106,11 +105,11 @@ describe('Image', () => {
});
});

test('render default "alt"', () => {
test('render without attribute "alt" if not provided', () => {
render(<Image src={imageSrc} qa={qaId} />);

const component = screen.getByRole('img');
expect(component).toHaveAttribute('alt', i18n('img-alt'));
expect(component).not.toHaveAttribute('alt');
});

test('render custom "alt"', () => {
Expand Down
3 changes: 0 additions & 3 deletions src/components/Image/i18n/en.json

This file was deleted.

8 changes: 0 additions & 8 deletions src/components/Image/i18n/index.ts

This file was deleted.

3 changes: 0 additions & 3 deletions src/components/Image/i18n/ru.json

This file was deleted.

2 changes: 1 addition & 1 deletion src/models/constructor-items/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ export interface AuthorItem {
firstName: string;
secondName: string;
description?: string;
avatar?: string | JSX.Element;
avatar?: ImageProps | JSX.Element;
}

export interface HeaderBreadCrumbsProps extends ClassNameProps {
Expand Down
2 changes: 1 addition & 1 deletion src/models/constructor-items/sub-blocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export interface HubspotFormProps extends HubspotEventHandlers, AnalyticsEventsB
export interface QuoteProps extends Themable, CardBaseProps {
text: string;
image: ThemedImage;
logo: string;
logo: ImageProps;
color?: string;
url?: string;
author?: AuthorItem;
Expand Down
7 changes: 2 additions & 5 deletions src/schema/validators/common.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {ImageProps, imageUrlPattern} from '../../components/Image/schema';
import {ImageProps} from '../../components/Image/schema';
import {
CustomControlsButtonPositioning,
CustomControlsType,
Expand Down Expand Up @@ -321,10 +321,7 @@ export const authorItem = {
type: 'string',
contentType: 'text',
},
avatar: {
type: 'string',
pattern: imageUrlPattern,
},
avatar: ImageProps,
description: {
type: 'string',
contentType: 'yfm',
Expand Down
4 changes: 3 additions & 1 deletion src/sub-blocks/Quote/Quote.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,16 @@ const Quote = (props: QuoteProps) => {
</div>
);

const logoProps = getMediaImage(logo);

return (
<div
className={b({theme: textTheme, border})}
style={color ? {backgroundColor: color} : {}}
>
<div key={text} className={b('content-wrapper')}>
<div>
<Image className={b('logo')} src={logo} />
<Image className={b('logo')} {...logoProps} />
<div className={b('content')}>
<span className={b('text')}>
<HTML>{text}</HTML>
Expand Down
10 changes: 8 additions & 2 deletions src/sub-blocks/Quote/__stories__/data.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,15 @@
"firstName": "Lorem",
"secondName": "ipsum",
"description": "Lorem ipsum",
"avatar": "https://storage.yandexcloud.net/cloud-www-assets/constructor/storybook/images/img_6-12_light.png"
"avatar": {
"src": "https://storage.yandexcloud.net/cloud-www-assets/constructor/storybook/images/img_6-12_light.png",
"alt": "Avatar alt"
}
},
"logo": "https://storage.yandexcloud.net/cloud-www-assets/constructor/storybook/images/img-mini_6-12_light.png"
"logo": {
"src": "https://storage.yandexcloud.net/cloud-www-assets/constructor/storybook/images/img-mini_6-12_light.png",
"alt": "Logo alt"
}
}
}
}
7 changes: 2 additions & 5 deletions src/sub-blocks/Quote/schema.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {ImageProps, imageUrlPattern} from '../../components/Image/schema';
import {ImageProps} from '../../components/Image/schema';
import {BaseProps, ThemeProps, authorItem, withTheme} from '../../schema/validators/common';

export const Quote = {
Expand All @@ -12,10 +12,7 @@ export const Quote = {
contentType: 'text',
},
image: withTheme(ImageProps),
logo: {
type: 'string',
pattern: imageUrlPattern,
},
logo: ImageProps,
color: {
type: 'string',
},
Expand Down
4 changes: 3 additions & 1 deletion src/text-transform/transformers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@ function transformBlock(
}

if ('children' in block && block.children) {
block.children = transformBlocks(block.children as ConstructorBlock[], lang, blocksConfig);
block.children = transformBlocks(block.children as ConstructorBlock[], lang, blocksConfig, {
plugins,
});
}

return block;
Expand Down

0 comments on commit 2786c81

Please sign in to comment.