UNPKG

6.34 kBJavaScriptView Raw
1var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
2 var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
3 if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
4 else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
5 return c > 3 && r && Object.defineProperty(target, key, r), r;
6};
7var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
8 if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
9 if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
10 return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
11};
12var _DuoyunSideNavigationElement_onClickChildren, _DuoyunSideNavigationElement_checkGroupStatus, _DuoyunSideNavigationElement_renderItem;
13import { connectStore, adoptedStyle, customElement, property } from '@mantou/gem/lib/decorators';
14import { html } from '@mantou/gem/lib/element';
15import { history } from '@mantou/gem/lib/history';
16import { createCSSSheet, css } from '@mantou/gem/lib/utils';
17import { theme } from '../lib/theme';
18import { commonHandle } from '../lib/hotkeys';
19import { focusStyle } from '../lib/styles';
20import { createPath, matchPath } from './route';
21import { DuoyunScrollBaseElement } from './base/scroll';
22import './link';
23const style = createCSSSheet(css `
24 :host(:where(:not([hidden]))) {
25 user-select: none;
26 display: flex;
27 }
28 :host,
29 .group-body,
30 .children {
31 flex-direction: column;
32 gap: 3px;
33 }
34 .group-body,
35 .children {
36 display: flex;
37 }
38 .item {
39 display: flex;
40 justify-content: space-between;
41 position: relative;
42 padding: 0.5em 0.75em;
43 font-size: 0.875em;
44 border-radius: ${theme.normalRound};
45 color: unset;
46 text-decoration: none;
47 line-height: 1.2;
48 }
49 .children .item {
50 padding-inline-start: calc(0.75em + 1em);
51 }
52 .item:where(:hover, [data-active], :state(active)) {
53 background-color: ${theme.hoverBackgroundColor};
54 }
55 .group {
56 margin-block: 1.2em;
57 }
58 .group + .group {
59 margin-top: 0;
60 }
61 .group-title {
62 cursor: default;
63 padding: 0.945em;
64 text-transform: uppercase;
65 font-size: 0.7em;
66 color: ${theme.describeColor};
67 }
68`);
69/**
70 * @customElement dy-side-navigation
71 */
72let DuoyunSideNavigationElement = class DuoyunSideNavigationElement extends DuoyunScrollBaseElement {
73 constructor() {
74 super({ delegatesFocus: true });
75 this.items = [];
76 // children open state
77 this.state = {};
78 _DuoyunSideNavigationElement_onClickChildren.set(this, (title) => {
79 this.setState({ [title]: !this.state[title] });
80 });
81 _DuoyunSideNavigationElement_checkGroupStatus.set(this, () => {
82 var _a;
83 const { path } = history.getParams();
84 const matchChildren = (e) => {
85 var _a;
86 if ('group' in e) {
87 e.group.forEach(matchChildren);
88 return;
89 }
90 const has = (_a = e.children) === null || _a === void 0 ? void 0 : _a.some((e) => e.pattern ? matchPath(e.pattern, path) : e.children ? e.children.some(matchChildren) : false);
91 if (has && e.title) {
92 this.setState({ [e.title]: true });
93 return true;
94 }
95 };
96 (_a = this.items) === null || _a === void 0 ? void 0 : _a.forEach(matchChildren);
97 });
98 _DuoyunSideNavigationElement_renderItem.set(this, ({ pattern, title = '<No Title>', slot, params, query, hash, children }) => {
99 return html `
100 <dy-active-link
101 class="item"
102 tabindex="0"
103 @keydown=${commonHandle}
104 @click=${children && (() => __classPrivateFieldGet(this, _DuoyunSideNavigationElement_onClickChildren, "f").call(this, title))}
105 doc-title=${title}
106 path=${pattern ? createPath({ pattern }, { params }) : ''}
107 query=${query ? query.toString() : ''}
108 hash=${hash || ''}
109 pattern=${!pattern ? '' : pattern.endsWith('*') ? pattern : `${pattern}/*`}
110 >
111 <span>${title}</span>${slot}
112 </dy-active-link>
113 ${children && this.state[title] ? html `<div class="children">${children.map(__classPrivateFieldGet(this, _DuoyunSideNavigationElement_renderItem, "f"))}</div>` : ''}
114 `;
115 });
116 this.willMount = () => {
117 this.memo(() => __classPrivateFieldGet(this, _DuoyunSideNavigationElement_checkGroupStatus, "f").call(this), () => [history.getParams().path]);
118 };
119 this.render = () => {
120 var _a;
121 if (!((_a = this.items) === null || _a === void 0 ? void 0 : _a.length))
122 return html ``;
123 return html `${this.items.map((item) => 'group' in item
124 ? html `
125 <div class="group" role="group">
126 <div class="group-title">${item.title}${item.slot}</div>
127 <div class="group-body">${item.group.map((e) => __classPrivateFieldGet(this, _DuoyunSideNavigationElement_renderItem, "f").call(this, e))}</div>
128 </div>
129 `
130 : __classPrivateFieldGet(this, _DuoyunSideNavigationElement_renderItem, "f").call(this, item))}`;
131 };
132 this.internals.role = 'navigation';
133 this.internals.ariaLabel = 'Side Navigation';
134 }
135};
136_DuoyunSideNavigationElement_onClickChildren = new WeakMap(), _DuoyunSideNavigationElement_checkGroupStatus = new WeakMap(), _DuoyunSideNavigationElement_renderItem = new WeakMap();
137__decorate([
138 property
139], DuoyunSideNavigationElement.prototype, "items", void 0);
140DuoyunSideNavigationElement = __decorate([
141 customElement('dy-side-navigation'),
142 adoptedStyle(style),
143 adoptedStyle(focusStyle),
144 connectStore(history.store)
145], DuoyunSideNavigationElement);
146export { DuoyunSideNavigationElement };
147//# sourceMappingURL=side-navigation.js.map
\No newline at end of file