Git Product home page Git Product logo

gen2tech / wc-hash-router Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 38.04 MB

wc-hash-router is a lightweight and versatile hash-based router library designed specifically for web components. Empower your web applications with seamless navigation and dynamic content loading, all while maintaining clean and modular code with the power of web components.

JavaScript 2.38% TypeScript 97.62%

wc-hash-router's Introduction

wc-hash-router

wc-hash-router is a lightweight and versatile JavaScript library for implementing hash-based routing in web applications. This library is specifically designed for use with web components, providing a seamless and efficient way to handle client-side navigation.

Features

  • Hash-Based Routing: Utilize the URL hash fragment to manage client-side navigation, enabling a smoother user experience without full page reloads.

  • Web Component Integration: Seamlessly integrate the library with your web components, allowing for modular and reusable routing solutions within your application.

  • Simple Configuration: Configure routes easily with a straightforward API, making it quick to set up and adapt to your application's specific needs.

  • Event Handling: Take advantage of event handling mechanisms to respond to route changes, allowing you to update your application's state and UI accordingly.

  • History Management: Manage browser history gracefully, providing users with the expected back and forward navigation experience.

Getting Started

To get started with wc-hash-router, follow these steps:

  1. Install wc-hash-router via npm:
npm install @gen2tech/wc-hash-router

OR

yarn add @gen2tech/wc-hash-router
  1. Import the library into your project:
import CreateWCRouter, { type RouteConfig } from '@gen2tech/wc-hash-router';
  1. Initialize wc-hash-router
  • There are two ways to initialize wc-hash-router
const router = CreateWCRouter('shr-wc-base','runTime') // Manipulate DOM while routing

OR

const router = CreateWCRouter('shr-wc-base','buildTime') // Default more on this
  1. Create routes
const routes: RouteConfig[] = [
    {
        path: '/',
        name: 'Home',
        element: `sample-two`, // Only Needed if on runTime
        render: () => <sample-one /> // Only Needed if on buildTime
    },
    {
        path: '/sample-one/:first/:middle?/:last?',
        name: 'SampleOne',
        element: `sample-one`,
        render: ({ first, middle, last }) => <sample-one first={first} middle={middle} last={last} />
    },
    {
        path: '/with-children',
        name: 'ChildrenRoutes',
        element: `with-children`,
        render: ({prop}) => <with-children prop={prop}/>,
        children: [
            {
                path: 'child-one/:prop1?',
                name: 'ChildrenRoute.One',
                element: `child-one`,
                render: ({prop1}) => <child-one prop-1={prop1} />,
            },
            {
                path: 'child-two/:prop2',
                name: 'ChildrenRoute.Two',
                element: `child-two`,
                render: ({prop2}) => <child-two prop-2={prop2} />,
            },
        ]
    }
]
  1. Add routes to the router
router.addRoutes(routes)
  1. Set middleware for before and after each route
router.beforeEachResolve((router, to, from) => {
    console.log(router, to, from)
    return true
}).afterEachResolve((router, to) => {
    console.log(router, to)
})

For more detailed information and examples, check out the documentation and example folder in this repository.

Contributing We welcome contributions! If you have ideas for improvements, new features, or bug fixes, feel free to open an issue or submit a pull request.

License This project is licensed under the MIT License.

wc-hash-router's People

Contributors

gen2tech avatar gen2work 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.