From 4265bfa571f4034c846e17a0610175a55bcc7ee5 Mon Sep 17 00:00:00 2001 From: tundera Date: Mon, 10 Jan 2022 15:09:21 -0500 Subject: [PATCH] feat: add universal chakra link component (#243) --- README.md | 2 +- .../template/components/Footer.tsx | 3 +- .../template/components/Link.tsx | 109 ++++++++++++++++++ .../template/components/LoginForm.tsx | 22 +--- .../template/components/Logo.tsx | 11 +- .../template/components/Nav.tsx | 12 +- .../template/components/SignupForm.tsx | 22 +--- .../template/layouts/LoggedOut.tsx | 12 +- 8 files changed, 136 insertions(+), 57 deletions(-) create mode 100644 packages/create-bison-app/template/components/Link.tsx diff --git a/README.md b/README.md index e2411700..592f2a4e 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@

Bison Logo -

The Full Stack Jamstack in-a-box.

+

The Full Stack Jamstack in-a-box

Bison is a starter repository created out of real-world apps at [Echobind](https://echobind.com). It represents our team's "Greenfield Web Stack" that we use when creating web apps for clients. diff --git a/packages/create-bison-app/template/components/Footer.tsx b/packages/create-bison-app/template/components/Footer.tsx index 8ad469b6..4970e312 100644 --- a/packages/create-bison-app/template/components/Footer.tsx +++ b/packages/create-bison-app/template/components/Footer.tsx @@ -1,6 +1,7 @@ -import { Center, Flex, Text, Link } from '@chakra-ui/layout'; +import { Center, Flex, Text } from '@chakra-ui/layout'; import { Logo } from './Logo'; +import { Link } from './Link'; export function Footer() { const year = new Date().getFullYear(); diff --git a/packages/create-bison-app/template/components/Link.tsx b/packages/create-bison-app/template/components/Link.tsx new file mode 100644 index 00000000..a6f25a09 --- /dev/null +++ b/packages/create-bison-app/template/components/Link.tsx @@ -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 = + | PropsWithChildren> + | PropsWithChildren & 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) => { + return isExternal ? ( + + {children} + + ) : ( + + {children} + + ); +}; + +export const ButtonLink = ({ + href, + as, + replace, + scroll, + shallow, + prefetch, + locale, + children, + ...chakraProps +}: LinkProps) => { + return ( + + + {children} + + + ); +}; + +export const BreadcrumbLink = ({ + href, + as, + replace, + scroll, + shallow, + prefetch, + locale, + children, + ...chakraProps +}: LinkProps) => { + return ( + + {children} + + ); +}; diff --git a/packages/create-bison-app/template/components/LoginForm.tsx b/packages/create-bison-app/template/components/LoginForm.tsx index 7e27de9a..4bdd4f72 100644 --- a/packages/create-bison-app/template/components/LoginForm.tsx +++ b/packages/create-bison-app/template/components/LoginForm.tsx @@ -1,23 +1,13 @@ import React, { useState } from 'react'; import { useRouter } from 'next/router'; -import NextLink from 'next/link'; -import { - Link, - Flex, - Text, - FormControl, - FormLabel, - Input, - Stack, - Button, - Circle, -} from '@chakra-ui/react'; +import { Flex, Text, FormControl, FormLabel, Input, Stack, Button, Circle } from '@chakra-ui/react'; import { useForm } from 'react-hook-form'; import { gql } from '@apollo/client'; import { EMAIL_REGEX } from '../constants'; import { useAuth } from '../context/auth'; import { ErrorText } from '../components/ErrorText'; +import { Link } from './Link'; import { setErrorsFromGraphQLErrors } from '../utils/setErrors'; import { LoginMutationVariables, useLoginMutation } from '../types'; @@ -116,11 +106,9 @@ export function LoginForm() { New User?{' '} - - - Sign Up - - + + Sign Up + diff --git a/packages/create-bison-app/template/components/Logo.tsx b/packages/create-bison-app/template/components/Logo.tsx index 1203a697..277e12e6 100644 --- a/packages/create-bison-app/template/components/Logo.tsx +++ b/packages/create-bison-app/template/components/Logo.tsx @@ -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 ( - - - MyApp - - + + MyApp + ); } diff --git a/packages/create-bison-app/template/components/Nav.tsx b/packages/create-bison-app/template/components/Nav.tsx index 5f6850dc..410e970f 100644 --- a/packages/create-bison-app/template/components/Nav.tsx +++ b/packages/create-bison-app/template/components/Nav.tsx @@ -1,6 +1,5 @@ import React from 'react'; import { - Link, Stack, useBreakpoint, Button, @@ -9,7 +8,8 @@ import { MenuList, MenuItem, } from '@chakra-ui/react'; -import NextLink from 'next/link'; + +import { Link } from './Link'; export function Nav() { const breakpoint = useBreakpoint(); @@ -23,9 +23,7 @@ export function Nav() { - - Link 1 - + Link 1 @@ -45,9 +43,7 @@ export function Nav() { ) : ( - - Link 1 - + Link 1 Link 2 Link 3 diff --git a/packages/create-bison-app/template/components/SignupForm.tsx b/packages/create-bison-app/template/components/SignupForm.tsx index e7e5b0dc..05f95674 100644 --- a/packages/create-bison-app/template/components/SignupForm.tsx +++ b/packages/create-bison-app/template/components/SignupForm.tsx @@ -1,16 +1,5 @@ import React, { useState } from 'react'; -import NextLink from 'next/link'; -import { - Link, - Flex, - Text, - FormControl, - FormLabel, - Input, - Stack, - Button, - Circle, -} from '@chakra-ui/react'; +import { Flex, Text, FormControl, FormLabel, Input, Stack, Button, Circle } from '@chakra-ui/react'; import { gql } from '@apollo/client'; import { useForm } from 'react-hook-form'; import { useRouter } from 'next/router'; @@ -20,6 +9,7 @@ import { setErrorsFromGraphQLErrors } from '../utils/setErrors'; import { SignupMutationVariables, useSignupMutation } from '../types'; import { EMAIL_REGEX } from '../constants'; +import { Link } from './Link'; import { ErrorText } from './ErrorText'; export const SIGNUP_MUTATION = gql` @@ -140,11 +130,9 @@ export function SignupForm() { Have an account?{' '} - - - Sign In - - + + Sign In + diff --git a/packages/create-bison-app/template/layouts/LoggedOut.tsx b/packages/create-bison-app/template/layouts/LoggedOut.tsx index f56a4c46..b57901c8 100644 --- a/packages/create-bison-app/template/layouts/LoggedOut.tsx +++ b/packages/create-bison-app/template/layouts/LoggedOut.tsx @@ -1,7 +1,7 @@ import React from 'react'; -import { Box, Flex, Button } from '@chakra-ui/react'; -import NextLink from 'next/link'; +import { Box, Flex } from '@chakra-ui/react'; +import { ButtonLink } from '../components/Link'; import { Logo } from '../components/Logo'; import { Footer } from '../components/Footer'; @@ -16,11 +16,9 @@ export function LoggedOutLayout({ children }: Props) { - - - + + Login +