grainjs 1.0.2 → 1.1.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 (138) hide show
  1. package/README.md +23 -71
  2. package/dist/cjs/index.js +5 -1
  3. package/dist/cjs/index.js.map +1 -1
  4. package/dist/cjs/lib/PriorityQueue.d.ts +1 -1
  5. package/dist/cjs/lib/_computed_queue.js +3 -3
  6. package/dist/cjs/lib/_computed_queue.js.map +1 -1
  7. package/dist/cjs/lib/binding.d.ts +11 -4
  8. package/dist/cjs/lib/binding.js +5 -5
  9. package/dist/cjs/lib/binding.js.map +1 -1
  10. package/dist/cjs/lib/computed.d.ts +49 -28
  11. package/dist/cjs/lib/computed.js +38 -52
  12. package/dist/cjs/lib/computed.js.map +1 -1
  13. package/dist/cjs/lib/dispose.d.ts +109 -96
  14. package/dist/cjs/lib/dispose.js +106 -80
  15. package/dist/cjs/lib/dispose.js.map +1 -1
  16. package/dist/cjs/lib/dom.d.ts +38 -18
  17. package/dist/cjs/lib/dom.js +44 -20
  18. package/dist/cjs/lib/dom.js.map +1 -1
  19. package/dist/cjs/lib/domComponent.d.ts +56 -48
  20. package/dist/cjs/lib/domComponent.js +66 -1
  21. package/dist/cjs/lib/domComponent.js.map +1 -1
  22. package/dist/cjs/lib/domComputed.d.ts +31 -21
  23. package/dist/cjs/lib/domComputed.js +14 -11
  24. package/dist/cjs/lib/domComputed.js.map +1 -1
  25. package/dist/cjs/lib/domDispose.d.ts +27 -12
  26. package/dist/cjs/lib/domDispose.js +26 -11
  27. package/dist/cjs/lib/domDispose.js.map +1 -1
  28. package/dist/cjs/lib/domForEach.d.ts +4 -3
  29. package/dist/cjs/lib/domForEach.js +10 -9
  30. package/dist/cjs/lib/domForEach.js.map +1 -1
  31. package/dist/cjs/lib/domImpl.d.ts +33 -10
  32. package/dist/cjs/lib/domImpl.js +28 -9
  33. package/dist/cjs/lib/domImpl.js.map +1 -1
  34. package/dist/cjs/lib/domMethods.d.ts +93 -47
  35. package/dist/cjs/lib/domMethods.js +88 -46
  36. package/dist/cjs/lib/domMethods.js.map +1 -1
  37. package/dist/cjs/lib/domevent.d.ts +87 -62
  38. package/dist/cjs/lib/domevent.js +84 -59
  39. package/dist/cjs/lib/domevent.js.map +1 -1
  40. package/dist/cjs/lib/emit.d.ts +62 -32
  41. package/dist/cjs/lib/emit.js +67 -53
  42. package/dist/cjs/lib/emit.js.map +1 -1
  43. package/dist/cjs/lib/kowrap.d.ts +6 -3
  44. package/dist/cjs/lib/kowrap.js +6 -3
  45. package/dist/cjs/lib/kowrap.js.map +1 -1
  46. package/dist/cjs/lib/obsArray.d.ts +91 -53
  47. package/dist/cjs/lib/obsArray.js +87 -55
  48. package/dist/cjs/lib/obsArray.js.map +1 -1
  49. package/dist/cjs/lib/observable.d.ts +25 -15
  50. package/dist/cjs/lib/observable.js +29 -18
  51. package/dist/cjs/lib/observable.js.map +1 -1
  52. package/dist/cjs/lib/pureComputed.d.ts +12 -15
  53. package/dist/cjs/lib/pureComputed.js +15 -18
  54. package/dist/cjs/lib/pureComputed.js.map +1 -1
  55. package/dist/cjs/lib/styled.d.ts +78 -61
  56. package/dist/cjs/lib/styled.js +26 -79
  57. package/dist/cjs/lib/styled.js.map +1 -1
  58. package/dist/cjs/lib/subscribe.d.ts +41 -37
  59. package/dist/cjs/lib/subscribe.js +31 -40
  60. package/dist/cjs/lib/subscribe.js.map +1 -1
  61. package/dist/cjs/lib/util.js +1 -0
  62. package/dist/cjs/lib/util.js.map +1 -1
  63. package/dist/cjs/lib/widgets/input.d.ts +3 -1
  64. package/dist/cjs/lib/widgets/input.js +6 -4
  65. package/dist/cjs/lib/widgets/input.js.map +1 -1
  66. package/dist/cjs/lib/widgets/select.d.ts +4 -2
  67. package/dist/cjs/lib/widgets/select.js +7 -5
  68. package/dist/cjs/lib/widgets/select.js.map +1 -1
  69. package/dist/esm/lib/_computed_queue.js +3 -3
  70. package/dist/esm/lib/_computed_queue.js.map +1 -1
  71. package/dist/esm/lib/binding.js +2 -2
  72. package/dist/esm/lib/binding.js.map +1 -1
  73. package/dist/esm/lib/computed.js +36 -50
  74. package/dist/esm/lib/computed.js.map +1 -1
  75. package/dist/esm/lib/dispose.js +104 -78
  76. package/dist/esm/lib/dispose.js.map +1 -1
  77. package/dist/esm/lib/dom.js +38 -18
  78. package/dist/esm/lib/dom.js.map +1 -1
  79. package/dist/esm/lib/domComponent.js +65 -0
  80. package/dist/esm/lib/domComponent.js.map +1 -1
  81. package/dist/esm/lib/domComputed.js +10 -7
  82. package/dist/esm/lib/domComputed.js.map +1 -1
  83. package/dist/esm/lib/domDispose.js +26 -11
  84. package/dist/esm/lib/domDispose.js.map +1 -1
  85. package/dist/esm/lib/domForEach.js +3 -2
  86. package/dist/esm/lib/domForEach.js.map +1 -1
  87. package/dist/esm/lib/domImpl.js +26 -7
  88. package/dist/esm/lib/domImpl.js.map +1 -1
  89. package/dist/esm/lib/domMethods.js +77 -35
  90. package/dist/esm/lib/domMethods.js.map +1 -1
  91. package/dist/esm/lib/domevent.js +84 -59
  92. package/dist/esm/lib/domevent.js.map +1 -1
  93. package/dist/esm/lib/emit.js +67 -53
  94. package/dist/esm/lib/emit.js.map +1 -1
  95. package/dist/esm/lib/kowrap.js +5 -2
  96. package/dist/esm/lib/kowrap.js.map +1 -1
  97. package/dist/esm/lib/obsArray.js +82 -50
  98. package/dist/esm/lib/obsArray.js.map +1 -1
  99. package/dist/esm/lib/observable.js +26 -15
  100. package/dist/esm/lib/observable.js.map +1 -1
  101. package/dist/esm/lib/pureComputed.js +15 -18
  102. package/dist/esm/lib/pureComputed.js.map +1 -1
  103. package/dist/esm/lib/styled.js +24 -77
  104. package/dist/esm/lib/styled.js.map +1 -1
  105. package/dist/esm/lib/subscribe.js +27 -36
  106. package/dist/esm/lib/subscribe.js.map +1 -1
  107. package/dist/esm/lib/util.js +1 -0
  108. package/dist/esm/lib/util.js.map +1 -1
  109. package/dist/esm/lib/widgets/input.js +3 -1
  110. package/dist/esm/lib/widgets/input.js.map +1 -1
  111. package/dist/esm/lib/widgets/select.js +3 -1
  112. package/dist/esm/lib/widgets/select.js.map +1 -1
  113. package/dist/grain-full.debug.js +2146 -3062
  114. package/dist/grain-full.debug.js.map +7 -0
  115. package/dist/grain-full.min.js +6 -2
  116. package/dist/grain-full.min.js.map +7 -1
  117. package/lib/binding.ts +9 -2
  118. package/lib/computed.ts +56 -56
  119. package/lib/dispose.ts +110 -85
  120. package/lib/dom.ts +39 -20
  121. package/lib/domComponent.ts +66 -57
  122. package/lib/domComputed.ts +29 -19
  123. package/lib/domDispose.ts +28 -11
  124. package/lib/domForEach.ts +7 -3
  125. package/lib/domImpl.ts +30 -7
  126. package/lib/domMethods.ts +101 -46
  127. package/lib/domevent.ts +85 -60
  128. package/lib/emit.ts +64 -50
  129. package/lib/kowrap.ts +5 -2
  130. package/lib/obsArray.ts +89 -54
  131. package/lib/observable.ts +26 -15
  132. package/lib/pureComputed.ts +16 -22
  133. package/lib/styled.ts +85 -71
  134. package/lib/subscribe.ts +41 -45
  135. package/lib/util.ts +1 -0
  136. package/lib/widgets/input.ts +3 -1
  137. package/lib/widgets/select.ts +3 -1
  138. package/package.json +38 -27
