All Projects → ayndqy → svelte-micro

ayndqy / svelte-micro

Licence: MIT license
Light & reactive one-component router for Svelte

Programming Languages

typescript
32286 projects
Svelte
593 projects

Projects that are alternatives of or similar to svelte-micro

svelte-router
Router component for Svelte
Stars: ✭ 63 (-22.22%)
Mutual labels:  router, svelte
Svelte Router
Svelte Router adds routing to your Svelte apps. It's designed for Single Page Applications (SPA). Includes localisation, guards and nested layouts.
Stars: ✭ 310 (+282.72%)
Mutual labels:  router, svelte
Curi
A JavaScript router for single-page applications
Stars: ✭ 262 (+223.46%)
Mutual labels:  router, svelte
svelte-starter-kit
Svelte starter kit — router, state management and testing included.
Stars: ✭ 16 (-80.25%)
Mutual labels:  router, svelte
MVVM-Sample
Swift MVVM Sample project. Made with ReactiveCocoa, Swinject and Routers
Stars: ✭ 21 (-74.07%)
Mutual labels:  reactive, router
Abstract State Router
Like ui-router, but without all the Angular. The best way to structure a single-page webapp.
Stars: ✭ 288 (+255.56%)
Mutual labels:  router, svelte
Crayon
Simple framework agnostic UI router for SPAs
Stars: ✭ 310 (+282.72%)
Mutual labels:  router, svelte
Svelte Router
Simple Svelte Router for Single Page Applications (SPA).
Stars: ✭ 44 (-45.68%)
Mutual labels:  router, svelte
Yrv
Your routing! (for Svelte)
Stars: ✭ 143 (+76.54%)
Mutual labels:  router, svelte
Svelte Store Router
Store-based router for Svelte
Stars: ✭ 54 (-33.33%)
Mutual labels:  router, svelte
Ngqp
Declaratively synchronize form controls with the URL
Stars: ✭ 65 (-19.75%)
Mutual labels:  reactive, router
react-mobx-router5
React components for routing solution using router5 and mobx
Stars: ✭ 58 (-28.4%)
Mutual labels:  reactive, router
Svelte Css Vars
✨ Ever wanted to have reactive css variables in svelte? What if I told you there's a way?
Stars: ✭ 150 (+85.19%)
Mutual labels:  reactive, svelte
KotlinReactiveMS
An educational project to learn reactive programming with Spring 5 and Kotlin
Stars: ✭ 33 (-59.26%)
Mutual labels:  reactive
sirix-svelte-frontend
A GUI console for SirixDB, using Svelte/Sapper.
Stars: ✭ 23 (-71.6%)
Mutual labels:  svelte
Translucide
CMS efficient, léger, simple à prendre en main, customisable et écoconçu !
Stars: ✭ 31 (-61.73%)
Mutual labels:  light
nodekit
[Moved to Codeberg] A Small Web server.
Stars: ✭ 68 (-16.05%)
Mutual labels:  svelte
webstone
Start your next full-stack application with Webstone and configure it as you go.
Stars: ✭ 71 (-12.35%)
Mutual labels:  svelte
awesome-solid-js
Curated resources on building sites with SolidJS, a brand new way(now 1.0) to build Javascript based interactive web applications. A very close looking cousin to React/JSX by syntax, and to Svelte by few important principles(compiler and fine-grained reactivity), it's a highly optimised way to deliver web applications with best-in-class performa…
Stars: ✭ 317 (+291.36%)
Mutual labels:  reactive
cakcuk
A Command Bot Interface builder, CLI-based to easily create your CLI commands for your Workspace
Stars: ✭ 13 (-83.95%)
Mutual labels:  svelte

Svelte Micro

Light & reactive one-component router for Svelte

Table of content

Installation

$ npm i svelte-micro

Example

<script>
  import { Route } from "svelte-micro";
</script>

