Skip to content

Commit

Permalink
Add transition example
Browse files Browse the repository at this point in the history
  • Loading branch information
mefechoel committed Jan 18, 2021
1 parent 482a1b9 commit 971009e
Show file tree
Hide file tree
Showing 11 changed files with 334 additions and 26 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/**/node_modules
example/**/public/build
example/**/yarn.lock
example/**/package-lock.json
test/testApp/public/build
.vscode
dist
Expand Down
73 changes: 47 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,22 +45,22 @@ React-esque hooks.
- [Testing](#testing)
- [API](#api)
- [Components](#components)
- [Router](#router)
- [Link](#link)
- [Route](#route)
- [`Router`](#router)
- [`Link`](#link)
- [`Route`](#route)
- [Hooks](#hooks)
- [useNavigate](#usenavigate)
- [useLocation](#uselocation)
- [useResolve](#useresolve)
- [useResolvable](#useresolvable)
- [useMatch](#usematch)
- [useParams](#useparams)
- [useFocus](#usefocus)
- [`useNavigate`](#usenavigate)
- [`useLocation`](#uselocation)
- [`useResolve`](#useresolve)
- [`useResolvable`](#useresolvable)
- [`useMatch`](#usematch)
- [`useParams`](#useparams)
- [`useFocus`](#usefocus)
- [Programmatic Navigation](#programmatic-navigation)
- [navigate](#navigate)
- [`navigate`](#navigate)
- [Actions](#actions)
- [link](#link-1)
- [links](#links)
- [`link`](#link-1)
- [`links`](#links)
- [Custom History](#custom-history)

## Getting started
Expand All @@ -79,13 +79,16 @@ React-esque hooks.
https://github.com/mefechoel/svelte-navigator/tree/master/example/ssr
[example-url-bar]:
https://github.com/mefechoel/svelte-navigator/tree/master/example/url-bar
[example-transitions]:
https://github.com/mefechoel/svelte-navigator/tree/master/example/transitions
[repl-basic-client-side]:
https://svelte.dev/repl/451fd183e0d3403cb7800101f7d799fb
[repl-custom-hash-history]:
https://svelte.dev/repl/195011a49a714e22b1a335037e124458
[repl-private-routes]: https://svelte.dev/repl/c81d8f3dff584065a82b2d3ea7cd4aee
[repl-lazy-loading]: https://svelte.dev/repl/09abb8c287f745169f66f62d51f766d5
[repl-url-bar]: https://svelte.dev/repl/dc82bb89447647edb0d7ed8cbe7999ae
[repl-transitions]: https://svelte.dev/repl/c81d8f3dff584065a82b2d3ea7cd4aee

Look at the [example folder][example-folder-url] for a few example project
setups, or checkout the examples in the Svelte REPL:
Expand All @@ -101,6 +104,8 @@ setups, or checkout the examples in the Svelte REPL:
- SSR ([examples][example-ssr])
- Using hash based navigation with a custom history
([examples][example-custom-hash-history], [REPL][repl-custom-hash-history])
- Smoothly transitioning between route views using svelte transitions
([examples][example-transitions], [REPL][repl-transitions])

## Installation

Expand All @@ -127,6 +132,7 @@ Basic Setup for a client-side SPA:
import Home from "./routes/Home.svelte";
import About from "./routes/About.svelte";
import Blog from "./routes/Blog.svelte";
import Search from "./routes/Search.svelte";
</script>

<Router>
Expand All @@ -146,6 +152,9 @@ Basic Setup for a client-side SPA:
</Route>
<Route path=":id" component={BlogPost} />
</Route>
<Route path="search/:query" let:params>
<Search query={params.query} />
</Route>
</div>
</Router>
```
Expand Down Expand Up @@ -387,25 +396,37 @@ export default renderWithRouter;
</Router>
```

Then import it in your test script:

```js
import MyComponent from "./MyComponent.svelte";
import renderWithRouter from "../test/renderWithRouter";

it("works", () => {
const { getByTestId } = renderWithRouter(MyComponent);
expect(getByTestId("my-input")).toHaveValue("my-value");
});
```

## API

- [Components](#components)
- [Router](#router)
- [Link](#link)
- [Route](#route)
- [`Router`](#router)
- [`Link`](#link)
- [`Route`](#route)
- [Hooks](#hooks)
- [useNavigate](#usenavigate)
- [useLocation](#uselocation)
- [useResolve](#useresolve)
- [useResolvable](#useresolvable)
- [useMatch](#usematch)
- [useParams](#useparams)
- [useFocus](#usefocus)
- [`useNavigate`](#usenavigate)
- [`useLocation`](#uselocation)
- [`useResolve`](#useresolve)
- [`useResolvable`](#useresolvable)
- [`useMatch`](#usematch)
- [`useParams`](#useparams)
- [`useFocus`](#usefocus)
- [Programmatic Navigation](#programmatic-navigation)
- [navigate](#navigate)
- [`navigate`](#navigate)
- [Actions](#actions)
- [link](#link-1)
- [links](#links)
- [`link`](#link-1)
- [`links`](#links)
- [Custom History](#custom-history)

### Components
Expand Down
13 changes: 13 additions & 0 deletions example/transitions/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
## Route Transitions

An example showcasing how to animate the transition between two routes.

### Try it out

```bash
git clone https://github.com/mefechoel/svelte-navigator.git
cd svelte-navigator/example/transitions
npm install # if you're using yarn run 'yarn install'
npm start # 'yarn start'
# open browser at 'http://localhost:6065'
```
20 changes: 20 additions & 0 deletions example/transitions/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"scripts": {
"start": "rollup -c -w",
"serve": "sirv public --single --port 6065"
},
"dependencies": {
"svelte-navigator": "^3.1.5"
},
"devDependencies": {
"@rollup/plugin-commonjs": "^17.0.0",
"@rollup/plugin-node-resolve": "^11.1.0",
"@rollup/plugin-replace": "^2.3.4",
"rollup": "2.36.2",
"rollup-plugin-css-only": "^3.1.0",
"rollup-plugin-livereload": "^2.0.0",
"rollup-plugin-svelte": "^7.0.0",
"sirv-cli": "^1.0.10",
"svelte": "^3.31.2"
}
}
13 changes: 13 additions & 0 deletions example/transitions/public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Svelte Navigator</title>
<link rel="stylesheet" href="/build/bundle.css" />
</head>
<body>
<div id="root"></div>
<script src="/build/main.js"></script>
</body>
</html>
80 changes: 80 additions & 0 deletions example/transitions/rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import svelte from "rollup-plugin-svelte";
import { nodeResolve as resolve } from "@rollup/plugin-node-resolve";
import commonjs from "@rollup/plugin-commonjs";
import livereload from "rollup-plugin-livereload";
import replace from "@rollup/plugin-replace";
import css from "rollup-plugin-css-only";

const production = !process.env.ROLLUP_WATCH;

function serve() {
let server;

function toExit() {
if (server) server.kill(0);
}

return {
writeBundle() {
if (server) return;
// eslint-disable-next-line global-require
server = require("child_process").spawn(
"npm",
["run", "serve", "--", "--dev"],
{
stdio: ["ignore", "inherit", "inherit"],
shell: true,
},
);

process.on("SIGTERM", toExit);
process.on("exit", toExit);
},
};
}

export default {
input: "src/main.js",
output: {
sourcemap: true,
format: "iife",
name: "app",
dir: "public/build",
},
plugins: [
svelte({
compilerOptions: {
// enable run-time checks when not in production
dev: !production,
},
}),
// we'll extract any component CSS out into
// a separate file - better for performance
css({ output: "bundle.css" }),

// If you have external dependencies installed from
// npm, you'll most likely need these plugins. In
// some cases you'll need additional configuration -
// consult the documentation for details:
// https://github.com/rollup/plugins/tree/master/packages/commonjs
resolve({
browser: true,
dedupe: ["svelte"],
}),
commonjs(),
replace({
process: `(${JSON.stringify({ env: { NODE_ENV: "development" } })})`,
}),

// In dev mode, call `yarn serve` once
// the bundle has been generated
!production && serve(),

// Watch the `public` directory and refresh the
// browser on changes when not in production
!production && livereload("public"),
],
watch: {
clearScreen: false,
},
};
93 changes: 93 additions & 0 deletions example/transitions/src/App.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<script>
import { Router, Route, Link } from "svelte-navigator";
import GoodBoi from "./GoodBoi.svelte";
import RouteTransition from "./RouteTransition.svelte";
import TransitionContainer from "./TransitionContainer.svelte";
</script>

<Router>
<div class="page-wrapper">
<div class="page">
<h1>Route Transitions</h1>
<nav>
<Link to="/">Home</Link>
<Link to="about">About</Link>
<Link to="something">Something</Link>
</nav>
<main>
<TransitionContainer>
<Route path="/">
<RouteTransition x={200} duration={900}>
<h2>Home</h2>
<GoodBoi />
<p>
Lorem ipsum dolor sit amet consectetur adipisicing elit. Eveniet
itaque illo harum, fugit fuga non repellat accusamus commodi
deserunt deleniti dignissimos magni iste facilis! Voluptatibus
adipisci provident ipsa aut repellat!
</p>
<p>
Lorem ipsum dolor sit amet consectetur adipisicing elit. Eveniet
itaque illo harum, fugit fuga non repellat accusamus commodi
deserunt deleniti dignissimos magni iste facilis! Voluptatibus
adipisci provident ipsa aut repellat!
</p>
</RouteTransition>
</Route>
<Route path="about">
<RouteTransition x={200} duration={900}>
<h2>About</h2>
<GoodBoi />
<p>
Lorem ipsum dolor sit amet consectetur adipisicing elit. Eveniet
itaque illo harum, fugit fuga non repellat accusamus commodi
deserunt deleniti dignissimos magni iste facilis! Voluptatibus
adipisci provident ipsa aut repellat!
</p>
<p>
Lorem ipsum dolor sit amet consectetur adipisicing elit. Eveniet
itaque illo harum, fugit fuga non repellat accusamus commodi
deserunt deleniti dignissimos magni iste facilis! Voluptatibus
adipisci provident ipsa aut repellat!
</p>
</RouteTransition>
</Route>
<Route path="something">
<RouteTransition x={200} duration={900}>
<h2>Something</h2>
<GoodBoi />
<p>
Lorem ipsum dolor sit amet consectetur adipisicing elit. Eveniet
itaque illo harum, fugit fuga non repellat accusamus commodi
deserunt deleniti dignissimos magni iste facilis! Voluptatibus
adipisci provident ipsa aut repellat!
</p>
<p>
Lorem ipsum dolor sit amet consectetur adipisicing elit. Eveniet
itaque illo harum, fugit fuga non repellat accusamus commodi
deserunt deleniti dignissimos magni iste facilis! Voluptatibus
adipisci provident ipsa aut repellat!
</p>
</RouteTransition>
</Route>
</TransitionContainer>
</main>
</div>
</div>
</Router>

<style>
:global(*) {
font-family: sans-serif;
}

.page-wrapper {
width: 100%;
display: flex;
justify-content: center;
}

.page {
width: 70vw;
}
</style>
25 changes: 25 additions & 0 deletions example/transitions/src/GoodBoi.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<script>
import { onMount } from "svelte";

let src = "";

onMount(() => {
fetch("https://dog.ceo/api/breeds/image/random")
.then(res => res.json())
.then(({ message }) => {
src = message;
});
});
</script>

<img {src} alt="A very good boi" />

<style>
img {
display: block;
width: 600px;
height: 400px;
object-fit: cover;
background: #ddd;
}
</style>
Loading

0 comments on commit 971009e

Please # to comment.