@mdui/shared 1.0.0

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.
Files changed (80) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +14 -0
  3. package/controllers/form.d.ts +53 -0
  4. package/controllers/form.js +214 -0
  5. package/controllers/has-slot.d.ts +17 -0
  6. package/controllers/has-slot.js +46 -0
  7. package/controllers/hover.d.ts +34 -0
  8. package/controllers/hover.js +62 -0
  9. package/decorators/default-value.d.ts +16 -0
  10. package/decorators/default-value.js +40 -0
  11. package/decorators/watch.d.ts +17 -0
  12. package/decorators/watch.js +37 -0
  13. package/helpers/animate.d.ts +2 -0
  14. package/helpers/animate.js +37 -0
  15. package/helpers/breakpoint.d.ts +49 -0
  16. package/helpers/breakpoint.js +102 -0
  17. package/helpers/decorator.d.ts +16 -0
  18. package/helpers/decorator.js +18 -0
  19. package/helpers/delay.d.ts +4 -0
  20. package/helpers/delay.js +6 -0
  21. package/helpers/event.d.ts +7 -0
  22. package/helpers/event.js +17 -0
  23. package/helpers/modal.d.ts +12 -0
  24. package/helpers/modal.js +52 -0
  25. package/helpers/motion.d.ts +13 -0
  26. package/helpers/motion.js +20 -0
  27. package/helpers/observeResize.d.ts +16 -0
  28. package/helpers/observeResize.js +57 -0
  29. package/helpers/queue.d.ts +18 -0
  30. package/helpers/queue.js +25 -0
  31. package/helpers/scroll.d.ts +14 -0
  32. package/helpers/scroll.js +28 -0
  33. package/helpers/slot.d.ts +10 -0
  34. package/helpers/slot.js +31 -0
  35. package/helpers/tabbable.d.ts +8 -0
  36. package/helpers/tabbable.js +82 -0
  37. package/helpers/template.d.ts +2 -0
  38. package/helpers/template.js +2 -0
  39. package/helpers/throttle.d.ts +14 -0
  40. package/helpers/throttle.js +28 -0
  41. package/helpers/uniqueId.d.ts +4 -0
  42. package/helpers/uniqueId.js +7 -0
  43. package/icons/arrow-right.d.ts +11 -0
  44. package/icons/arrow-right.js +14 -0
  45. package/icons/cancel--outlined.d.ts +11 -0
  46. package/icons/cancel--outlined.js +14 -0
  47. package/icons/check-box-outline-blank.d.ts +11 -0
  48. package/icons/check-box-outline-blank.js +14 -0
  49. package/icons/check-box.d.ts +11 -0
  50. package/icons/check-box.js +14 -0
  51. package/icons/check.d.ts +11 -0
  52. package/icons/check.js +14 -0
  53. package/icons/circle.d.ts +11 -0
  54. package/icons/circle.js +14 -0
  55. package/icons/clear.d.ts +11 -0
  56. package/icons/clear.js +14 -0
  57. package/icons/error.d.ts +11 -0
  58. package/icons/error.js +14 -0
  59. package/icons/indeterminate-check-box.d.ts +11 -0
  60. package/icons/indeterminate-check-box.js +14 -0
  61. package/icons/radio-button-unchecked.d.ts +11 -0
  62. package/icons/radio-button-unchecked.js +14 -0
  63. package/icons/shared/style.d.ts +1 -0
  64. package/icons/shared/style.js +2 -0
  65. package/icons/shared/svg-tag.d.ts +2 -0
  66. package/icons/shared/svg-tag.js +3 -0
  67. package/icons/visibility-off.d.ts +11 -0
  68. package/icons/visibility-off.js +14 -0
  69. package/icons/visibility.d.ts +11 -0
  70. package/icons/visibility.js +14 -0
  71. package/lit-styles/component-style.d.ts +1 -0
  72. package/lit-styles/component-style.js +2 -0
  73. package/mixin.less +380 -0
  74. package/mixins/anchor.d.ts +22 -0
  75. package/mixins/anchor.js +25 -0
  76. package/mixins/focusable.d.ts +19 -0
  77. package/mixins/focusable.js +251 -0
  78. package/mixins/scrollBehavior.d.ts +24 -0
  79. package/mixins/scrollBehavior.js +202 -0
  80. package/package.json +35 -0