@@ -28,10 +28,11 @@ const dispose_1 = require("./dispose");
28
28
  const emit_1 = require("./emit");
29
29
  var _computed_queue_2 = require("./_computed_queue");
30
30
  Object.defineProperty(exports, "bundleChanges", { enumerable: true, get: function () { return _computed_queue_2.bundleChanges; } });
31
+ /**
32
+ * Base class for several variants of observable values.
33
+ */
31
34
  class BaseObservable {
32
- /**
33
- * Internal constructor for an Observable. You should use observable() function instead.
34
- */
35
+ // Internal constructor for an Observable. You should use observable() function instead.
35
36
  constructor(value) {
36
37
  this._onChange = new emit_1.Emitter();
37
38
  this._value = value;
@@ -39,13 +40,13 @@ class BaseObservable {
39
40
  /**
40
41
  * Returns the value of the observable. It is fast and does not create a subscription.
41
42
  * (It is similar to knockout's peek()).
42
- * @returns {Object} The current value of the observable.
43
+ * @returns The current value of the observable.
43
44
  */
44
45
  get() { return this._value; }
45
46
  /**
46
47
  * Sets the value of the observable. If the value differs from the previously set one, then
47
48
  * listeners to this observable will get called with (newValue, oldValue) as arguments.
48
- * @param {Object} value: The new value to set.
49
+ * @param value - The new value to set.
49
50
  */
50
51
  set(value) {
51
52
  if (value !== this._value) {
@@ -60,13 +61,13 @@ class BaseObservable {
60
61
  this._value = value;
61
62
  this._onChange.emit(value, prev);
62
63
  this._disposeOwned();
63
- _computed_queue_1.compute();
64
+ (0, _computed_queue_1.compute)();
64
65
  }
65
66
  /**
66
67
  * Adds a callback to listen to changes in the observable.
67
- * @param {Function} callback: Function, called on changes with (newValue, oldValue) arguments.
68
- * @param {Object} optContext: Context for the function.
69
- * @returns {Listener} Listener object. Its dispose() method removes the callback.
68
+ * @param callback - Function, called on changes with (newValue, oldValue) arguments.
69
+ * @param optContext - Context for the function.
70
+ * @returns Listener object. Its dispose() method removes the callback.
70
71
  */
71
72
  addListener(callback, optContext) {
72
73
  return this._onChange.addListener(callback, optContext);
@@ -80,7 +81,7 @@ class BaseObservable {
80
81
  /**
81
82
  * Sets a single callback to be called when a listener is added or removed. It overwrites any
82
83
  * previously-set such callback.
83
- * @param {Function} changeCB(hasListeners): Function to call after a listener is added or
84
+ * @param changeCB - Function to call after a listener is added or
84
85
  * removed. It's called with a boolean indicating whether this observable has any listeners.
85
86
  * Pass in `null` to unset the callback. Note that it can be called multiple times in a row
86
87
  * with hasListeners `true`.
@@ -91,6 +92,7 @@ class BaseObservable {
91
92
  /**
92
93
  * Used by subscriptions to keep track of dependencies. An observable that has dependnecies,
93
94
  * such as a computed observable, would override this method.
95
+ * @internal
94
96
  */
95
97
  _getDepItem() {
96
98
  return null;
@@ -109,25 +111,31 @@ class BaseObservable {
109
111
  isDisposed() {
110
112
  return this._onChange.isDisposed();
111
113
  }
114
+ /** @internal */
112
115
  _disposeOwned(arg) { }
113
116
  /**
114
117
  * Allow derived classes to emit change events with an additional third argument describing the
115
118
  * change. It always emits the event without checking for value equality.
119
+ * @internal
116
120
  */
117
121
  _setWithArg(value, arg) {
118
122
  const prev = this._value;
119
123
  this._value = value;
120
124
  this._onChange.emit(value, prev, arg);
121
125
  this._disposeOwned(arg);
122
- _computed_queue_1.compute();
126
+ (0, _computed_queue_1.compute)();
123
127
  }
124
128
  }
125
129
  exports.BaseObservable = BaseObservable;
130
+ /**
131
+ * An Observable holds a value and allows subscribing to changes.
132
+ */
126
133
  class Observable extends BaseObservable {
127
134
  constructor() {
128
135
  super(...arguments);
129
136
  this._owned = undefined;
130
137
  }
138
+ /** @internal */
131
139
  // See module-level holder() function below for documentation.
132
140
  static holder(value) {
133
141
  const obs = new Observable(value);
@@ -138,7 +146,7 @@ class Observable extends BaseObservable {
138
146
  * Creates a new Observable with the given initial value, and owned by owner.
139
147
  */
140
148
  static create(owner, value) {
141
- return dispose_1.setDisposeOwner(owner, new Observable(value));
149
+ return (0, dispose_1.setDisposeOwner)(owner, new Observable(value));
142
150
  }
143
151
  /**
144
152
  * The use an observable for a disposable object, use it a DisposableOwner:
@@ -154,6 +162,7 @@ class Observable extends BaseObservable {
154
162
  this._owned = value;
155
163
  return value;
156
164
  }
165
+ /** @internal */
157
166
  _disposeOwned() {
158
167
  if (this._owned) {
159
168
  this._owned.dispose();
@@ -164,8 +173,8 @@ class Observable extends BaseObservable {
164
173
  exports.Observable = Observable;
165
174
  /**
166
175
  * Creates a new Observable with the initial value of optValue if given or undefined if omitted.
167
- * @param {Object} optValue: The initial value to set.
168
- * @returns {Observable} The newly created observable.
176
+ * @param optValue - The initial value to set.
177
+ * @returns The newly created observable.
169
178
  */
170
179
  function observable(value) {
171
180
  return new Observable(value);
@@ -173,16 +182,18 @@ function observable(value) {
173
182
  exports.observable = observable;
174
183
  /**
175
184
  * Creates a new Observable with an initial disposable value owned by this observable, e.g.
176
- *
185
+ * ```
177
186
  * const obs = obsHolder<D>(D.create(null, ...args));
187
+ * ```
178
188
  *
179
- * This is needed because using simply observable<D>(value) would not cause the observable to take
189
+ * This is needed because using simply `observable<D>(value)` would not cause the observable to take
180
190
  * ownership of value (i.e. to dispose it later). This function is a less hacky equivalent to:
181
- *
191
+ * ```
182
192
  * const obs = observable<D>(null as any);
183
193
  * D.create(obs, ...args);
194
+ * ```
184
195
  *
185
- * To allow nulls, use observable<D|null>(null); then the obsHolder() constructor is not needed.
196
+ * To allow nulls, use `observable<D|null>(null)`; then the obsHolder() constructor is not needed.
186
197
  */
187
198
  function obsHolder(value) {
188
199
  return Observable.holder(value);
@@ -1 +1 @@
1
- {"version":3,"file":"observable.js","sourceRoot":"","sources":["../../../lib/observable.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;;GAqBG;;;AAEH,uDAAmD;AACnD,uCAA0E;AAC1E,iCAAyC;AAEzC,qDAAgD;AAAxC,gHAAA,aAAa,OAAA;AAErB,MAAa,cAAc;IAIzB;;OAEG;IACH,YAAY,KAAQ;QAClB,IAAI,CAAC,SAAS,GAAG,IAAI,cAAO,EAAE,CAAC;QAC/B,IAAI,CAAC,MAAM,GAAG,KAAK,CAAC;IACtB,CAAC;IAED;;;;OAIG;IACI,GAAG,KAAQ,OAAO,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC;IAEvC;;;;OAIG;IACI,GAAG,CAAC,KAAQ;QACjB,IAAI,KAAK,KAAK,IAAI,CAAC,MAAM,EAAE;YACzB,IAAI,CAAC,aAAa,CAAC,KAAK,CAAC,CAAC;SAC3B;IACH,CAAC;IAED;;OAEG;IACI,aAAa,CAAC,KAAQ;QAC3B,MAAM,IAAI,GAAG,IAAI,CAAC,MAAM,CAAC;QACzB,IAAI,CAAC,MAAM,GAAG,KAAK,CAAC;QACpB,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,KAAK,EAAE,IAAI,CAAC,CAAC;QACjC,IAAI,CAAC,aAAa,EAAE,CAAC;QACrB,yBAAO,EAAE,CAAC;IACZ,CAAC;IAED;;;;;OAKG;IACI,WAAW,CAAC,QAAmC,EAAE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C;AAED;;;;;;;;;;;;;;GAcG;AACH,SAAgB,SAAS,CAAI,KAAsB;IACjD,OAAO,UAAU,CAAC,MAAM,CAAI,KAAK,CAAC,CAAC;AACrC,CAAC;AAFD,8BAEC"}
@@ -1,33 +1,32 @@
1
+ import { DepItem } from './_computed_queue';
2
+ import { Observable } from './observable';
3
+ import { ISubscribable, UseCB } from './subscribe';
1
4
  /**
2
- * pureComputed.js implements a variant of computed() suitable for use with a pure read function
3
- * (free of side-effects). A pureComputed is only subscribed to its dependencies when something is
5
+ * `PureComputed` is a variant of `Computed` suitable for use with a pure read function
6
+ * (free of side-effects). A `PureComputed` is only subscribed to its dependencies when something is
4
7
  * subscribed to it. At other times, it is not subscribed to anything, and calls to `get()` will
5
- * recompute its value each time by calling its read() function.
8
+ * recompute its value each time by calling its `read()` function.
6
9
  *
7
- * Its syntax and usage are otherwise exactly as for a computed.
10
+ * Its syntax and usage are otherwise exactly as for a `Computed`.
8
11
  *
9
- * In addition to being cheaper when unused, a pureComputed() also avoids leaking memory when
12
+ * In addition to being cheaper when unused, a `PureComputed` also avoids leaking memory when
10
13
  * unused (since it's not registered with dependencies), so it is not necessary to dispose it.
11
14
  */
12
- import { DepItem } from './_computed_queue';
13
- import { Observable } from './observable';
14
- import { ISubscribable, UseCB } from './subscribe';
15
15
  export declare class PureComputed<T> extends Observable<T> {
16
16
  private _callback;
17
17
  private _write;
18
18
  private _sub;
19
19
  private readonly _dependencies;
20
20
  private _inCall;
21
- /**
22
- * Internal constructor for a PureComputed. You should use pureComputed() function instead.
23
- */
24
21
  constructor(callback: (use: UseCB, ...args: any[]) => T, dependencies: ReadonlyArray<ISubscribable>);
22
+ /** @internal */
25
23
  _getDepItem(): DepItem;
24
+ /** @override */
26
25
  get(): T;
27
26
  /**
28
27
  * "Sets" the value of the pure computed by calling the write() callback if one was provided in
29
28
  * the constructor. Throws an error if there was no such callback (not a "writable" computed).
30
- * @param {Object} value: The value to pass to the write() callback.
29
+ * @param value - The value to pass to the write() callback.
31
30
  */
32
31
  set(value: T): void;
33
32
  /**
@@ -44,9 +43,7 @@ export declare class PureComputed<T> extends Observable<T> {
44
43
  private _read;
45
44
  }
46
45
  /**
47
- * This is the type-checking interface for pureComputed(), which allows TypeScript to do helpful
48
- * type-checking when using it. We can only support a fixed number of argumnets (explicit
49
- * dependencies), but 5 should almost always be enough.
46
+ * Creates and returns a new PureComputed. The interface is identical to that of a Computed.
50
47
  */
51
48
  export declare function pureComputed<T>(cb: (use: UseCB) => T): PureComputed<T>;
52
49
  export declare function pureComputed<A, T>(a: Observable<A>, cb: (use: UseCB, a: A) => T): PureComputed<T>;
@@ -1,15 +1,4 @@
1
1
  "use strict";
2
- /**
3
- * pureComputed.js implements a variant of computed() suitable for use with a pure read function
4
- * (free of side-effects). A pureComputed is only subscribed to its dependencies when something is
5
- * subscribed to it. At other times, it is not subscribed to anything, and calls to `get()` will
6
- * recompute its value each time by calling its read() function.
7
- *
8
- * Its syntax and usage are otherwise exactly as for a computed.
9
- *
10
- * In addition to being cheaper when unused, a pureComputed() also avoids leaking memory when
11
- * unused (since it's not registered with dependencies), so it is not necessary to dispose it.
12
- */
13
2
  Object.defineProperty(exports, "__esModule", { value: true });
14
3
  exports.pureComputed = exports.PureComputed = void 0;
15
4
  const observable_1 = require("./observable");
@@ -22,10 +11,19 @@ function _useFunc(obs) {
22
11
  }
23
12
  // Constant empty array, which we use to avoid allocating new read-only empty arrays.
24
13
  const emptyArray = [];
14
+ /**
15
+ * `PureComputed` is a variant of `Computed` suitable for use with a pure read function
16
+ * (free of side-effects). A `PureComputed` is only subscribed to its dependencies when something is
17
+ * subscribed to it. At other times, it is not subscribed to anything, and calls to `get()` will
18
+ * recompute its value each time by calling its `read()` function.
19
+ *
20
+ * Its syntax and usage are otherwise exactly as for a `Computed`.
21
+ *
22
+ * In addition to being cheaper when unused, a `PureComputed` also avoids leaking memory when
23
+ * unused (since it's not registered with dependencies), so it is not necessary to dispose it.
24
+ */
25
25
  class PureComputed extends observable_1.Observable {
26
- /**
27
- * Internal constructor for a PureComputed. You should use pureComputed() function instead.
28
- */
26
+ // Internal constructor for a PureComputed. You should use pureComputed() function instead.
29
27
  constructor(callback, dependencies) {
30
28
  // At initialization we force an undefined value even though it's not of type T: it's not
31
29
  // actually used as get() is overridden.
@@ -37,10 +35,12 @@ class PureComputed extends observable_1.Observable {
37
35
  this._inCall = false;
38
36
  this.setListenerChangeCB(this._onListenerChange, this);
39
37
  }
38
+ /** @internal */
40
39
  _getDepItem() {
41
40
  this._activate();
42
41
  return this._sub._getDepItem();
43
42
  }
43
+ /** @override */
44
44
  get() {
45
45
  if (!this._sub && !this._inCall) {
46
46
  // _inCall member prevents infinite recursion.
@@ -62,7 +62,7 @@ class PureComputed extends observable_1.Observable {
62
62
  /**
63
63
  * "Sets" the value of the pure computed by calling the write() callback if one was provided in
64
64
  * the constructor. Throws an error if there was no such callback (not a "writable" computed).
65
- * @param {Object} value: The value to pass to the write() callback.
65
+ * @param value - The value to pass to the write() callback.
66
66
  */
67
67
  set(value) { this._write(value); }
68
68
  /**
@@ -104,9 +104,6 @@ class PureComputed extends observable_1.Observable {
104
104
  }
105
105
  }
106
106
  exports.PureComputed = PureComputed;
107
- /**
108
- * Creates and returns a new PureComputed. The interface is identical to that of a Computed.
109
- */
110
107
  function pureComputed(...args) {
111
108
  const readCb = args.pop();
112
109
  // The cast helps ensure that Observable is compatible with ISubscribable abstraction that we use.
@@ -1 +1 @@
1
- {"version":3,"file":"pureComputed.js","sourceRoot":"","sources":["../../../lib/pureComputed.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;GAUG;;;AAIH,6CAAwD;AACxD,2CAAiF;AAEjF,SAAS,QAAQ;IACf,MAAM,IAAI,KAAK,CAAC,0CAA0C,CAAC,CAAC;AAC9D,CAAC;AAED,SAAS,QAAQ,CAAI,GAAiD;IACpE,OAAO,CAAC,KAAK,IAAI,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,GAAG,EAAE,CAAC,CAAC,C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CAAC;IAClB,CAAC;IAEO,SAAS;QACf,IAAI,CAAC,IAAI,CAAC,IAAI,EAAE;YACd,IAAI,CAAC,IAAI,GAAG,IAAI,wBAAY,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,IAAI,CAAC,aAAa,CAAC,CAAC;SACzE;IACH,CAAC;IAEO,iBAAiB,CAAC,YAAqB;QAC7C,IAAI,YAAY,EAAE;YAChB,IAAI,CAAC,SAAS,EAAE,CAAC;SAClB;aAAM,IAAI,IAAI,CAAC,IAAI,EAAE;YACpB,IAAI,CAAC,IAAI,CAAC,OAAO,EAAE,CAAC;YACpB,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;SAClB;IACH,CAAC;IAEO,KAAK,CAAC,GAAQ,EAAE,GAAG,IAAW;QACpC,KAAK,CAAC,GAAG,CAAC,IAAI,CAAC,SAAS,CAAC,GAAG,EAAE,GAAG,IAAI,CAAC,CAAC,CAAC;IAC1C,CAAC;CACF;AA5FD,oCA4FC;AA2BD,SAAgB,YAAY,CAAC,GAAG,IAAW;IACzC,MAAM,MAAM,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC;IAC1B,kGAAkG;IAClG,OAAO,IAAI,YAAY,CAAM,MAAM,EAAE,IAAI,CAAC,CAAC;AAC7C,CAAC;AAJD,oCAIC"}
@@ -1,79 +1,96 @@
1
+ import { IDomArgs, TagElem, TagName } from './domImpl';
2
+ import { cls } from './domMethods';
3
+ export interface IClsName {
4
+ className: string;
5
+ cls: typeof cls;
6
+ }
7
+ export type DomCreateFunc<R, Args extends IDomArgs<R> = IDomArgs<R>> = (...args: Args) => R;
1
8
  /**
2
9
  * In-code styling for DOM components, inspired by Reacts Styled Components.
3
10
  *
4
11
  * Usage:
5
- * const title = styled('h1', `
6
- * font-size: 1.5em;
7
- * text-align: center;
8
- * color: palevioletred;
9
- * `);
10
- *
11
- * const wrapper = styled('section', `
12
- * padding: 4em;
13
- * background: papayawhip;
14
- * `);
15
- *
16
- * wrapper(title('Hello world'))
17
- *
18
- * This generates class names for title and wrapper, adds the styles to the document on first use,
19
- * and the result is equivalent to:
20
- *
21
- * dom(`section.${wrapper.className}`, dom(`h1.${title.className}`, 'Hello world'));
22
- *
23
- * Calls to styled() should happen at the top level, at import time, in order to register all
12
+ * ```ts
13
+ * const cssTitle = styled('h1', `
14
+ * font-size: 1.5em;
15
+ * text-align: center;
16
+ * color: palevioletred;
17
+ * `);
18
+ *
19
+ * const cssWrapper = styled('section', `
20
+ * padding: 4em;
21
+ * background: papayawhip;
22
+ * `);
23
+ *
24
+ * cssWrapper(cssTitle('Hello world'))
25
+ * ```
26
+ *
27
+ * This generates class names for `cssTitle` and `cssWrapper`, adds the styles to the document on
28
+ * first use, and the result is equivalent to:
29
+ * ```ts
30
+ * dom(`section.${cssWrapper.className}`, dom(`h1.${cssTitle.className}`, 'Hello world'));
31
+ * ```
32
+ *
33
+ * What `styled(tag)` returns is a function that takes the same arguments `...args` as
34
+ * `dom(tag, ...args)`. In particular, you may call it with all the arguments that
35
+ * [`dom()`](#dom) takes: content, DOM methods, event handlers, etc.
36
+ *
37
+ * Calls to `styled()` should happen at the top level, at import time, in order to register all
24
38
  * styles upfront. Actual work happens the first time a style is needed to create an element.
25
- * Calling styled() elsewhere than at top level is wasteful and bad for performance.
26
- *
27
- * You may create a style that modifies an existing styled() or other component, e.g.
39
+ * Calling `styled()` elsewhere than at top level is wasteful and bad for performance.
28
40
  *
29
- * const title2 = styled(title, `font-size: 1rem; color: red;`);
41
+ * You may create a style that modifies an existing `styled()` or other component, e.g.
42
+ * ```ts
43
+ * const cssTitle2 = styled(cssTitle, `font-size: 1rem; color: red;`);
44
+ * ```
30
45
  *
31
- * Calling title2('Foo') becomes equivalent to dom(`h1.${title.className}.${title2.className}`).
46
+ * Now calling `cssTitle2('Foo')` becomes equivalent to
47
+ * `dom('h1', {className: cssTitle.className + ' ' + cssTitle2.className})`.
32
48
  *
33
49
  * Styles may incorporate other related styles by nesting them under the main one as follows:
34
- *
35
- * const myButton = styled('button', `
36
- * border-radius: 0.5rem;
37
- * border: 1px solid grey;
38
- * font-size: 1rem;
39
- *
40
- * &:active {
41
- * background: lightblue;
42
- * }
43
- * &-small {
44
- * font-size: 0.6rem;
45
- * }
46
- * `);
50
+ * ```ts
51
+ * const myButton = styled('button', `
52
+ * border-radius: 0.5rem;
53
+ * border: 1px solid grey;
54
+ * font-size: 1rem;
55
+ *
56
+ * &:active {
57
+ * background: lightblue;
58
+ * }
59
+ * &-small {
60
+ * font-size: 0.6rem;
61
+ * }
62
+ * `);
63
+ * ```
47
64
  *
48
65
  * In nested styles, ampersand (&) gets replaced with the generated .className of the main element.
49
66
  *
50
- * The resulting styled component provides a .cls() helper to simplify using prefixed classes. It
51
- * behaves as dom.cls(), but prefixes the class names with the generated className of the main
67
+ * The resulting styled component provides a `.cls()` helper to simplify using prefixed classes. It
68
+ * behaves as `dom.cls()`, but prefixes the class names with the generated className of the main
52
69
  * element. E.g. for the example above,
70
+ * ```ts
71
+ * myButton(myButton.cls('-small'), 'Test')
72
+ * ```
53
73
  *
54
- * myButton(myButton.cls('-small'), 'Test')
55
- *
56
- * creates a button with both the myButton style above, and the style specified under "&-small".
57
- *
58
- * Animations with @keyframes may be created with a unique name by using the keyframes() helper:
59
- *
60
- * const rotate360 = keyframes(`
61
- * from { transform: rotate(0deg); }
62
- * to { transform: rotate(360deg); }
63
- * `);
64
- *
65
- * const Rotate = styled('div', `
66
- * display: inline-block;
67
- * animation: ${rotate360} 2s linear infinite;
68
- * `);
74
+ * creates a button with both the `myButton` style above, and the style specified under "&-small".
69
75
  */
70
- import { IDomArgs, TagElem, TagName } from './domImpl';
71
- import { cls } from './domMethods';
72
- export interface IClsName {
73
- className: string;
74
- cls: typeof cls;
75
- }
76
- export declare type DomCreateFunc<R, Args extends IDomArgs<R> = IDomArgs<R>> = (...args: Args) => R;
77
76
  export declare function styled<Tag extends TagName>(tag: Tag, styles: string): DomCreateFunc<TagElem<Tag>> & IClsName;
78
77
  export declare function styled<Args extends any[], R extends Element>(creator: (...args: Args) => R, styles: string): typeof creator & IClsName;
78
+ /**
79
+ * Animations with `@keyframes` may be created with a unique name by using the keyframes() helper:
80
+ * ```ts
81
+ * const rotate360 = keyframes(`
82
+ * from { transform: rotate(0deg); }
83
+ * to { transform: rotate(360deg); }
84
+ * `);
85
+ *
86
+ * const Rotate = styled('div', `
87
+ * display: inline-block;
88
+ * animation: ${rotate360} 2s linear infinite;
89
+ * `);
90
+ * ```
91
+ *
92
+ * This function returns simply a string with the generated name. Note that keyframes do not
93
+ * support nesting or ampersand (&) handling, like `styled()` does, since these would be difficult
94
+ * and are entirely unneeded.
95
+ */
79
96
  export declare function keyframes(styles: string): string;
@@ -1,73 +1,4 @@
1
1
  "use strict";
2
- /**
3
- * In-code styling for DOM components, inspired by Reacts Styled Components.
4
- *
5
- * Usage:
6
- * const title = styled('h1', `
7
- * font-size: 1.5em;
8
- * text-align: center;
9
- * color: palevioletred;
10
- * `);
11
- *
12
- * const wrapper = styled('section', `
13
- * padding: 4em;
14
- * background: papayawhip;
15
- * `);
16
- *
17
- * wrapper(title('Hello world'))
18
- *
19
- * This generates class names for title and wrapper, adds the styles to the document on first use,
20
- * and the result is equivalent to:
21
- *
22
- * dom(`section.${wrapper.className}`, dom(`h1.${title.className}`, 'Hello world'));
23
- *
24
- * Calls to styled() should happen at the top level, at import time, in order to register all
25
- * styles upfront. Actual work happens the first time a style is needed to create an element.
26
- * Calling styled() elsewhere than at top level is wasteful and bad for performance.
27
- *
28
- * You may create a style that modifies an existing styled() or other component, e.g.
29
- *
30
- * const title2 = styled(title, `font-size: 1rem; color: red;`);
31
- *
32
- * Calling title2('Foo') becomes equivalent to dom(`h1.${title.className}.${title2.className}`).
33
- *
34
- * Styles may incorporate other related styles by nesting them under the main one as follows:
35
- *
36
- * const myButton = styled('button', `
37
- * border-radius: 0.5rem;
38
- * border: 1px solid grey;
39
- * font-size: 1rem;
40
- *
41
- * &:active {
42
- * background: lightblue;
43
- * }
44
- * &-small {
45
- * font-size: 0.6rem;
46
- * }
47
- * `);
48
- *
49
- * In nested styles, ampersand (&) gets replaced with the generated .className of the main element.
50
- *
51
- * The resulting styled component provides a .cls() helper to simplify using prefixed classes. It
52
- * behaves as dom.cls(), but prefixes the class names with the generated className of the main
53
- * element. E.g. for the example above,
54
- *
55
- * myButton(myButton.cls('-small'), 'Test')
56
- *
57
- * creates a button with both the myButton style above, and the style specified under "&-small".
58
- *
59
- * Animations with @keyframes may be created with a unique name by using the keyframes() helper:
60
- *
61
- * const rotate360 = keyframes(`
62
- * from { transform: rotate(0deg); }
63
- * to { transform: rotate(360deg); }
64
- * `);
65
- *
66
- * const Rotate = styled('div', `
67
- * display: inline-block;
68
- * animation: ${rotate360} 2s linear infinite;
69
- * `);
70
- */
71
2
  Object.defineProperty(exports, "__esModule", { value: true });
72
3
  exports.keyframes = exports.styled = void 0;
73
4
  // Use the browser globals in a way that allows replacing them with mocks in tests.
@@ -81,7 +12,7 @@ function styled(creator, styles) {
81
12
  // Creator function reflects the input, with only the addition of style.use() at the end. Note
82
13
  // that it needs to be at the end because creator() might take special initial arguments.
83
14
  const newCreator = (typeof creator === 'string') ?
84
- (...args) => style.addToElem(domImpl_1.dom(creator, ...args)) :
15
+ (...args) => style.addToElem((0, domImpl_1.dom)(creator, ...args)) :
85
16
  (...args) => style.addToElem(creator(...args));
86
17
  return Object.assign(newCreator, {
87
18
  className: style.className,
@@ -89,8 +20,24 @@ function styled(creator, styles) {
89
20
  });
90
21
  }
91
22
  exports.styled = styled;
92
- // Keyframes produces simply a string with the generated name. Note that these does not support
93
- // nesting or ampersand (&) handling, since these would be difficult and are entirely unneeded.
23
+ /**
24
+ * Animations with `@keyframes` may be created with a unique name by using the keyframes() helper:
25
+ * ```ts
26
+ * const rotate360 = keyframes(`
27
+ * from { transform: rotate(0deg); }
28
+ * to { transform: rotate(360deg); }
29
+ * `);
30
+ *
31
+ * const Rotate = styled('div', `
32
+ * display: inline-block;
33
+ * animation: ${rotate360} 2s linear infinite;
34
+ * `);
35
+ * ```
36
+ *
37
+ * This function returns simply a string with the generated name. Note that keyframes do not
38
+ * support nesting or ampersand (&) handling, like `styled()` does, since these would be difficult
39
+ * and are entirely unneeded.
40
+ */
94
41
  function keyframes(styles) {
95
42
  return (new KeyframePiece(styles)).className;
96
43
  }
@@ -114,23 +61,23 @@ function getNextStyleNum() {
114
61
  return g._grainNextStyleNum = (g._grainNextStyleNum || 0) + 1;
115
62
  }
116
63
  class StylePiece {
117
- constructor(_styles) {
118
- this._styles = _styles;
119
- this._mounted = false;
120
- this.className = StylePiece._nextClassName();
121
- StylePiece._unmounted.add(this);
122
- }
123
64
  // Generate a new css class name. The suffix ensures that names like "&2" can't cause a conflict.
124
65
  static _nextClassName() { return `_grain${getNextStyleNum()}_`; }
125
66
  // Mount all unmounted StylePieces, and clear the _unmounted map.
126
67
  static _mountAll() {
127
68
  const sheet = Array.from(this._unmounted, (p) => p._createRules()).join("\n\n");
128
- browserGlobals_1.G.document.head.appendChild(domImpl_1.dom('style', sheet));
69
+ browserGlobals_1.G.document.head.appendChild((0, domImpl_1.dom)('style', sheet));
129
70
  for (const piece of this._unmounted) {
130
71
  piece._mounted = true;
131
72
  }
132
73
  this._unmounted.clear();
133
74
  }
75
+ constructor(_styles) {
76
+ this._styles = _styles;
77
+ this._mounted = false;
78
+ this.className = StylePiece._nextClassName();
79
+ StylePiece._unmounted.add(this);
80
+ }
134
81
  addToElem(elem) {
135
82
  if (!this._mounted) {
136
83
  StylePiece._mountAll();
@@ -1 +1 @@
1
- {"version":3,"file":"styled.js","sourceRoot":"","sources":["../../../lib/styled.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoEG;;;AAEH,mFAAmF;AACnF,qDAAmC;AACnC,uCAA0D;AAC1D,6CAA4C;AAe5C,SAAgB,MAAM,CAAC,OAAY,EAAE,MAAc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