godown 3.0.0 → 3.0.2

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 (251) hide show
  1. package/build/godown+lit.iife.js +12 -12
  2. package/build/godown+lit.iife.js.map +1 -1
  3. package/build/godown+lit.js +12 -12
  4. package/build/godown+lit.js.map +1 -1
  5. package/build/godown+lit.umd.js +12 -12
  6. package/build/godown+lit.umd.js.map +1 -1
  7. package/build/godown.iife.js +11 -11
  8. package/build/godown.js +11 -11
  9. package/build/godown.js.map +1 -1
  10. package/build/godown.umd.js +11 -11
  11. package/build/godown.umd.js.map +1 -1
  12. package/components/alert.d.ts.map +1 -1
  13. package/components/alert.js +1 -1
  14. package/components/alert.js.map +1 -1
  15. package/components/avatar.d.ts.map +1 -1
  16. package/components/avatar.js +1 -1
  17. package/components/avatar.js.map +1 -1
  18. package/components/badge.d.ts.map +1 -1
  19. package/components/badge.js +1 -1
  20. package/components/badge.js.map +1 -1
  21. package/components/breath.d.ts.map +1 -1
  22. package/components/breath.js +1 -1
  23. package/components/breath.js.map +1 -1
  24. package/components/button.d.ts.map +1 -1
  25. package/components/button.js +1 -1
  26. package/components/button.js.map +1 -1
  27. package/components/card.d.ts.map +1 -1
  28. package/components/card.js +1 -1
  29. package/components/card.js.map +1 -1
  30. package/components/carousel.d.ts.map +1 -1
  31. package/components/carousel.js +1 -1
  32. package/components/carousel.js.map +1 -1
  33. package/components/details.d.ts.map +1 -1
  34. package/components/details.js +1 -1
  35. package/components/details.js.map +1 -1
  36. package/components/dialog.d.ts +3 -2
  37. package/components/dialog.d.ts.map +1 -1
  38. package/components/dialog.js +1 -1
  39. package/components/dialog.js.map +1 -1
  40. package/components/divider.d.ts.map +1 -1
  41. package/components/divider.js +1 -1
  42. package/components/divider.js.map +1 -1
  43. package/components/dragbox.d.ts.map +1 -1
  44. package/components/dragbox.js +1 -1
  45. package/components/dragbox.js.map +1 -1
  46. package/components/flex.d.ts.map +1 -1
  47. package/components/flex.js.map +1 -1
  48. package/components/form.d.ts.map +1 -1
  49. package/components/form.js.map +1 -1
  50. package/components/grid.d.ts.map +1 -1
  51. package/components/grid.js.map +1 -1
  52. package/components/heading.d.ts.map +1 -1
  53. package/components/heading.js +1 -1
  54. package/components/heading.js.map +1 -1
  55. package/components/input.d.ts.map +1 -1
  56. package/components/input.js +1 -1
  57. package/components/input.js.map +1 -1
  58. package/components/layout.d.ts.map +1 -1
  59. package/components/layout.js +1 -1
  60. package/components/layout.js.map +1 -1
  61. package/components/link.js.map +1 -1
  62. package/components/progress.d.ts.map +1 -1
  63. package/components/progress.js +1 -1
  64. package/components/progress.js.map +1 -1
  65. package/components/range.d.ts.map +1 -1
  66. package/components/range.js +1 -1
  67. package/components/range.js.map +1 -1
  68. package/components/rotate.d.ts.map +1 -1
  69. package/components/rotate.js +1 -1
  70. package/components/rotate.js.map +1 -1
  71. package/components/router.d.ts +21 -26
  72. package/components/router.d.ts.map +1 -1
  73. package/components/router.js +1 -1
  74. package/components/router.js.map +1 -1
  75. package/components/select.d.ts.map +1 -1
  76. package/components/select.js +1 -1
  77. package/components/select.js.map +1 -1
  78. package/components/skeleton.d.ts.map +1 -1
  79. package/components/skeleton.js +1 -1
  80. package/components/skeleton.js.map +1 -1
  81. package/components/split.d.ts.map +1 -1
  82. package/components/split.js +1 -1
  83. package/components/split.js.map +1 -1
  84. package/components/switch.d.ts.map +1 -1
  85. package/components/switch.js +1 -1
  86. package/components/switch.js.map +1 -1
  87. package/components/text.d.ts.map +1 -1
  88. package/components/text.js +1 -1
  89. package/components/text.js.map +1 -1
  90. package/components/time.d.ts.map +1 -1
  91. package/components/time.js.map +1 -1
  92. package/components/tooltip.d.ts.map +1 -1
  93. package/components/tooltip.js +1 -1
  94. package/components/tooltip.js.map +1 -1
  95. package/components/typewriter.d.ts.map +1 -1
  96. package/components/typewriter.js +1 -1
  97. package/components/typewriter.js.map +1 -1
  98. package/core/global-style.d.ts.map +1 -1
  99. package/core/global-style.js +1 -1
  100. package/core/global-style.js.map +1 -1
  101. package/core/super-anchor.d.ts.map +1 -1
  102. package/core/super-anchor.js +1 -1
  103. package/core/super-anchor.js.map +1 -1
  104. package/core/super-input.d.ts +1 -0
  105. package/core/super-input.d.ts.map +1 -1
  106. package/core/super-input.js +1 -1
  107. package/core/super-input.js.map +1 -1
  108. package/core/super-openable.d.ts.map +1 -1
  109. package/core/super-openable.js.map +1 -1
  110. package/custom-elements.json +1 -1
  111. package/dev/components/alert.d.ts.map +1 -1
  112. package/dev/components/alert.js +82 -69
  113. package/dev/components/alert.js.map +1 -1
  114. package/dev/components/avatar.d.ts.map +1 -1
  115. package/dev/components/avatar.js +52 -36
  116. package/dev/components/avatar.js.map +1 -1
  117. package/dev/components/badge.d.ts.map +1 -1
  118. package/dev/components/badge.js +57 -50
  119. package/dev/components/badge.js.map +1 -1
  120. package/dev/components/breath.d.ts.map +1 -1
  121. package/dev/components/breath.js +20 -14
  122. package/dev/components/breath.js.map +1 -1
  123. package/dev/components/button.d.ts.map +1 -1
  124. package/dev/components/button.js +13 -13
  125. package/dev/components/button.js.map +1 -1
  126. package/dev/components/card.d.ts.map +1 -1
  127. package/dev/components/card.js +40 -35
  128. package/dev/components/card.js.map +1 -1
  129. package/dev/components/carousel.d.ts.map +1 -1
  130. package/dev/components/carousel.js +63 -43
  131. package/dev/components/carousel.js.map +1 -1
  132. package/dev/components/details.d.ts.map +1 -1
  133. package/dev/components/details.js +72 -61
  134. package/dev/components/details.js.map +1 -1
  135. package/dev/components/dialog.d.ts +3 -2
  136. package/dev/components/dialog.d.ts.map +1 -1
  137. package/dev/components/dialog.js +63 -65
  138. package/dev/components/dialog.js.map +1 -1
  139. package/dev/components/divider.d.ts.map +1 -1
  140. package/dev/components/divider.js +28 -23
  141. package/dev/components/divider.js.map +1 -1
  142. package/dev/components/dragbox.d.ts.map +1 -1
  143. package/dev/components/dragbox.js +19 -17
  144. package/dev/components/dragbox.js.map +1 -1
  145. package/dev/components/flex.d.ts.map +1 -1
  146. package/dev/components/flex.js +16 -11
  147. package/dev/components/flex.js.map +1 -1
  148. package/dev/components/form.d.ts.map +1 -1
  149. package/dev/components/form.js +4 -4
  150. package/dev/components/form.js.map +1 -1
  151. package/dev/components/grid.d.ts.map +1 -1
  152. package/dev/components/grid.js +16 -11
  153. package/dev/components/grid.js.map +1 -1
  154. package/dev/components/heading.d.ts.map +1 -1
  155. package/dev/components/heading.js +62 -40
  156. package/dev/components/heading.js.map +1 -1
  157. package/dev/components/input.d.ts.map +1 -1
  158. package/dev/components/input.js +32 -24
  159. package/dev/components/input.js.map +1 -1
  160. package/dev/components/layout.d.ts.map +1 -1
  161. package/dev/components/layout.js +43 -30
  162. package/dev/components/layout.js.map +1 -1
  163. package/dev/components/link.js +1 -1
  164. package/dev/components/link.js.map +1 -1
  165. package/dev/components/progress.d.ts.map +1 -1
  166. package/dev/components/progress.js +51 -42
  167. package/dev/components/progress.js.map +1 -1
  168. package/dev/components/range.d.ts.map +1 -1
  169. package/dev/components/range.js +33 -30
  170. package/dev/components/range.js.map +1 -1
  171. package/dev/components/rotate.d.ts.map +1 -1
  172. package/dev/components/rotate.js +12 -5
  173. package/dev/components/rotate.js.map +1 -1
  174. package/dev/components/router.d.ts +21 -26
  175. package/dev/components/router.d.ts.map +1 -1
  176. package/dev/components/router.js +22 -18
  177. package/dev/components/router.js.map +1 -1
  178. package/dev/components/select.d.ts.map +1 -1
  179. package/dev/components/select.js +65 -57
  180. package/dev/components/select.js.map +1 -1
  181. package/dev/components/skeleton.d.ts.map +1 -1
  182. package/dev/components/skeleton.js +65 -61
  183. package/dev/components/skeleton.js.map +1 -1
  184. package/dev/components/split.d.ts.map +1 -1
  185. package/dev/components/split.js +66 -58
  186. package/dev/components/split.js.map +1 -1
  187. package/dev/components/switch.d.ts.map +1 -1
  188. package/dev/components/switch.js +88 -79
  189. package/dev/components/switch.js.map +1 -1
  190. package/dev/components/text.d.ts.map +1 -1
  191. package/dev/components/text.js +51 -45
  192. package/dev/components/text.js.map +1 -1
  193. package/dev/components/time.d.ts.map +1 -1
  194. package/dev/components/time.js +13 -2
  195. package/dev/components/time.js.map +1 -1
  196. package/dev/components/tooltip.d.ts.map +1 -1
  197. package/dev/components/tooltip.js +31 -23
  198. package/dev/components/tooltip.js.map +1 -1
  199. package/dev/components/typewriter.d.ts.map +1 -1
  200. package/dev/components/typewriter.js +35 -27
  201. package/dev/components/typewriter.js.map +1 -1
  202. package/dev/core/global-style.d.ts.map +1 -1
  203. package/dev/core/global-style.js +9 -9
  204. package/dev/core/global-style.js.map +1 -1
  205. package/dev/core/super-anchor.d.ts.map +1 -1
  206. package/dev/core/super-anchor.js +27 -26
  207. package/dev/core/super-anchor.js.map +1 -1
  208. package/dev/core/super-input.d.ts +1 -0
  209. package/dev/core/super-input.d.ts.map +1 -1
  210. package/dev/core/super-input.js +87 -69
  211. package/dev/core/super-input.js.map +1 -1
  212. package/dev/core/super-openable.d.ts.map +1 -1
  213. package/dev/core/super-openable.js +1 -2
  214. package/dev/core/super-openable.js.map +1 -1
  215. package/package.json +3 -3
  216. package/src/components/alert.ts +84 -75
  217. package/src/components/avatar.ts +54 -40
  218. package/src/components/badge.ts +68 -65
  219. package/src/components/breath.ts +23 -19
  220. package/src/components/button.ts +14 -14
  221. package/src/components/card.ts +42 -40
  222. package/src/components/carousel.ts +65 -47
  223. package/src/components/details.ts +74 -65
  224. package/src/components/dialog.ts +69 -66
  225. package/src/components/divider.ts +33 -30
  226. package/src/components/dragbox.ts +22 -22
  227. package/src/components/flex.ts +31 -28
  228. package/src/components/form.ts +7 -9
  229. package/src/components/grid.ts +31 -28
  230. package/src/components/heading.ts +67 -49
  231. package/src/components/input.ts +37 -31
  232. package/src/components/layout.ts +45 -34
  233. package/src/components/link.ts +1 -1
  234. package/src/components/progress.ts +53 -46
  235. package/src/components/range.ts +58 -52
  236. package/src/components/rotate.ts +15 -10
  237. package/src/components/router.ts +46 -46
  238. package/src/components/select.ts +74 -70
  239. package/src/components/skeleton.ts +67 -65
  240. package/src/components/split.ts +73 -69
  241. package/src/components/switch.ts +90 -83
  242. package/src/components/text.ts +53 -49
  243. package/src/components/time.ts +14 -3
  244. package/src/components/tooltip.ts +32 -24
  245. package/src/components/typewriter.ts +37 -31
  246. package/src/core/global-style.ts +11 -12
  247. package/src/core/super-anchor.ts +29 -30
  248. package/src/core/super-input.ts +91 -74
  249. package/src/core/super-openable.ts +2 -8
  250. package/vscode.html-custom-data.json +1 -1
  251. package/web-types.json +1 -1
