mtrl-addons 0.1.0 → 0.1.1

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 (92) hide show
  1. package/.cursorrules +117 -0
  2. package/AI.md +241 -0
  3. package/build.js +170 -0
  4. package/bun.lock +792 -0
  5. package/index.ts +7 -0
  6. package/package.json +10 -17
  7. package/scripts/analyze-orphaned-functions.ts +387 -0
  8. package/src/components/index.ts +45 -0
  9. package/src/components/list/api.ts +314 -0
  10. package/src/components/list/config.ts +352 -0
  11. package/src/components/list/constants.ts +56 -0
  12. package/src/components/list/features/api.ts +428 -0
  13. package/src/components/list/features/index.ts +31 -0
  14. package/src/components/list/features/list-manager.ts +502 -0
  15. package/src/components/list/features.ts +112 -0
  16. package/src/components/list/index.ts +39 -0
  17. package/src/components/list/list.ts +234 -0
  18. package/src/components/list/types.ts +513 -0
  19. package/src/core/collection/base-collection.ts +100 -0
  20. package/src/core/collection/collection-composer.ts +178 -0
  21. package/src/core/collection/collection.ts +745 -0
  22. package/src/core/collection/constants.ts +172 -0
  23. package/src/core/collection/events.ts +428 -0
  24. package/src/core/collection/features/api/loading.ts +279 -0
  25. package/src/core/collection/features/operations/data-operations.ts +147 -0
  26. package/src/core/collection/index.ts +104 -0
  27. package/src/core/collection/state.ts +497 -0
  28. package/src/core/collection/types.ts +404 -0
  29. package/src/core/compose/features/collection.ts +119 -0
  30. package/src/core/compose/features/index.ts +39 -0
  31. package/src/core/compose/features/performance.ts +161 -0
  32. package/src/core/compose/features/selection.ts +213 -0
  33. package/src/core/compose/features/styling.ts +108 -0
  34. package/src/core/compose/index.ts +31 -0
  35. package/src/core/index.ts +167 -0
  36. package/src/core/layout/config.ts +102 -0
  37. package/src/core/layout/index.ts +168 -0
  38. package/src/core/layout/jsx.ts +174 -0
  39. package/src/core/layout/schema.ts +963 -0
  40. package/src/core/layout/types.ts +92 -0
  41. package/src/core/list-manager/api.ts +599 -0
  42. package/src/core/list-manager/config.ts +593 -0
  43. package/src/core/list-manager/constants.ts +268 -0
  44. package/src/core/list-manager/features/api.ts +58 -0
  45. package/src/core/list-manager/features/collection/collection.ts +705 -0
  46. package/src/core/list-manager/features/collection/index.ts +17 -0
  47. package/src/core/list-manager/features/viewport/constants.ts +42 -0
  48. package/src/core/list-manager/features/viewport/index.ts +16 -0
  49. package/src/core/list-manager/features/viewport/item-size.ts +274 -0
  50. package/src/core/list-manager/features/viewport/loading.ts +263 -0
  51. package/src/core/list-manager/features/viewport/placeholders.ts +281 -0
  52. package/src/core/list-manager/features/viewport/rendering.ts +575 -0
  53. package/src/core/list-manager/features/viewport/scrollbar.ts +495 -0
  54. package/src/core/list-manager/features/viewport/scrolling.ts +795 -0
  55. package/src/core/list-manager/features/viewport/template.ts +220 -0
  56. package/src/core/list-manager/features/viewport/viewport.ts +654 -0
  57. package/src/core/list-manager/features/viewport/virtual.ts +309 -0
  58. package/src/core/list-manager/index.ts +279 -0
  59. package/src/core/list-manager/list-manager.ts +206 -0
  60. package/src/core/list-manager/types.ts +439 -0
  61. package/src/core/list-manager/utils/calculations.ts +290 -0
  62. package/src/core/list-manager/utils/range-calculator.ts +349 -0
  63. package/src/core/list-manager/utils/speed-tracker.ts +273 -0
  64. package/src/index.ts +17 -0
  65. package/src/styles/components/_list.scss +244 -0
  66. package/src/styles/index.scss +12 -0
  67. package/src/types/mtrl.d.ts +6 -0
  68. package/test/benchmarks/layout/advanced.test.ts +656 -0
  69. package/test/benchmarks/layout/comparison.test.ts +519 -0
  70. package/test/benchmarks/layout/performance-comparison.test.ts +274 -0
  71. package/test/benchmarks/layout/real-components.test.ts +733 -0
  72. package/test/benchmarks/layout/simple.test.ts +321 -0
  73. package/test/benchmarks/layout/stress.test.ts +990 -0
  74. package/test/collection/basic.test.ts +304 -0
  75. package/test/components/list.test.ts +256 -0
  76. package/test/core/collection/collection.test.ts +394 -0
  77. package/test/core/collection/failed-ranges.test.ts +270 -0
  78. package/test/core/compose/features.test.ts +183 -0
  79. package/test/core/layout/layout.test.ts +201 -0
  80. package/test/core/list-manager/features/collection.test.ts +704 -0
  81. package/test/core/list-manager/features/viewport.test.ts +698 -0
  82. package/test/core/list-manager/list-manager.test.ts +593 -0
  83. package/test/core/list-manager/utils/calculations.test.ts +433 -0
  84. package/test/core/list-manager/utils/range-calculator.test.ts +569 -0
  85. package/test/core/list-manager/utils/speed-tracker.test.ts +530 -0
  86. package/test/utils/dom-helpers.ts +275 -0
  87. package/test/utils/performance-helpers.ts +392 -0
  88. package/tsconfig.build.json +14 -0
  89. package/tsconfig.json +20 -0
  90. package/dist/index.d.ts +0 -5
  91. package/dist/index.js +0 -38
  92. package/dist/index.mjs +0 -8
