@wsxjs/wsx-router 0.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +245 -0
- package/dist/index.cjs +1 -0
- package/dist/index.js +409 -0
- package/package.json +63 -0
- package/src/RouterUtils.ts +260 -0
- package/src/WsxLink.css +74 -0
- package/src/WsxLink.wsx +162 -0
- package/src/WsxRouter.css +11 -0
- package/src/WsxRouter.wsx +180 -0
- package/src/WsxView.css +51 -0
- package/src/WsxView.wsx +102 -0
- package/src/index.ts +18 -0
package/src/WsxView.css
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/* WSX View 样式 */
|
|
2
|
+
:host {
|
|
3
|
+
display: block;
|
|
4
|
+
width: 100%;
|
|
5
|
+
height: 100%;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
.route-view {
|
|
9
|
+
width: 100%;
|
|
10
|
+
height: 100%;
|
|
11
|
+
position: relative;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/* 加载状态 */
|
|
15
|
+
:host([loading]) .route-view {
|
|
16
|
+
opacity: 0.5;
|
|
17
|
+
transition: opacity 0.3s ease;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/* 错误状态 */
|
|
21
|
+
:host([error]) .route-view {
|
|
22
|
+
border: 1px solid var(--error-color, #ff0000);
|
|
23
|
+
padding: 1rem;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/* 动画支持预留 */
|
|
27
|
+
.route-view.entering {
|
|
28
|
+
animation: fadeIn 0.3s ease-out;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
.route-view.leaving {
|
|
32
|
+
animation: fadeOut 0.3s ease-in;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
@keyframes fadeIn {
|
|
36
|
+
from {
|
|
37
|
+
opacity: 0;
|
|
38
|
+
}
|
|
39
|
+
to {
|
|
40
|
+
opacity: 1;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
@keyframes fadeOut {
|
|
45
|
+
from {
|
|
46
|
+
opacity: 1;
|
|
47
|
+
}
|
|
48
|
+
to {
|
|
49
|
+
opacity: 0;
|
|
50
|
+
}
|
|
51
|
+
}
|
package/src/WsxView.wsx
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
/** @jsxImportSource @wsxjs/wsx-core */
|
|
2
|
+
import { WebComponent, autoRegister, createLogger } from "@wsxjs/wsx-core";
|
|
3
|
+
import styles from "./WsxView.css?inline";
|
|
4
|
+
|
|
5
|
+
const logger = createLogger("WsxView");
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* 路由视图组件 - 条件渲染的容器
|
|
9
|
+
*
|
|
10
|
+
* 属性:
|
|
11
|
+
* - route: 路由路径(支持参数)
|
|
12
|
+
* - component: 要渲染的组件名称
|
|
13
|
+
* - params: 路由参数(自动注入)
|
|
14
|
+
*
|
|
15
|
+
* 使用示例:
|
|
16
|
+
* ```html
|
|
17
|
+
* <wsx-view route="/users/:id" component="user-detail"></wsx-view>
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
@autoRegister({ tagName: "wsx-view" })
|
|
21
|
+
export default class WsxView extends WebComponent {
|
|
22
|
+
static observedAttributes = ["route", "component", "params"];
|
|
23
|
+
|
|
24
|
+
private component: string | null = null;
|
|
25
|
+
private params: Record<string, string> = {};
|
|
26
|
+
private componentInstance: HTMLElement | null = null;
|
|
27
|
+
|
|
28
|
+
constructor() {
|
|
29
|
+
super({
|
|
30
|
+
styles,
|
|
31
|
+
styleName: "wsx-view",
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
render() {
|
|
36
|
+
return <div class="route-view" part="view"></div>;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
protected onConnected() {
|
|
40
|
+
// Check if component attribute is already set and load it
|
|
41
|
+
const componentName = this.getAttribute("component");
|
|
42
|
+
if (componentName && !this.componentInstance) {
|
|
43
|
+
this.loadComponent(componentName);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
protected onAttributeChanged(name: string, _oldValue: string, newValue: string) {
|
|
48
|
+
if (name === "component" && newValue && !this.componentInstance) {
|
|
49
|
+
this.loadComponent(newValue);
|
|
50
|
+
} else if (name === "params" && this.componentInstance) {
|
|
51
|
+
// 更新组件参数
|
|
52
|
+
try {
|
|
53
|
+
this.params = JSON.parse(newValue);
|
|
54
|
+
Object.entries(this.params).forEach(([key, value]) => {
|
|
55
|
+
this.componentInstance!.setAttribute(key, value);
|
|
56
|
+
});
|
|
57
|
+
} catch (e) {
|
|
58
|
+
logger.error("Failed to parse params:", e);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
private async loadComponent(componentName: string) {
|
|
64
|
+
// 清理旧组件
|
|
65
|
+
if (this.componentInstance) {
|
|
66
|
+
this.componentInstance.remove();
|
|
67
|
+
this.componentInstance = null;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// 检查组件是否已注册
|
|
71
|
+
const elementClass = customElements.get(componentName);
|
|
72
|
+
|
|
73
|
+
if (!elementClass) {
|
|
74
|
+
logger.warn(`Component ${componentName} not found in customElements registry`);
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
this.componentInstance = document.createElement(componentName);
|
|
79
|
+
|
|
80
|
+
// 传递初始参数
|
|
81
|
+
if (Object.keys(this.params).length > 0) {
|
|
82
|
+
Object.entries(this.params).forEach(([key, value]) => {
|
|
83
|
+
this.componentInstance!.setAttribute(key, value);
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
const container = this.shadowRoot?.querySelector(".route-view");
|
|
88
|
+
if (container) {
|
|
89
|
+
container.appendChild(this.componentInstance);
|
|
90
|
+
} else {
|
|
91
|
+
logger.error("Route view container not found");
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
protected onDisconnected() {
|
|
96
|
+
// 清理组件实例
|
|
97
|
+
if (this.componentInstance) {
|
|
98
|
+
this.componentInstance.remove();
|
|
99
|
+
this.componentInstance = null;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WSX Router - Native History API-based routing for WSX Framework
|
|
3
|
+
*
|
|
4
|
+
* 提供基于原生 History API 的路由功能:
|
|
5
|
+
* - 零依赖路由器组件
|
|
6
|
+
* - 声明式路由配置
|
|
7
|
+
* - 支持参数路由和嵌套路由
|
|
8
|
+
* - 内置导航链接组件
|
|
9
|
+
* - 丰富的路由工具函数
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
// 导出路由组件 (using default imports to align with base-components)
|
|
13
|
+
export { default as WsxRouter } from "./WsxRouter.wsx";
|
|
14
|
+
export { default as WsxView } from "./WsxView.wsx";
|
|
15
|
+
export { default as WsxLink } from "./WsxLink.wsx";
|
|
16
|
+
|
|
17
|
+
// 导出工具类和类型
|
|
18
|
+
export * from "./RouterUtils";
|