Skip to content

Commit

Permalink
add UniservsalLink component #44
Browse files Browse the repository at this point in the history
  • Loading branch information
ukorvl committed Aug 3, 2023
1 parent 75443e4 commit 7ceaa68
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 23 deletions.
4 changes: 2 additions & 2 deletions app/not-found.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Link from 'next/link';
import UniservsalLink from '@/components/UniservsalLink/UniservsalLink';

/**
* @returns React component.
Expand All @@ -9,7 +9,7 @@ export default function NotFound() {
<h2>Not Found</h2>
<p>Could not find requested resource</p>
<p>
View <Link href="/blog">all posts</Link>
View <UniservsalLink href="/blog">all posts</UniservsalLink>
</p>
</div>
);
Expand Down
6 changes: 3 additions & 3 deletions app/page.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import clsx from 'clsx';
import Link from 'next/link';
import AppearInViewport from '@/components/AppearInViewport/AppearInViewport';
import MainPageVideoPlayer from '@/components/MainPageVideoPlayer/MainPageVideoPlayer';
import UniservsalLink from '@/components/UniservsalLink/UniservsalLink';

const containerCn = clsx('relative', 'z-10', 'flex', 'flex-col', 'items-center', 'grow');
const titleCn = clsx('font-light', 'text-8xl');
Expand Down Expand Up @@ -33,12 +33,12 @@ export default function Home() {
className={exploreCn}
variants={exploreLinkVariants}
>
<Link
<UniservsalLink
href="/about"
className={exploreLinkCn}
>
EXPLORE
</Link>
</UniservsalLink>
</AppearInViewport>
</div>
<MainPageVideoPlayer />
Expand Down
6 changes: 3 additions & 3 deletions components/Footer/Location.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import clsx from 'clsx';
import Link from 'next/link';
import {memo} from 'react';
import UniservsalLink from '../UniservsalLink/UniservsalLink';

const {NEXT_PUBLIC_LOCATION_ADDRESS_TEXT, NEXT_PUBLIC_LOCATION_GOOGLE_MAPS_LINK} = process.env;

Expand All @@ -14,13 +14,13 @@ const linkCn = clsx('animated-link');
function Location() {
return (
<div className={containerCn}>
<Link
<UniservsalLink
href={NEXT_PUBLIC_LOCATION_GOOGLE_MAPS_LINK ?? ''}
target="_blank"
className={linkCn}
>
<address className={addressCn}>{NEXT_PUBLIC_LOCATION_ADDRESS_TEXT}</address>
</Link>
</UniservsalLink>
</div>
);
}
Expand Down
5 changes: 3 additions & 2 deletions components/Footer/Mail.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import clsx from 'clsx';
import {memo} from 'react';
import UniservsalLink from '../UniservsalLink/UniservsalLink';

const {NEXT_PUBLIC_CONTACT_EMAIL} = process.env;

Expand All @@ -12,12 +13,12 @@ const anchorCn = clsx('animated-link');
function Mail() {
return (
<div className={containerCn}>
<a
<UniservsalLink
className={anchorCn}
href={`mailto:${NEXT_PUBLIC_CONTACT_EMAIL}`}
>
{NEXT_PUBLIC_CONTACT_EMAIL}
</a>
</UniservsalLink>
</div>
);
}
Expand Down
6 changes: 3 additions & 3 deletions components/Menu/SocialLinksList.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Link from 'next/link';
import {memo} from 'react';
import clsx from 'clsx';
import UniservsalLink from '../UniservsalLink/UniservsalLink';

const {NEXT_PUBLIC_FACEBOOK_ADDRESS, NEXT_PUBLIC_INSTAGRAM_ADDRESS, NEXT_PUBLIC_TELEGRAM_ADDRESS} =
process.env;
Expand Down Expand Up @@ -30,14 +30,14 @@ function SocialLinksList() {
return (
<div className={containerCn}>
{linksConfig.map(({title, href}) => (
<Link
<UniservsalLink
key={title}
href={href ?? ''}
target="_blank"
className={linkCn}
>
{title.toUpperCase()}
</Link>
</UniservsalLink>
))}
</div>
);
Expand Down
6 changes: 3 additions & 3 deletions components/SocialIcons/SocialIcons.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

import {FontAwesomeIcon} from '@fortawesome/react-fontawesome';
import clsx from 'clsx';
import Link from 'next/link';
import {twMerge} from 'tailwind-merge';
import {memo} from 'react';
import {m} from 'framer-motion';
import iconsConfig from './iconsConfig';
import AppearInViewport from '../AppearInViewport/AppearInViewport';
import UniservsalLink from '../UniservsalLink/UniservsalLink';

const iconsCn = clsx(
'flex',
Expand Down Expand Up @@ -54,15 +54,15 @@ function SocialIcons() {
key={href}
variants={variants}
>
<Link
<UniservsalLink
href={href}
target={target}
>
<FontAwesomeIcon
className={twMerge(iconCn, className)}
{...rest}
/>
</Link>
</UniservsalLink>
</m.div>
))}
</AppearInViewport>
Expand Down
14 changes: 7 additions & 7 deletions components/TopMenu/TopMenu.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
'use client';

import Link from 'next/link';
import clsx from 'clsx';
import {m} from 'framer-motion';
import {menuItemsConfig, socialLinksConfig} from './topMenuCongif';
import Menu from '../Menu/Menu';
import UniservsalLink from '../UniservsalLink/UniservsalLink';

const variants = {
open: {
Expand Down Expand Up @@ -37,24 +37,24 @@ export default function TopMenu() {
className={itemsListCn}
>
<Menu.Item className={homeMenuItem}>
<Link
<UniservsalLink
href="/"
className={linkCn}
>
SALSAVIVA
</Link>
</UniservsalLink>
</Menu.Item>
{menuItemsConfig.map(({href, text, bgImgPath}, idx) => (
<Menu.Item
key={idx}
bgImgPath={bgImgPath}
>
<Link
<UniservsalLink
href={href}
className={linkCn}
>
{text}
</Link>
</UniservsalLink>
</Menu.Item>
))}
</m.ul>
Expand All @@ -67,13 +67,13 @@ export default function TopMenu() {
key={idx}
className={socialLinkMenuItem}
>
<Link
<UniservsalLink
href={href}
className={linkCn}
target="_blank"
>
{text.toUpperCase()}
</Link>
</UniservsalLink>
</Menu.Item>
))}
</m.ul>
Expand Down
51 changes: 51 additions & 0 deletions components/UniservsalLink/UniservsalLink.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import Link, {LinkProps} from 'next/link';
import {AnchorHTMLAttributes, forwardRef} from 'react';

/**
* Props.
*/
type UniservsalLinkProps = Omit<AnchorHTMLAttributes<HTMLAnchorElement>, keyof LinkProps> &
LinkProps;

const externalLinkOpenings = ['https://', 'http://', 'mailto:', 'tel:'];

/**
* @param href Href.
* @returns True if link is external.
*/
function isLinkExternal(href: string) {
return externalLinkOpenings.some(opening => href.startsWith(opening));
}

/**
* @returns React component.
*/
const UniservsalLink = forwardRef<HTMLAnchorElement, UniservsalLinkProps>(function UniservsalLink(
{children, href, ...props},
ref,
) {
if (typeof href === 'object' || !isLinkExternal(href)) {
return (
<Link
href={href}
ref={ref}
{...props}
>
{children}
</Link>
);
}

return (
<a
href={href}
rel="noopener noreferrer"
ref={ref}
{...props}
>
{children}
</a>
);
});

export default UniservsalLink;

0 comments on commit 7ceaa68

Please sign in to comment.