@htmlplus/element 3.1.3 → 3.2.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.
@@ -1204,6 +1204,22 @@ const styles$1 = (input) => {
1204
1204
  .join('; ');
1205
1205
  };
1206
1206
 
1207
+ const toCSSColor = (input) => {
1208
+ return isCSSColor(input) ? input : undefined;
1209
+ };
1210
+
1211
+ const toCSSUnit = (input) => {
1212
+ if (input == null || input === '') {
1213
+ return;
1214
+ }
1215
+ if (typeof input === 'number' || !isNaN(Number(input))) {
1216
+ return `${input}px`;
1217
+ }
1218
+ if (/^\d+(\.\d+)?(px|pt|cm|mm|in|em|rem|%|vw|vh)$/.test(input)) {
1219
+ return input;
1220
+ }
1221
+ };
1222
+
1207
1223
  function toDecorator(util, ...parameters) {
1208
1224
  return function (target, key) {
1209
1225
  defineProperty(target, key, {
@@ -1755,6 +1771,70 @@ function State() {
1755
1771
  };
1756
1772
  }
1757
1773
 
1774
+ // TODO: check the logic
1775
+ function Style() {
1776
+ return function (target, key) {
1777
+ const LAST = Symbol();
1778
+ const SHEET = Symbol();
1779
+ appendToMethod(target, LIFECYCLE_UPDATED, function () {
1780
+ let sheet = this[SHEET];
1781
+ let value = this[key];
1782
+ const update = (value) => (result) => {
1783
+ if (value && value !== this[LAST])
1784
+ return;
1785
+ sheet.replaceSync(toCssString(result));
1786
+ this[LAST] = undefined;
1787
+ };
1788
+ if (!sheet) {
1789
+ sheet = new CSSStyleSheet();
1790
+ this[SHEET] = sheet;
1791
+ shadowRoot(this)?.adoptedStyleSheets.push(sheet);
1792
+ }
1793
+ if (typeof value === 'function') {
1794
+ value = value.call(this);
1795
+ }
1796
+ if (value instanceof Promise) {
1797
+ value
1798
+ .then(update(this[LAST] = value))
1799
+ .catch((error) => {
1800
+ throw new Error('TODO', { cause: error });
1801
+ });
1802
+ }
1803
+ else {
1804
+ update()(value);
1805
+ }
1806
+ });
1807
+ };
1808
+ }
1809
+ const toCssString = (input, parent) => {
1810
+ if (typeof input == 'string') {
1811
+ return input.trim();
1812
+ }
1813
+ if (Array.isArray(input)) {
1814
+ return input
1815
+ .map((item) => toCssString(item, parent))
1816
+ .filter(Boolean)
1817
+ .join('\n');
1818
+ }
1819
+ if (typeof input != 'object')
1820
+ return '';
1821
+ let result = '';
1822
+ for (const key of Object.keys(input)) {
1823
+ const value = input[key];
1824
+ const ignore = [null, undefined, false].includes(value);
1825
+ if (ignore)
1826
+ continue;
1827
+ const cssKey = key.replace(/[A-Z]/g, (match) => `-${match.toLowerCase()}`);
1828
+ if (typeof value === 'object') {
1829
+ result += `${cssKey} {${toCssString(value, cssKey)}}`;
1830
+ }
1831
+ else {
1832
+ result += `${cssKey}: ${value};`;
1833
+ }
1834
+ }
1835
+ return parent ? result : `:host {${result}}`;
1836
+ };
1837
+
1758
1838
  /**
1759
1839
  * Monitors `@Property` and `@State` to detect changes.
1760
1840
  * The decorated method will be called after any changes,
@@ -1789,4 +1869,4 @@ const attributes = attributes$2;
1789
1869
  const html = html$1;
1790
1870
  const styles = styles$1;
1791
1871
 
1792
- export { Bind as B, Consumer as C, Direction as D, Element as E, Host as H, IsRTL as I, Listen as L, Method as M, Provider as P, Query as Q, Slots as S, Watch as W, dispatch as a, isRTL as b, classes as c, direction as d, queryAll as e, off as f, getConfig as g, host as h, isCSSColor as i, setConfig as j, Event as k, Property as l, QueryAll as m, State as n, on as o, attributes as p, query as q, html as r, slots as s, toUnit as t, styles as u };
1872
+ export { Bind as B, Consumer as C, Direction as D, Element as E, Host as H, IsRTL as I, Listen as L, Method as M, Provider as P, Query as Q, Slots as S, Watch as W, dispatch as a, toCSSUnit as b, classes as c, direction as d, isRTL as e, queryAll as f, getConfig as g, host as h, isCSSColor as i, off as j, toUnit as k, setConfig as l, Event as m, Property as n, on as o, QueryAll as p, query as q, State as r, slots as s, toCSSColor as t, Style as u, attributes as v, html as w, styles as x };
@@ -188,6 +188,8 @@ declare function Slots$1(): (target: HTMLPlusElement, key: PropertyKey) => void;
188
188
  */
189
189
  declare function State(): (target: HTMLPlusElement, key: PropertyKey) => void;
190
190
 
191
+ declare function Style(): (target: HTMLPlusElement, key: PropertyKey) => void;
192
+
191
193
  /**
192
194
  * Monitors `@Property` and `@State` to detect changes.
193
195
  * The decorated method will be called after any changes,
@@ -297,6 +299,10 @@ type Slots = {
297
299
  */
298
300
  declare const slots: (target: HTMLElement | HTMLPlusElement) => Slots;
299
301
 
302
+ declare const toCSSColor: (input: string) => string | undefined;
303
+
304
+ declare const toCSSUnit: (input?: number | string | null) => string | undefined;
305
+
300
306
  /**
301
307
  * Converts a value to a unit.
302
308
  */
@@ -306,4 +312,4 @@ declare const attributes: any;
306
312
  declare const html: any;
307
313
  declare const styles: any;
308
314
 
309
- export { styles as A, Bind as B, type Config as C, Direction as D, Element$1 as E, Host as H, IsRTL as I, type ListenOptions as L, Method as M, Provider as P, Query as Q, Slots$1 as S, Watch as W, dispatch as a, isRTL as b, classes as c, direction as d, queryAll as e, off as f, getConfig as g, host as h, isCSSColor as i, setConfig as j, type ConfigOptions as k, Consumer as l, type EventEmitter as m, type EventOptions as n, on as o, Event as p, query as q, Listen as r, slots as s, toUnit as t, type PropertyOptions as u, Property as v, QueryAll as w, State as x, attributes as y, html as z };
315
+ export { Style as A, Bind as B, type Config as C, Direction as D, Element$1 as E, attributes as F, html as G, Host as H, IsRTL as I, styles as J, type ListenOptions as L, Method as M, Provider as P, Query as Q, Slots$1 as S, Watch as W, dispatch as a, toCSSUnit as b, classes as c, direction as d, isRTL as e, queryAll as f, getConfig as g, host as h, isCSSColor as i, off as j, toUnit as k, setConfig as l, type ConfigOptions as m, Consumer as n, on as o, type EventEmitter as p, query as q, type EventOptions as r, slots as s, toCSSColor as t, Event as u, Listen as v, type PropertyOptions as w, Property as x, QueryAll as y, State as z };
package/dist/client.d.ts CHANGED
@@ -1 +1 @@
1
- export { B as Bind, C as Config, k as ConfigOptions, l as Consumer, D as Direction, E as Element, p as Event, m as EventEmitter, n as EventOptions, H as Host, I as IsRTL, r as Listen, L as ListenOptions, M as Method, v as Property, u as PropertyOptions, P as Provider, Q as Query, w as QueryAll, S as Slots, x as State, W as Watch, c as classes, d as direction, a as dispatch, g as getConfig, h as host, i as isCSSColor, b as isRTL, f as off, o as on, q as query, e as queryAll, j as setConfig, s as slots, t as toUnit } from './client-5FqNUiuz.js';
1
+ export { B as Bind, C as Config, m as ConfigOptions, n as Consumer, D as Direction, E as Element, u as Event, p as EventEmitter, r as EventOptions, H as Host, I as IsRTL, v as Listen, L as ListenOptions, M as Method, x as Property, w as PropertyOptions, P as Provider, Q as Query, y as QueryAll, S as Slots, z as State, A as Style, W as Watch, c as classes, d as direction, a as dispatch, g as getConfig, h as host, i as isCSSColor, e as isRTL, j as off, o as on, q as query, f as queryAll, l as setConfig, s as slots, t as toCSSColor, b as toCSSUnit, k as toUnit } from './client-DdIyknz4.js';
package/dist/client.js CHANGED
@@ -1,3 +1,3 @@
1
- export { B as Bind, C as Consumer, D as Direction, E as Element, k as Event, H as Host, I as IsRTL, L as Listen, M as Method, l as Property, P as Provider, Q as Query, m as QueryAll, S as Slots, n as State, W as Watch, c as classes, d as direction, a as dispatch, g as getConfig, h as host, i as isCSSColor, b as isRTL, f as off, o as on, q as query, e as queryAll, j as setConfig, s as slots, t as toUnit } from './client-BsD9JZY4.js';
1
+ export { B as Bind, C as Consumer, D as Direction, E as Element, m as Event, H as Host, I as IsRTL, L as Listen, M as Method, n as Property, P as Provider, Q as Query, p as QueryAll, S as Slots, r as State, u as Style, W as Watch, c as classes, d as direction, a as dispatch, g as getConfig, h as host, i as isCSSColor, e as isRTL, j as off, o as on, q as query, f as queryAll, l as setConfig, s as slots, t as toCSSColor, b as toCSSUnit, k as toUnit } from './client-Cu3jtWIP.js';
2
2
  import 'change-case';
3
3
  import './constants.js';
@@ -1 +1 @@
1
- export { y as attributes, z as html, A as styles } from './client-5FqNUiuz.js';
1
+ export { F as attributes, G as html, J as styles } from './client-DdIyknz4.js';
package/dist/internal.js CHANGED
@@ -1,3 +1,3 @@
1
- export { p as attributes, r as html, u as styles } from './client-BsD9JZY4.js';
1
+ export { v as attributes, w as html, x as styles } from './client-Cu3jtWIP.js';
2
2
  import 'change-case';
3
3
  import './constants.js';
@@ -8,7 +8,7 @@ import template from '@babel/template';
8
8
  import { pascalCase, kebabCase, camelCase, capitalCase } from 'change-case';
9
9
  import ora from 'ora';
10
10
  import path, { join, resolve, dirname } from 'node:path';
11
- import { KEY, COMMENT_AUTO_ADDED, DECORATOR_PROPERTY, STATIC_TAG, UTILS_STYLES_LOCAL, UTILS_PATH, DECORATOR_PROPERTY_TYPE, UTILS_STYLES_IMPORTED, ELEMENT_HOST_NAME, UTILS_HTML_IMPORTED, UTILS_HTML_LOCAL, TYPE_OBJECT, TYPE_ARRAY, TYPE_NULL, TYPE_STRING, TYPE_ENUM, TYPE_NUMBER, TYPE_DATE, TYPE_BOOLEAN, UTILS_ATTRIBUTES_IMPORTED, UTILS_ATTRIBUTES_LOCAL, DECORATOR_CSS_VARIABLE, DECORATOR_EVENT, DECORATOR_METHOD, DECORATOR_STATE, STATIC_STYLE, STYLE_IMPORTED, PACKAGE_NAME, DECORATOR_ELEMENT } from './constants.js';
11
+ import { KEY, COMMENT_AUTO_ADDED, DECORATOR_PROPERTY, STATIC_TAG, UTILS_STYLES_LOCAL, UTILS_PATH, DECORATOR_PROPERTY_TYPE, UTILS_STYLES_IMPORTED, ELEMENT_HOST_NAME, UTILS_HTML_IMPORTED, UTILS_HTML_LOCAL, TYPE_OBJECT, TYPE_NULL, TYPE_ARRAY, TYPE_STRING, TYPE_ENUM, TYPE_NUMBER, TYPE_DATE, TYPE_BOOLEAN, UTILS_ATTRIBUTES_IMPORTED, UTILS_ATTRIBUTES_LOCAL, DECORATOR_CSS_VARIABLE, DECORATOR_EVENT, DECORATOR_METHOD, DECORATOR_STATE, STATIC_STYLE, STYLE_IMPORTED, PACKAGE_NAME, DECORATOR_ELEMENT } from './constants.js';
12
12
 
13
13
  const logger = ora({
14
14
  color: 'yellow'
@@ -512,7 +512,9 @@ const customElement = (options) => {
512
512
  return attribute.type == 'JSXSpreadAttribute';
513
513
  });
514
514
  if (hasSpreadAttribute) {
515
- parts.push(' ', TODO(t.identifier('TODO'), attributes));
515
+ parts.push(' ', 'ref=', t.arrowFunctionExpression([
516
+ t.identifier('$element')
517
+ ], TODO(t.identifier('$element'), attributes)));
516
518
  }
517
519
  else {
518
520
  for (const attribute of attributes) {
@@ -624,7 +626,9 @@ const customElement = (options) => {
624
626
  case 'TSStringKeyword':
625
627
  type |= TYPE_STRING;
626
628
  break;
629
+ case 'Array':
627
630
  case 'TSArrayType':
631
+ case 'TSTupleType':
628
632
  type |= TYPE_ARRAY;
629
633
  break;
630
634
  case 'TSLiteralType':
@@ -635,12 +639,7 @@ const customElement = (options) => {
635
639
  break;
636
640
  case 'Object':
637
641
  case 'TSObjectKeyword':
638
- type |= TYPE_OBJECT;
639
- break;
640
- case 'Array':
641
- case 'TSTupleType':
642
- type |= TYPE_ARRAY;
643
- break;
642
+ case 'TSMappedType':
644
643
  case 'TSTypeLiteral':
645
644
  type |= TYPE_OBJECT;
646
645
  break;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@htmlplus/element",
3
- "version": "3.1.3",
3
+ "version": "3.2.0",
4
4
  "license": "MIT",
5
5
  "sideEffects": false,
6
6
  "author": "Masood Abdolian <m.abdolian@gmail.com>",