Skip to content

Commit

Permalink
add social icon component
Browse files Browse the repository at this point in the history
  • Loading branch information
cooperlarson committed Aug 21, 2024
1 parent 6ba9c5f commit 0749b27
Show file tree
Hide file tree
Showing 10 changed files with 132 additions and 4 deletions.
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
"payload": "cross-env PAYLOAD_CONFIG_PATH=src/payload/payload.config.ts payload"
},
"dependencies": {
"@fortawesome/fontawesome-svg-core": "^6.6.0",
"@fortawesome/free-brands-svg-icons": "^6.6.0",
"@fortawesome/free-solid-svg-icons": "^6.6.0",
"@fortawesome/react-fontawesome": "^0.2.2",
"@payloadcms/bundler-webpack": "^1.0.7",
"@payloadcms/db-postgres": "^0.8.5",
"@payloadcms/plugin-cloud": "^3.0.1",
Expand Down
25 changes: 25 additions & 0 deletions src/app/_components/Header/Social/index.module.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
@use '../../../_css/queries.scss' as *;

.socialIcons {
display: flex;
gap: calc(var(--base) / 4) var(--base);
flex-wrap: wrap;
opacity: 1;
transition: opacity 100ms linear;
visibility: visible;
padding-right: 20px;
}

.socialIcon {
color: rgba(255, 255, 255, 1);
font-size: 2em;

:hover {
color: rgba(255, 255, 255, 0.8)
}
}

.hide {
opacity: 0;
visibility: hidden;
}
33 changes: 33 additions & 0 deletions src/app/_components/Header/Social/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
'use client'

import React from 'react'
import { faGithub } from '@fortawesome/free-brands-svg-icons'

import { Header as HeaderType } from '../../../../payload/payload-types'
import { CMSLink } from '../../Link'

import classes from './index.module.scss'

export const HeaderSocial: React.FC<{ header: HeaderType }> = ({ header }) => {
const socialItems = header?.socialIcons || []

const fetchSocialIcon = (key: string) => {
if (key === 'github') return faGithub
throw Error('Unknown icon: ' + key)
}

return (
<div className={classes.socialIcons}>
{socialItems.map(({ link, faIcon }, i) => {
return (
<CMSLink
className={classes.socialIcon}
key={i}
{...link}
faIcon={fetchSocialIcon(faIcon)}
/>
)
})}
</div>
)
}
4 changes: 4 additions & 0 deletions src/app/_components/Header/index.module.scss
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
padding: var(--base) 0;
}

.navLinks {
display: flex;
}

.wrap {
display: flex;
justify-content: space-between;
Expand Down
6 changes: 5 additions & 1 deletion src/app/_components/Header/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { Header } from '../../../payload/payload-types'
import { fetchHeader } from '../../_api/fetchGlobals'
import { Gutter } from '../Gutter'
import { HeaderNav } from './Nav'
import { HeaderSocial } from './Social'

import classes from './index.module.scss'

Expand Down Expand Up @@ -40,7 +41,10 @@ export async function Header() {
src="https://res.cloudinary.com/ddddyraui/image/upload/cl-logo_e5nuxu.png"
/>
</Link>
<HeaderNav header={header} />
<div className={classes.navLinks}>
<HeaderSocial header={header} />
<HeaderNav header={header} />
</div>
</Gutter>
</header>
</>
Expand Down
16 changes: 14 additions & 2 deletions src/app/_components/Link/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import React from 'react'
import { IconDefinition } from '@fortawesome/free-brands-svg-icons'
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
import Link from 'next/link'

import { Page } from '../../../payload/payload-types'
Expand All @@ -17,6 +19,7 @@ type CMSLinkType = {
children?: React.ReactNode
className?: string
invert?: ButtonProps['invert']
faIcon?: IconDefinition
}

export const CMSLink: React.FC<CMSLinkType> = ({
Expand All @@ -29,6 +32,7 @@ export const CMSLink: React.FC<CMSLinkType> = ({
children,
className,
invert,
faIcon,
}) => {
const href =
type === 'reference' && typeof reference?.value === 'object' && reference.value.slug
Expand All @@ -43,7 +47,11 @@ export const CMSLink: React.FC<CMSLinkType> = ({
const newTabProps = newTab ? { target: '_blank', rel: 'noopener noreferrer' } : {}

if (href || url) {
return (
return faIcon ? (
<a {...newTabProps} className={className} href={href}>
<FontAwesomeIcon icon={faIcon} />
</a>
) : (
<Link {...newTabProps} href={href || url} className={className}>
{label && label}
{children && children}
Expand All @@ -52,7 +60,11 @@ export const CMSLink: React.FC<CMSLinkType> = ({
}
}

return (
return faIcon ? (
<a className={className} href={href}>
<FontAwesomeIcon icon={faIcon} />
</a>
) : (
<Button
className={className}
newTab={newTab}
Expand Down
2 changes: 1 addition & 1 deletion src/app/_css/common.scss
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
@forward './queries.scss';
@forward './type.scss';
@forward './type.scss';
4 changes: 4 additions & 0 deletions src/app/_graphql/globals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ export const HEADER = `
navItems {
link ${LINK_FIELDS({ disableAppearance: true })}
}
socialIcons {
faIcon,
link ${LINK_FIELDS({ disableAppearance: true })}
}
}
`

Expand Down
26 changes: 26 additions & 0 deletions src/payload/globals/Header.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,31 @@ export const Header: GlobalConfig = {
}),
],
},
{
name: 'socialIcons',
type: 'array',
maxRows: 6,
fields: [
{
name: 'faIcon',
type: 'select',
hasMany: false,
required: false,
admin: {
isClearable: true,
isSortable: true,
},
options: [
{
label: 'Github',
value: 'github',
},
],
},
link({
appearances: false,
}),
],
},
],
}
16 changes: 16 additions & 0 deletions src/payload/payload-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -921,6 +921,22 @@ export interface Header {
id?: string | null;
}[]
| null;
socialIcons?:
| {
faIcon?: 'github' | null;
link: {
type?: ('reference' | 'custom') | null;
newTab?: boolean | null;
reference?: {
relationTo: 'pages';
value: number | Page;
} | null;
url?: string | null;
label: string;
};
id?: string | null;
}[]
| null;
updatedAt?: string | null;
createdAt?: string | null;
}
Expand Down

0 comments on commit 0749b27

Please sign in to comment.