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

@moxb/stellar-router-react

v0.4.8

Published

Stellar router: React widgets

Downloads

19

Readme

Stellar Router - React

Stellar Router is a router for web applications. This package provides the React integration. For more info about the design goals and features of this routing system, see the @moxb/stellar-router-core package.

Usage

Initialization

On application boot, you should set up your routingStore instance. Then you should rap your application with the <StellarRouterProvider> component, and pass in the created routing store:

const routingStore = createRoutingStore();

const App = () => (
    <StellarRouterProvider store={routingStore} >
        <MainAppLayout />
    </StellarRouterProvider>
);

Now you can start to add routing- and navigation-related React components to your app, and they will automatically connect to the routing store. To manually access the individual store components from your code, you can use the provided useLocationManager(), useTokenManager() and useLinkGenerator() React hooks.

Defining the state space for UI.

When defining the state space, instead of the generic StateSpace interface, we will use the StateSpaceUI interface, which defines that the every menu labels is a UIFragment and app content is specified by UIFragmentSpec.

export const mainMenu: UIStateSpace = {
    subStates: [
        {
            root: true,
            fragment: <div>Welcome!</div>,
        },
        {
            key: 'one',
            fragment: <div>Part One</div>,
        },
        {
            key: 'two',
            fragment: <div>Part Two</div>,
        },
        {
            key: 'three',
            label: <b>Three</b>,
            fragment: <div>Part Three</div>,
        }
    ],
};

Showing content based on state

The main entry point here the <LocationDependentArea> component. Basically, this is a React component that you can place somewhere on your layout, and it will display content that is appropriate, based on the current state of the application, as described in the state space.

(Naturally, LocationDependentArea needs to be provided with the definition of the state space.)

This component doesn't actually control any navigation events, it just sits there and renders the current content, based on the URL.

Basic example:

import { mainMenu } from "./menu-structure.tsx";

const MainAppLayout = () => (
    <div>
        <h1>My cool app</h1>
        <LocationDependentArea
            id="main-app-layout"
            stateSpace={mainMenu}
        />
    </div>
);

See LocationDependentAreaProps for configuration options.

Another important widget is rootOrDetails.

// TODO

Widgets

  • The most important widget for navigation is the <NavLink>, which can render a link that will trigger navigation changes.

Basic examples:

// Prepare an UrlArg for interacting with the "lang" url query param
const lang = locationManager.defineStringArg("lang", "en");

const links = () => (
    <div>
        <NavLink to={["foo", "bar"]}>
            Go to /foo/bar
        </NavLink>
        <NavLink appendTokens={["fav"]}>
            Append /fav to the current path
        </NavLink>
        <NavLink removeTokenCount={1}>
            Go up one level
        </NavLink>
        <NavLink argChanges={[setArg(lang, "de")]}>
            Switch to German
        </NavLink>
    </div>
);

See CoreLinkProps and AnchorParams about supported parameters.

  • Besides NavLink, there is also BoundNavLink, which is similar to NavLink, except that it takes the information about where to go from a BoundLink, which is a data object probably living in a store, and is passed to the UI widget as the operation prop, in the usual @moxb/moxb fashion.

  • Another way to change the location is to use a Redirect widget: rendering <Redirect to={["foo", "bar"]} /> will navigate the application to /foo/bar.

  • The NavRefRedirect component is responsible for executing redirects based on base64-encoded NavRef links. Just put it into a menu (under the preferred url prefix used for the redirects), and it will handle the rest. I.e. add this to the root state space:

{
    key: 'redirects',
    hidden: true,
    fragment: NavRefRedirect,
},

See also