-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add
eslint-plugin-react
(#251)
- Loading branch information
1 parent
b8b39e6
commit 2434231
Showing
13 changed files
with
690 additions
and
302 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
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,49 +1,61 @@ | ||
import * as React from 'react' | ||
import type { AnchorHTMLAttributes } from 'react' | ||
import type * as React from 'react' | ||
import { useInView } from 'react-intersection-observer' | ||
|
||
import { useRouter } from '../hooks/useRouter' | ||
import useRoute from '../hooks/useRoute' | ||
|
||
interface TuonoLinkProps { | ||
interface TuonoLinkProps extends React.AnchorHTMLAttributes<HTMLAnchorElement> { | ||
/** | ||
* If "true" the route gets loaded when the link enters the viewport. Default "true" | ||
* If "true" the route gets loaded when the link enters the viewport. | ||
* @default true | ||
*/ | ||
preload?: boolean | ||
|
||
/** | ||
* If "false" the scroll offset will be kept across page navigation. Default "true" | ||
* If "false" the scroll offset will be kept across page navigation. | ||
* @default true | ||
*/ | ||
scroll?: boolean | ||
} | ||
|
||
export default function Link( | ||
componentProps: AnchorHTMLAttributes<HTMLAnchorElement> & TuonoLinkProps, | ||
componentProps: TuonoLinkProps, | ||
): React.JSX.Element { | ||
const { preload = true, scroll = true, ...props } = componentProps | ||
const { | ||
preload = true, | ||
scroll = true, | ||
children, | ||
href, | ||
onClick, | ||
...rest | ||
} = componentProps | ||
|
||
const router = useRouter() | ||
const route = useRoute(props.href) | ||
const route = useRoute(href) | ||
const { ref } = useInView({ | ||
onChange(inView) { | ||
if (inView && preload) route?.component.preload() | ||
}, | ||
triggerOnce: true, | ||
}) | ||
|
||
const handleTransition: React.MouseEventHandler<HTMLAnchorElement> = (e) => { | ||
e.preventDefault() | ||
props.onClick?.(e) | ||
const handleTransition: React.MouseEventHandler<HTMLAnchorElement> = ( | ||
event, | ||
) => { | ||
event.preventDefault() | ||
onClick?.(event) | ||
|
||
if (props.href?.startsWith('#')) { | ||
window.location.hash = props.href | ||
if (href?.startsWith('#')) { | ||
window.location.hash = href | ||
return | ||
} | ||
|
||
router.push(props.href || '', { scroll }) | ||
router.push(href || '', { scroll }) | ||
} | ||
|
||
return ( | ||
<a {...props} ref={ref} onClick={handleTransition}> | ||
{props.children} | ||
<a {...rest} href={href} ref={ref} onClick={handleTransition}> | ||
{children} | ||
</a> | ||
) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
{ | ||
"extends": "../../tsconfig.json", | ||
"compilerOptions": { | ||
"jsx": "react" | ||
"jsx": "react-jsx" | ||
}, | ||
"include": ["src", "tests", "vite.config.ts"] | ||
} |
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,7 +1,7 @@ | ||
{ | ||
"extends": "../../tsconfig.json", | ||
"compilerOptions": { | ||
"jsx": "react" | ||
"jsx": "react-jsx" | ||
}, | ||
"include": ["src", "tests", "vite.config.ts"] | ||
} |
Oops, something went wrong.