Skip to content

Commit

Permalink
The Route component itself supports setting native DOM properties.
Browse files Browse the repository at this point in the history
  • Loading branch information
iuroc committed Jan 9, 2024
1 parent 95db5a2 commit 6f9a731
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 38 deletions.
3 changes: 2 additions & 1 deletion js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ window.addEventListener('hashchange', () => {
activeRoute.val = nowRoute();
});
const Route = (config, ...rest) => {
const { name, onFirst, onLoad, ...otherProp } = config;
let firstLoad = true;
van.derive(() => {
if (activeRoute.val.name == config.name) {
Expand All @@ -22,7 +23,7 @@ const Route = (config, ...rest) => {
config.onLoad(activeRoute.val);
}
});
return div({ hidden: () => config.name != activeRoute.val.name }, rest);
return div({ hidden: () => config.name != activeRoute.val.name, ...otherProp }, rest);
};
const routeTo = (name = 'home', args = []) => {
if (args.length == 0) {
Expand Down
71 changes: 71 additions & 0 deletions js/vanjs-router-1.3.0.nomodule.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
import van from 'vanjs-core';
var div = van.tags.div;
/** 从 `location.hash` 获取当前路由 */
var nowRoute = function () {
var _a;
var li = location.hash.split('/');
var route = { name: (_a = li[1]) !== null && _a !== void 0 ? _a : 'home', args: li.slice(2) };
return route;
};
var activeRoute = van.state(nowRoute());
window.addEventListener('hashchange', function () {
activeRoute.val = nowRoute();
});
var Route = function (config) {
var rest = [];
for (var _i = 1; _i < arguments.length; _i++) {
rest[_i - 1] = arguments[_i];
}
var name = config.name, onFirst = config.onFirst, onLoad = config.onLoad, otherProp = __rest(config, ["name", "onFirst", "onLoad"]);
var firstLoad = true;
van.derive(function () {
if (activeRoute.val.name == config.name) {
if (firstLoad && config.onFirst) {
config.onFirst(activeRoute.val);
firstLoad = false;
}
if (config.onLoad)
config.onLoad(activeRoute.val);
}
});
return div(__assign({ hidden: function () { return config.name != activeRoute.val.name; } }, otherProp), rest);
};
var routeTo = function (name, args) {
if (name === void 0) { name = 'home'; }
if (args === void 0) { args = []; }
if (args.length == 0) {
if (name == 'home') {
location.hash = '';
history.replaceState(null, '', './');
}
else
location.hash = "/".concat(name);
}
else
location.hash = "/".concat(name, "/").concat(args.join('/'));
};
// export { Route, routeTo }
Object.defineProperty(window, 'Route', Route);
Object.defineProperty(window, 'routeTo', routeTo);
68 changes: 34 additions & 34 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"dependencies": {
"vanjs-core": "^1.2.7"
"vanjs-core": "^1.3.0"
},
"name": "vanjs-router",
"version": "1.1.7",
Expand Down
8 changes: 6 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import van, { ChildDom } from 'vanjs-core'
import van, { ChildDom, PropValueOrDerived } from 'vanjs-core'

const { div } = van.tags

Expand All @@ -9,14 +9,17 @@ interface Route {
/** 路由参数 */
readonly args: readonly string[]
}

interface RouteSetting {
/** 路由名称 */
readonly name: string
/** 初次路由载入 */
onFirst?: (route: Route) => void
/** 每次路由载入 */
onLoad?: (route: Route) => void
[key: string]: PropValueOrDerived | undefined
}

/** 从 `location.hash` 获取当前路由 */
const nowRoute = () => {
const li = location.hash.split('/')
Expand All @@ -31,6 +34,7 @@ window.addEventListener('hashchange', () => {
})

const Route = (config: RouteSetting, ...rest: readonly ChildDom[]) => {
const { name, onFirst, onLoad, ...otherProp } = config
let firstLoad = true
van.derive(() => {
if (activeRoute.val.name == config.name) {
Expand All @@ -41,7 +45,7 @@ const Route = (config: RouteSetting, ...rest: readonly ChildDom[]) => {
if (config.onLoad) config.onLoad(activeRoute.val)
}
})
return div({ hidden: () => config.name != activeRoute.val.name }, rest)
return div({ hidden: () => config.name != activeRoute.val.name, ...otherProp }, rest)
}

const routeTo = (name: Route['name'] = 'home', args: any[] = []) => {
Expand Down

0 comments on commit 6f9a731

Please sign in to comment.