keyframekit 1.0.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 (41) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +122 -0
  3. package/dist/KeyframeKit.d.ts +139 -0
  4. package/dist/KeyframeKit.d.ts.map +1 -0
  5. package/dist/KeyframeKit.js +294 -0
  6. package/dist/KeyframeKit.js.map +1 -0
  7. package/docs/.vitepress/components/Playground/Playground.js +243 -0
  8. package/docs/.vitepress/components/Playground/Playground.vue +206 -0
  9. package/docs/.vitepress/components/Playground/defaultExample.js +175 -0
  10. package/docs/.vitepress/components/Playground/interFont.js +14 -0
  11. package/docs/.vitepress/components/Playground/themes/githubDark.js +402 -0
  12. package/docs/.vitepress/components/Playground/themes/githubLight.js +399 -0
  13. package/docs/.vitepress/components/Playground/themes.js +24 -0
  14. package/docs/.vitepress/config.ts +64 -0
  15. package/docs/.vitepress/referenceNavigation.ts +37 -0
  16. package/docs/.vitepress/theme/base-styles.css +91 -0
  17. package/docs/.vitepress/theme/env.d.ts +5 -0
  18. package/docs/.vitepress/theme/index.ts +40 -0
  19. package/docs/docs/index.md +136 -0
  20. package/docs/docs/public/icon.png +0 -0
  21. package/docs/docs/public/playground/KeyframeKit/dist/KeyframeKit.d.ts +139 -0
  22. package/docs/docs/public/playground/KeyframeKit/dist/KeyframeKit.d.ts.map +1 -0
  23. package/docs/docs/public/playground/KeyframeKit/dist/KeyframeKit.js +294 -0
  24. package/docs/docs/public/playground/KeyframeKit/dist/KeyframeKit.js.map +1 -0
  25. package/docs/docs/reference/_media/LICENSE +21 -0
  26. package/docs/docs/reference/classes/KeyframeEffectParameters.md +95 -0
  27. package/docs/docs/reference/classes/ParsedKeyframes.md +49 -0
  28. package/docs/docs/reference/index.md +20 -0
  29. package/docs/docs/reference/interfaces/KeyframesFactory.md +151 -0
  30. package/docs/docs/reference/navigation.json +64 -0
  31. package/docs/docs/reference/type-aliases/KeyframeArgument.md +9 -0
  32. package/docs/docs/reference/type-aliases/KeyframesFactorySource.md +9 -0
  33. package/docs/docs/reference/type-aliases/ParsedKeyframesRules.md +15 -0
  34. package/docs/docs/reference/variables/default.md +7 -0
  35. package/docs/package.json +25 -0
  36. package/docs/typedoc/plugin-param-names.js +52 -0
  37. package/docs/typedoc.json +63 -0
  38. package/package.json +37 -0
  39. package/src/KeyframeKit.ts +508 -0
  40. package/tsconfig.json +47 -0
  41. package/vercel.json +13 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Ben Hatsor
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,122 @@
1
+ # [KeyframeKit](https://keyframekit.berryscript.com)
2
+
3
+ Intuitive, powerful and performant tools for working with CSS animations in JavaScript.
4
+ Powered by the [Web Animations API][1].
5
+
6
+ ## Installation
7
+
8
+ ### NPM
9
+ ```sh
10
+ npm install keyframekit
11
+ ```
12
+
13
+ ## Usage
14
+
15
+ ### Playing CSS-defined animations with JS
16
+
17
+ In your CSS:
18
+ ```css
19
+ @keyframes rotate-small { ... }
20
+ ```
21
+
22
+ Then, in JS:
23
+ ```js
24
+ import KeyframeKit from 'keyframekit';
25
+
26
+ const documentStyleSheets = await KeyframeKit.getDocumentStyleSheetsOnLoad();
27
+
28
+ // get animation keyframes from stylesheet
29
+ const rotateSmallAnimKeyframes = KeyframeKit.getStyleSheetKeyframes({
30
+ of: 'rotate-small',
31
+ in: documentStyleSheets
32
+ });
33
+
34
+ // then, define your animation
35
+ const rotateSmallAnim = rotateSmallAnimKeyframes.toKeyframeEffect({
36
+ duration: 700,
37
+ easing: 'ease'
38
+ });
39
+
40
+ // finally, attach it to an element:
41
+ const attachedAnim = rotateSmallAnim.toAnimation({
42
+ target: document.querySelector('.el')
43
+ });
44
+
45
+ attachedAnim.play();
46
+ ```
47
+
48
+ The primary reason to play your animation with JS is because you get way more control over its playback:
49
+ ```js
50
+ attachedAnim.pause();
51
+ attachedAnim.playbackRate = -1;
52
+ const progress = attachedAnim.overallProgress; // 0 to 1 (Baseline newly available)
53
+ await attachedAnim.finished;
54
+ ```
55
+ [...and more.][2]
56
+
57
+ ### Defining animations in JS
58
+
59
+ This is useful for when you want to have all your animation code in one place.
60
+
61
+ ```js
62
+ import { KeyframeEffectParameters } from 'keyframekit';
63
+
64
+ // define your animation
65
+ const linkTextHoverAnim = new KeyframeEffectParameters({
66
+
67
+ keyframes: {
68
+ // 0 to 1. equivalent to CSS keyframe percentage values:
69
+ offset: [0, 0.499, 0.5, 1],
70
+ // respective CSS property keyframes:
71
+ clipPath: ['inset(0 0 0 0)', 'inset(100% 0 0 0)', 'inset(0 0 100% 0)', 'inset(0 0 0 0)'],
72
+ top: ['0', '-20px', '20px', '0']
73
+ },
74
+
75
+ options: {
76
+ duration: 700,
77
+ easing: 'ease'
78
+ }
79
+
80
+ });
81
+
82
+ // then, attach it to an element:
83
+ const attachedAnim = linkTextHoverAnim.toAnimation({
84
+ target: document.querySelector('.link')
85
+ });
86
+
87
+ attachedAnim.play();
88
+ ```
89
+
90
+ ### Importing a stylesheet directly
91
+
92
+ Instead of getting an animation from the document's stylesheets, you can also import it directly from a CSS file.
93
+
94
+ ```js
95
+ import KeyframeKit from 'keyframekit';
96
+
97
+ const styleSheet = await KeyframeKit.importStyleSheet('./styles.css');
98
+
99
+ // get animation keyframes from stylesheet
100
+ const rotateSmallAnimKeyframes = KeyframeKit.getStyleSheetKeyframes({
101
+ of: 'rotate-small',
102
+ in: styleSheet
103
+ });
104
+ ```
105
+
106
+ > Note: `@import` rules won't be resolved in stylesheets imported in this way. [More info.][3]
107
+
108
+ ### Full reference
109
+ [See here.](/reference)
110
+
111
+ ## Typing
112
+
113
+ This library is fully compatable with native JS, but it also has full spec-compliant type support, including declaration files and source maps.
114
+
115
+ ## License
116
+
117
+ [MIT](LICENSE)
118
+
119
+
120
+ [1]: https://developer.mozilla.org/en-US/docs/Web/API/Web_Animations_API
121
+ [2]: https://developer.mozilla.org/en-US/docs/Web/API/Web_Animations_API/Using_the_Web_Animations_API
122
+ [3]: https://github.com/WICG/construct-stylesheets/issues/119#issuecomment-588352418
@@ -0,0 +1,139 @@
1
+ export type KeyframesFactorySource = StyleSheetList | CSSStyleSheet;
2
+ declare class KeyframesFactory {
3
+ #private;
4
+ readonly Error: {
5
+ readonly KeyframesRuleNameTypeError: {
6
+ new (message?: string): {
7
+ message: string;
8
+ name: string;
9
+ stack?: string;
10
+ cause?: unknown;
11
+ };
12
+ new (message?: string, options?: ErrorOptions): {
13
+ message: string;
14
+ name: string;
15
+ stack?: string;
16
+ cause?: unknown;
17
+ };
18
+ new (message?: string): {
19
+ message: string;
20
+ name: string;
21
+ stack?: string;
22
+ cause?: unknown;
23
+ };
24
+ new (message?: string, options?: ErrorOptions): {
25
+ message: string;
26
+ name: string;
27
+ stack?: string;
28
+ cause?: unknown;
29
+ };
30
+ isError(error: unknown): error is Error;
31
+ };
32
+ readonly SourceTypeError: {
33
+ new (message?: string): {
34
+ message: string;
35
+ name: string;
36
+ stack?: string;
37
+ cause?: unknown;
38
+ };
39
+ new (message?: string, options?: ErrorOptions): {
40
+ message: string;
41
+ name: string;
42
+ stack?: string;
43
+ cause?: unknown;
44
+ };
45
+ new (message?: string): {
46
+ message: string;
47
+ name: string;
48
+ stack?: string;
49
+ cause?: unknown;
50
+ };
51
+ new (message?: string, options?: ErrorOptions): {
52
+ message: string;
53
+ name: string;
54
+ stack?: string;
55
+ cause?: unknown;
56
+ };
57
+ isError(error: unknown): error is Error;
58
+ };
59
+ readonly StyleSheetImportError: {
60
+ new (message?: string): {
61
+ message: string;
62
+ name: string;
63
+ stack?: string;
64
+ cause?: unknown;
65
+ };
66
+ new (message?: string, options?: ErrorOptions): {
67
+ message: string;
68
+ name: string;
69
+ stack?: string;
70
+ cause?: unknown;
71
+ };
72
+ isError(error: unknown): error is Error;
73
+ };
74
+ };
75
+ getDocumentStyleSheetsOnLoad({ document }?: {
76
+ document?: Document;
77
+ }): Promise<StyleSheetList>;
78
+ /** @remarks
79
+ * Note: `@import` rules won't be resolved in imported stylesheets.
80
+ * See https://github.com/WICG/construct-stylesheets/issues/119#issuecomment-588352418. */
81
+ importStyleSheet(url: string): Promise<CSSStyleSheet>;
82
+ /** @param obj.of The name of the `@keyframes` rule to get keyframes from. */
83
+ getStyleSheetKeyframes({ of: ruleName, in: source }: {
84
+ of: string;
85
+ in: KeyframesFactorySource;
86
+ }): ParsedKeyframes | undefined;
87
+ getAllStyleSheetKeyframesRules({ in: source }: {
88
+ in: KeyframesFactorySource;
89
+ }): ParsedKeyframesRules;
90
+ parseKeyframesRule({ rule: keyframes }: {
91
+ rule: CSSKeyframesRule;
92
+ }): ParsedKeyframes;
93
+ }
94
+ declare const _default: KeyframesFactory;
95
+ export default _default;
96
+ export type { KeyframesFactory };
97
+ /** https://drafts.csswg.org/web-animations-1/#processing-a-keyframes-argument */
98
+ export type KeyframeArgument = Keyframe[] | PropertyIndexedKeyframes;
99
+ /** https://drafts.csswg.org/web-animations-1/#the-keyframeeffect-interface */
100
+ export declare class KeyframeEffectParameters {
101
+ #private;
102
+ keyframes: KeyframeArgument;
103
+ options: KeyframeEffectOptions;
104
+ /**
105
+ * @param obj.keyframes [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/Web_Animations_API/Keyframe_Formats)
106
+ * @param obj.options [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/KeyframeEffect/KeyframeEffect#options)
107
+ */
108
+ constructor({ keyframes, options }: {
109
+ keyframes: KeyframeArgument;
110
+ options?: number | KeyframeEffectOptions;
111
+ });
112
+ /**
113
+ * @param obj.target An element to attach the animation to.
114
+ * @param obj.options Additional keyframe effect options. Can override existing keys.
115
+ * [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/KeyframeEffect/KeyframeEffect#options)
116
+ *
117
+ * @see Specifications:
118
+ * - https://drafts.csswg.org/web-animations-1/#the-keyframeeffect-interface
119
+ * - https://drafts.csswg.org/web-animations-1/#the-animation-interface
120
+ */
121
+ toAnimation({ target, options: additionalOptions, timeline }: {
122
+ target: Element | null;
123
+ options?: number | KeyframeEffectOptions;
124
+ timeline?: AnimationTimeline;
125
+ }): Animation;
126
+ }
127
+ export declare class ParsedKeyframes {
128
+ keyframes: Keyframe[];
129
+ constructor(keyframes: Keyframe[]);
130
+ /**
131
+ * @param options
132
+ * [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/KeyframeEffect/KeyframeEffect#options)
133
+ */
134
+ toKeyframeEffect(options: number | KeyframeEffectOptions | null): KeyframeEffectParameters;
135
+ }
136
+ export type ParsedKeyframesRules = {
137
+ [ruleName: string]: ParsedKeyframes;
138
+ };
139
+ //# sourceMappingURL=KeyframeKit.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"KeyframeKit.d.ts","sourceRoot":"","sources":["../src/KeyframeKit.ts"],"names":[],"mappings":"AAgBA,MAAM,MAAM,sBAAsB,GAA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,EAAE,OAAO,GAAG,IAAI,CAAC;QACvB,OAAO,CAAC,EAAE,MAAM,GAAG,qBAAqB,CAAC;QACzC,QAAQ,CAAC,EAAE,iBAAiB,CAAA;KAC7B,GAAG,SAAS;CAqCd;AAGD,qBAAa,eAAe;IAE1B,SAAS,EAAE,QAAQ,EAAE,CAAC;gBAEV,SAAS,EAAE,QAAQ,EAAE;IAIjC;;;OAGG;IACH,gBAAgB,CACd,OAAO,EAAE,MAAM,GAAG,qBAAqB,GAAG,IAAI,GAC7C,wBAAwB;CAwB5B;AAGD,MAAM,MAAM,oBAAoB,GAAG;IACjC,CAAC,QAAQ,EAAE,MAAM,GAAG,eAAe,CAAA;CACpC,CAAC"}
@@ -0,0 +1,294 @@
1
+ //
2
+ // KeyframeKit
3
+ // Ben Hatsor
4
+ //
5
+ // See README.md for usage.
6
+ //
7
+ const CHARS = {
8
+ PERCENT_SIGN: '%',
9
+ HYPHEN_MINUS: '-',
10
+ DOUBLE_HYPHEN_MINUS: '--',
11
+ WEBKIT_PREFIX: '-webkit-'
12
+ };
13
+ class KeyframesFactory {
14
+ Error = {
15
+ KeyframesRuleNameTypeError: class KeyframesRuleNameTypeError extends TypeError {
16
+ message = `Keyframes rule name must be a string.`;
17
+ },
18
+ SourceTypeError: class SourceTypeError extends TypeError {
19
+ message = `Source must be either a Document, a ShadowRoot or a CSSStyleSheet instance.`;
20
+ },
21
+ StyleSheetImportError: class StyleSheetImportError extends Error {
22
+ message = `The stylesheet could not be imported.`;
23
+ }
24
+ };
25
+ async getDocumentStyleSheetsOnLoad({ document = window.document } = {}) {
26
+ await waitForDocumentLoad({
27
+ document: document
28
+ });
29
+ return document.styleSheets;
30
+ }
31
+ /** @remarks
32
+ * Note: `@import` rules won't be resolved in imported stylesheets.
33
+ * See https://github.com/WICG/construct-stylesheets/issues/119#issuecomment-588352418. */
34
+ async importStyleSheet(url) {
35
+ const resp = await fetch(url);
36
+ if (!resp.ok) {
37
+ throw new this.Error.StyleSheetImportError();
38
+ }
39
+ const respText = await resp.text();
40
+ // remove file name from URL to get base URL
41
+ const baseURL = url.split('/').slice(0, -1).join('/');
42
+ const styleSheet = new CSSStyleSheet({
43
+ baseURL: baseURL
44
+ });
45
+ await styleSheet.replace(respText);
46
+ return styleSheet;
47
+ }
48
+ /** @param obj.of The name of the `@keyframes` rule to get keyframes from. */
49
+ getStyleSheetKeyframes({ of: ruleName, in: source }) {
50
+ if (typeof ruleName !== 'string') {
51
+ throw new this.Error.KeyframesRuleNameTypeError();
52
+ }
53
+ if (source instanceof StyleSheetList) {
54
+ return this.#getStyleSheetKeyframesInStyleSheetList({
55
+ of: ruleName,
56
+ styleSheetList: source
57
+ });
58
+ }
59
+ else if (source instanceof CSSStyleSheet) {
60
+ return this.#getStyleSheetKeyframesInStyleSheet({
61
+ of: ruleName,
62
+ styleSheet: source
63
+ });
64
+ }
65
+ else {
66
+ throw new this.Error.SourceTypeError();
67
+ }
68
+ }
69
+ #getStyleSheetKeyframesInStyleSheetList({ of: ruleName, styleSheetList }) {
70
+ for (const styleSheet of styleSheetList) {
71
+ const keyframesRule = this.#getStyleSheetKeyframesInStyleSheet({
72
+ of: ruleName,
73
+ styleSheet: styleSheet
74
+ });
75
+ if (keyframesRule !== undefined) {
76
+ return keyframesRule;
77
+ }
78
+ }
79
+ }
80
+ #getStyleSheetKeyframesInStyleSheet({ of: ruleName, styleSheet }) {
81
+ for (const rule of styleSheet.cssRules) {
82
+ if (!(rule instanceof CSSKeyframesRule)) {
83
+ continue;
84
+ }
85
+ if (rule.name === ruleName) {
86
+ const keyframes = this.parseKeyframesRule({
87
+ rule: rule
88
+ });
89
+ return keyframes;
90
+ }
91
+ }
92
+ }
93
+ getAllStyleSheetKeyframesRules({ in: source }) {
94
+ if (source instanceof StyleSheetList) {
95
+ return this.#getAllStyleSheetKeyframesRulesInStyleSheetList({
96
+ styleSheetList: source
97
+ });
98
+ }
99
+ else if (source instanceof CSSStyleSheet) {
100
+ return this.#getAllStyleSheetKeyframesRulesInStyleSheet({
101
+ styleSheet: source
102
+ });
103
+ }
104
+ else {
105
+ throw new this.Error.SourceTypeError();
106
+ }
107
+ }
108
+ #getAllStyleSheetKeyframesRulesInStyleSheetList({ styleSheetList }) {
109
+ let keyframesRules = {};
110
+ for (const styleSheet of styleSheetList) {
111
+ const styleSheetKeyframesRules = this.#getAllStyleSheetKeyframesRulesInStyleSheet({
112
+ styleSheet: styleSheet
113
+ });
114
+ keyframesRules = {
115
+ ...keyframesRules,
116
+ ...styleSheetKeyframesRules
117
+ };
118
+ }
119
+ return keyframesRules;
120
+ }
121
+ #getAllStyleSheetKeyframesRulesInStyleSheet({ styleSheet }) {
122
+ let keyframesRules = {};
123
+ for (const rule of styleSheet.cssRules) {
124
+ if (!(rule instanceof CSSKeyframesRule)) {
125
+ continue;
126
+ }
127
+ const keyframes = this.parseKeyframesRule({
128
+ rule: rule
129
+ });
130
+ keyframesRules[rule.name] = keyframes;
131
+ }
132
+ return keyframesRules;
133
+ }
134
+ parseKeyframesRule({ rule: keyframes }) {
135
+ let parsedKeyframes = [];
136
+ for (const keyframe of keyframes) {
137
+ // remove trailing '%'
138
+ /// https://drafts.csswg.org/css-animations/#dom-csskeyframerule-keytext
139
+ const percentString = removeSuffix({
140
+ of: keyframe.keyText,
141
+ suffix: CHARS.PERCENT_SIGN
142
+ });
143
+ const percent = Number(percentString);
144
+ const offset = percent / 100;
145
+ let parsedProperties = {};
146
+ for (const propertyName of keyframe.style) {
147
+ /// https://developer.mozilla.org/en-US/docs/Web/API/CSSStyleDeclaration/getPropertyValue
148
+ const propertyValue = keyframe.style.getPropertyValue(propertyName);
149
+ /// https://drafts.csswg.org/web-animations-1/#ref-for-animation-property-name-to-idl-attribute-name%E2%91%A0
150
+ const attributeName = this.#animationPropertyNameToIDLAttributeName(propertyName);
151
+ parsedProperties[attributeName] = propertyValue;
152
+ }
153
+ const parsedKeyframe = {
154
+ ...parsedProperties,
155
+ offset: offset
156
+ };
157
+ parsedKeyframes.push(parsedKeyframe);
158
+ }
159
+ const parsedKeyframesInstance = new ParsedKeyframes(parsedKeyframes);
160
+ return parsedKeyframesInstance;
161
+ }
162
+ /** https://drafts.csswg.org/web-animations-1/#animation-property-name-to-idl-attribute-name */
163
+ #animationPropertyNameToIDLAttributeName(property) {
164
+ if (this.#isCustomPropertyName(property))
165
+ return property;
166
+ if (property === 'float')
167
+ return 'cssFloat';
168
+ if (property === 'offset')
169
+ return 'cssOffset';
170
+ // https://drafts.csswg.org/cssom/#ref-for-supported-css-property%E2%91%A2
171
+ const lowercaseFirst = this.#isWebkitCasedAttribute(property);
172
+ return this.#cssPropertyToIDLAttribute(property, lowercaseFirst);
173
+ }
174
+ /** https://drafts.csswg.org/cssom/#css-property-to-idl-attribute */
175
+ #cssPropertyToIDLAttribute(property, lowercaseFirst = false) {
176
+ let output = '';
177
+ let uppercaseNext = false;
178
+ if (lowercaseFirst) {
179
+ property = property.slice(1);
180
+ }
181
+ for (const c of property) {
182
+ if (c === CHARS.HYPHEN_MINUS) {
183
+ uppercaseNext = true;
184
+ }
185
+ else if (uppercaseNext) {
186
+ uppercaseNext = false;
187
+ output += c.toUpperCase();
188
+ }
189
+ else {
190
+ output += c;
191
+ }
192
+ }
193
+ return output;
194
+ }
195
+ /** https://drafts.csswg.org/css-variables-2/#typedef-custom-property-name */
196
+ #isCustomPropertyName(property) {
197
+ return property.startsWith(CHARS.DOUBLE_HYPHEN_MINUS) &&
198
+ property !== CHARS.DOUBLE_HYPHEN_MINUS;
199
+ }
200
+ /** https://drafts.csswg.org/cssom/#ref-for-supported-css-property%E2%91%A2 */
201
+ #isWebkitCasedAttribute(property) {
202
+ return property.startsWith(CHARS.WEBKIT_PREFIX);
203
+ }
204
+ }
205
+ export default new KeyframesFactory();
206
+ /** https://drafts.csswg.org/web-animations-1/#the-keyframeeffect-interface */
207
+ export class KeyframeEffectParameters {
208
+ keyframes;
209
+ options;
210
+ /**
211
+ * @param obj.keyframes [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/Web_Animations_API/Keyframe_Formats)
212
+ * @param obj.options [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/KeyframeEffect/KeyframeEffect#options)
213
+ */
214
+ constructor({ keyframes, options = {} }) {
215
+ this.keyframes = keyframes;
216
+ this.options = this.#parseOptionsArg(options);
217
+ }
218
+ /**
219
+ * @param obj.target An element to attach the animation to.
220
+ * @param obj.options Additional keyframe effect options. Can override existing keys.
221
+ * [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/KeyframeEffect/KeyframeEffect#options)
222
+ *
223
+ * @see Specifications:
224
+ * - https://drafts.csswg.org/web-animations-1/#the-keyframeeffect-interface
225
+ * - https://drafts.csswg.org/web-animations-1/#the-animation-interface
226
+ */
227
+ toAnimation({ target, options: additionalOptions = {}, timeline = document.timeline }) {
228
+ additionalOptions = this.#parseOptionsArg(additionalOptions);
229
+ // override existing option keys with additional options
230
+ const options = {
231
+ ...this.options, ...additionalOptions
232
+ };
233
+ const keyframeEffect = new KeyframeEffect(target, this.keyframes, options);
234
+ const animation = new Animation(keyframeEffect, timeline);
235
+ return animation;
236
+ }
237
+ /** https://drafts.csswg.org/web-animations-1/#dom-keyframeeffect-keyframeeffect-target-keyframes-options-options
238
+ https://drafts.csswg.org/web-animations-1/#dom-effecttiming-duration */
239
+ #parseOptionsArg(options) {
240
+ if (typeof options === 'number') {
241
+ return { duration: options };
242
+ }
243
+ return options;
244
+ }
245
+ }
246
+ export class ParsedKeyframes {
247
+ keyframes;
248
+ constructor(keyframes) {
249
+ this.keyframes = keyframes;
250
+ }
251
+ /**
252
+ * @param options
253
+ * [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/KeyframeEffect/KeyframeEffect#options)
254
+ */
255
+ toKeyframeEffect(options) {
256
+ let keyframeEffect;
257
+ // convert (required) nullable to optional
258
+ if (options !== null) {
259
+ keyframeEffect = new KeyframeEffectParameters({
260
+ keyframes: this.keyframes,
261
+ options: options
262
+ });
263
+ }
264
+ else {
265
+ keyframeEffect = new KeyframeEffectParameters({
266
+ keyframes: this.keyframes
267
+ });
268
+ }
269
+ return keyframeEffect;
270
+ }
271
+ }
272
+ // MARK: - Util
273
+ function removeSuffix({ of: string, suffix }) {
274
+ return string.slice(0, -suffix.length);
275
+ }
276
+ async function waitForDocumentLoad({ document }) {
277
+ if (document.readyState === 'complete') {
278
+ return;
279
+ }
280
+ const { promise, resolve } = Promise.withResolvers();
281
+ function onReadyStateChange() {
282
+ if (document.readyState === 'complete') {
283
+ resolve(null);
284
+ }
285
+ }
286
+ const listener = [
287
+ 'readystatechange',
288
+ onReadyStateChange
289
+ ];
290
+ document.addEventListener(...listener);
291
+ await promise;
292
+ document.removeEventListener(...listener);
293
+ }
294
+ //# sourceMappingURL=KeyframeKit.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"KeyframeKit.js","sourceRoot":"","sources":["../src/KeyframeKit.ts"],"names":[],"mappings":"AAAA,EAAE;AACF,cAAc;AACd,aAAa;AACb,GAAG;AACH,2BAA2B;AAC3B,EAAE;AAGF,MAAM,KAAK,GAAG;IACZ,YAAY,EAAE,GAAG;IACjB,YAAY,EAAE,GAAG;IACjB,mBAAmB,EAAE,IAAI;IACzB,aAAa,EAAE,UAAU;CACjB,CAAC;AAKX,MAAM,gBAAgB;IAEX,KAAK,GAAG;QACf,0BAA0B,EAAE,MAAM,0BAA2B,SAAQ,SAAS;YAC5E,OAAO,GAAG,uCAAuC,CAAC;SACnD;QACD,eAAe,EAAE,MAAM,eAAgB,SAAQ,SAAS;YACtD,OAAO,GAAG,6EAA6E,CAAC;SACzF;QACD,qBAAqB,EAAE,MAAM,qBAAsB,SAAQ,KAAK;YAC9D,OAAO,GAAG,uCAAuC,CAAC;SACnD;KACO,CAAC;IAGX,KAAK,CAAC,4BAA4B,CAAC,EAAE,QAAQ,GAAG,MAAM,CAAC,QAAQ,KAE3D,EAAE;QAEJ,MAAM,mBAAmB,CAAC;YACxB,QAAQ,EAAE,QAAQ;SACnB,CAAC,CAAC;QAE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