forked from marimo-team/marimo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: mo.routes() (marimo-team#1356)
* feat: mo.routes() * lint * lazy/catch-all improvements * fixes * lint * fix test
- Loading branch information
Showing
21 changed files
with
574 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,3 +18,5 @@ MD001: false | |
MD040: false | ||
# ol-prefix | ||
MD029: false | ||
# no-trailing-punctuation | ||
MD026: false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# Routes | ||
|
||
```{eval-rst} | ||
.. marimo-embed:: | ||
:size: medium | ||
@app.cell | ||
def __(): | ||
mo.sidebar( | ||
[ | ||
mo.md("# marimo"), | ||
mo.nav_menu( | ||
{ | ||
"#/": f"{mo.icon('lucide:home')} Home", | ||
"#/about": f"{mo.icon('lucide:user')} About", | ||
"#/contact": f"{mo.icon('lucide:phone')} Contact", | ||
"Links": { | ||
"https://twitter.com/marimo_io": "Twitter", | ||
"https://github.com/marimo-team/marimo": "GitHub", | ||
}, | ||
}, | ||
orientation="vertical", | ||
), | ||
] | ||
) | ||
return | ||
@app.cell | ||
def __(): | ||
mo.routes({ | ||
"#/": mo.md("# Home"), | ||
"#/about": mo.md("# About"), | ||
"#/contact": mo.md("# Contact"), | ||
mo.routes.CATCH_ALL: mo.md("# Home"), | ||
}) | ||
return | ||
``` | ||
|
||
```{eval-rst} | ||
.. autofunction:: marimo.routes | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,7 +8,6 @@ Marimo Cloud is currently in beta: if you'd like access, email us at | |
[[email protected]](mailto:[email protected]) or book time on [our | ||
calendar](calendly.com/akshay-marimo) and we'll onboard you very quickly. | ||
|
||
|
||
## Marimo Cloud | ||
|
||
Marimo Cloud seamlessly augments local development on marimo notebooks with | ||
|
@@ -23,7 +22,7 @@ Today, marimo cloud has two main features: | |
|
||
We have many more features planned for the near future, and **big ideas** on | ||
how Marimo Cloud can supercharge the entire lifecycle of working with and | ||
experimenting on data, from small workloads to very large ones as well. | ||
experimenting on data, from small workloads to very large ones as well. | ||
|
||
## Reach out to us! | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* Copyright 2024 Marimo. All rights reserved. */ | ||
import React, { PropsWithChildren, useEffect, useMemo, useState } from "react"; | ||
|
||
import { z } from "zod"; | ||
import { IStatelessPlugin, IStatelessPluginProps } from "../stateless-plugin"; | ||
import { TinyRouter } from "@/utils/routes"; | ||
import useEvent from "react-use-event-hook"; | ||
|
||
interface Data { | ||
/** | ||
* Route paths to render. | ||
*/ | ||
routes: string[]; | ||
} | ||
|
||
export class RoutesPlugin implements IStatelessPlugin<Data> { | ||
tagName = "marimo-routes"; | ||
|
||
validator = z.object({ | ||
routes: z.array(z.string()), | ||
}); | ||
|
||
render(props: IStatelessPluginProps<Data>): JSX.Element { | ||
return <RoutesComponent {...props.data}>{props.children}</RoutesComponent>; | ||
} | ||
} | ||
|
||
const RoutesComponent = ({ | ||
routes, | ||
children, | ||
}: PropsWithChildren<Data>): JSX.Element => { | ||
const childCount = React.Children.count(children); | ||
if (childCount !== routes.length) { | ||
throw new Error( | ||
`Expected ${routes.length} children, but got ${childCount}`, | ||
); | ||
} | ||
|
||
const router = useMemo(() => new TinyRouter(routes), [routes]); | ||
const [matched, setMatched] = useState<string | null>(() => { | ||
const match = router.match(window.location); | ||
return match ? match[1] : null; | ||
}); | ||
|
||
const handleFindMatch = useEvent((location: Location) => { | ||
const match = router.match(location); | ||
setMatched(match ? match[1] : null); | ||
}); | ||
|
||
useEffect(() => { | ||
// Listen for route changes | ||
const listener = (e: PopStateEvent | HashChangeEvent) => { | ||
handleFindMatch(window.location); | ||
}; | ||
window.addEventListener("hashchange", listener); | ||
window.addEventListener("popstate", listener); | ||
return () => { | ||
window.removeEventListener("hashchange", listener); | ||
window.removeEventListener("popstate", listener); | ||
}; | ||
}, [handleFindMatch]); | ||
|
||
if (!matched) { | ||
// eslint-disable-next-line react/jsx-no-useless-fragment | ||
return <></>; | ||
} | ||
|
||
const matchedIndex = routes.indexOf(matched); | ||
const child = React.Children.toArray(children)[matchedIndex]; | ||
|
||
// eslint-disable-next-line react/jsx-no-useless-fragment | ||
return <>{child}</>; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* Copyright 2024 Marimo. All rights reserved. */ | ||
import { describe, it, expect } from "vitest"; | ||
import { TinyRouter } from "../routes"; | ||
|
||
describe("TinyRouter.match", () => { | ||
it("should return the correct match and template for a given location", () => { | ||
const templates = ["/path1", "/path2"]; | ||
const router = new TinyRouter(templates); | ||
|
||
const location1 = { hash: "", pathname: "/path1" } as Location; | ||
const location2 = { hash: "", pathname: "/path2" } as Location; | ||
|
||
expect(router.match(location1)).toEqual([expect.any(Object), "/path1"]); | ||
expect(router.match(location2)).toEqual([expect.any(Object), "/path2"]); | ||
}); | ||
|
||
it("should return false if no match is found", () => { | ||
const templates = ["/path1", "/path2"]; | ||
const router = new TinyRouter(templates); | ||
|
||
const location = { hash: "", pathname: "/path3" } as Location; | ||
|
||
expect(router.match(location)).toBe(false); | ||
}); | ||
|
||
it("should return the correct match for hash locations", () => { | ||
const templates = ["#/path1", "#/path2"]; | ||
const router = new TinyRouter(templates); | ||
|
||
const location = { hash: "#/path1", pathname: "" } as Location; | ||
|
||
expect(router.match(location)).toEqual([expect.any(Object), "#/path1"]); | ||
}); | ||
|
||
it("order should matter for nested routes", () => { | ||
const templates = ["/path1", "/path1/nested"]; | ||
const router = new TinyRouter(templates); | ||
|
||
const location = { hash: "", pathname: "/path1/nested" } as Location; | ||
|
||
expect(router.match(location)).toEqual([ | ||
expect.any(Object), | ||
"/path1/nested", | ||
]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* Copyright 2024 Marimo. All rights reserved. */ | ||
|
||
import { match, Match, MatchFunction } from "path-to-regexp"; | ||
|
||
export class TinyRouter { | ||
private routes: Array<{ | ||
template: string; | ||
pathFunction: MatchFunction; | ||
}>; | ||
|
||
constructor(templates: string[]) { | ||
this.routes = templates.map((template) => { | ||
return { | ||
template, | ||
pathFunction: match(template), | ||
}; | ||
}); | ||
} | ||
|
||
match(location: Location): [Match, template: string] | false { | ||
for (const { pathFunction, template } of this.routes) { | ||
const match = | ||
pathFunction(location.hash) || pathFunction(location.pathname); | ||
if (match) { | ||
return [match, template]; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.