Skip to content

Commit

Permalink
new config action
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnPremKumar committed Mar 23, 2021
1 parent 61b5f1b commit 5a5f7f7
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 12 deletions.
9 changes: 4 additions & 5 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ router(<routes>, <options>);

### As legacy javascript files using script tags. ###
```javascript
// path-to-regexp need to be loaded before steroid-router.min.js
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/index.js"></script>
<script src="https://cdn.jsdelivr.net/npm/steroid-router@latest/dist/steroid-router.min.js"></script>
...
<script>
Expand Down Expand Up @@ -105,7 +103,7 @@ Note: Always first matching route will be used.

Navigation
----------
steroid-router supports both declative and imperative navigation approach
steroid-router supports both declarative and imperative navigation approach

### Declarative navigation
Just add the class `steroid-route` to the anchor tag `<a>` to make it as steroid route.
Expand All @@ -117,18 +115,19 @@ where,

Additonally, optinal navigation parameters can be passed using `data-*` attributes like below:
```html
<a class="steroid-route" href="/course" data-withstack="true" data-withstate='{"id":2}' data-withtitle="course">Course</a>
<a class="steroid-route" href="/course" data-withstack="true" data-withstate='{"id":2}' data-withtitle="course" data-withaction="true">Course</a>
```
### Imperative navigation
Router function returns object with navigation menthod named navigateTo()
```javascript
routeHandler.navigateTo(url, withStack?, state?, title?);
routeHandler.navigateTo(url, withStack?, state?, title?, action?);
```
- `url` - the client side route to navigate to
- `withStack` (optional) - whether the current route route need to be stacked. `default: false`
- `state` (optional) - state object to pass to the route
- `title` (optional) - Title of the route - not widely adopted (Refer: https://github.com/whatwg/html/issues/2174 for more details)
- `action` (optional) - Whether route action should be triggered - useful for post navigation url updates. `default: true`
Server
-------
Expand Down
1 change: 0 additions & 1 deletion docs/demo/200.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
<div id="root" class="container mx-auto d-flex flex-column align-items-center">
<h1>This is index page</h1>
</div>
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/index.js"></script>
<script src="https://cdn.jsdelivr.net/npm/steroid-router@latest/dist/steroid-router.min.js"></script>
<script type="text/javascript" src="/js/index.js"></script>
</body>
Expand Down
1 change: 0 additions & 1 deletion docs/demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
<div id="root" class="container mx-auto d-flex flex-column align-items-center">
<h1>This is index page</h1>
</div>
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/dist/index.js"></script>
<script src="https://cdn.jsdelivr.net/npm/steroid-router@latest/dist/steroid-router.min.js"></script>
<script type="text/javascript" src="/js/index.js"></script>
</body>
Expand Down
30 changes: 30 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"rollup-plugin-commonjs": "^10.1.0",
"rollup-plugin-eslint": "^7.0.0",
"rollup-plugin-exclude-dependencies-from-bundle": "^1.1.14",
"rollup-plugin-external-globals": "^0.6.1",
"rollup-plugin-terser": "^7.0.2"
},
"dependencies": {
Expand Down
2 changes: 0 additions & 2 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ export default [
input,
plugins: [
nodeResolve(),
excludeDependenciesFromBundle(),
commonjs(),
babel({
babelHelpers: "bundled",
}),
Expand Down
10 changes: 7 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@ const router = (routes, options = {}) => {
}
};

const navigateTo = (url, withStack = false, state = {}, title = '') => {
const navigateTo = (url, withStack = false, state = {}, title = '', action = true) => {
if (!withStack) {
window.history.replaceState(state, title, url);
} else {
window.history.pushState(state, title, url);
}
handleRoute();
if (action) {
handleRoute();
}
};

const listener = () => {
Expand All @@ -46,7 +48,9 @@ const router = (routes, options = {}) => {
const withStack = Boolean(target.getAttribute('data-withstack'));
const withState = target.getAttribute('data-withstate');
const withTitle = target.getAttribute('data-withtitle');
navigateTo(path, (currentPath !== path && withStack), JSON.parse(withState), withTitle || '');
let withAction = target.getAttribute('data-withaction');
withAction = withAction ? Boolean(withAction) : true;
navigateTo(path, (currentPath !== path && withStack), JSON.parse(withState), withTitle || '', withAction);
}
});
};
Expand Down

0 comments on commit 5a5f7f7

Please sign in to comment.