Projects

Pantry

Client-side framework combining my libraries Cascade for UI and Crumbs for routing

View / Source
Type

library

Active

2024 - 2026

Status

Done

Tags

Framework, Routing, UI Components, TypeScript

Projects
Pantrylibrary2024 - 2026View / Source

Crumbs

Introducing Pantry (◕‿◕✿)

Pantry is a front-end framework which utilizes two of my libraries. Cascade for user interfaces and Crumbs for client-side routing.

I know what you’re thinking. No, sorry, I’m in a relationship. But here’s one real-world application purely written with Pantry if that helps. Our online community’s Trackmania leaderboard. In case you think it’s slow, every page requests dynamic data, so it takes a split second.


Okay, how?

First, we define an application. This is very similar to how the router is defined, except instead of HTML or a string, we provide Cascade components. To learn about these concepts, please check out the aforementioned project pages.

import { div, h1, p, pre } from '@dolanske/cascade'
import { getRoute } from '@dolanske/crumbs'
import { createApp, Link } from '@dolanske/pantry'

const app = createApp({
  // Provide a route component directly
  '/': div(
    h1('HOME'),
    Link('About us', '/about'),
  ),
  // Provide an object with more options per-route
  '/user/:id': {
    // Before the route is rendered, we can fetch any data and provide it via props
    async loader(params) {
      return fetch(`https://swapi.dev/api/people/${params.id}`)
        .then(r => r.json())
    },
    // Renders when loader throws
    fallback: p('Whoops, something went wrong :/'),
    // The page component
    component: div().setup((ctx, props) => {
      // Every route props object will contain two properties
      // props.$data - if route has loader, the resolved dataset will be here
      // props.$params - dynamic path parameters (eg.: /user/:id)
      ctx.nest(
        pre(JSON.stringify(props.$data, null, 2))
      )

      // If you want access to all route properties besides data and params. You
      // can call this function anywhere after a page has loaded. Please note,
      // it only returns the current state. The object is not reactive
      const route = getRoute()
    }),
  }
})

// Start the app (you need to call this or nothing will happen)
app.run('#app')

The app instance exposes two more methods:

// Will close the app and disable all existing links. The page will stay the way
// it was, but it will no longer be possible to navigate to new pages etc.
app.stop()

// Provide a global fallback component. Route-level fallbacks will be prioritized
app.errorFallback(component)

Now, the app root element will render the route-provided components. And that’s it for Pantry. It just brings together the two coolest libraries ever (both of which I made).