@@ -1,5 +1,5 @@
1
1
  import { attr, classList, godown, isNil, joinProperties, part, styles } from "@godown/element";
2
- import { css, html, type TemplateResult } from "lit";
2
+ import { type TemplateResult, css, html } from "lit";
3
3
  import { property, queryAll, state } from "lit/decorators.js";
4
4
 
5
5
  import { cssGlobalVars, scopePrefix } from "../core/global-style.js";
@@ -34,7 +34,7 @@ const cssScope = scopePrefix(protoName);
34
34
  display: block;
35
35
  }
36
36
 
37
- :host([contents]) [part=root] {
37
+ :host([contents]) [part="root"] {
38
38
  width: inherit;
39
39
  }
40
40
 
@@ -48,7 +48,7 @@ const cssScope = scopePrefix(protoName);
48
48
  background: var(${cssScope}--handle-active);
49
49
  }
50
50
 
51
- [part=root] {
51
+ [part="root"] {
52
52
  min-height: inherit;
53
53
  position: relative;
54
54
  border-radius: inherit;
@@ -58,9 +58,9 @@ const cssScope = scopePrefix(protoName);
58
58
  height: var(${cssScope}--track-width);
59
59
  }
60
60
 
61
- [part=track] {
61
+ [part="track"] {
62
62
  height: 100%;
63
- min-height:inherit;
63
+ min-height: inherit;
64
64
  display: flex;
65
65
  position: absolute;
66
66
  pointer-events: none;
@@ -71,7 +71,7 @@ const cssScope = scopePrefix(protoName);
71
71
  width: max(calc(var(--to) - var(--from)), calc(var(--from) - var(--to)));
72
72
  }
73
73
 
74
- [part=handle] {
74
+ [part="handle"] {
75
75
  width: 1em;
76
76
  height: 1em;
77
77
  display: flex;
@@ -97,7 +97,7 @@ const cssScope = scopePrefix(protoName);
97
97
  transform: translate(-25%, -50%);
98
98
  }
99
99
 
100
- [vertical] [part=track] {
100
+ [vertical] [part="track"] {
101
101
  width: 100%;
102
102
  height: max(calc(var(--to) - var(--from)), calc(var(--from) - var(--to)));
103
103
  top: min(var(--from), var(--to));
@@ -105,12 +105,12 @@ const cssScope = scopePrefix(protoName);
105
105
  }
106
106
  `,
107
107
  css`
108
- [part=handle] {
108
+ [part="handle"] {
109
109
  left: var(--handle);
110
110
  top: 0;
111
111
  }
112
112
 
113
- [vertical] [part=handle] {
113
+ [vertical] [part="handle"] {
114
114
  top: var(--handle);
115
115
  left: 0;
116
116
  }
@@ -207,42 +207,44 @@ class Range extends SuperInput {
207
207
  const to = Math.max(...rangeValue);
208
208
  const gap = this.max - this.min;
209
209
 
210
- return html`<div
211
- part="root"
212
- ${attr(this.observedRecord)}
213
- @mousedown="${this.disabled ? null : this._handleMousedownRoot}"
214
- style="${
215
- joinProperties({
216
- "--from": `${((from - this.min) / gap) * 100}%`,
217
- "--to": `${((to - this.min) / gap) * 100}%`,
218
- ...(this.range
219
- ? Object.fromEntries(
220
- rangeValue.map((value, index) => [`--handle-${index}`, `${((value - this.min) / gap) * 100}%`]),
221
- )
222
- : {}),
223
- })
224
- }"
225
- >
226
- <div part="track"></div>
227
- ${this.range ? (this.value as number[]).map((_, index) => this.renderHandle(index)) : this.renderHandle(0)}
228
- </div>`;
210
+ return html`
211
+ <div
212
+ part="root"
213
+ ${attr(this.observedRecord)}
214
+ @mousedown="${this.disabled ? null : this._handleMousedownRoot}"
215
+ style="${joinProperties({
216
+ "--from": `${((from - this.min) / gap) * 100}%`,
217
+ "--to": `${((to - this.min) / gap) * 100}%`,
218
+ ...(this.range
219
+ ? Object.fromEntries(
220
+ rangeValue.map((value, index) => [`--handle-${index}`, `${((value - this.min) / gap) * 100}%`]),
221
+ )
222
+ : {}),
223
+ })}"
224
+ >
225
+ <div part="track"></div>
226
+ ${this.range ? (this.value as number[]).map((_, index) => this.renderHandle(index)) : this.renderHandle(0)}
227
+ </div>
228
+ `;
229
229
  }
230
230
 
231
231
  protected renderHandle(index: number): TemplateResult<1> {
232
232
  const { range } = this;
233
233
  const end = !range || index === (this.value as number[]).length - 1;
234
- return html`<i
235
- tabindex="0"
236
- part="handle"
237
- class="${classList({ "last-focus": this.lastFocus === index })}"
238
- @mousedown="${this.disabled ? null : this.createMouseDown(index)}"
239
- @focus="${this.disabled ? null : () => this.focusHandle(index)}"
240
- @blur="${this.disabled ? null : this.blurHandle}"
241
- style="z-index:${this._focusStack.indexOf(index) + 1};--handle:var(--${
242
- /* In single-handle mod or end, it is max value */
243
- (!range && end) ? "to" : `handle-${index}`})"
234
+ return html`
235
+ <i
236
+ tabindex="0"
237
+ part="handle"
238
+ class="${classList({ "last-focus": this.lastFocus === index })}"
239
+ @mousedown="${this.disabled ? null : this.createMouseDown(index)}"
240
+ @focus="${this.disabled ? null : () => this.focusHandle(index)}"
241
+ @blur="${this.disabled ? null : this.blurHandle}"
242
+ style="z-index:${this._focusStack.indexOf(index) + 1};--handle:var(--${
243
+ /* In single-handle mod or end, it is max value */
244
+ !range && end ? "to" : `handle-${index}`
245
+ })"
244
246
  ></i>
245
- `;
247
+ `;
246
248
  }
247
249
 
248
250
  private _keydownEvent: EventListenerOrEventListenerObject;
@@ -275,10 +277,10 @@ class Range extends SuperInput {
275
277
  return (e: KeyboardEvent): void => {
276
278
  if (e.key === "ArrowLeft" || e.key === "ArrowDown") {
277
279
  e.preventDefault();
278
- this.createSetValue(index)(old => old - this.step);
280
+ this.createSetValue(index)((old) => old - this.step);
279
281
  } else if (e.key === "ArrowRight" || e.key === "ArrowUp") {
280
282
  e.preventDefault();
281
- this.createSetValue(index)(old => old + this.step);
283
+ this.createSetValue(index)((old) => old + this.step);
282
284
  }
283
285
  };
284
286
  }
@@ -292,9 +294,10 @@ class Range extends SuperInput {
292
294
 
293
295
  createSetValue(index: number) {
294
296
  return (numberOrModifier: number | ((value: number) => number)): void => {
295
- const number = typeof numberOrModifier === "number"
296
- ? this.normalizeValue(numberOrModifier)
297
- : numberOrModifier(this.rangeValue[index]);
297
+ const number =
298
+ typeof numberOrModifier === "number"
299
+ ? this.normalizeValue(numberOrModifier)
300
+ : numberOrModifier(this.rangeValue[index]);
298
301
  let newValue: any = number;
299
302
  if (this.range) {
300
303
  newValue = [...this.rangeValue];
@@ -319,8 +322,11 @@ class Range extends SuperInput {
319
322
  * Ensure that the values do not exceed the range of max and min.
320
323
  */
321
324
  protected normalizeValue(value: number): number {
322
- if (value > this.max) { value -= this.step; }
323
- else if (value < this.min) { value += this.step; }
325
+ if (value > this.max) {
326
+ value -= this.step;
327
+ } else if (value < this.min) {
328
+ value += this.step;
329
+ }
324
330
  return value;
325
331
  }
326
332
 
@@ -328,10 +334,10 @@ class Range extends SuperInput {
328
334
  const value = this._computeValue(e);
329
335
  const index = this.range
330
336
  ? this.rangeValue.reduce((acc, item, index) => {
331
- const diff = Math.abs(value - item);
332
- const prevDiff = Math.abs(value - this.rangeValue[acc]);
333
- return diff < prevDiff ? index : acc;
334
- }, 0)
337
+ const diff = Math.abs(value - item);
338
+ const prevDiff = Math.abs(value - this.rangeValue[acc]);
339
+ return diff < prevDiff ? index : acc;
340
+ }, 0)
335
341
  : 0;
336
342
 
337
343
  const set = this.createSetValue(index);
@@ -382,12 +388,12 @@ class Range extends SuperInput {
382
388
  }
383
389
 
384
390
  sort(): number | number[] {
385
- return this.value = this.toSorted();
391
+ return (this.value = this.toSorted());
386
392
  }
387
393
 
388
394
  toSorted(): number | number[] {
389
395
  if (this.range) {
390
- return [...this.value as number[]].sort((a, b) => a - b);
396
+ return [...(this.value as number[])].sort((a, b) => a - b);
391
397
  }
392
398
  return this.value;
393
399
  }
@@ -1,5 +1,5 @@
1
1
  import { godown, htmlSlot, part, styles } from "@godown/element";
2
- import { css, html, type TemplateResult } from "lit";
2
+ import { type TemplateResult, css, html } from "lit";
3
3
 
4
4
  import { GlobalStyle, scopePrefix } from "../core/global-style.js";
5
5
 
@@ -12,8 +12,7 @@ const cssScope = scopePrefix(protoName);
12
12
  * @category wrapper
13
13
  */
14
14
  @godown(protoName)
15
- @styles(
16
- css`
15
+ @styles(css`
17
16
  :host {
18
17
  display: block;
19
18
  width: -moz-fit-content;
@@ -39,20 +38,26 @@ const cssScope = scopePrefix(protoName);
39
38
  margin: calc(-1 * var(${cssScope}--offset));
40
39
  }
41
40
 
42
- [part=slot] {
41
+ [part="slot"] {
43
42
  z-index: 2;
44
43
  }
45
- `,
46
- )
44
+ `)
47
45
  class Rotate extends GlobalStyle {
48
46
  @part("root")
49
47
  protected _root: HTMLElement;
50
48
 
51
49
  protected render(): TemplateResult<1> {
52
- return html`<div part="root">
53
- <div part="slot" @mousemove="${this._handleRotate}">${htmlSlot()}</div>
54
- <i @mouseleave="${this.reset}"></i>
55
- </div>`;
50
+ return html`
51
+ <div part="root">
52
+ <div
53
+ part="slot"
54
+ @mousemove="${this._handleRotate}"
55
+ >
56
+ ${htmlSlot()}
57
+ </div>
58
+ <i @mouseleave="${this.reset}"></i>
59
+ </div>
60
+ `;
56
61
  }
57
62
 
58
63
  reset(): void {
@@ -1,9 +1,26 @@
1
- import { godown, htmlSlot, RouteTree, styles } from "@godown/element";
2
- import { css, type PropertyValueMap, type TemplateResult } from "lit";
1
+ import { RouteTree, godown, htmlSlot, omit, styles } from "@godown/element";
2
+ import { type PropertyValueMap, type TemplateResult, css } from "lit";
3
3
  import { property, state } from "lit/decorators.js";
4
4
 
5
5
  import { GlobalStyle } from "../core/global-style.js";
6
6
 
7
+ interface RouteState {
8
+ pathname: string;
9
+ params: Record<string, string>;
10
+ path: string;
11
+ }
12
+
13
+ interface RouteResult extends RouteState {
14
+ component: unknown;
15
+ }
16
+
17
+ interface RouteItem {
18
+ [key: PropertyKey]: unknown;
19
+ path: string;
20
+ render?: (state?: RouteState) => unknown;
21
+ component?: unknown;
22
+ }
23
+
7
24
  const protoName = "router";
8
25
 
9
26
  /**
@@ -13,7 +30,7 @@ const protoName = "router";
13
30
  *
14
31
  * It has two methods to collect routes.
15
32
  *
16
- * 1. From field `routes`, an array, each elements require "path" and "component".
33
+ * 1. From field `routes`, an array, each elements require "path".
17
34
  * 2. From child elements, which have the slot attribute for matching routes.
18
35
  *
19
36
  * If only the method 1 is used, set `type` to `"field"`.
@@ -29,20 +46,18 @@ const protoName = "router";
29
46
  * @category navigation
30
47
  */
31
48
  @godown(protoName)
32
- @styles(css`:host{display:contents;}`)
49
+ @styles(css`
50
+ :host {
51
+ display: contents;
52
+ }
53
+ `)
33
54
  class Router extends GlobalStyle {
34
55
  static routerInstances: Set<Router> = new Set<Router>();
35
56
 
36
57
  private __fieldRouteTree: RouteTree = new RouteTree();
37
58
  private __slottedRouteTree: RouteTree = new RouteTree();
38
- private __cacheRecord = new Map<string, ReturnType<typeof this.useRouter>>();
39
- private __routes: (
40
- & Record<string, any>
41
- & {
42
- path: string;
43
- component?: unknown;
44
- }
45
- )[];
59
+ private __cacheRecord = new Map<string, RouteResult>();
60
+ private __routes: RouteItem[];
46
61
 
47
62
  /**
48
63
  * Render result.
@@ -53,20 +68,24 @@ class Router extends GlobalStyle {
53
68
  /**
54
69
  * Dynamic parameters record.
55
70
  */
56
- @state()
57
- params: Record<string, string> = {};
71
+ get params(): Record<string, string> {
72
+ if (!this.path) {
73
+ return {};
74
+ }
75
+ return RouteTree.parseParams(this.pathname, this.path);
76
+ }
58
77
 
59
78
  /**
60
79
  * Value of matched path in routes.
61
80
  */
62
81
  @state()
63
- path: string;
82
+ path?: string;
64
83
 
65
84
  /**
66
85
  * Current pathname (equals to location.pathname).
67
86
  */
68
87
  @property()
69
- pathname: string;
88
+ pathname: string = location.pathname;
70
89
 
71
90
  /**
72
91
  * Rendered content when there is no match.
@@ -98,11 +117,7 @@ class Router extends GlobalStyle {
98
117
  this.collectFieldRoutes(value);
99
118
  }
100
119
 
101
- get routes(): (Record<string, any> & {
102
- path: string;
103
- render?: (ur: ReturnType<Router["useRouter"]>) => unknown;
104
- component?: unknown;
105
- })[] {
120
+ get routes(): RouteItem[] {
106
121
  return this.__routes;
107
122
  }
108
123
 
@@ -111,7 +126,6 @@ class Router extends GlobalStyle {
111
126
  }
112
127
 
113
128
  protected render(): unknown {
114
- this.params = {};
115
129
  if (this.cache) {
116
130
  const cached = this.__cacheRecord.get(this.pathname);
117
131
  if (cached) {
@@ -129,13 +143,12 @@ class Router extends GlobalStyle {
129
143
  default:
130
144
  this.component = this.fieldComponent() ?? this.slottedComponent();
131
145
  }
132
- return this.component ?? this.default ?? null;
146
+ return this.component ?? this.default;
133
147
  }
134
148
 
135
149
  connectedCallback(): void {
136
150
  super.connectedCallback();
137
151
  Router.routerInstances.add(this);
138
- this.pathname ??= location.pathname;
139
152
 
140
153
  if (this.type !== "field") {
141
154
  const mutationObserver = new MutationObserver(this.collectSlottedRoutes);
@@ -153,12 +166,7 @@ class Router extends GlobalStyle {
153
166
  Router.routerInstances.delete(this);
154
167
  }
155
168
 
156
- useRouter(): {
157
- pathname: string;
158
- params: Record<string, string>;
159
- path: string;
160
- component: unknown;
161
- } {
169
+ useRouter(): RouteResult {
162
170
  return {
163
171
  pathname: this.pathname,
164
172
  params: this.params,
@@ -170,12 +178,7 @@ class Router extends GlobalStyle {
170
178
  /**
171
179
  * Callback function when the route changes.
172
180
  */
173
- routeChangeCallback: (params: {
174
- pathname: string;
175
- params: Record<string, string>;
176
- path: string;
177
- component: unknown | TemplateResult;
178
- }, first: boolean) => void = null;
181
+ routeChangeCallback: (params: RouteResult, first: boolean) => void = null;
179
182
 
180
183
  protected updated(changedProperties: PropertyValueMap<this>): void {
181
184
  const shouldDispatch = changedProperties.has("pathname") || changedProperties.has("path");
@@ -201,14 +204,15 @@ class Router extends GlobalStyle {
201
204
  return null;
202
205
  }
203
206
 
204
- this.params = this.parseParams(this.path, this.pathname);
205
207
  const route = this.routes.find((r) => r.path === query);
206
208
  if (!route) {
207
209
  return null;
208
210
  }
209
211
 
212
+ console.log(this.path);
213
+
210
214
  if ("render" in route) {
211
- return route.render?.(this.useRouter()) || null;
215
+ return route.render?.(omit(this.useRouter(), "component")) || null;
212
216
  }
213
217
 
214
218
  return route.component;
@@ -230,7 +234,7 @@ class Router extends GlobalStyle {
230
234
  if (!this.path) {
231
235
  return null;
232
236
  }
233
- this.params = this.parseParams(query, this.pathname);
237
+
234
238
  return htmlSlot(this.path);
235
239
  }
236
240
 
@@ -240,7 +244,7 @@ class Router extends GlobalStyle {
240
244
  collectSlottedRoutes(): void {
241
245
  this.__slottedRouteTree = new RouteTree();
242
246
  this.clear();
243
- this._slottedNames.forEach(slotName => {
247
+ this._slottedNames.forEach((slotName) => {
244
248
  this.__slottedRouteTree.insert(slotName);
245
249
  });
246
250
  }
@@ -256,19 +260,15 @@ class Router extends GlobalStyle {
256
260
  });
257
261
  }
258
262
 
259
- parseParams(routeTemplate: string, path: string = this.pathname): Record<string, string> {
260
- return RouteTree.parseParams(path, routeTemplate);
261
- }
262
-
263
263
  static updateAll(): void {
264
264
  this.routerInstances.forEach((i) => {
265
265
  i.handlePopstate();
266
266
  });
267
267
  }
268
268
 
269
- handlePopstate = this.events.add(window, "popstate", () => {
269
+ handlePopstate: () => void = this.events.add(window, "popstate", () => {
270
270
  this.pathname = location.pathname;
271
- }) as () => void;
271
+ });
272
272
  }
273
273
 
274
274
  export default Router;
@@ -1,6 +1,6 @@
1
- import { attr, godown, type HandlerEvent, htmlSlot, part, styles } from "@godown/element";
1
+ import { type HandlerEvent, attr, godown, htmlSlot, part, styles } from "@godown/element";
2
2
  import svgCaretDown from "@godown/f7-icon/icons/chevron-down.js";
3
- import { css, html, nothing, type TemplateResult } from "lit";
3
+ import { type TemplateResult, css, html, nothing } from "lit";
4
4
  import { property, state } from "lit/decorators.js";
5
5
 
6
6
  import Input from "./input.js";
@@ -46,31 +46,29 @@ const protoName = "select";
46
46
  * @category input
47
47
  */
48
48
  @godown(protoName)
49
- @styles(
50
- css`
51
- [part=input] {
52
- text-overflow: ellipsis;
53
- }
49
+ @styles(css`
50
+ [part="input"] {
51
+ text-overflow: ellipsis;
52
+ }
54
53
 
55
- [part=content] {
56
- position: absolute;
57
- width: 100%;
58
- visibility: hidden;
59
- }
54
+ [part="content"] {
55
+ position: absolute;
56
+ width: 100%;
57
+ visibility: hidden;
58
+ }
60
59
 
61
- [direction=bottom] [part=content] {
62
- top: 100%;
63
- }
60
+ [direction="bottom"] [part="content"] {
61
+ top: 100%;
62
+ }
64
63
 
65
- [direction=top] [part=content] {
66
- bottom: 100%;
67
- }
64
+ [direction="top"] [part="content"] {
65
+ bottom: 100%;
66
+ }
68
67
 
69
- [visible] [part=content] {
70
- visibility: visible
71
- }
72
- `,
73
- )
68
+ [visible] [part="content"] {
69
+ visibility: visible;
70
+ }
71
+ `)
74
72
  class Select extends Input {
75
73
  // @ts-ignore
76
74
  value: string | string[];
@@ -99,41 +97,54 @@ class Select extends Input {
99
97
  protected lastChecked: HTMLElement;
100
98
  protected defaultText: string;
101
99
  protected defaultChecked: HTMLElement[];
102
- private _store: { value: string; text: string; }[] = [];
100
+ private _store: { value: string; text: string }[] = [];
103
101
 
104
102
  protected render(): TemplateResult<1> {
105
- return html`<div
106
- part="root"
107
- ${
108
- attr({
109
- ...this.observedRecord,
110
- direction: this.direction || this.autoDirection,
111
- })
112
- }
113
- class="input-field"
114
- >
115
- ${[
116
- this._renderPrefix(),
117
- html`<input
118
- part="input"
119
- dir="${this.dir || nothing}"
120
- id="${this.makeId}"
121
- .value="${this.text}"
122
- type="${this.type}"
123
- placeholder="${this.placeholder || nothing}"
124
- ?autofocus="${this.autofocus}"
125
- autocapitalize="${this.autocapitalize || nothing}"
126
- autocomplete="${this.autocomplete || nothing}"
127
- ?disabled="${this.disabled}"
128
- @focus="${this._handleFocus}"
129
- @input="${this._handleInput}"
130
- >`,
131
- html`<label for="${this.makeId}" part="suffix">
132
- <i part="icon">${svgCaretDown()}</i>
133
- </label>`,
134
- html`<label for="${this.makeId}" part="content"> ${htmlSlot()} </label>`,
135
- ]}
136
- </div>`;
103
+ return html`
104
+ <div
105
+ part="root"
106
+ ${attr({
107
+ ...this.observedRecord,
108
+ direction: this.direction || this.autoDirection,
109
+ })}
110
+ class="input-field"
111
+ >
112
+ ${[
113
+ this._renderPrefix(),
114
+ html`
115
+ <input
116
+ part="input"
117
+ type="${this.type}"
118
+ .value="${this.text}"
119
+ ?autofocus="${this.autofocus}"
120
+ ?disabled="${this.disabled}"
121
+ autocapitalize="${this.autocapitalize || nothing}"
122
+ autocomplete="${this.autocomplete || nothing}"
123
+ placeholder="${this.placeholder || nothing}"
124
+ id="${this.makeId}"
125
+ @focus="${this._handleFocus}"
126
+ @input="${this._handleInput}"
127
+ />
128
+ `,
129
+ html`
130
+ <label
131
+ for="${this.makeId}"
132
+ part="suffix"
133
+ >
134
+ <i part="icon">${svgCaretDown()}</i>
135
+ </label>
136
+ `,
137
+ html`
138
+ <label
139
+ for="${this.makeId}"
140
+ part="content"
141
+ >
142
+ ${htmlSlot()}
143
+ </label>
144
+ `,
145
+ ]}
146
+ </div>
147
+ `;
137
148
  }
138
149
 
139
150
  protected _handleFocus(): void {
@@ -177,11 +188,7 @@ class Select extends Input {
177
188
  protected _connectedInit(): void {
178
189
  if (!this.value) {
179
190
  const checked = [...this.querySelectorAll<HTMLElement>("[checked]")];
180
- const list = this.multiple
181
- ? checked
182
- : checked.length
183
- ? [this.lastChecked = checked[0]]
184
- : [];
191
+ const list = this.multiple ? checked : checked.length ? [(this.lastChecked = checked[0])] : [];
185
192
  list.forEach((element: HTMLElement) => {
186
193
  const operation = this.select(this.optionValue(element), element.textContent);
187
194
  updateChecked(element, operation);
@@ -199,23 +206,23 @@ class Select extends Input {
199
206
  reset(): void {
200
207
  this.value = this.default;
201
208
  this.text = this.defaultText;
202
- this.querySelectorAll<HTMLElement>("[checked]").forEach(element => updateChecked(element, 0));
203
- this.defaultChecked.forEach(element => updateChecked(element, 1));
209
+ this.querySelectorAll<HTMLElement>("[checked]").forEach((element) => updateChecked(element, 0));
210
+ this.defaultChecked.forEach((element) => updateChecked(element, 1));
204
211
  }
205
212
 
206
213
  select(value: string, text?: string): 0 | 1 {
207
214
  text ||= value;
208
215
  let operation: 0 | 1 = 0;
209
216
  if (this.multiple) {
210
- const i = this._store.findIndex(s => s.value === value);
217
+ const i = this._store.findIndex((s) => s.value === value);
211
218
  if (i > -1) {
212
219
  this._store.splice(i, 1);
213
220
  } else {
214
221
  this._store.push({ value, text });
215
222
  operation = 1;
216
223
  }
217
- this.value = this._store.map(s => s.value);
218
- this.text = this._store.map(s => s.text).join(", ");
224
+ this.value = this._store.map((s) => s.value);
225
+ this.text = this._store.map((s) => s.text).join(", ");
219
226
  } else {
220
227
  if (this.value === value) {
221
228
  this.value = "";
@@ -236,15 +243,12 @@ class Select extends Input {
236
243
  [...this.children].forEach((element: HTMLElement) => {
237
244
  this.filterCallback(
238
245
  element,
239
- !query
240
- || contain(this.optionValue(element), query)
241
- || contain(element.textContent, query),
246
+ !query || contain(this.optionValue(element), query) || contain(element.textContent, query),
242
247
  query,
243
248
  );
244
249
  });
245
250
  }
246
251
 
247
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
248
252
  filterCallback(element: HTMLElement, match: boolean, query: string): void {
249
253
  element.style.display = match ? "" : "none";
250
254
  }