πŸ“¦ justjavac / gpui-router

A router for GPUI App.

β˜… 53 stars β‘‚ 8 forks πŸ‘ 53 watching βš–οΈ MIT License
πŸ“₯ Clone https://github.com/justjavac/gpui-router.git
HTTPS git clone https://github.com/justjavac/gpui-router.git
SSH git clone git@github.com:justjavac/gpui-router.git
CLI gh repo clone justjavac/gpui-router
θΏ·ζΈ‘ θΏ·ζΈ‘ Update dependencies: bump ashpd to version 0.11.1 and zmij to version 1.0.15 with updated checksums b198462 3 days ago πŸ“ History
πŸ“‚ main View all commits β†’
πŸ“ .cargo
πŸ“ .github
πŸ“ crates
πŸ“„ .gitignore
πŸ“„ .rustfmt.toml
πŸ“„ Cargo.lock
πŸ“„ Cargo.toml
πŸ“„ LICENSE
πŸ“„ README.md
πŸ“„ README.md

gpui-router

ci Crate Crates.io Total Downloads Documentation License

A router for GPUI App, inspired by React-Router.

Features

  • Nested Routes
  • Index Routes
  • Dynamic Segments
  • Wildcard Routes
  • Navigation Links

Usage

use gpui::prelude::*;
use gpui::{App, Application, Context, Window, WindowOptions, div};
use gpui_router::{NavLink, Outlet, Route, Routes, init as router_init};

struct HelloWorld {}

impl Render for HelloWorld {
  fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
    div()
      .child(
        Routes::new().child(
          Route::new().path("/").element(|_, _| layout()).children(vec![
              Route::new().index().element(|_, _| home()),
              Route::new().path("about").element(|_, _| about()),
              Route::new().path("dashboard").element(|_, _| dashboard()),
              Route::new().path("{*not_match}").element(|_, _| not_match()),
            ]),
        ),
      )
  }
}

fn layout() -> impl IntoElement {
  div()
    .child(NavLink::new().to("/").child(div().child("Home")))
    .child(NavLink::new().to("/about").child(div().child("About")))
    .child(NavLink::new().to("/dashboard").child(div().child("Dashboard")))
    .child(NavLink::new().to("/nothing-here").child(div().child("Not Match")))
    .child(Outlet::new())
}

fn home() -> impl IntoElement {
  div().child("Home")
}

fn about() -> impl IntoElement {
  div().child("About")
}

fn dashboard() -> impl IntoElement {
  div().child("Dashboard")
}

fn not_match() -> impl IntoElement {
  div()
    .child(div().child("Nothing to see here!"))
    .child(NavLink::new().to("/").child(div().child("Go to the home page")))
}

fn main() {
  Application::new().run(|cx: &mut App| {
    router_init(cx);
    cx.activate(true);
    cx.open_window(WindowOptions::default(), |_, cx| cx.new(|_cx| HelloWorld {}))
      .unwrap();
  });
}

Note: The element() method now accepts a closure that returns an IntoElement. This allows for lazy evaluation of route elements - they are only rendered when the route matches, improving performance when you have many routes.

Examples

See the examples folder for more usage examples.

License

This project is licensed under the MIT License. See LICENSE for details.