-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0601119
commit 4934ab7
Showing
17 changed files
with
204 additions
and
13 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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,113 @@ | ||
import * as React from "react"; | ||
import clsx from "clsx"; | ||
import { useRouter } from "next/router"; | ||
import NextLink, { LinkProps as NextLinkProps } from "next/link"; | ||
import MuiLink, { LinkProps as MuiLinkProps } from "@mui/material/Link"; | ||
import { styled } from "@mui/material/styles"; | ||
|
||
// Add support for the sx prop for consistency with the other branches. | ||
const Anchor = styled("a")({}); | ||
|
||
export interface NextLinkComposedProps | ||
extends Omit<React.AnchorHTMLAttributes<HTMLAnchorElement>, "href">, | ||
Omit<NextLinkProps, "href" | "as"> { | ||
to: NextLinkProps["href"]; | ||
linkAs?: NextLinkProps["as"]; | ||
href?: NextLinkProps["href"]; | ||
} | ||
|
||
export const NextLinkComposed = React.forwardRef< | ||
HTMLAnchorElement, | ||
NextLinkComposedProps | ||
>(function NextLinkComposed(props, ref) { | ||
const { | ||
to, | ||
linkAs, | ||
href, | ||
replace, | ||
scroll, | ||
shallow, | ||
prefetch, | ||
locale, | ||
...other | ||
} = props; | ||
|
||
return ( | ||
<NextLink | ||
href={to} | ||
prefetch={prefetch} | ||
as={linkAs} | ||
replace={replace} | ||
scroll={scroll} | ||
shallow={shallow} | ||
passHref | ||
locale={locale} | ||
> | ||
<Anchor ref={ref} {...other} /> | ||
</NextLink> | ||
); | ||
}); | ||
|
||
export type LinkProps = { | ||
activeClassName?: string; | ||
as?: NextLinkProps["as"]; | ||
href: NextLinkProps["href"]; | ||
noLinkStyle?: boolean; | ||
} & Omit<NextLinkComposedProps, "to" | "linkAs" | "href"> & | ||
Omit<MuiLinkProps, "href">; | ||
|
||
// A styled version of the Next.js Link component: | ||
// https://nextjs.org/docs/#with-link | ||
const Link = React.forwardRef<HTMLAnchorElement, LinkProps>(function Link( | ||
props, | ||
ref | ||
) { | ||
const { | ||
activeClassName = "active", | ||
as: linkAs, | ||
className: classNameProps, | ||
href, | ||
noLinkStyle, | ||
role, // Link don't have roles. | ||
...other | ||
} = props; | ||
|
||
const router = useRouter(); | ||
const pathname = typeof href === "string" ? href : href.pathname; | ||
const className = clsx(classNameProps, { | ||
[activeClassName]: router.pathname === pathname && activeClassName, | ||
}); | ||
|
||
const isExternal = | ||
typeof href === "string" && | ||
(href.indexOf("http") === 0 || href.indexOf("mailto:") === 0); | ||
|
||
if (isExternal) { | ||
if (noLinkStyle) { | ||
// @ts-expect-error | ||
return <Anchor className={className} href={href} ref={ref} {...other} />; | ||
} | ||
|
||
// @ts-expect-error | ||
return <MuiLink className={className} href={href} ref={ref} {...other} />; | ||
} | ||
|
||
if (noLinkStyle) { | ||
return ( | ||
<NextLinkComposed className={className} ref={ref} to={href} {...other} /> | ||
); | ||
} | ||
|
||
return ( | ||
<MuiLink | ||
component={NextLinkComposed} | ||
linkAs={linkAs} | ||
className={className} | ||
ref={ref} | ||
to={href} | ||
{...other} | ||
/> | ||
); | ||
}); | ||
|
||
export default Link; |
File renamed without changes.
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,50 @@ | ||
import { | ||
Button, | ||
ButtonProps, | ||
ListItemButton, | ||
ListItemButtonProps, | ||
} from "@mui/material"; | ||
import NextLink, { LinkProps as NextLinkProps } from "next/link"; | ||
import pick from "lodash/pick"; | ||
import omit from "lodash/omit"; | ||
|
||
const nextLinkProps: Array<keyof NextLinkProps> = [ | ||
"href", | ||
"as", | ||
"replace", | ||
"scroll", | ||
"shallow", | ||
"passHref", | ||
"prefetch", | ||
"locale", | ||
]; | ||
/** | ||
* Button to be used when using href and pointing toward a local page | ||
*/ | ||
export const NextMuiButton = ({ | ||
children, | ||
...props | ||
}: ButtonProps & NextLinkProps) => { | ||
const linkProps = pick(props, nextLinkProps); | ||
const buttonProps = omit(props, nextLinkProps); | ||
return ( | ||
<Button {...buttonProps}> | ||
<NextLink {...linkProps}>{children}</NextLink> | ||
</Button> | ||
); | ||
}; | ||
/** | ||
* Button to be used when using href and pointing toward a local page | ||
*/ | ||
export const NextMuiListItemButton = ({ | ||
children, | ||
...props | ||
}: ListItemButtonProps & NextLinkProps) => { | ||
const linkProps = pick(props, nextLinkProps); | ||
const buttonProps = omit(props, nextLinkProps); | ||
return ( | ||
<ListItemButton {...buttonProps}> | ||
<NextLink {...linkProps}>{children}</NextLink> | ||
</ListItemButton> | ||
); | ||
}; |
21 changes: 21 additions & 0 deletions
21
packages/@vulcanjs/next-mui/components/stories/Link.stories.tsx
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,21 @@ | ||
import React from "react"; | ||
import { NextLinkComposed, NextLinkComposedProps } from "../Link"; | ||
// import { action } from "@storybook/addon-actions"; | ||
import { Story, Meta } from "@storybook/react"; | ||
|
||
export default { | ||
title: "next-mui/NextMuiLink", | ||
component: NextLinkComposed, | ||
// decorators: [(Story) => <div><Story /></div>, | ||
args: {}, | ||
} as Meta<NextLinkComposedProps>; | ||
|
||
const Template: Story<NextLinkComposedProps> = (args) => ( | ||
<NextLinkComposed {...args} /> | ||
); | ||
|
||
// please keep this default story as is => it serves as a basis for Jest unit tests as well | ||
export const DefaultNextMuiLink = Template.bind({}); | ||
|
||
// export const Basic = Template.bind({}) | ||
// Basic.args = { ...DefaultNextMuiLink.args/*, add other props here */ } |
File renamed without changes.
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,8 @@ | ||
export * from "./emotion/createEmotionCache"; | ||
|
||
export { default as Link } from "./components/Link"; // deprecated alias | ||
export { default as NextMuiLink } from "./components/Link"; // recommanded alias to use | ||
export { | ||
NextMuiButton, | ||
NextMuiListItemButton, | ||
} from "./components/NextMuiButton"; // other components accepting links |
2 changes: 1 addition & 1 deletion
2
...s/@vulcanjs/next-material-ui/package.json → packages/@vulcanjs/next-mui/package.json
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
File renamed without changes.
File renamed without changes.
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
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
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