{"id":20504304,"url":"https://github.com/stencil-community/stencil-router","last_synced_at":"2025-04-07T14:14:10.725Z","repository":{"id":38297934,"uuid":"97612965","full_name":"stencil-community/stencil-router","owner":"stencil-community","description":"A simple router for Stencil apps and sites","archived":false,"fork":false,"pushed_at":"2024-03-05T15:18:37.000Z","size":1712,"stargazers_count":187,"open_issues_count":55,"forks_count":55,"subscribers_count":21,"default_branch":"master","last_synced_at":"2024-05-02T06:09:36.434Z","etag":null,"topics":["stencil","stencil-apps","stencil-router","stenciljs"],"latest_commit_sha":null,"homepage":"https://stenciljs.com/","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/stencil-community.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-07-18T15:08:56.000Z","updated_at":"2024-06-18T15:26:22.999Z","dependencies_parsed_at":"2024-06-18T15:26:04.358Z","dependency_job_id":"8dc68935-551f-499d-ae0c-e7a521d27cca","html_url":"https://github.com/stencil-community/stencil-router","commit_stats":{"total_commits":299,"total_committers":21,"mean_commits":"14.238095238095237","dds":"0.47157190635451507","last_synced_commit":"73bd1571a9acc646d39162be2ec36b3587fc3112"},"previous_names":["ionic-team/stencil-router"],"tags_count":71,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stencil-community%2Fstencil-router","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stencil-community%2Fstencil-router/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stencil-community%2Fstencil-router/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stencil-community%2Fstencil-router/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/stencil-community","download_url":"https://codeload.github.com/stencil-community/stencil-router/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247234921,"owners_count":20905854,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["stencil","stencil-apps","stencil-router","stenciljs"],"created_at":"2024-11-15T19:37:16.377Z","updated_at":"2025-04-07T14:14:10.707Z","avatar_url":"https://github.com/stencil-community.png","language":"TypeScript","funding_links":[],"categories":["Plugins"],"sub_categories":["Community"],"readme":"\n# @stencil-community/router\n\nStencil Router V2 is an experimental new router for stencil that focus in:\n\n- **Lightweight** (600bytes)\n- **Treeshakable** (not used features are not included in the final build)\n- **Simple**, provide the bare mininum but it make it extendable with hooks.\n- **No DOM**: Router is not render any extra DOM element, to keep styling simple.\n- **Fast**: As fast and lightweight as writing your own router with if statements.\n\n## How does it work?\n\nThis router backs up the `document.location` in a `@stencil/store`, this way we can respond to changes in document.location is a much simpler, way, not more subscribes, no more event listeners events to connect and disconnect.\n\nFunctional Components are the used to collect the list of routes, finally the `Switch` renders only the selected route.\n\n\n## Install\n\n```bash\nnpm install @stencil-community/router --save-dev\n```\n\n## Examples\n\n```tsx\nimport { createRouter, Route } from '@stencil-community/router';\n\nconst Router = createRouter();\n\n@Component({\n  tag: 'app-root',\n})\nexport class AppRoot {\n\n  render() {\n    return (\n      \u003cHost\u003e\n        \u003cRouter.Switch\u003e\n\n          \u003cRoute path=\"/\"\u003e\n            \u003ch1\u003eWelcome\u003c/h1\u003e\n            \u003cp\u003eWelcome to the new stencil-router demo\u003c/p\u003e\n          \u003c/Route\u003e\n\n          \u003cRoute path={/^\\/account/}\u003e\n            \u003capp-account\u003e\u003c/app-account\u003e\n          \u003c/Route\u003e\n\n        \u003c/Router.Switch\u003e\n      \u003c/Host\u003e\n    );\n  }\n}\n```\n\n### Redirects\n```tsx\n\u003cHost\u003e\n  \u003cRouter.Switch\u003e\n\n    \u003cRoute path=\"/\" to=\"/main\"/\u003e\n    \u003cRoute path={/^account/} to=\"/error\"/\u003e\n\n  \u003c/Router.Switch\u003e\n\u003c/Host\u003e\n```\n\n### Params\n\nRoute can take an optional `render` property that will pass down the params. This method should be used instead of JSX children.\n\nRegex or functional matches have the chance to generate an object of params when the URL matches.\n\n\n```tsx\nimport { createRouter, Route, match } from '@stencil-community/router';\n\nconst Router = createRouter();\n\n\u003cHost\u003e\n  \u003cRouter.Switch\u003e\n\n    \u003cRoute\n      path={/^acc(ou)nt/}\n      render={(params) =\u003e (\n        \u003cp\u003e{params[1]}\u003c/p\u003e\n      )}\n    /\u003e\n\n    \u003cRoute\n      path={match('/blog/:page')}\n      render={({page}) =\u003e \u003cblog-post page={page}\u003e}\n    /\u003e\n\n    \u003cRoute\n      path={(url) =\u003e {\n        if (url.includes('hello')) {\n          return {user: 'hello'}\n        }\n        return undefined;\n      }}\n      render={({user}) =\u003e (\n        \u003ch1\u003eUser: {user}\u003c/h1\u003e\n      )}\n    /\u003e\n\n  \u003c/Router.Switch\u003e\n\u003c/Host\u003e\n```\n\nA simple router, inspired by React Router v4, for Stencil apps and vanilla Web Component apps.\n### Links\n\nThe `href()` function will inject all the handles to an native `anchor`, without extra DOM.\n\n```tsx\nimport { createRouter, Route, href } from '@stencil-community/router';\n\nconst Router = createRouter();\n\n\u003cHost\u003e\n  \u003cRouter.Switch\u003e\n\n    \u003cRoute path=\"/main\"\u003e\n      \u003ca {...href('/main')} class=\"my-link\"\u003eGo to blog\u003c/a\u003e\n    \u003c/Route\u003e\n\n    \u003cRoute path=\"/blog\"\u003e\n      \u003ca {...href('/main')}\u003eGo to main\u003c/a\u003e\n    \u003c/Route\u003e\n\n  \u003c/Router.Switch\u003e\n\u003c/Host\u003e\n```\n\n\n### Dynamic routes (guards)\n\n```tsx\n@Component({\n  tag: 'app-root',\n})\nexport class AppRoot {\n\n  @State() logged = false;\n  render() {\n    return (\n      \u003cHost\u003e\n        \u003cRouter.Switch\u003e\n\n          {this.logged \u0026\u0026 (\n            \u003cRoute path=\"/account\"\u003e\n              \u003capp-account\u003e\u003c/app-account\u003e\n            \u003c/Route\u003e\n          )}\n\n          {!this.logged \u0026\u0026 (\n            \u003cRoute path=\"/account\" to=\"/error\"/\u003e\n          )\n\n        \u003c/Router.Switch\u003e\n      \u003c/Host\u003e\n    );\n  }\n}\n```\n\n### Subscriptions to route changes\n\nBecause the router uses `@stencil/store` its trivial to subscribe to changes in the locations, activeRoute, or even the list of routes.\n\n```tsx\nimport { createRouter, Route } from '@stencil-community/router';\n\nconst Router = createRouter();\n\n@Component({\n  tag: 'app-root',\n})\nexport class AppRoot {\n  componentWillLoad() {\n    Router.onChange('url', (newValue: InternalRouterState['url'], _oldValue: InternalRouterState['url']) =\u003e {\n      // Access fields such as pathname, search, etc. from newValue\n\n      // This would be a good place to send a Google Analytics event, for example\n    });\n  }\n\n  render() {\n    const activePath = Router.state.activeRoute?.path;\n\n    return (\n      \u003cHost\u003e\n        \u003caside\u003e\n          \u003ca class={{'active': activePath === '/main'}}\u003eMain\u003c/a\u003e\n          \u003ca class={{'active': activePath === '/account'}}\u003eAccount\u003c/a\u003e\n        \u003c/aside\u003e\n\n        \u003cRouter.Switch\u003e\n\n          \u003cRoute path=\"/main\"\u003e\n            \u003ch1\u003eWelcome\u003c/h1\u003e\n            \u003cp\u003eWelcome to the new stencil-router demo\u003c/p\u003e\n          \u003c/Route\u003e\n\n          \u003cRoute path='/account'\u003e\n            \u003capp-account\u003e\u003c/app-account\u003e\n          \u003c/Route\u003e\n\n        \u003c/Router.Switch\u003e\n      \u003c/Host\u003e\n    );\n  }\n}\n```\nThe routes state includes:\n```tsx\n  url: URL;\n  activeRoute?: RouteEntry;\n  urlParams: { [key: string]: string };\n  routes: RouteEntry[];\n```\n\n[wiki]: https://github.com/stencil-community/stencil-router/wiki\n\n[npm-badge]: https://img.shields.io/npm/v/@stencil-community/router.svg\n[npm-badge-url]: https://www.npmjs.com/package/@stencil-community/router\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstencil-community%2Fstencil-router","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fstencil-community%2Fstencil-router","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstencil-community%2Fstencil-router/lists"}