1 | var __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 | };
|
7 | var __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 | };
|
12 | var _DuoyunButtonElement_instances, _DuoyunButtonElement_color_get, _DuoyunButtonElement_onClickDropdown;
|
13 | var DuoyunButtonElement_1;
|
14 | import { connectStore, adoptedStyle, customElement, attribute, property, boolattribute, refobject, state, part, } from '@mantou/gem/lib/decorators';
|
15 | import { GemElement, html } from '@mantou/gem/lib/element';
|
16 | import { history } from '@mantou/gem/lib/history';
|
17 | import { createCSSSheet, css, QueryString } from '@mantou/gem/lib/utils';
|
18 | import { theme, getSemanticColor } from '../lib/theme';
|
19 | import { icons } from '../lib/icons';
|
20 | import { commonHandle } from '../lib/hotkeys';
|
21 | import { focusStyle } from '../lib/styles';
|
22 | import { createHistoryParams } from './route';
|
23 | import { ContextMenu } from './menu';
|
24 | const style = createCSSSheet(css `
|
25 | :host(:where(:not([hidden]))) {
|
26 | --color: ${theme.backgroundColor};
|
27 | display: inline-flex;
|
28 | align-items: stretch;
|
29 | line-height: 1.2;
|
30 | cursor: default;
|
31 | user-select: none;
|
32 | font-size: 0.875em;
|
33 | border-radius: ${theme.normalRound};
|
34 | white-space: nowrap;
|
35 | }
|
36 | .content,
|
37 | .dropdown {
|
38 | position: relative;
|
39 | color: var(--color);
|
40 | background: var(--bg);
|
41 | border: 1px solid var(--bg);
|
42 | }
|
43 | .content {
|
44 | flex-grow: 1;
|
45 | display: flex;
|
46 | align-items: center;
|
47 | justify-content: center;
|
48 | gap: 0.3em;
|
49 | padding: 0.5em 1.5em;
|
50 | min-width: 3em;
|
51 | border-radius: inherit;
|
52 | }
|
53 | .dropdown {
|
54 | display: flex;
|
55 | border-radius: 0 ${theme.normalRound} ${theme.normalRound} 0;
|
56 | margin-inline-start: -1px;
|
57 | padding-inline: 0.2em;
|
58 | width: 1.4em;
|
59 | }
|
60 | .icon {
|
61 | width: 1.2em;
|
62 | }
|
63 | :host([small]) {
|
64 | font-size: 0.75em;
|
65 | }
|
66 | :host([small]) .content {
|
67 | min-width: auto;
|
68 | padding: 0.5em 0.8em;
|
69 | }
|
70 | :host([small]) .dropdown {
|
71 | padding: 0.5em;
|
72 | }
|
73 | :host([type='reverse']) :where(.content, .dropdown) {
|
74 | color: var(--bg);
|
75 | border-color: var(--bg);
|
76 | background: transparent;
|
77 | }
|
78 | :host([color='normal']) {
|
79 | --bg: ${theme.primaryColor};
|
80 | }
|
81 | :host([color='danger']) {
|
82 | --bg: ${theme.negativeColor};
|
83 | }
|
84 | :host([color='cancel']) {
|
85 | --bg: ${theme.hoverBackgroundColor};
|
86 | --color: ${theme.textColor};
|
87 | }
|
88 | :host([disabled]) {
|
89 | cursor: not-allowed;
|
90 | --bg: ${theme.disabledColor};
|
91 | }
|
92 | :where(
|
93 | :host(:where([data-active], :state(active))) .content,
|
94 | .content:where(:hover),
|
95 | .dropdown:where(:hover, [data-active], :state(active))
|
96 | )::after {
|
97 | content: '';
|
98 | position: absolute;
|
99 | inset: -1px;
|
100 | background-color: currentColor;
|
101 | border-radius: inherit;
|
102 | opacity: 0.1;
|
103 | }
|
104 | :host([disabled]) ::after {
|
105 | content: none;
|
106 | }
|
107 | `);
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 | let DuoyunButtonElement = DuoyunButtonElement_1 = class DuoyunButtonElement extends GemElement {
|
116 | constructor() {
|
117 | super({ delegatesFocus: true });
|
118 | _DuoyunButtonElement_instances.add(this);
|
119 | _DuoyunButtonElement_onClickDropdown.set(this, async (e) => {
|
120 | e.stopPropagation();
|
121 | if (this.disabled)
|
122 | return;
|
123 | if (this.dropdown) {
|
124 | const { element } = this.dropdownRef;
|
125 | const { right, bottom } = element.getBoundingClientRect();
|
126 | const { width } = this.getBoundingClientRect();
|
127 | element.active = true;
|
128 | await ContextMenu.open(this.dropdown, {
|
129 | x: right - width,
|
130 | y: bottom,
|
131 | width: `${width}px`,
|
132 | });
|
133 | element.active = false;
|
134 | }
|
135 | });
|
136 | this.render = () => {
|
137 | return html `
|
138 | <style>
|
139 | :host {
|
140 | --bg: ${__classPrivateFieldGet(this, _DuoyunButtonElement_instances, "a", _DuoyunButtonElement_color_get)};
|
141 | }
|
142 | </style>
|
143 | <div
|
144 | tabindex="0"
|
145 | role="button"
|
146 | aria-disabled=${this.disabled}
|
147 | @keydown=${commonHandle}
|
148 | class="content"
|
149 | part=${DuoyunButtonElement_1.button}
|
150 | >
|
151 | ${this.icon ? html `<dy-use class="icon" .element=${this.icon}></dy-use>` : ''}
|
152 | <slot></slot>
|
153 | </div>
|
154 | ${this.dropdown
|
155 | ? html `
|
156 | <style>
|
157 | div.content {
|
158 | border-start-end-radius: 0;
|
159 | border-end-end-radius: 0;
|
160 | }
|
161 | span.dropdown {
|
162 | border-start-start-radius: 0;
|
163 | border-end-start-radius: 0;
|
164 | }
|
165 | </style>
|
166 | <dy-use
|
167 | ref=${this.dropdownRef.ref}
|
168 | class="dropdown"
|
169 | part=${DuoyunButtonElement_1.dropdown}
|
170 | tabindex="0"
|
171 | @keydown=${commonHandle}
|
172 | role="button"
|
173 | aria-disabled=${this.disabled}
|
174 | @click=${__classPrivateFieldGet(this, _DuoyunButtonElement_onClickDropdown, "f")}
|
175 | .element=${icons.expand}
|
176 | ></dy-use>
|
177 | `
|
178 | : ''}
|
179 | `;
|
180 | };
|
181 | this.addEventListener('click', () => {
|
182 | if (this.disabled)
|
183 | return;
|
184 | if (this.route) {
|
185 | history.push(createHistoryParams(this.route, {
|
186 | title: this.route.title,
|
187 | params: this.params,
|
188 | query: new QueryString(this.query),
|
189 | }));
|
190 | }
|
191 | });
|
192 | }
|
193 | };
|
194 | _DuoyunButtonElement_onClickDropdown = new WeakMap(), _DuoyunButtonElement_instances = new WeakSet(), _DuoyunButtonElement_color_get = function _DuoyunButtonElement_color_get() {
|
195 | return getSemanticColor(this.color) || this.color || theme.primaryColor;
|
196 | };
|
197 | __decorate([
|
198 | attribute
|
199 | ], DuoyunButtonElement.prototype, "type", void 0);
|
200 | __decorate([
|
201 | attribute
|
202 | ], DuoyunButtonElement.prototype, "color", void 0);
|
203 | __decorate([
|
204 | boolattribute
|
205 | ], DuoyunButtonElement.prototype, "small", void 0);
|
206 | __decorate([
|
207 | boolattribute
|
208 | ], DuoyunButtonElement.prototype, "disabled", void 0);
|
209 | __decorate([
|
210 | property
|
211 | ], DuoyunButtonElement.prototype, "dropdown", void 0);
|
212 | __decorate([
|
213 | property
|
214 | ], DuoyunButtonElement.prototype, "route", void 0);
|
215 | __decorate([
|
216 | property
|
217 | ], DuoyunButtonElement.prototype, "params", void 0);
|
218 | __decorate([
|
219 | property
|
220 | ], DuoyunButtonElement.prototype, "query", void 0);
|
221 | __decorate([
|
222 | property
|
223 | ], DuoyunButtonElement.prototype, "icon", void 0);
|
224 | __decorate([
|
225 | state
|
226 | ], DuoyunButtonElement.prototype, "active", void 0);
|
227 | __decorate([
|
228 | refobject
|
229 | ], DuoyunButtonElement.prototype, "dropdownRef", void 0);
|
230 | __decorate([
|
231 | part
|
232 | ], DuoyunButtonElement, "button", void 0);
|
233 | __decorate([
|
234 | part
|
235 | ], DuoyunButtonElement, "dropdown", void 0);
|
236 | DuoyunButtonElement = DuoyunButtonElement_1 = __decorate([
|
237 | customElement('dy-button'),
|
238 | adoptedStyle(style),
|
239 | adoptedStyle(focusStyle),
|
240 | connectStore(icons)
|
241 | ], DuoyunButtonElement);
|
242 | export { DuoyunButtonElement };
|
243 |
|
\ | No newline at end of file |