-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(link): add support for client side navigation (#16)
* refactor: move useDomRef into hooks directory * feat(link): use @react-aria/utils router to handle client side navigation * fix(hooks): export use design system context
- Loading branch information
1 parent
dba68fa
commit f7b4fc5
Showing
10 changed files
with
89 additions
and
32 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 |
---|---|---|
@@ -1,30 +1,36 @@ | ||
import type { ComponentWithoutAs } from '@/utilities/types' | ||
import type { LinkProps as ComponentProps } from 'react-aria-components' | ||
import type { LinkProps } from 'react-aria-components' | ||
|
||
import { link } from '@giantnodes/theme' | ||
import React from 'react' | ||
import { Link as Component } from 'react-aria-components' | ||
import { Link } from 'react-aria-components' | ||
|
||
import { useLink } from '@/components/link/use-link.hook' | ||
import { useDomRef } from '@/hooks/use-dom-ref' | ||
import { useLink } from '@/hooks/use-link.hook' | ||
|
||
export type LinkProps = ComponentWithoutAs<'a'> & ComponentProps | ||
type ComponentProps = ComponentWithoutAs<'a'> & LinkProps | ||
|
||
const Link = React.forwardRef<HTMLAnchorElement, LinkProps>((props, ref) => { | ||
const { children, className, ...rest } = props | ||
const Component = React.forwardRef<HTMLAnchorElement, LinkProps>((props, ref) => { | ||
const { children, className } = props | ||
|
||
const { slots } = useLink() | ||
const dom = useDomRef(ref) | ||
const { ...rest } = useLink(props, dom) | ||
|
||
const getProps = React.useCallback( | ||
const slots = React.useMemo(() => link({}), []) | ||
|
||
const component = React.useMemo<LinkProps>( | ||
() => ({ | ||
ref, | ||
className: slots.base({ className }), | ||
className: slots.link({ class: className?.toString() }), | ||
...rest, | ||
}), | ||
[ref, slots, className, rest] | ||
) | ||
|
||
return <Component {...getProps()}>{children}</Component> | ||
return <Link {...component}>{children}</Link> | ||
}) | ||
|
||
Link.displayName = 'Link' | ||
Component.displayName = 'Link' | ||
|
||
export default Link | ||
export { ComponentProps as LinkProps } | ||
export default Component |
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,29 @@ | ||
import { RouterProvider } from '@react-aria/utils' | ||
|
||
import { createContext } from '@/utilities/context' | ||
|
||
export type UseDesignSystemProps = React.PropsWithChildren & { | ||
/** | ||
* Provides a client side router to all components that contain links | ||
*/ | ||
navigate?: (path: string) => void | ||
} | ||
|
||
export type UseDesignSystemReturn = ReturnType<typeof useDesignSystem> | ||
|
||
export const useDesignSystem = ({ navigate, children }: UseDesignSystemProps) => { | ||
let contents = children | ||
|
||
if (navigate) { | ||
contents = <RouterProvider navigate={navigate}>{contents}</RouterProvider> | ||
} | ||
|
||
return contents | ||
} | ||
|
||
export const [DesignSystemContext, useDesignSystemContext] = createContext<UseDesignSystemReturn>({ | ||
name: 'DesignSystemContext', | ||
strict: true, | ||
errorMessage: | ||
'useDesignSystemContext: `context` is undefined. Seems you forgot to wrap component within <DesignSystem.Provider />', | ||
}) |
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,25 @@ | ||
import type { FocusableElement, PressEvent } from '@react-types/shared' | ||
import type React from 'react' | ||
import type { AriaLinkOptions } from 'react-aria' | ||
|
||
import { shouldClientNavigate, useRouter } from '@react-aria/utils' | ||
import { useLink as useAriaLink } from 'react-aria' | ||
|
||
export const useLink = (props: AriaLinkOptions, ref: React.RefObject<FocusableElement>) => { | ||
const { onPress: onAriaPress, ...rest } = props | ||
const router = useRouter() | ||
|
||
const onPress = (event: PressEvent) => { | ||
const { target } = event | ||
|
||
if (!(target instanceof HTMLAnchorElement)) return | ||
|
||
if (!router.isNative && target.href && shouldClientNavigate(target, event)) { | ||
router.open(target, event) | ||
} | ||
} | ||
|
||
return useAriaLink({ ...rest, onPress }, ref) | ||
} | ||
|
||
export type UseLinkReturn = ReturnType<typeof useLink> |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.