2.1.1 • Published 27 days ago

kabouter v2.1.1

Weekly downloads
-
License
-
Repository
github
Last release
27 days ago

kabouter

A minimalist ~(2.7kb gzipped), batteries included router for react 🍀

Path with variables

Will strictly match all non [variable] path statements

import { useRoute } from 'kabouter'

const Books = () => {
  const route = useRoute('books/[book]/[page]')
  const { book, page } = route.path
  return (
    <div
      onClick={() => {
        // Will result in path "/book/mybook/1"
        route.setPath({
          book,
          page: page + 1,
        })
      }}
    >
      {book} {page}
    </div>
  )
}

Nested paths

Allows composition of different path routes together

import { useRoute } from 'kabouter'

const Page = () => {
  const route = useRoute('[page]')
  return <div>{route.path.page}</div>
}

const Books = () => {
  const route = useRoute('books/[book]')
  const { id } = route.path
  // Creates nested route page will now use "/books/mybook/1"
  return <div>{route.nest(<Page />)}</div>
}

Query parameters

Use query parameters, set a param to null to clear, pass { overwrite: true } as option to overwrite current query params, default is merge

import { useRoute } from 'kabouter'

const Counter = () => {
  const route = useRoute()
  return (
    <div
      onClick={() => {
        // Merges by default
        route.setQuery({
          counter: route.query.counter + 1,
        })
      }}
    >
      {route.query.counter}
    </div>
  )
}

Hash

Listen and modify the location hash

import { useRoute } from 'kabouter'

const Counter = () => {
  const route = useRoute()
  return (
    <div
      onClick={() => {
        route.setHash(route.hash + 1)
      }}
    >
      {route.hash}
    </div>
  )
}

Location

Listens and modify the whole location

import { useRoute } from 'kabouter'

const Counter = () => {
  const route = useRoute()
  return (
    <div onClick={() => route.setLocation('/something')}>{route.location}</div>
  )
}

useSearchParam

Hook to interact with a single search param, set to null to clear

import { useSearchParam } from 'kabouter'

const Counter = () => {
  const [counter, setCounter] = useSearchParam('counter', 0)
  return (
    <div
      onClick={() => {
        setCounter((counter) => counter + 1)
      }}
    >
      {counter}
    </div>
  )
}

SSR

Example how to use kabouter on a server

import React from 'react'
import { renderToPipeableStream } from 'react-dom/server'
import { useRoute, Router } from 'kabouter'

const Books = () => {
  const route = useRoute('books/[book]/[page]')
  const { book, page } = route.path
  return (
    <div
      onClick={() => {
        // Will result in path "/book/mybook/1"
        route.setPath({
          book,
          page: page + 1,
        })
      }}
    >
      {book} {page}
    </div>
  )
}

const serve = (response) => {
  renderToPipeableStream(
    <Router location={{ path: '/books/book1/20' }}>
      <Books />
    </Router>
    {}
  ).pipe(response)
}

Link component

Link element supports all set functionality of useRoute, creates an <a> tag with the parsed url. Attaches to the closest route.nest()

import React from 'react'
import { renderToPipeableStream } from 'react-dom/server'
import { useRoute, Router } from 'kabouter'

const Page = () => {
  return (
    <div>
      <Link path={{ book: 'a book' }}>Go to bla!</Link>
    </div>
  )
}

const Books = () => {
  const route = useRoute('books/[book]')
  const { id } = route.path
  // Creates nested route page will now use "/books/mybook/1"
  return <div>{route.nest(<Page />)}</div>
}
2.0.3

28 days ago

2.1.1

27 days ago

2.0.2

28 days ago

2.0.5

27 days ago

2.0.4

28 days ago

2.1.0

27 days ago

2.0.1

29 days ago

2.0.0

29 days ago

1.4.9

1 month ago

1.4.11

1 month ago

1.4.10

1 month ago

1.4.7

1 month ago

1.4.12

1 month ago

1.4.6

11 months ago

1.4.5

12 months ago

1.4.4

12 months ago

1.4.2

1 year ago

1.4.1

1 year ago

1.4.0

1 year ago

1.3.2

1 year ago

1.3.1

1 year ago

1.3.0

1 year ago

1.2.14

1 year ago

1.2.13

1 year ago

1.2.12

1 year ago

1.2.11

1 year ago

1.2.10

1 year ago

1.2.9

1 year ago

1.2.8

1 year ago

1.2.7

1 year ago

1.2.6

1 year ago

1.2.5

1 year ago

1.2.4

1 year ago

1.2.3

1 year ago

1.2.2

1 year ago

1.2.1

1 year ago

1.2.0

1 year ago

1.1.1

1 year ago

1.1.0

1 year ago

1.0.0

1 year ago