npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

simple-marko-router

v1.1.1

Published

A small and simple router for MarkoJS.

Downloads

12

Readme

simple-marko-router

This router was made for use with MarkoJS. It is lightweight and simple, with Marko as the only production dependency.

Currently tested using @marko/serve.

Demo: https://paradxil.github.io/simple-marko-router/

Getting started

npm i simple-marko-router

Add the router component to your app:

router default-route='/'
    @route path='/'
        home
    @route path='/login'
        login

Use the router-link component for navigation:

router-link path='/login'
    a -- Go to login

On page load the router will automatically navigate to the correct page based on the url. The default-route will be shown when no other routes match.

Optionally a base-route attribute can be defined that will be pre-pended to all paths.

router default-route='/' base-route='/app'
    @route path='/'
        home

Component attribute

Instead of nesting tags you want to display under @route, you can assign the component attribute an imported marko tag. This allows you to provide a single component that will be shown for that route. Route params for these components can be accessed using input.params.

You can use different styles for different routes. However defining the component attribute will prevent that route from rendering its body content.

import HomePage from '<home>';

router default-route='/home'
    @route path='/home' component=HomePage
    @route path='/about'
        h1 -- About Me
        p -- more content

Route params

simple-marko-router supports route parameters such as /project/:id or /:userid/view/:projectid.

Parameter values are passed back to the nested tag @route. For more information on Marko tag parameters see https://markojs.com/docs/syntax/#parameters.

router default-route='/project/this-is-the-project-id'
    @route|{params}| path='/project/:id'
        $console.log(params.id)
//Prints out 'this-is-the-project-id'

Components rendered using the component attribute will be passed the route params as part of their input: input.params.

/// App.marko
import Project from './Project.marko'

router default-route='/project/this-is-the-project-id'
    @route path='/project/:id' component=Project


/// Project.marko
p -- ${input.params.id}

Navigation

If using router-link does not meet your needs programmatic navigation is also possible. This is useful for redirection after an async process finishes.

Simply import and call navigate with a path string.

import {navigate} from 'simple-marko-router';

class {
    async handleClick() {
        await fetchUserData();
        navigate('/dashboard');
    }
}

a on-click('handleClick') -- Login

Known bugs/issues

Due to either a limitation with MarkoJs or my implementation, stateful content in a route will not always update correctly when the state is the parent of the router.

For example this will not work, neither the text input nor the p tag will update correctly on input.

class {
    onCreate() {
        this.state = {
            inputText: null
        };
    }

    setInput(event) {
        this.state.inputText = event.target.value;
    }
}

router default-route='/'
    @route path='/'
        input value=state.inputText on-input('setInput')
        p -- You input: ${state.inputText}

Instead break this into two components, and nest the state under the route.

/// app.marko
router default-route='/'
    @route path='/'
        inputdemo

/// inputdemo.marko
class {
    onCreate() {
        this.state = {
            inputText: null
        };
    }

    setInput(event) {
        this.state.inputText = event.target.value;
    }
}

input value=state.inputText on-input('setInput')
p -- You input: ${state.inputText}

Limitations & Road Map

The following is a list of planned features (and current limitations).

  • No method for passing initial input to components shown using the component attribute.
  • Need a way to pass input using navigate and router-link.