mtrl 0.2.0 → 0.2.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.
@@ -72,6 +72,13 @@ export interface CreateElementOptions {
72
72
  [key: string]: any;
73
73
  }
74
74
 
75
+ /**
76
+ * Event handler storage to facilitate cleanup
77
+ */
78
+ export interface EventHandlerStorage {
79
+ [eventName: string]: EventListener;
80
+ }
81
+
75
82
  /**
76
83
  * Creates a DOM element with the specified options
77
84
  *
@@ -117,21 +124,57 @@ export const createElement = (options: CreateElementOptions = {}): HTMLElement =
117
124
  // Handle all other attributes
118
125
  const allAttrs = { ...attrs, ...rest };
119
126
  Object.entries(allAttrs).forEach(([key, value]) => {
120
- if (value != null) element.setAttribute(key, value);
127
+ if (value != null) element.setAttribute(key, String(value));
121
128
  });
122
129
 
123
- // Handle event forwarding if context has emit method
124
- if (context?.emit && forwardEvents) {
125
- Object.entries(forwardEvents).forEach(([nativeEvent, eventConfig]) => {
126
- const shouldForward = typeof eventConfig === 'function'
127
- ? eventConfig
128
- : () => true;
130
+ // Initialize event handler storage if not present
131
+ if (!element.__eventHandlers) {
132
+ element.__eventHandlers = {};
133
+ }
129
134
 
130
- element.addEventListener(nativeEvent, (event) => {
131
- if (shouldForward({ ...context, element }, event)) {
132
- context.emit(nativeEvent, { event });
135
+ // Handle event forwarding if context has emit method or is a component with on method
136
+ if (forwardEvents && (context?.emit || context?.on)) {
137
+ Object.entries(forwardEvents).forEach(([nativeEvent, eventConfig]) => {
138
+ // Create a wrapper handler function to evaluate condition and forward event
139
+ const handler = (event: Event) => {
140
+ // Determine if the event should be forwarded
141
+ let shouldForward = true;
142
+
143
+ if (typeof eventConfig === 'function') {
144
+ try {
145
+ // If it's a function, call with component context and event
146
+ shouldForward = eventConfig({ ...context, element }, event);
147
+ } catch (error) {
148
+ console.warn(`Error in event condition for ${nativeEvent}:`, error);
149
+ shouldForward = false;
150
+ }
151
+ } else {
152
+ // If it's a boolean, use directly
153
+ shouldForward = Boolean(eventConfig);
133
154
  }
134
- });
155
+
156
+ // Forward the event if condition passes
157
+ if (shouldForward) {
158
+ if (context.emit) {
159
+ context.emit(nativeEvent, { event, element, originalEvent: event });
160
+ } else if (context.on) {
161
+ // This is a component with on method but no emit method
162
+ // Dispatch a custom event that can be listened to
163
+ const customEvent = new CustomEvent(nativeEvent, {
164
+ detail: { event, element, originalEvent: event },
165
+ bubbles: true,
166
+ cancelable: true
167
+ });
168
+ element.dispatchEvent(customEvent);
169
+ }
170
+ }
171
+ };
172
+
173
+ // Store the handler for future removal
174
+ element.__eventHandlers[nativeEvent] = handler;
175
+
176
+ // Add the actual event listener
177
+ element.addEventListener(nativeEvent, handler);
135
178
  });
136
179
  }
137
180
 
@@ -147,6 +190,19 @@ export const createElement = (options: CreateElementOptions = {}): HTMLElement =
147
190
  return element;
148
191
  };
149
192
 
193
+ /**
194
+ * Removes event handlers from an element
195
+ * @param element - Element to cleanup
196
+ */
197
+ export const removeEventHandlers = (element: HTMLElement): void => {
198
+ if (element.__eventHandlers) {
199
+ Object.entries(element.__eventHandlers).forEach(([eventName, handler]) => {
200
+ element.removeEventListener(eventName, handler);
201
+ });
202
+ delete element.__eventHandlers;
203
+ }
204
+ };
205
+
150
206
  /**
151
207
  * Higher-order function to add attributes to an element
152
208
  * @param {Record<string, any>} attrs - Attributes to add
@@ -185,4 +241,11 @@ export const withContent = (content: Node | string) =>
185
241
  element.textContent = content;
186
242
  }
187
243
  return element;
188
- };
244
+ };
245
+
246
+ // Extend HTMLElement interface to add eventHandlers property
247
+ declare global {
248
+ interface HTMLElement {
249
+ __eventHandlers?: EventHandlerStorage;
250
+ }
251
+ }
@@ -180,6 +180,18 @@ $z-index: (
180
180
  ) !default;
181
181
 
182
182
  // Component-specific tokens
183
+ @function card($key) {
184
+ $card: (
185
+ 'width': 344px,
186
+ 'width-small': 344px,
187
+ 'width-medium': 480px,
188
+ 'width-large': 624px,
189
+ 'border-radius': 12px,
190
+ 'padding': 16px
191
+ );
192
+
193
+ @return map-get($card, $key);
194
+ }
183
195
  $button: (
184
196
  'height': 40px,
185
197
  'min-width': 64px,