@@ -0,0 +1,24 @@
1
+ import '@mdui/jq/methods/css.js';
2
+ import type { JQ } from '@mdui/jq/shared/core.js';
3
+ import type { Constructor } from '@open-wc/dedupe-mixin';
4
+ import type { LitElement } from 'lit';
5
+ type ScrollBehavior = 'hide' | 'shrink' | 'elevate';
6
+ export type ScrollPaddingPosition = 'top' | 'bottom';
7
+ export declare class ScrollBehaviorMixinInterface extends LitElement {
8
+ scrollTarget?: string | HTMLElement | JQ<HTMLElement>;
9
+ scrollBehavior?: ScrollBehavior;
10
+ scrollThreshold?: number;
11
+ protected updateContainerPadding(): void;
12
+ protected hasScrollBehavior(behavior: ScrollBehavior | ScrollBehavior[]): boolean;
13
+ }
14
+ /**
15
+ * 滚动行为
16
+ *
17
+ * 父类需要实现
18
+ * @property() public scrollBehavior
19
+ * protected runScrollThreshold(isScrollingUp: boolean, scrollTop: number): void;
20
+ * protected runScrollNoThreshold(isScrollingUp: boolean, scrollTop: number): void;
21
+ * protected get scrollPaddingPosition(): ScrollPaddingPosition
22
+ */
23
+ export declare const ScrollBehaviorMixin: <T extends Constructor<LitElement>>(superclass: T) => Constructor<ScrollBehaviorMixinInterface> & T;
24
+ export {};
@@ -0,0 +1,202 @@
1
+ import { __decorate } from "tslib";
2
+ import { property } from 'lit/decorators.js';
3
+ import { $ } from '@mdui/jq/$.js';
4
+ import '@mdui/jq/methods/css.js';
5
+ import { isNodeName } from '@mdui/jq/shared/helper.js';
6
+ import { watch } from '../decorators/watch.js';
7
+ /**
8
+ * 滚动行为
9
+ *
10
+ * 父类需要实现
11
+ * @property() public scrollBehavior
12
+ * protected runScrollThreshold(isScrollingUp: boolean, scrollTop: number): void;
13
+ * protected runScrollNoThreshold(isScrollingUp: boolean, scrollTop: number): void;
14
+ * protected get scrollPaddingPosition(): ScrollPaddingPosition
15
+ */
16
+ export const ScrollBehaviorMixin = (superclass) => {
17
+ class ScrollBehaviorMixinClass extends superclass {
18
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
19
+ constructor(...args) {
20
+ super(...args);
21
+ /**
22
+ * 上次滚动后,垂直方向的距离(滚动距离超过 scrollThreshold 才记录)
23
+ */
24
+ this.lastScrollTopThreshold = 0;
25
+ /**
26
+ * 上次滚动后,垂直方向的距离(无视 scrollThreshold,始终记录)
27
+ */
28
+ this.lastScrollTopNoThreshold = 0;
29
+ /**
30
+ * 父元素是否是 `mdui-layout`
31
+ */
32
+ this.isParentLayout = false;
33
+ this.onListeningScroll = this.onListeningScroll.bind(this);
34
+ }
35
+ /**
36
+ * 滚动时,如果需要给 container 添加 padding,添加在顶部还是底部
37
+ */
38
+ get scrollPaddingPosition() {
39
+ throw new Error('Must implement scrollPaddingPosition getter');
40
+ }
41
+ onScrollTargetChange(oldValue, newValue) {
42
+ // 仅在有值切换到无值、或无值切换到有值时,更新
43
+ if ((oldValue && !newValue) || (!oldValue && newValue)) {
44
+ this.updateContainerPadding();
45
+ }
46
+ if (!this.scrollBehavior) {
47
+ return;
48
+ }
49
+ const oldListening = this.getListening(oldValue);
50
+ if (oldListening) {
51
+ oldListening.removeEventListener('scroll', this.onListeningScroll);
52
+ }
53
+ const newListening = this.getListening(newValue);
54
+ if (newListening) {
55
+ this.updateScrollTop(newListening);
56
+ newListening.addEventListener('scroll', this.onListeningScroll);
57
+ }
58
+ }
59
+ onScrollBehaviorChange(oldValue, newValue) {
60
+ // 仅在有值切换到无值、或无值切换到有值时,更新
61
+ if ((oldValue && !newValue) || (!oldValue && newValue)) {
62
+ this.updateContainerPadding();
63
+ }
64
+ const listening = this.getListening(this.scrollTarget);
65
+ if (!listening) {
66
+ return;
67
+ }
68
+ if (this.scrollBehavior) {
69
+ this.updateScrollTop(listening);
70
+ listening.addEventListener('scroll', this.onListeningScroll);
71
+ }
72
+ else {
73
+ listening.removeEventListener('scroll', this.onListeningScroll);
74
+ }
75
+ }
76
+ connectedCallback() {
77
+ super.connectedCallback();
78
+ this.isParentLayout = isNodeName(this.parentElement, 'mdui-layout');
79
+ this.updateContainerPadding();
80
+ }
81
+ disconnectedCallback() {
82
+ super.disconnectedCallback();
83
+ this.updateContainerPadding(false);
84
+ }
85
+ /**
86
+ * scrollBehavior 包含多个滚动行为,用空格分割
87
+ * 用该方法判断指定滚动行为是否在 scrollBehavior 中
88
+ * @param behavior 为数组时,只要其中一个行为在 scrollBehavior 中,即返回 `true`
89
+ */
90
+ hasScrollBehavior(behavior) {
91
+ var _a, _b;
92
+ const behaviors = ((_b = (_a = this.scrollBehavior) === null || _a === void 0 ? void 0 : _a.split(' ')) !== null && _b !== void 0 ? _b : []);
93
+ if (Array.isArray(behavior)) {
94
+ return !!behaviors.filter((v) => behavior.includes(v)).length;
95
+ }
96
+ else {
97
+ return behaviors.includes(behavior);
98
+ }
99
+ }
100
+ /**
101
+ * 执行滚动事件,在滚动距离超过 scrollThreshold 时才会执行
102
+ * Note: 父类可以按需实现该方法
103
+ * @param isScrollingUp 是否向上滚动
104
+ * @param scrollTop 距离 scrollTarget 顶部的距离
105
+ */
106
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
107
+ runScrollThreshold(isScrollingUp, scrollTop) {
108
+ return;
109
+ }
110
+ /**
111
+ * 执行滚动事件,会无视 scrollThreshold,始终会执行
112
+ * @param isScrollingUp 是否向上滚动
113
+ * @param scrollTop 距离 scrollTarget 顶部的距离
114
+ */
115
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
116
+ runScrollNoThreshold(isScrollingUp, scrollTop) {
117
+ return;
118
+ }
119
+ /**
120
+ * 更新滚动容器的 padding,避免内容被 navigation-bar 覆盖
121
+ * 仅 scrollBehavior 包含 hide、shrink 时,添加 padding
122
+ * @param withPadding 该值为 false 时,为移除 padding
123
+ */
124
+ updateContainerPadding(withPadding = true) {
125
+ const container = this.getContainer(this.scrollTarget);
126
+ if (!container || this.isParentLayout) {
127
+ return;
128
+ }
129
+ const propName = this.scrollPaddingPosition === 'top' ? 'paddingTop' : 'paddingBottom';
130
+ if (withPadding) {
131
+ const propValue = this.getListening(this.scrollTarget) &&
132
+ ['fixed', 'absolute'].includes($(this).css('position'))
133
+ ? this.offsetHeight
134
+ : null;
135
+ $(container).css({ [propName]: propValue });
136
+ }
137
+ else {
138
+ $(container).css({ [propName]: null });
139
+ }
140
+ }
141
+ onListeningScroll() {
142
+ const listening = this.getListening(this.scrollTarget);
143
+ window.requestAnimationFrame(() => this.onScroll(listening));
144
+ }
145
+ /**
146
+ * 滚动事件,这里过滤掉不符合条件的滚动
147
+ */
148
+ onScroll(listening) {
149
+ var _a;
150
+ const scrollTop = (_a = listening.scrollY) !== null && _a !== void 0 ? _a : listening.scrollTop;
151
+ // 无视 scrollThreshold 的回调
152
+ if (this.lastScrollTopNoThreshold !== scrollTop) {
153
+ this.runScrollNoThreshold(scrollTop < this.lastScrollTopNoThreshold, scrollTop);
154
+ this.lastScrollTopNoThreshold = scrollTop;
155
+ }
156
+ // 滚动距离大于 scrollThreshold 时才执行的回调
157
+ if (Math.abs(scrollTop - this.lastScrollTopThreshold) >
158
+ (this.scrollThreshold || 0)) {
159
+ this.runScrollThreshold(scrollTop < this.lastScrollTopThreshold, scrollTop);
160
+ this.lastScrollTopThreshold = scrollTop;
161
+ }
162
+ }
163
+ /**
164
+ * 重新更新 lastScrollTopThreshold、lastScrollTopNoThreshold 的值
165
+ * 用于在 scrollTarget、scrollBehavior 变更时,重新设置 lastScrollTopThreshold、lastScrollTopNoThreshold 的初始值
166
+ */
167
+ updateScrollTop(listening) {
168
+ var _a;
169
+ this.lastScrollTopThreshold = this.lastScrollTopNoThreshold =
170
+ (_a = listening.scrollY) !== null && _a !== void 0 ? _a : listening.scrollTop;
171
+ }
172
+ /**
173
+ * 获取组件需要监听哪个元素的滚动状态
174
+ */
175
+ getListening(target) {
176
+ return target ? $(target)[0] : window;
177
+ }
178
+ /**
179
+ * 获取组件在哪个容器内滚动
180
+ */
181
+ getContainer(target) {
182
+ return target ? $(target)[0] : document.body;
183
+ }
184
+ }
185
+ __decorate([
186
+ property({ attribute: 'scroll-target' })
187
+ ], ScrollBehaviorMixinClass.prototype, "scrollTarget", void 0);
188
+ __decorate([
189
+ property({ reflect: true, attribute: 'scroll-behavior' })
190
+ ], ScrollBehaviorMixinClass.prototype, "scrollBehavior", void 0);
191
+ __decorate([
192
+ property({ type: Number, reflect: true, attribute: 'scroll-threshold' })
193
+ ], ScrollBehaviorMixinClass.prototype, "scrollThreshold", void 0);
194
+ __decorate([
195
+ watch('scrollTarget')
196
+ ], ScrollBehaviorMixinClass.prototype, "onScrollTargetChange", null);
197
+ __decorate([
198
+ watch('scrollBehavior')
199
+ ], ScrollBehaviorMixinClass.prototype, "onScrollBehaviorChange", null);
200
+ // @ts-ignore
201
+ return ScrollBehaviorMixinClass;
202
+ };
package/package.json ADDED
@@ -0,0 +1,35 @@
1
+ {
2
+ "name": "@mdui/shared",
3
+ "version": "1.0.0",
4
+ "description": "mdui 项目的公共部分",
5
+ "type": "module",
6
+ "files": [
7
+ "controllers",
8
+ "decorators",
9
+ "helpers",
10
+ "icons",
11
+ "lit-styles",
12
+ "mixins",
13
+ "LICENSE",
14
+ "mixin.less",
15
+ "package.json",
16
+ "README.md"
17
+ ],
18
+ "repository": {
19
+ "type": "git",
20
+ "url": "git+https://github.com/zdhxiong/mdui.git"
21
+ },
22
+ "author": "zdhxiong <zdhxiong@gmail.com>",
23
+ "license": "MIT",
24
+ "bugs": {
25
+ "url": "https://github.com/zdhxiong/mdui/issues"
26
+ },
27
+ "homepage": "https://github.com/zdhxiong/mdui#readme",
28
+ "dependencies": {
29
+ "@open-wc/dedupe-mixin": "^1.3.1",
30
+ "lit": "^2.7.5",
31
+ "ssr-window": "^4.0.2",
32
+ "tslib": "^2.6.0",
33
+ "@mdui/jq": "3.0.0"
34
+ }
35
+ }