0.2.12 • Published 3 months ago

clink-react v0.2.12

Weekly downloads
-
License
Unlicense
Repository
github
Last release
3 months ago

Clink (Chakra link)

Installation

npm install clink-react

Usage

import { BrowserRouter, Routes, Route } from 'react-router-dom'
import Clink from 'clink-react'

<BrowserRouter>
  <ChakraProvider>
    <Clink to='/' color='blue'>Home</Clink>
    <Clink to='/test'>Test</Clink>
    <Clink to='http://npmjs.com' isExternal>NPM</Clink>

    <Routes>
      <Route path='/' element={<>Home page</>} />
      <Route path='/test' element={<>Test page</>} />
    </Routes>
  </ChakraProvider>
</BrowserRouter>

Problem

Chakra UI's Link component provides styling, but React Router's Link component provides routing functionality. Effectively combining them in a reusable component requires boilerplate in the form of combining prop types or awkward wrapping, as well as aliasing one or both imports.

Without Clink

import { HStack, Link as ChakraLink, LinkProps as ChakraLinkProps } from '@chakra-ui/react'
import { Link as ReactRouterLink, LinkProps as ReactRouterLinkProps } from 'react-router-dom'

function NavbarLink (props: ChakraLinkProps & ReactRouterLinkProps): JSX.Element {
  return <ChakraLink as={ReactLink} color='blue' {...props} />
}

function Navbar () {
  return (
    <HStack>
      <NavbarLink to='http://npmjs.com' isExternal>NPM</NavbarLink>
      <NavbarLink to='/about'>About</NavbarLink>
    </HStack>
  )
}

Solution

Clink collapses both components into one. It takes that takes union of their props types as its props. It can receive any props that either Chakra or React Router's Link can. The type of the props it can receive is exported as ClinkProps.

With Clink

import { HStack } from '@chakra-ui/react'
import Clink, { ClinkProps } from 'clink-react'

function NavbarLink (props: ClinkProps): JSX.Element {
  return <Clink color={blue} {...props} />
}

function Navbar () {
  return (
    <HStack>
      <NavbarLink to='http://npmjs.com' isExternal>NPM</NavbarLink>
      <NavbarLink to='/about'>About</NavbarLink>
    </HStack>
  )
}
0.2.12

3 months ago

0.2.11

3 months ago

0.2.10

3 months ago

0.2.9

3 months ago

0.2.8

3 months ago

0.2.1

3 months ago

0.2.0

3 months ago

0.2.7

3 months ago

0.2.6

3 months ago

0.2.3

3 months ago

0.2.2

3 months ago

0.2.5

3 months ago

0.2.4

3 months ago

0.1.8

7 months ago

0.1.7

1 year ago

0.1.6

1 year ago

0.1.5

1 year ago

0.1.4

2 years ago

0.1.3

2 years ago

0.1.2

2 years ago

0.1.1

2 years ago

0.1.0

2 years ago