Skip to content

Commit

Permalink
feat(ui): create custom <a/> component
Browse files Browse the repository at this point in the history
  • Loading branch information
mateusfg7 committed Jun 6, 2023
1 parent e9ba923 commit a68a3ab
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
19 changes: 19 additions & 0 deletions src/app/(blog)/post/[slug]/components/anchor.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { AnchorHTMLAttributes, DetailedHTMLProps, ReactNode } from 'react'

import { ArrowSquareOut } from '@/shared/lib/phosphor-icons'

interface Props
extends DetailedHTMLProps<
AnchorHTMLAttributes<HTMLAnchorElement>,
HTMLAnchorElement
> {
children: ReactNode
}

export function Anchor({ children, href, ...props }: Props) {
return (
<a className="inline-flex items-center gap-1" href={href} {...props}>
{children} <ArrowSquareOut />
</a>
)
}
15 changes: 14 additions & 1 deletion src/app/(blog)/post/[slug]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from 'react'
import type { Metadata } from 'next'
import type { MDXComponents } from 'mdx/types'
import Link from 'next/link'
import { useMDXComponent } from 'next-contentlayer/hooks'
import { FiTag } from 'react-icons/fi'
Expand All @@ -11,6 +12,7 @@ import { Folder } from '@/shared/lib/phosphor-icons'
import { Date } from '@/shared/components/date'
import { CalendarBlank, Clock } from '@/shared/components/icons'
import { TopButton } from './components/top-button'
import { Anchor } from './components/anchor'

interface Props {
params: { slug: string }
Expand All @@ -30,6 +32,17 @@ export function generateMetadata({ params }: Props): Metadata {
}
}

const mdxComponents: MDXComponents = {
a: ({ children, href, ...props }) =>
href?.startsWith('http') ? (
<Anchor href={href} {...props}>
{children}
</Anchor>
) : (
<a href={href}>{children}</a>
)
}

export default function Page({ params }: Props) {
const post = allPosts.find(post => post.id === params.slug) as Post

Expand Down Expand Up @@ -97,7 +110,7 @@ export default function Page({ params }: Props) {
</div>
</div>
<div className="post-content">
<MDXContent />
<MDXContent components={mdxComponents} />
</div>
<TopButton />
</div>
Expand Down

0 comments on commit a68a3ab

Please sign in to comment.