<Route>
  <!-- Always will be shown -->
  <nav>
    <a href="/">Home</a>
    <a href="/portfolio">Portfolio</a>
    <a href="/about-us/story">Our story</a>
    <!-- External link -->
    <a href="https://github.com/ayndqy/svelte-micro" external-href>Github</a>
  </nav>

  <!-- Will be shown only when the page path is equal to the '/' -->
  <Route path="/">
    <h1>Home page</h1>
    <p>Feel at home!</p>
  </Route>

  <Route path="/portfolio">
    <h1>Portfolio</h1>

    <Route path="/">
      <h2>Portfolio main page</h2>
      <a href="/portfolio/sites">Sites</a>
      <a href="/portfolio/apps">Apps</a>
    </Route>

    <Route path="/sites">
      <h2>Sites</h2>
      <a href="/portfolio">Back to portfolio main page</a>
    </Route>

    <Route path="/apps">
      <h2>Apps</h2>
      <a href="/portfolio">Back to portfolio main page</a>
    </Route>

    <Route fallback>
      <h2>Route not found in portfolio :(</h2>
      <a href="/portfolio">Back to portfolio main page</a>
    </Route>
  </Route>

  <Route path="/about-us/story">
    <h1>Our story</h1>
  </Route>

  <Route fallback>
    <h1>Route not found :(</h1>
    <a href="/">Back to home</a>
  </Route>
</Route>

This code shows the capabilities of svelte-micro

Links

<a
  href="/"
  external-href={false}
/>

All links that begin with 'https://', 'http://', '//', mailto:, tel: will be ignored by svelte-micro

If you want svelte-micro to ignore all links use linkHandle option

Component

<script>
  import { Route } from 'svelte-micro';
</script>

<!-- Default props value -->
<Route
  fallback={false}
  path="/"
/>

All <Route /> props are reactive

Stores

<script>
  import { path, query, hash } from 'svelte-micro';
</script>

Current path is {$path}
Current query is {$query}
Current hash is {$hash}
  • $path

  • $query

  • $hash

Methods

import { router, pathToArray } from 'svelte-micro'
  • router.push(href = '/')
    Push new url to history

  • router.replace(href = '/')
    Replace current url in history

  • router.setOptions(changedOptions = {})
    Set options for router

  • pathToArray(path)
    For example: '/about-us/story' will be ['/about-us', '/story']

Options

import { router } from 'svelte-micro'

// Default values
router.setOptions({
  linkHandle: true,
})

Tips

Scroll behavior control

import { path } from 'svelte-micro'

// Disable browser scroll behavior control
if ('scrollRestoration' in history) {
  history.scrollRestoration = 'manual'
}

// On path change reset scroll position
path.subscribe(() => window.scrollTo(0, 0))

By default svelte-micro doesn't control scroll behavior, but it's easy to do on your own

Stores usage

<script>
  import { path, query, hash } from 'svelte-micro';

  // For example: current location equals to '/somepath?text=Hello#modal'
  // $path = '/somepath'
  // $query = '?text=Hello'
  // $hash = '#modal'

  // If you want to get an object with data from the query
  $: queryData = Object.fromEntries(new URLSearchParams($query).entries());
</script>

<!-- Query usage example -->
{queryData?.text}
<!-- Result: Hello -->

<!-- Hash usage example -->
{#if $hash === '#modal'}
  <div class="modal">
    Hello from modal!
  </div>
{/if}

Guarded route

<script>
  import { Route } from 'svelte-micro';

  let isUserAuthenticated = true;
</script>

<Route>
  {#if isUserAuthenticated}

    <Route path="/profile">
      <h1>Welcome!</h1>
      <button on:click={() => (isUserAuthenticated = false)}>Log out</button>
    </Route>

  {:else}

    <Route path="/profile">
      <h1>You are not authenticated :\</h1>
      <button on:click={() => (isUserAuthenticated = true)}>Log in</button>
    </Route>

  {/if}
</Route>

Active link

<script>
  import { path } from 'svelte-micro';

  let href = '/home';
</script>

<a {href} class:active={href === $path}>Home</a>

<style>
  a.active {
    color: red;
  }
</style>

Transitions

<script>
  import { Route } from 'svelte-micro';
  import { fade } from 'svelte/transition';
</script>

<Route>
  <nav>
    <a href="/">Home</a>
    <a href="/about">About</a>
  </nav>

  <Route path="/">
    <div transition:fade>
      <h1>Home page</h1>
    </div>
  </Route>

  <Route path="/about">
    <div transition:fade>
      <h1>About page</h1>
    </div>
  </Route>
</Route>
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].