@@ -0,0 +1,220 @@
1
+ /**
2
+ * Viewport Template System
3
+ * Handles all template-related functionality for item rendering
4
+ * Part of the viewport feature as rendering is a display concern
5
+ */
6
+
7
+ import { PLACEHOLDER } from "../../constants";
8
+ import { VIEWPORT_CONSTANTS } from "./constants";
9
+
10
+ /**
11
+ * Gets default item template if none provided
12
+ */
13
+ export const getDefaultTemplate = <T = any>(): ((
14
+ item: T,
15
+ index: number
16
+ ) => string) => {
17
+ return (item: T, index: number) => {
18
+ // Check if this is a placeholder item
19
+ const isPlaceholder =
20
+ item &&
21
+ typeof item === "object" &&
22
+ (item as any)[PLACEHOLDER.PLACEHOLDER_FLAG];
23
+ const placeholderClass = isPlaceholder
24
+ ? ` ${VIEWPORT_CONSTANTS.PLACEHOLDER.CSS_CLASS}`
25
+ : "";
26
+
27
+ if (typeof item === "string") {
28
+ return `<div class="mtrl-list-item__content${placeholderClass}">${item}</div>`;
29
+ }
30
+
31
+ if (typeof item === "object" && item !== null) {
32
+ // Try common properties
33
+ const obj = item as any;
34
+ const text =
35
+ obj.text || obj.title || obj.name || obj.label || String(item);
36
+ const subtitle = obj.subtitle || obj.description || obj.secondary;
37
+
38
+ return `
39
+ <div class="mtrl-list-item__content${placeholderClass}">
40
+ <div class="mtrl-list-item__primary">${text}</div>
41
+ ${
42
+ subtitle
43
+ ? `<div class="mtrl-list-item__secondary">${subtitle}</div>`
44
+ : ""
45
+ }
46
+ </div>
47
+ `;
48
+ }
49
+
50
+ return `<div class="mtrl-list-item__content${placeholderClass}">${String(
51
+ item
52
+ )}</div>`;
53
+ };
54
+ };
55
+
56
+ /**
57
+ * Gets loading template
58
+ */
59
+ export const getLoadingTemplate = (): string => {
60
+ return `
61
+ <div class="mtrl-list-item mtrl-list-item--loading">
62
+ <div class="mtrl-list-item__content">
63
+ <div class="mtrl-list-item__primary">Loading...</div>
64
+ </div>
65
+ </div>
66
+ `;
67
+ };
68
+
69
+ /**
70
+ * Gets empty state template
71
+ */
72
+ export const getEmptyTemplate = (): string => {
73
+ return `
74
+ <div class="mtrl-list-item mtrl-list-item--empty">
75
+ <div class="mtrl-list-item__content">
76
+ <div class="mtrl-list-item__primary">No items available</div>
77
+ </div>
78
+ </div>
79
+ `;
80
+ };
81
+
82
+ /**
83
+ * Gets error template
84
+ */
85
+ export const getErrorTemplate = (error: Error): string => {
86
+ return `
87
+ <div class="mtrl-list-item mtrl-list-item--error">
88
+ <div class="mtrl-list-item__content">
89
+ <div class="mtrl-list-item__primary">Error: ${error.message}</div>
90
+ </div>
91
+ </div>
92
+ `;
93
+ };
94
+
95
+ /**
96
+ * Converts renderItem object structure to template function
97
+ * Handles object-based template definitions with variable substitution
98
+ */
99
+ export const convertRenderItemToTemplate = (
100
+ renderItem: any
101
+ ): ((item: any, index: number) => HTMLElement) => {
102
+ if (!renderItem || typeof renderItem !== "object") {
103
+ return getDefaultTemplate();
104
+ }
105
+
106
+ return (item: any, index: number): HTMLElement => {
107
+ return createElementFromTemplate(renderItem, item, index);
108
+ };
109
+ };
110
+
111
+ /**
112
+ * Creates DOM element from template object structure
113
+ */
114
+ export const createElementFromTemplate = (
115
+ template: any,
116
+ item: any,
117
+ index: number
118
+ ): HTMLElement => {
119
+ const {
120
+ tag = "div",
121
+ className,
122
+ attributes = {},
123
+ children = [],
124
+ textContent,
125
+ style,
126
+ } = template;
127
+
128
+ // Create the element
129
+ const element = document.createElement(tag);
130
+
131
+ // Apply className
132
+ if (className) {
133
+ element.className = substituteVariables(className, item, index);
134
+ }
135
+
136
+ // Apply attributes
137
+ for (const [key, value] of Object.entries(attributes)) {
138
+ if (typeof value === "string") {
139
+ element.setAttribute(key, substituteVariables(value, item, index));
140
+ } else {
141
+ element.setAttribute(key, String(value));
142
+ }
143
+ }
144
+
145
+ // Apply textContent
146
+ if (textContent) {
147
+ element.textContent = substituteVariables(textContent, item, index);
148
+ }
149
+
150
+ // Apply inline styles
151
+ if (style && typeof style === "object") {
152
+ for (const [key, value] of Object.entries(style)) {
153
+ if (typeof value === "string") {
154
+ (element.style as any)[key] = substituteVariables(value, item, index);
155
+ } else {
156
+ (element.style as any)[key] = String(value);
157
+ }
158
+ }
159
+ }
160
+
161
+ // Process children recursively
162
+ if (Array.isArray(children)) {
163
+ children.forEach((child) => {
164
+ if (child && typeof child === "object") {
165
+ const childElement = createElementFromTemplate(child, item, index);
166
+ element.appendChild(childElement);
167
+ }
168
+ });
169
+ }
170
+
171
+ return element;
172
+ };
173
+
174
+ /**
175
+ * Substitutes template variables like {{name}} with actual values
176
+ */
177
+ export const substituteVariables = (
178
+ template: string,
179
+ item: any,
180
+ index: number
181
+ ): string => {
182
+ if (!template || typeof template !== "string") {
183
+ return String(template || "");
184
+ }
185
+
186
+ return template.replace(/\{\{([^}]+)\}\}/g, (match, expression) => {
187
+ try {
188
+ // Create a simple context for evaluation
189
+ const context = { ...item, index, item };
190
+
191
+ // Handle simple property access (e.g., {{name}}, {{user.email}})
192
+ const value = expression.split(".").reduce((obj: any, prop: string) => {
193
+ return obj && obj[prop.trim()];
194
+ }, context);
195
+
196
+ // Handle fallback expressions (e.g., {{name || id}})
197
+ if (value === undefined && expression.includes("||")) {
198
+ const parts = expression.split("||").map((part) => part.trim());
199
+ for (const part of parts) {
200
+ const fallbackValue = part
201
+ .split(".")
202
+ .reduce((obj: any, prop: string) => {
203
+ return obj && obj[prop.trim()];
204
+ }, context);
205
+ if (fallbackValue !== undefined) {
206
+ return String(fallbackValue);
207
+ }
208
+ }
209
+ }
210
+
211
+ return value !== undefined ? String(value) : "";
212
+ } catch (error) {
213
+ console.warn(
214
+ `Template variable substitution failed for "${expression}":`,
215
+ error
216
+ );
217
+ return match; // Return original if substitution fails
218
+ }
219
+ });
220
+ };