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: use appropriate heading levels for blog post cards #103

Merged
merged 3 commits into from
Sep 25, 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
23 changes: 13 additions & 10 deletions src/components/PostCard/PostCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,33 @@ import React, {useContext, useMemo} from 'react';
import {CardBase, HTML, MetrikaGoal, YFMWrapper} from '@gravity-ui/page-constructor';

import {LikesContext} from '../../contexts/LikesContext';
import {PostData} from '../../models/common';
import {PostCardSize, PostCardTitleHeadingLevel, PostData} from '../../models/common';
import {block} from '../../utils/cn';
import {SuggestPostInfo} from '../PostInfo/SuggestPostInfo';

import './PostCard.scss';

const b = block('post-card');

type PostCardProps = {
post: PostData;
fullWidth?: boolean;
showTag?: boolean;
size?: 's' | 'm';
size?: PostCardSize;
titleHeadingLevel?: PostCardTitleHeadingLevel;
/**
* @deprecated Metrika will be deleted after launch of analyticsEvents
*/
metrikaGoals?: MetrikaGoal;
};

const b = block('post-card');

export const PostCard: React.FC<PostCardProps> = ({
post,
metrikaGoals,
fullWidth = false,
size = 's',
size = PostCardSize.SMALL,
showTag = false,
titleHeadingLevel = PostCardTitleHeadingLevel.H3,
}) => {
const {
title: postTitle,
Expand Down Expand Up @@ -70,13 +72,14 @@ export const PostCard: React.FC<PostCardProps> = ({
{showTag && tags?.[0]?.name && (
<div className={b('tag', {size})}>{tags[0].name}</div>
)}
{title && (
<h4 className={b('title', {size})}>
{title &&
React.createElement(
titleHeadingLevel,
{className: b('title', {size})},
<span>
<HTML>{title}</HTML>
</span>
</h4>
)}
</span>,
)}
{description && (
<YFMWrapper
className={b('description')}
Expand Down
6 changes: 3 additions & 3 deletions src/components/PostInfo/SuggestPostInfo.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react';

import {useLikes} from '../../hooks/useLikes';
import {PostData, QAProps, ToggleLikeCallbackType} from '../../models/common';
import {PostCardSize, PostData, QAProps, ToggleLikeCallbackType} from '../../models/common';
import {block} from '../../utils/cn';

import {Date} from './components/Date';
Expand All @@ -16,7 +16,7 @@ export interface SuggestPostInfoProps
extends Pick<PostData, 'date' | 'readingTime' | 'hasUserLike'>,
QAProps {
postId: PostData['blogPostId'];
size?: 's' | 'm';
size?: PostCardSize;
likes?: {
likesCount?: number;
hasUserLike?: boolean;
Expand All @@ -43,7 +43,7 @@ export const SuggestPostInfo: React.FC<SuggestPostInfoProps> = ({
date,
readingTime,
likes,
size = 's',
size = PostCardSize.SMALL,
qa,
}) => {
const {hasUserLike, likesCount, handleLike} = useLikes({
Expand Down
5 changes: 3 additions & 2 deletions src/components/PostInfo/components/Date.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, {useContext} from 'react';

import {LocaleContext} from '../../../contexts/LocaleContext';
import {PostCardSize} from '../../../models/common';
import {block} from '../../../utils/cn';
import {format} from '../../../utils/date';

Expand All @@ -10,10 +11,10 @@ const b = block('post-info');

type DateProps = {
date: string | number;
size?: 's' | 'm';
size?: PostCardSize;
};

export const Date: React.FC<DateProps> = ({date, size = 's'}) => {
export const Date: React.FC<DateProps> = ({date, size = PostCardSize.SMALL}) => {
const {locale} = useContext(LocaleContext);

return <div className={b('item', {size})}>{format(date, 'longDate', locale?.code)}</div>;
Expand Down
21 changes: 18 additions & 3 deletions src/components/Posts/Posts.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {CardLayoutBlock} from '@gravity-ui/page-constructor';
import {Button} from '@gravity-ui/uikit';

import {Keyset, i18} from '../../i18n';
import {PostData} from '../../models/common';
import {PostCardSize, PostCardTitleHeadingLevel, PostData} from '../../models/common';
import {block} from '../../utils/cn';
import {Paginator} from '../Paginator/Paginator';
import {PostCard} from '../PostCard/PostCard';
Expand Down Expand Up @@ -50,7 +50,13 @@ export const Posts: React.FC<PostCardProps> = ({
<div id={containerId} className={b('cards-container', {isLoading: isFetching})}>
{pinnedPostOnPage && currentPage === 1 && (
<div className={b('pinned-container')}>
<PostCard post={pinnedPostOnPage} size="m" fullWidth showTag />
<PostCard
post={pinnedPostOnPage}
size={PostCardSize.MEDIUM}
fullWidth
showTag
titleHeadingLevel={PostCardTitleHeadingLevel.H2}
/>
</div>
)}
{postsOnPage?.length ? (
Expand All @@ -63,7 +69,16 @@ export const Posts: React.FC<PostCardProps> = ({
}}
>
{postsOnPage?.map((post) => (
<PostCard key={post.id} post={post} showTag />
<PostCard
key={post.id}
post={post}
showTag
titleHeadingLevel={
pinnedPostOnPage
? PostCardTitleHeadingLevel.H3
: PostCardTitleHeadingLevel.H2
}
/>
))}
</CardLayoutBlock>
) : (
Expand Down
10 changes: 10 additions & 0 deletions src/models/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,3 +201,13 @@ export type FetchArgs = {
export interface QAProps {
qa?: string;
}

export enum PostCardSize {
SMALL = 's',
MEDIUM = 'm',
}

export enum PostCardTitleHeadingLevel {
H2 = 'h2',
H3 = 'h3',
}