Coder Social home page Coder Social logo

ch3ber / yourrouter Goto Github PK

View Code? Open in Web Editor NEW
2.0 1.0 0.0 1.97 MB

Vanilla JavaScript router.

Home Page: https://ch3ber.github.io/yourRouter/

License: MIT License

JavaScript 2.41% HTML 2.99% TypeScript 94.52% Shell 0.08%
router typecript typescript-library vanilla-router npm npm-package npm-publish hacktoberfest client-routing javascript

yourrouter's Introduction

Logo

What is yourrouter? ๐Ÿค”

A powerful router based on client-side routing.

Features ๐Ÿš€

  • Doesn't reload the page
  • Support for dynamic routes
  • Support template rendering
  • Redirection without reloading the page
  • Bundle with 0 dependencies
  • Full typescript support

Working on new features ๐Ÿ› ๏ธ

  • Support for query params
  • Documentation with project examples

Documentation ๐Ÿ“

Quick Setup โฑ๏ธ

Start with the quick configuration of yourrouter. See the full documentation here

Installation

  npm i yourrouter
  pnpm add yourrouter
  yarn add yourrouter

Import yourrouter

Import yourrouter to your index file.

// src/index.js

import Router from "yourrouter";

Set your configuration

Set the configuration of your Router to start creating routes using create() method.

// src/index.js

Router.create({
  path404: "/notFound",
  renderId: "#app",
});

path404 Name of route with 404 HTTP status code.

renderId Optional id where the templates will be rendered. renderId use document.querySelector() to find the id, you can use #app in your html file to select an id or .app to css class.

Execute code in a route

// src/index.js

import Router from "yourrouter";

const config = {
  path404: "/notFound",
};

const router = Router.create(config);

router.addRoute("/", () => {
  console.log("welcome to the main route!");
});

router.addRoute("/foo", () => {
  console.log("welcome to the foo route!");
});

Get the Router instance

You can get the instance of your Router in any file using the get() method.

// src/pages/about/index.js"

const router = Router.get();

Template rendering ๐Ÿญ

Render the content of your route in the HTML. You should return a funtion into addRoute callback, this function can be sync or async and should return the template to render in the document.

Note: To active the template rendering you should set the renderId in your Router config.

// src/index.js

import Router from "yourrouter";

const router = Router.create({
  path404: "/notFound",
  renderId: "#app", // use #app to id and .app to css class
});

router.addRoute("/", () => {
  console.log("Hello home page");

  return () => "<p>Home page!</p>"; // function to that return's the teamplate to render in your page
});

Example with template rendering disable

// src/index.js

import Router from "yourrouter";

const router = Router.create({
  path404: "/notFound",
});

router.addRoute("/", () => {
  console.log("Hello home page");

  // render the content in the #app element
  document.querySelector("#app").innerHTML = "<p>Home page!</p>";
});

Examples list โœ…

Devstore

E-commerce SPA with template rendering, created without javascript frameworks.

Contributing ๐Ÿค

Contribution guideline

Feedback ๐Ÿ“ฃ

If you have any feedback, please reach out to us at @ch3ber_dev on twitter

Authors ๐Ÿ‘ฅ

yourrouter's People

Contributors

ch3ber avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.