-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add universal chakra link component (#243)
- Loading branch information
tundera
authored
Jan 10, 2022
1 parent
dbe5380
commit 4265bfa
Showing
8 changed files
with
136 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
import type { PropsWithChildren } from 'react'; | ||
import type { LinkProps as NextLinkProps } from 'next/link'; | ||
import React from 'react'; | ||
import NextLink from 'next/link'; | ||
import { | ||
Button as ChakraButton, | ||
ButtonProps as ChakraButtonProps, | ||
BreadcrumbLink as ChakraBreadcrumbLink, | ||
BreadcrumbLinkProps as ChakraBreadcrumbLinkProps, | ||
Link as ChakraLink, | ||
LinkProps as ChakraLinkProps, | ||
} from '@chakra-ui/react'; | ||
|
||
export type NextLinkAs = NextLinkProps['as']; | ||
|
||
export type LinkProps<T> = | ||
| PropsWithChildren<NextLinkProps & Omit<T, 'as'>> | ||
| PropsWithChildren<Omit<NextLinkProps, 'as'> & T>; | ||
|
||
type ChakraLinkAs = ChakraLinkProps['as']; | ||
|
||
// Has to be a new component because both chakra and next share the `as` keyword | ||
export const Link = ({ | ||
href, | ||
as, | ||
replace, | ||
scroll, | ||
shallow, | ||
prefetch, | ||
locale, | ||
isExternal, | ||
children, | ||
...chakraProps | ||
}: LinkProps<ChakraLinkProps>) => { | ||
return isExternal ? ( | ||
<ChakraLink href={href} as={as as ChakraLinkAs} isExternal {...chakraProps}> | ||
{children} | ||
</ChakraLink> | ||
) : ( | ||
<NextLink | ||
passHref={true} | ||
href={href} | ||
as={as as NextLinkAs} | ||
replace={replace} | ||
scroll={scroll} | ||
shallow={shallow} | ||
prefetch={prefetch} | ||
locale={locale} | ||
> | ||
<ChakraLink {...chakraProps}>{children}</ChakraLink> | ||
</NextLink> | ||
); | ||
}; | ||
|
||
export const ButtonLink = ({ | ||
href, | ||
as, | ||
replace, | ||
scroll, | ||
shallow, | ||
prefetch, | ||
locale, | ||
children, | ||
...chakraProps | ||
}: LinkProps<ChakraButtonProps>) => { | ||
return ( | ||
<NextLink | ||
passHref={true} | ||
href={href} | ||
as={as as NextLinkAs} | ||
replace={replace} | ||
scroll={scroll} | ||
shallow={shallow} | ||
prefetch={prefetch} | ||
locale={locale} | ||
> | ||
<ChakraButton as="a" {...chakraProps}> | ||
{children} | ||
</ChakraButton> | ||
</NextLink> | ||
); | ||
}; | ||
|
||
export const BreadcrumbLink = ({ | ||
href, | ||
as, | ||
replace, | ||
scroll, | ||
shallow, | ||
prefetch, | ||
locale, | ||
children, | ||
...chakraProps | ||
}: LinkProps<ChakraBreadcrumbLinkProps>) => { | ||
return ( | ||
<NextLink | ||
passHref={true} | ||
href={href} | ||
as={as as NextLinkAs} | ||
replace={replace} | ||
scroll={scroll} | ||
shallow={shallow} | ||
prefetch={prefetch} | ||
locale={locale} | ||
> | ||
<ChakraBreadcrumbLink {...chakraProps}>{children}</ChakraBreadcrumbLink> | ||
</NextLink> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,12 @@ | ||
import React from 'react'; | ||
import { Heading } from '@chakra-ui/react'; | ||
import NextLink from 'next/link'; | ||
|
||
import { Link } from './Link'; | ||
|
||
export function Logo() { | ||
return ( | ||
<NextLink href="/" passHref> | ||
<Heading as="a" size="md"> | ||
MyApp | ||
</Heading> | ||
</NextLink> | ||
<Heading as={Link} href="/" size="md"> | ||
MyApp | ||
</Heading> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters