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

add component for link buttons #35

Merged
merged 4 commits into from
Jun 11, 2024
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
17 changes: 2 additions & 15 deletions src/common/Blocks/CTACenteredBlock/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Link from 'next/link';
import type { CTACenteredBlock as CTACenteredBlockType } from '@/payload-types';
import { LinkButton } from '../../Components/LinkButtons';

export const CTACenteredBlock = ({ data }: { data: CTACenteredBlockType | null | undefined }) => {
if (!data) return null;
Expand All @@ -22,20 +22,7 @@ export const CTACenteredBlock = ({ data }: { data: CTACenteredBlockType | null |
</div>

<div className="mt-10 flex items-center justify-center gap-x-6">
{data.links?.map(({ link }, index) => (
<Link
href={link.url ? link.url : ''}
key={index}
className={`rounded-md px-3.5 py-2.5 text-sm font-semibold shadow-sm ${
link.appearance === 'primary'
? 'bg-indigo-600 text-white hover:bg-indigo-500 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600'
: 'text-gray-900 bg-white'
}`}
target={link.newTab ? '_blank' : '_self'}
>
{link.label}
</Link>
))}
{data.links?.map(({ link }, index) => <LinkButton link={link} key={index} />)}
</div>
</div>
</section>
Expand Down
17 changes: 2 additions & 15 deletions src/common/Blocks/CTAWithSplitContentAndButtonBlock/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Link from 'next/link';
import { LinkButton } from '../../Components/LinkButtons';
import type { CTAWithSplitContentAndButtonBlock as CTAWithSplitContentAndButtonBlockType } from '@/payload-types';

export const CTAWithSplitContentAndButtonBlock = ({
Expand Down Expand Up @@ -27,20 +27,7 @@ export const CTAWithSplitContentAndButtonBlock = ({
</div>

<div className="mt-10 flex items-center gap-x-6 lg:mt-0 lg:flex-shrink-0">
{data.links?.map(({ link }, index) => (
<Link
href={link.url ? link.url : ''}
key={index}
className={`rounded-md px-3.5 py-2.5 text-sm font-semibold shadow-sm ${
link.appearance === 'primary'
? 'bg-indigo-600 text-white hover:bg-indigo-500 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600'
: 'text-gray-900 bg-white'
}`}
target={link.newTab ? '_blank' : '_self'}
>
{link.label}
</Link>
))}
{data.links?.map(({ link }, index) => <LinkButton link={link} key={index} />)}
</div>
</div>
</div>
Expand Down
18 changes: 2 additions & 16 deletions src/common/Blocks/CTAWithSplitContentAndImageBlock/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import Link from 'next/link';
import type { CTAWithSplitContentAndImageBlock as CTAWithSplitContentAndImageBlockType } from '@/payload-types';

import Image from 'next/image';
import { LinkButton } from '../../Components/LinkButtons';

export const CTAWithSplitContentAndImageBlock = ({
data,
Expand Down Expand Up @@ -48,20 +47,7 @@ export const CTAWithSplitContentAndImageBlock = ({
</div>

<div className="mt-10 flex items-center justify-center gap-x-6 lg:justify-start">
{data.links?.map(({ link }, index) => (
<Link
href={link.url ? link.url : ''}
key={index}
className={`rounded-md px-3.5 py-2.5 text-sm font-semibold shadow-sm ${
link.appearance === 'primary'
? 'bg-indigo-600 text-white hover:bg-indigo-500 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600'
: 'text-gray-900 bg-white'
}`}
target={link.newTab ? '_blank' : '_self'}
>
{link.label}
</Link>
))}
{data.links?.map(({ link }, index) => <LinkButton link={link} key={index} />)}
</div>
</div>
<div className="relative mt-16 h-80 lg:mt-8">
Expand Down
17 changes: 2 additions & 15 deletions src/common/Blocks/ContentBlock/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { LinkButton } from '../../Components/LinkButtons';
import { ContentBlock as ContentBlockType } from '@/payload-types';
import Link from 'next/link';

export const ContentBlock = ({ data }: { data: ContentBlockType | null | undefined }) => {
if (!data) return null;
Expand Down Expand Up @@ -27,20 +27,7 @@ export const ContentBlock = ({ data }: { data: ContentBlockType | null | undefin
{richText_html && <div dangerouslySetInnerHTML={{ __html: richText_html }} />}
{enableLink &&
links &&
links.map(({ link }, index) => (
<Link
key={index}
href={link.url ? link.url : ''}
target={link.newTab ? '_blank' : '_self'}
className={`mr-2 mt-2 inline-block rounded border border-indigo-600 ${
link.appearance === 'primary' && 'bg-indigo-600'
} px-12 py-3 text-sm font-medium ${
link.appearance === 'primary' ? 'text-white' : 'text-indigo-600'
} transition hover:bg-indigo-700 focus:outline-none focus:ring focus:ring-yellow-400`}
>
{link.label}
</Link>
))}
links.map(({ link }, index) => <LinkButton link={link} key={index} />)}
</div>
))}
</div>
Expand Down
10 changes: 4 additions & 6 deletions src/common/Blocks/FeatureWithThreeColumnBlock/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Link from 'next/link';
import { CustomIcon } from '../../Components/CustomIcon';
import type { FeatureWithThreeColumnBlock as FeatureWithThreeColumnBlockType } from '@/payload-types';
import { LinkButton } from '../../Components/LinkButtons';

export const FeatureWithThreeColumnBlock = ({
data,
Expand All @@ -27,12 +27,10 @@ export const FeatureWithThreeColumnBlock = ({
)}
<p className="mt-6">
{link && (
<Link
href={link.url ? link.url : ''}
<LinkButton
link={link}
className={`text-sm font-semibold leading-6 text-indigo-600`}
>
{link.label}
</Link>
/>
)}
</p>
</dd>
Expand Down
16 changes: 2 additions & 14 deletions src/common/Blocks/HeroWithBackgroundBlock/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Link from 'next/link';
import type { HeroWithBackgroundBlock as HeroWithBackgroundBlockType } from '@/payload-types';
import { MediaImage } from '../../Mediaimage';
import { LinkButton } from '../../Components/LinkButtons';

export const HeroWithBackgroundBlock = ({
data,
Expand Down Expand Up @@ -41,19 +41,7 @@ export const HeroWithBackgroundBlock = ({
</div>

<div className="mt-6">
{data.links?.map(({ link }, index) => (
<Link
href={link.url ? link.url : ''}
key={index}
className={`mr-2 mt-2 inline-block rounded border border-indigo-600 ${
link.appearance === 'primary' && 'bg-indigo-600'
} px-12 py-3 text-sm font-medium ${
link.appearance === 'primary' ? 'text-white' : 'text-indigo-600'
} transition hover:bg-indigo-700 focus:outline-none focus:ring focus:ring-yellow-400`}
>
{link.label}
</Link>
))}
{data.links?.map(({ link }, index) => <LinkButton link={link} key={index} />)}
</div>
</div>
{data.backgroundType === 'backgroundImage' && data.backgroundImage && (
Expand Down
17 changes: 2 additions & 15 deletions src/common/Blocks/HeroWithSplitContentAndImageBlock/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Link from 'next/link';
import type { HeroWithSplitContentAndImageBlock as HeroWithSplitContentAndImageBlockType } from '@/payload-types';
import { MediaImage } from '../../Mediaimage';
import { LinkButton } from '../../Components/LinkButtons';

export const HeroWithSplitContentAndImageBlock = ({
data,
Expand Down Expand Up @@ -42,20 +42,7 @@ export const HeroWithSplitContentAndImageBlock = ({
</div>

<div className="mt-6">
{data.links?.map(({ link }, index) => (
<Link
href={link.url ? link.url : ''}
key={index}
className={`mr-2 mt-2 inline-block rounded border border-indigo-600 ${
link.appearance === 'primary' && 'bg-indigo-600'
} px-12 py-3 text-sm font-medium ${
link.appearance === 'primary' ? 'text-white' : 'text-indigo-600'
} transition hover:bg-indigo-700 focus:outline-none focus:ring focus:ring-yellow-400`}
target={link.newTab ? '_blank' : '_self'}
>
{link.label}
</Link>
))}
{data.links?.map(({ link }, index) => <LinkButton link={link} key={index} />)}
</div>
</div>
</div>
Expand Down
79 changes: 79 additions & 0 deletions src/common/Components/LinkButtons/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import { Page } from '@/payload-types';
import Link from 'next/link';

interface Link {
type?: ('reference' | 'custom') | null;
newTab?: boolean | null;
reference?: {
relationTo: 'pages';
value: string | Page;
} | null;
url?: string | null;
label?: string;
appearance?: ('primary' | 'secondary') | null;
}

export const LinkButton = ({ link, className }: { link: Link; className?: string }) => {
if (link.type === 'custom') {
if (link.appearance)
return (
link && (
<Link
href={link.url ? link.url : ''}
className={`mr-2 mt-2 inline-block rounded border border-indigo-600 ${
link.appearance === 'primary' && 'bg-indigo-600'
} px-12 py-3 text-sm font-medium ${
link.appearance === 'primary' ? 'text-white' : 'text-indigo-600'
} transition hover:bg-indigo-700 hover:text-white focus:outline-none focus:ring focus:ring-yellow-400`}
target={link.newTab ? '_blank' : '_self'}
>
{link.label}
</Link>
)
);

return (
link && (
<Link
href={link.url ? link.url : ''}
className={`${className} text-sm font-semibold leading-6 text-indigo-600`}
>
{JSON.stringify(link)}
{link.label}
</Link>
)
);
}

if (link.type === 'reference') {
const doc = link.reference?.value;
if (typeof doc !== 'string' && doc?.slug) {
if (link.appearance) {
return (
link && (
<Link
className={`mr-2 mt-2 inline-block rounded border border-indigo-600 ${
link.appearance === 'primary' && 'bg-indigo-600'
} px-12 py-3 text-sm font-medium ${
link.appearance === 'primary' ? 'text-white' : 'text-indigo-600'
} transition hover:bg-indigo-700 hover:text-white focus:outline-none focus:ring focus:ring-yellow-400`}
target={link.newTab ? '_blank' : '_self'}
href={doc.slug}
>
{doc.title}
</Link>
)
);
}

return (
<Link
className={`${className} text-gray-500 transition hover:text-gray-500/75`}
href={doc.slug}
>
{doc.title}
</Link>
);
}
}
};
2 changes: 1 addition & 1 deletion tailwind.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
export default {
content: [
'./src/pages/**/*.{js,ts,jsx,tsx,mdx}',
'./src/components/**/*.{js,ts,jsx,tsx,mdx}',
'./src/Components/**/*.{js,ts,jsx,tsx,mdx}',
'./src/common/**/*.{js,ts,jsx,tsx,mdx}',
'./src/app/**/*.{js,ts,jsx,tsx,mdx}',
],
Expand Down