Skip to content

Commit

Permalink
feat: add SocialSharingMeta component (#363)
Browse files Browse the repository at this point in the history
* feat: add SocialSharingMeta component

* refactor: change SocialSharingMeta from class to functional component
  • Loading branch information
Pavel-Tyan authored Jan 24, 2025
1 parent 0a1c8ef commit 6fca7bd
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 0 deletions.
93 changes: 93 additions & 0 deletions src/components/SocialSharingMeta/SocialSharingMeta.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import React, {Fragment} from 'react';
import {Helmet} from 'react-helmet';

export type MetaProps = JSX.IntrinsicElements['meta'];

export interface SocialSharingMetaProps {
url?: string;
title?: string;
description?: string;
image?: string;
imageWidth?: number;
imageHeight?: number;
type?: string;
locale?: string;
extra?: MetaProps[];
withoutHelmet?: boolean;
children?: React.ReactNode;
}

export const SocialSharingMeta: React.FC<SocialSharingMetaProps> = (props) => {
const renderSchemaOrgTag = (name: string, value?: string | number) => {
return value && <meta key={`${name}-${value}`} itemProp={name} content={String(value)} />;
};

const renderOpenGraphTag = (name: string, value?: string | number) => {
return (
value && (
<meta key={`${name}-${value}`} property={`og:${name}`} content={String(value)} />
)
);
};

const renderTwitterTag = (name: string, value?: string | number) => {
return (
value && (
<meta key={`${name}-${value}`} name={`twitter:${name}`} content={String(value)} />
)
);
};

const renderExtraMeta = (props: MetaProps, index: number) => {
return React.createElement('meta', {...props, key: `extra-${index}`});
};

const {
url,
title,
description,
image,
imageWidth,
imageHeight,
type,
locale,
extra,
children,
withoutHelmet,
} = props;
let meta = [
renderSchemaOrgTag('name', title),
renderSchemaOrgTag('description', description),
renderSchemaOrgTag('image', image),

renderOpenGraphTag('type', type),
renderOpenGraphTag('url', url),
renderOpenGraphTag('title', title),
renderOpenGraphTag('description', description),
renderOpenGraphTag('image', image),
renderOpenGraphTag('image:width', imageWidth),
renderOpenGraphTag('image:height', imageHeight),
renderOpenGraphTag('locale', locale),

title && renderTwitterTag('card', 'summary_large_image'),
renderTwitterTag('title', title),
renderTwitterTag('description', description),
renderTwitterTag('image', image),
].filter(Boolean);

if (extra) {
meta = meta.concat(extra.map(renderExtraMeta));
}

return withoutHelmet ? (
<Fragment>
{meta}
{children}
</Fragment>
) : (
<Helmet>
{meta}
{children}
</Helmet>
);
};
1 change: 1 addition & 0 deletions src/components/SocialSharingMeta/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './SocialSharingMeta';

0 comments on commit 6fca7bd

Please sign in to comment.