keyframekit 1.1.3 → 1.1.5
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.
- package/README.md +7 -4
- package/dist/KeyframeKit.d.ts +41 -8
- package/dist/KeyframeKit.d.ts.map +1 -1
- package/dist/KeyframeKit.js +38 -5
- package/dist/KeyframeKit.js.map +1 -1
- package/docs/.vitepress/components/Playground/Playground.vue +3 -1
- package/docs/.vitepress/config.ts +5 -2
- package/docs/.vitepress/theme/base-styles.css +4 -0
- package/docs/docs/get-started.md +127 -0
- package/docs/docs/index.md +1 -126
- package/docs/docs/public/og-image.png +0 -0
- package/docs/docs/public/playground/KeyframeKit/dist/KeyframeKit.d.ts +41 -8
- package/docs/docs/public/playground/KeyframeKit/dist/KeyframeKit.d.ts.map +1 -1
- package/docs/docs/public/playground/KeyframeKit/dist/KeyframeKit.js +38 -5
- package/docs/docs/public/playground/KeyframeKit/dist/KeyframeKit.js.map +1 -1
- package/docs/docs/reference/classes/KeyframeEffectParameters.md +5 -3
- package/docs/docs/reference/index.md +1 -1
- package/docs/docs/reference/interfaces/KeyframesFactory.md +33 -4
- package/docs/docs/reference/navigation.json +4 -4
- package/docs/docs/reference/type-aliases/CSSStyleSheetSource.md +7 -0
- package/docs/package.json +3 -6
- package/docs/typedoc/plugin-markdown-fix.js +43 -0
- package/docs/typedoc/plugin-param-names.js +10 -45
- package/docs/{typedoc.json → typedoc.config.js} +25 -10
- package/package.json +1 -1
- package/src/KeyframeKit.ts +42 -10
- package/vercel.json +1 -0
- package/docs/docs/reference/_media/LICENSE +0 -21
- package/docs/docs/reference/type-aliases/KeyframesFactorySource.md +0 -9
package/README.md
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
|
-
# <img src="/docs/docs/public/icon.png" width="30px" height="40px">
|
|
1
|
+
# [<img src="/docs/docs/public/icon.png" width="30px" height="40px"> KeyframeKit][1]
|
|
2
2
|
|
|
3
3
|
Unlock fine-grained control over your CSS animations with JavaScript.
|
|
4
4
|
|
|
5
|
+
[](/LICENSE) [](https://www.npmjs.com/package/keyframekit)
|
|
6
|
+
|
|
5
7
|
## About
|
|
6
8
|
|
|
7
9
|
The [Web Animations API][2] opens the browser's animation engine to developers and provides unprecedented control and performance when animating on the web. But confusingly, it provides no easy way to use your existing CSS animations with the API. So I wrote a lightweight, typed, spec-compliant library to convert stylesheet keyframes to Web Animations API-compatible animations, letting you play your CSS-defined animations right in JS. [Read more.][3]
|
|
@@ -77,7 +79,7 @@ const rotateSmallAnimKeyframes = KeyframeKit.getStyleSheetKeyframes({
|
|
|
77
79
|
|
|
78
80
|
### Defining animations in JS
|
|
79
81
|
|
|
80
|
-
The `KeyframeEffectParameters` class provides a more convenient way to define your animations in JS than is offered natively.
|
|
82
|
+
The [`KeyframeEffectParameters`][6] class provides a more convenient way to define your animations in JS than is offered natively.
|
|
81
83
|
It's useful for when you want to have all your animation code in one place.
|
|
82
84
|
|
|
83
85
|
```js
|
|
@@ -110,7 +112,7 @@ attachedAnim.play();
|
|
|
110
112
|
```
|
|
111
113
|
|
|
112
114
|
### Full reference
|
|
113
|
-
[See here.][
|
|
115
|
+
[See here.][7]
|
|
114
116
|
|
|
115
117
|
## Typing
|
|
116
118
|
|
|
@@ -126,4 +128,5 @@ This library is fully compatable with native JS, but it also has full spec-compl
|
|
|
126
128
|
[3]: https://benhatsor.medium.com/99573ef4738b
|
|
127
129
|
[4]: https://developer.mozilla.org/en-US/docs/Web/API/Web_Animations_API/Using_the_Web_Animations_API
|
|
128
130
|
[5]: https://github.com/WICG/construct-stylesheets/issues/119#issuecomment-588352418
|
|
129
|
-
[6]: https://keyframekit.berryscript.com/reference
|
|
131
|
+
[6]: https://keyframekit.berryscript.com/reference/classes/KeyframeEffectParameters
|
|
132
|
+
[7]: https://keyframekit.berryscript.com/reference
|
package/dist/KeyframeKit.d.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
export type
|
|
1
|
+
export type CSSStyleSheetSource = CSSStyleSheet | StyleSheetList;
|
|
2
2
|
declare class KeyframesFactory {
|
|
3
3
|
#private;
|
|
4
4
|
readonly Error: {
|
|
5
|
+
/** Keyframes rule name must be a string. */
|
|
5
6
|
readonly KeyframesRuleNameTypeError: {
|
|
6
7
|
new (message?: string): {
|
|
7
8
|
message: string;
|
|
@@ -29,6 +30,7 @@ declare class KeyframesFactory {
|
|
|
29
30
|
};
|
|
30
31
|
isError(error: unknown): error is Error;
|
|
31
32
|
};
|
|
33
|
+
/** Source must be either a `CSSStyleSheet` or a `StyleSheetList`. */
|
|
32
34
|
readonly SourceTypeError: {
|
|
33
35
|
new (message?: string): {
|
|
34
36
|
message: string;
|
|
@@ -56,6 +58,7 @@ declare class KeyframesFactory {
|
|
|
56
58
|
};
|
|
57
59
|
isError(error: unknown): error is Error;
|
|
58
60
|
};
|
|
61
|
+
/** The stylesheet could not be imported. */
|
|
59
62
|
readonly StyleSheetImportError: {
|
|
60
63
|
new (message?: string): {
|
|
61
64
|
message: string;
|
|
@@ -72,21 +75,48 @@ declare class KeyframesFactory {
|
|
|
72
75
|
isError(error: unknown): error is Error;
|
|
73
76
|
};
|
|
74
77
|
};
|
|
78
|
+
/**
|
|
79
|
+
* Gets a document's stylesheets when it loads,
|
|
80
|
+
* or immediately returns them if it's already loaded.
|
|
81
|
+
* @param obj
|
|
82
|
+
* @param obj.document The document to get stylesheets from.
|
|
83
|
+
*/
|
|
75
84
|
getDocumentStyleSheetsOnLoad({ document }?: {
|
|
76
85
|
document?: Document;
|
|
77
86
|
}): Promise<StyleSheetList>;
|
|
78
|
-
/**
|
|
87
|
+
/**
|
|
88
|
+
* Imports a stylesheet from a URL.
|
|
89
|
+
* @param url The URL of the stylesheet to import.
|
|
90
|
+
* @remarks
|
|
79
91
|
* Note: `@import` rules won't be resolved in imported stylesheets.
|
|
80
|
-
* See https://github.com/WICG/construct-stylesheets/issues/119#issuecomment-588352418.
|
|
92
|
+
* See https://github.com/WICG/construct-stylesheets/issues/119#issuecomment-588352418.
|
|
93
|
+
*/
|
|
81
94
|
importStyleSheet(url: string): Promise<CSSStyleSheet>;
|
|
82
|
-
/**
|
|
95
|
+
/**
|
|
96
|
+
* Gets a CSS keyframes rule from a stylesheet or stylesheet list,
|
|
97
|
+
* then converts it to Web Animations API keyframes.
|
|
98
|
+
* @param obj
|
|
99
|
+
* @param obj.of The name of the `@keyframes` rule to get keyframes from.
|
|
100
|
+
* @param obj.in The stylesheet or stylesheet list where the rule resides.
|
|
101
|
+
*/
|
|
83
102
|
getStyleSheetKeyframes({ of: ruleName, in: source }: {
|
|
84
103
|
of: string;
|
|
85
|
-
in:
|
|
104
|
+
in: CSSStyleSheetSource;
|
|
86
105
|
}): ParsedKeyframes | undefined;
|
|
106
|
+
/**
|
|
107
|
+
* Gets all the CSS keyframes rules in a stylesheet or stylesheet list,
|
|
108
|
+
* then converts them to Web Animations API keyframes.
|
|
109
|
+
* @param obj
|
|
110
|
+
* @param obj.in The style sheet or style sheet list to get keyframes from.
|
|
111
|
+
*/
|
|
87
112
|
getAllStyleSheetKeyframesRules({ in: source }: {
|
|
88
|
-
in:
|
|
113
|
+
in: CSSStyleSheetSource;
|
|
89
114
|
}): ParsedKeyframesRules;
|
|
115
|
+
/**
|
|
116
|
+
* Converts a CSS keyframes rule to Web Animations API keyframes.
|
|
117
|
+
* @param obj
|
|
118
|
+
* @param obj.rule The rule to convert.
|
|
119
|
+
*/
|
|
90
120
|
parseKeyframesRule({ rule: keyframes }: {
|
|
91
121
|
rule: CSSKeyframesRule;
|
|
92
122
|
}): ParsedKeyframes;
|
|
@@ -102,6 +132,7 @@ export declare class KeyframeEffectParameters {
|
|
|
102
132
|
keyframes: KeyframeArgument;
|
|
103
133
|
options: KeyframeEffectOptions;
|
|
104
134
|
/**
|
|
135
|
+
* @param obj
|
|
105
136
|
* @param obj.keyframes [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/Web_Animations_API/Keyframe_Formats)
|
|
106
137
|
* @param obj.options [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/KeyframeEffect/KeyframeEffect#options)
|
|
107
138
|
*/
|
|
@@ -110,9 +141,11 @@ export declare class KeyframeEffectParameters {
|
|
|
110
141
|
options?: number | KeyframeEffectOptions;
|
|
111
142
|
});
|
|
112
143
|
/**
|
|
144
|
+
* @param obj
|
|
113
145
|
* @param obj.target An element to attach the animation to.
|
|
114
146
|
* @param obj.options Additional keyframe effect options. Can override existing keys.
|
|
115
|
-
*
|
|
147
|
+
* [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/KeyframeEffect/KeyframeEffect#options)
|
|
148
|
+
* @param obj.timeline [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/Animation/Animation#timeline)
|
|
116
149
|
*
|
|
117
150
|
* @see Specifications:
|
|
118
151
|
* - https://drafts.csswg.org/web-animations-1/#the-keyframeeffect-interface
|
|
@@ -121,7 +154,7 @@ export declare class KeyframeEffectParameters {
|
|
|
121
154
|
toAnimation({ target, options: additionalOptions, timeline }: {
|
|
122
155
|
target: Element | null;
|
|
123
156
|
options?: number | KeyframeEffectOptions;
|
|
124
|
-
timeline?: AnimationTimeline;
|
|
157
|
+
timeline?: AnimationTimeline | null;
|
|
125
158
|
}): Animation;
|
|
126
159
|
}
|
|
127
160
|
export declare class ParsedKeyframes {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"KeyframeKit.d.ts","sourceRoot":"","sources":["../src/KeyframeKit.ts"],"names":[],"mappings":"AAgBA,MAAM,MAAM,
|
|
1
|
+
{"version":3,"file":"KeyframeKit.d.ts","sourceRoot":"","sources":["../src/KeyframeKit.ts"],"names":[],"mappings":"AAgBA,MAAM,MAAM,mBAAmB,GAAG,aAAa,GAAG,cAAc,CAAC;AAEjE,cAAM,gBAAgB;;IAEpB,QAAQ,CAAC,KAAK;QACZ,4CAA4C;;;;;;;;;;;;;;;;;;;;;;;;;;;;QAI5C,qEAAqE;;;;;;;;;;;;;;;;;;;;;;;;;;;;QAIrE,4C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C,QAAQ,CAAC,EAAE,iBAAiB,GAAG,IAAI,CAAA;KACpC,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"}
|
package/dist/KeyframeKit.js
CHANGED
|
@@ -12,25 +12,38 @@ const CHARS = {
|
|
|
12
12
|
};
|
|
13
13
|
class KeyframesFactory {
|
|
14
14
|
Error = {
|
|
15
|
+
/** Keyframes rule name must be a string. */
|
|
15
16
|
KeyframesRuleNameTypeError: class KeyframesRuleNameTypeError extends TypeError {
|
|
16
17
|
message = `Keyframes rule name must be a string.`;
|
|
17
18
|
},
|
|
19
|
+
/** Source must be either a `CSSStyleSheet` or a `StyleSheetList`. */
|
|
18
20
|
SourceTypeError: class SourceTypeError extends TypeError {
|
|
19
|
-
message = `Source must be either a
|
|
21
|
+
message = `Source must be either a CSSStyleSheet or a StyleSheetList.`;
|
|
20
22
|
},
|
|
23
|
+
/** The stylesheet could not be imported. */
|
|
21
24
|
StyleSheetImportError: class StyleSheetImportError extends Error {
|
|
22
25
|
message = `The stylesheet could not be imported.`;
|
|
23
26
|
}
|
|
24
27
|
};
|
|
28
|
+
/**
|
|
29
|
+
* Gets a document's stylesheets when it loads,
|
|
30
|
+
* or immediately returns them if it's already loaded.
|
|
31
|
+
* @param obj
|
|
32
|
+
* @param obj.document The document to get stylesheets from.
|
|
33
|
+
*/
|
|
25
34
|
async getDocumentStyleSheetsOnLoad({ document = window.document } = {}) {
|
|
26
35
|
await waitForDocumentLoad({
|
|
27
36
|
document: document
|
|
28
37
|
});
|
|
29
38
|
return document.styleSheets;
|
|
30
39
|
}
|
|
31
|
-
/**
|
|
40
|
+
/**
|
|
41
|
+
* Imports a stylesheet from a URL.
|
|
42
|
+
* @param url The URL of the stylesheet to import.
|
|
43
|
+
* @remarks
|
|
32
44
|
* Note: `@import` rules won't be resolved in imported stylesheets.
|
|
33
|
-
* See https://github.com/WICG/construct-stylesheets/issues/119#issuecomment-588352418.
|
|
45
|
+
* See https://github.com/WICG/construct-stylesheets/issues/119#issuecomment-588352418.
|
|
46
|
+
*/
|
|
34
47
|
async importStyleSheet(url) {
|
|
35
48
|
const resp = await fetch(url);
|
|
36
49
|
if (!resp.ok) {
|
|
@@ -45,7 +58,13 @@ class KeyframesFactory {
|
|
|
45
58
|
await styleSheet.replace(respText);
|
|
46
59
|
return styleSheet;
|
|
47
60
|
}
|
|
48
|
-
/**
|
|
61
|
+
/**
|
|
62
|
+
* Gets a CSS keyframes rule from a stylesheet or stylesheet list,
|
|
63
|
+
* then converts it to Web Animations API keyframes.
|
|
64
|
+
* @param obj
|
|
65
|
+
* @param obj.of The name of the `@keyframes` rule to get keyframes from.
|
|
66
|
+
* @param obj.in The stylesheet or stylesheet list where the rule resides.
|
|
67
|
+
*/
|
|
49
68
|
getStyleSheetKeyframes({ of: ruleName, in: source }) {
|
|
50
69
|
if (typeof ruleName !== 'string') {
|
|
51
70
|
throw new this.Error.KeyframesRuleNameTypeError();
|
|
@@ -90,6 +109,12 @@ class KeyframesFactory {
|
|
|
90
109
|
}
|
|
91
110
|
}
|
|
92
111
|
}
|
|
112
|
+
/**
|
|
113
|
+
* Gets all the CSS keyframes rules in a stylesheet or stylesheet list,
|
|
114
|
+
* then converts them to Web Animations API keyframes.
|
|
115
|
+
* @param obj
|
|
116
|
+
* @param obj.in The style sheet or style sheet list to get keyframes from.
|
|
117
|
+
*/
|
|
93
118
|
getAllStyleSheetKeyframesRules({ in: source }) {
|
|
94
119
|
if (source instanceof StyleSheetList) {
|
|
95
120
|
return this.#getAllStyleSheetKeyframesRulesInStyleSheetList({
|
|
@@ -131,6 +156,11 @@ class KeyframesFactory {
|
|
|
131
156
|
}
|
|
132
157
|
return keyframesRules;
|
|
133
158
|
}
|
|
159
|
+
/**
|
|
160
|
+
* Converts a CSS keyframes rule to Web Animations API keyframes.
|
|
161
|
+
* @param obj
|
|
162
|
+
* @param obj.rule The rule to convert.
|
|
163
|
+
*/
|
|
134
164
|
parseKeyframesRule({ rule: keyframes }) {
|
|
135
165
|
let parsedKeyframes = [];
|
|
136
166
|
for (const keyframe of keyframes) {
|
|
@@ -208,6 +238,7 @@ export class KeyframeEffectParameters {
|
|
|
208
238
|
keyframes;
|
|
209
239
|
options;
|
|
210
240
|
/**
|
|
241
|
+
* @param obj
|
|
211
242
|
* @param obj.keyframes [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/Web_Animations_API/Keyframe_Formats)
|
|
212
243
|
* @param obj.options [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/KeyframeEffect/KeyframeEffect#options)
|
|
213
244
|
*/
|
|
@@ -216,9 +247,11 @@ export class KeyframeEffectParameters {
|
|
|
216
247
|
this.options = this.#parseOptionsArg(options);
|
|
217
248
|
}
|
|
218
249
|
/**
|
|
250
|
+
* @param obj
|
|
219
251
|
* @param obj.target An element to attach the animation to.
|
|
220
252
|
* @param obj.options Additional keyframe effect options. Can override existing keys.
|
|
221
|
-
*
|
|
253
|
+
* [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/KeyframeEffect/KeyframeEffect#options)
|
|
254
|
+
* @param obj.timeline [MDN Reference](https://developer.mozilla.org/en-US/docs/Web/API/Animation/Animation#timeline)
|
|
222
255
|
*
|
|
223
256
|
* @see Specifications:
|
|
224
257
|
* - https://drafts.csswg.org/web-animations-1/#the-keyframeeffect-interface
|
package/dist/KeyframeKit.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"KeyframeKit.js","sourceRoot":"","sources":["../src/KeyframeKit.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAGH,MAAM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G,OAAO,CAAC,aAAa,EAAE,CAAC;IAErD,SAAS,kBAAkB;QACzB,IAAI,QAAQ,CAAC,UAAU,KAAK,UAAU,EAAE,CAAC;YACvC,OAAO,CAAC,IAAI,CAAC,CAAC;QAChB,CAAC;IACH,CAAC;IAED,MAAM,QAAQ,GAAG;QACf,kBAAkB;QAClB,kBAAkB;KACV,CAAC;IAEX,QAAQ,CAAC,gBAAgB,CAAC,GAAG,QAAQ,CAAC,CAAC;IAEvC,MAAM,OAAO,CAAC;IAEd,QAAQ,CAAC,mBAAmB,CAAC,GAAG,QAAQ,CAAC,CAAC;AAE5C,CAAC"}
|
|
@@ -62,7 +62,7 @@ const { tabs, activeTab, isLoaded, setEditorRef } = playground(props, previewFra
|
|
|
62
62
|
--border-radius: 8px;
|
|
63
63
|
border-radius: var(--border-radius);
|
|
64
64
|
overflow: hidden;
|
|
65
|
-
margin-bottom: 48px;
|
|
65
|
+
/* margin-bottom: 48px; */
|
|
66
66
|
}
|
|
67
67
|
|
|
68
68
|
@media (max-width: 1000px) {
|
|
@@ -88,11 +88,13 @@ const { tabs, activeTab, isLoaded, setEditorRef } = playground(props, previewFra
|
|
|
88
88
|
}
|
|
89
89
|
}
|
|
90
90
|
|
|
91
|
+
/*
|
|
91
92
|
@media (min-width: 640px) {
|
|
92
93
|
.playground {
|
|
93
94
|
margin-bottom: 64px;
|
|
94
95
|
}
|
|
95
96
|
}
|
|
97
|
+
*/
|
|
96
98
|
|
|
97
99
|
.editor-pane {
|
|
98
100
|
display: flex;
|
|
@@ -11,6 +11,8 @@ export default defineConfig({
|
|
|
11
11
|
//'reference/:slug*': ':slug*'
|
|
12
12
|
},
|
|
13
13
|
|
|
14
|
+
cleanUrls: true,
|
|
15
|
+
|
|
14
16
|
head: [
|
|
15
17
|
['link', { rel: 'icon', href: '/icon.png' }],
|
|
16
18
|
['meta', { property: 'og:image', content: '/og-image.png' }],
|
|
@@ -41,14 +43,15 @@ export default defineConfig({
|
|
|
41
43
|
// https://vitepress.dev/reference/default-theme-config
|
|
42
44
|
nav: [
|
|
43
45
|
{ text: 'Home', link: '/' },
|
|
44
|
-
{ text: '
|
|
46
|
+
{ text: 'Get Started', link: '/get-started' },
|
|
47
|
+
{ text: 'Reference', link: '/reference/' }
|
|
45
48
|
],
|
|
46
49
|
|
|
47
50
|
sidebar: {
|
|
48
51
|
'/reference/': [
|
|
49
52
|
{
|
|
50
53
|
text: 'Reference',
|
|
51
|
-
link: '/reference',
|
|
54
|
+
link: '/reference/',
|
|
52
55
|
items: referenceNavigationItems
|
|
53
56
|
}
|
|
54
57
|
]
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
|
|
2
|
+
# Get Started
|
|
3
|
+
|
|
4
|
+
## About
|
|
5
|
+
|
|
6
|
+
The [Web Animations API][1] opens the browser's animation engine to developers and provides unprecedented control and performance when animating on the web. But confusingly, it provides no easy way to use your existing CSS animations with the API. So I wrote a lightweight, typed, spec-compliant library to convert stylesheet keyframes to Web Animations API-compatible animations, letting you play your CSS-defined animations right in JS. [Read more.][2]
|
|
7
|
+
|
|
8
|
+
## Installation
|
|
9
|
+
|
|
10
|
+
::: code-group
|
|
11
|
+
```sh [npm]
|
|
12
|
+
npm install keyframekit
|
|
13
|
+
```
|
|
14
|
+
:::
|
|
15
|
+
|
|
16
|
+
## Usage
|
|
17
|
+
|
|
18
|
+
### Playing CSS-defined animations with JS
|
|
19
|
+
|
|
20
|
+
In your CSS:
|
|
21
|
+
```css
|
|
22
|
+
@keyframes rotate-small { ... }
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
Then, in JS:
|
|
26
|
+
```js
|
|
27
|
+
import KeyframeKit from 'keyframekit';
|
|
28
|
+
|
|
29
|
+
const documentStyleSheets = await KeyframeKit.getDocumentStyleSheetsOnLoad();
|
|
30
|
+
|
|
31
|
+
// get animation keyframes from the document's stylesheets
|
|
32
|
+
const rotateSmallAnimKeyframes = KeyframeKit.getStyleSheetKeyframes({
|
|
33
|
+
of: 'rotate-small',
|
|
34
|
+
in: documentStyleSheets
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
// then, define your animation
|
|
38
|
+
const rotateSmallAnim = rotateSmallAnimKeyframes.toKeyframeEffect({
|
|
39
|
+
duration: 700,
|
|
40
|
+
easing: 'ease'
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
// finally, attach it to an element:
|
|
44
|
+
const attachedAnim = rotateSmallAnim.toAnimation({
|
|
45
|
+
target: document.querySelector('.el')
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
attachedAnim.play();
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
The primary reason to play your animation with JS is because you get way more control over its playback:
|
|
52
|
+
```js
|
|
53
|
+
attachedAnim.pause();
|
|
54
|
+
attachedAnim.playbackRate = -1;
|
|
55
|
+
const progress = attachedAnim.overallProgress; // 0 to 1 (Baseline newly available)
|
|
56
|
+
await attachedAnim.finished;
|
|
57
|
+
```
|
|
58
|
+
[...and more.][3]
|
|
59
|
+
|
|
60
|
+
### Importing animations directly from a CSS file
|
|
61
|
+
|
|
62
|
+
Instead of getting an animation from the document's stylesheets, you can also import it directly from a CSS file.
|
|
63
|
+
|
|
64
|
+
```js
|
|
65
|
+
import KeyframeKit from 'keyframekit';
|
|
66
|
+
|
|
67
|
+
const styleSheet = await KeyframeKit.importStyleSheet('./styles.css');
|
|
68
|
+
|
|
69
|
+
// get animation keyframes from stylesheet
|
|
70
|
+
const rotateSmallAnimKeyframes = KeyframeKit.getStyleSheetKeyframes({
|
|
71
|
+
of: 'rotate-small',
|
|
72
|
+
in: styleSheet
|
|
73
|
+
});
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
> Note: This won't resolve any `@import` rules in the stylesheet. [See more.][4]
|
|
77
|
+
|
|
78
|
+
### Defining animations in JS
|
|
79
|
+
|
|
80
|
+
The [`KeyframeEffectParameters`](reference/classes/KeyframeEffectParameters.md) class provides a more convenient way to define your animations in JS than is offered natively.
|
|
81
|
+
It's useful for when you want to have all your animation code in one place.
|
|
82
|
+
|
|
83
|
+
```js
|
|
84
|
+
import { KeyframeEffectParameters } from 'keyframekit';
|
|
85
|
+
|
|
86
|
+
// define your animation
|
|
87
|
+
const linkTextHoverAnim = new KeyframeEffectParameters({
|
|
88
|
+
|
|
89
|
+
keyframes: {
|
|
90
|
+
// 0 to 1. equivalent to CSS keyframe percentage values:
|
|
91
|
+
offset: [0, 0.499, 0.5, 1],
|
|
92
|
+
// respective CSS property keyframes:
|
|
93
|
+
clipPath: ['inset(0 0 0 0)', 'inset(100% 0 0 0)', 'inset(0 0 100% 0)', 'inset(0 0 0 0)'],
|
|
94
|
+
top: ['0', '-20px', '20px', '0']
|
|
95
|
+
},
|
|
96
|
+
|
|
97
|
+
options: {
|
|
98
|
+
duration: 700,
|
|
99
|
+
easing: 'ease'
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
// then, attach it to an element:
|
|
105
|
+
const attachedAnim = linkTextHoverAnim.toAnimation({
|
|
106
|
+
target: document.querySelector('.link')
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
attachedAnim.play();
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
### Full reference
|
|
113
|
+
[See here.](/reference/index.md)
|
|
114
|
+
|
|
115
|
+
## Typing
|
|
116
|
+
|
|
117
|
+
This library is fully compatable with native JS, but it also has full spec-compliant type support, including declaration files and source maps.
|
|
118
|
+
|
|
119
|
+
## License
|
|
120
|
+
|
|
121
|
+
[MIT](https://github.com/benhatsor/KeyframeKit/blob/main/LICENSE)
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
[1]: https://developer.mozilla.org/en-US/docs/Web/API/Web_Animations_API
|
|
125
|
+
[2]: https://benhatsor.medium.com/99573ef4738b
|
|
126
|
+
[3]: https://developer.mozilla.org/en-US/docs/Web/API/Web_Animations_API/Using_the_Web_Animations_API
|
|
127
|
+
[4]: https://github.com/WICG/construct-stylesheets/issues/119#issuecomment-588352418
|
package/docs/docs/index.md
CHANGED
|
@@ -8,135 +8,10 @@ hero:
|
|
|
8
8
|
actions:
|
|
9
9
|
- theme: brand
|
|
10
10
|
text: Get Started
|
|
11
|
-
link:
|
|
11
|
+
link: /get-started
|
|
12
12
|
- theme: alt
|
|
13
13
|
text: Reference
|
|
14
14
|
link: /reference
|
|
15
15
|
---
|
|
16
16
|
|
|
17
17
|
<Playground/>
|
|
18
|
-
|
|
19
|
-
## About
|
|
20
|
-
|
|
21
|
-
The [Web Animations API][2] opens the browser's animation engine to developers and provides unprecedented control and performance when animating on the web. But confusingly, it provides no easy way to use your existing CSS animations with the API. So I wrote a lightweight, typed, spec-compliant library to convert stylesheet keyframes to Web Animations API-compatible animations, letting you play your CSS-defined animations right in JS. [Read more.][3]
|
|
22
|
-
|
|
23
|
-
## Installation
|
|
24
|
-
|
|
25
|
-
::: code-group
|
|
26
|
-
```sh [npm]
|
|
27
|
-
npm install keyframekit
|
|
28
|
-
```
|
|
29
|
-
:::
|
|
30
|
-
|
|
31
|
-
## Usage
|
|
32
|
-
|
|
33
|
-
### Playing CSS-defined animations with JS
|
|
34
|
-
|
|
35
|
-
In your CSS:
|
|
36
|
-
```css
|
|
37
|
-
@keyframes rotate-small { ... }
|
|
38
|
-
```
|
|
39
|
-
|
|
40
|
-
Then, in JS:
|
|
41
|
-
```js
|
|
42
|
-
import KeyframeKit from 'keyframekit';
|
|
43
|
-
|
|
44
|
-
const documentStyleSheets = await KeyframeKit.getDocumentStyleSheetsOnLoad();
|
|
45
|
-
|
|
46
|
-
// get animation keyframes from the document's stylesheets
|
|
47
|
-
const rotateSmallAnimKeyframes = KeyframeKit.getStyleSheetKeyframes({
|
|
48
|
-
of: 'rotate-small',
|
|
49
|
-
in: documentStyleSheets
|
|
50
|
-
});
|
|
51
|
-
|
|
52
|
-
// then, define your animation
|
|
53
|
-
const rotateSmallAnim = rotateSmallAnimKeyframes.toKeyframeEffect({
|
|
54
|
-
duration: 700,
|
|
55
|
-
easing: 'ease'
|
|
56
|
-
});
|
|
57
|
-
|
|
58
|
-
// finally, attach it to an element:
|
|
59
|
-
const attachedAnim = rotateSmallAnim.toAnimation({
|
|
60
|
-
target: document.querySelector('.el')
|
|
61
|
-
});
|
|
62
|
-
|
|
63
|
-
attachedAnim.play();
|
|
64
|
-
```
|
|
65
|
-
|
|
66
|
-
The primary reason to play your animation with JS is because you get way more control over its playback:
|
|
67
|
-
```js
|
|
68
|
-
attachedAnim.pause();
|
|
69
|
-
attachedAnim.playbackRate = -1;
|
|
70
|
-
const progress = attachedAnim.overallProgress; // 0 to 1 (Baseline newly available)
|
|
71
|
-
await attachedAnim.finished;
|
|
72
|
-
```
|
|
73
|
-
[...and more.][3]
|
|
74
|
-
|
|
75
|
-
### Importing animations directly from a CSS file
|
|
76
|
-
|
|
77
|
-
Instead of getting an animation from the document's stylesheets, you can also import it directly from a CSS file.
|
|
78
|
-
|
|
79
|
-
```js
|
|
80
|
-
import KeyframeKit from 'keyframekit';
|
|
81
|
-
|
|
82
|
-
const styleSheet = await KeyframeKit.importStyleSheet('./styles.css');
|
|
83
|
-
|
|
84
|
-
// get animation keyframes from stylesheet
|
|
85
|
-
const rotateSmallAnimKeyframes = KeyframeKit.getStyleSheetKeyframes({
|
|
86
|
-
of: 'rotate-small',
|
|
87
|
-
in: styleSheet
|
|
88
|
-
});
|
|
89
|
-
```
|
|
90
|
-
|
|
91
|
-
> Note: This won't resolve any `@import` rules in the stylesheet. [See more.][4]
|
|
92
|
-
|
|
93
|
-
### Defining animations in JS
|
|
94
|
-
|
|
95
|
-
The `KeyframeEffectParameters` class provides a more convenient way to define your animations in JS than is offered natively.
|
|
96
|
-
It's useful for when you want to have all your animation code in one place.
|
|
97
|
-
|
|
98
|
-
```js
|
|
99
|
-
import { KeyframeEffectParameters } from 'keyframekit';
|
|
100
|
-
|
|
101
|
-
// define your animation
|
|
102
|
-
const linkTextHoverAnim = new KeyframeEffectParameters({
|
|
103
|
-
|
|
104
|
-
keyframes: {
|
|
105
|
-
// 0 to 1. equivalent to CSS keyframe percentage values:
|
|
106
|
-
offset: [0, 0.499, 0.5, 1],
|
|
107
|
-
// respective CSS property keyframes:
|
|
108
|
-
clipPath: ['inset(0 0 0 0)', 'inset(100% 0 0 0)', 'inset(0 0 100% 0)', 'inset(0 0 0 0)'],
|
|
109
|
-
top: ['0', '-20px', '20px', '0']
|
|
110
|
-
},
|
|
111
|
-
|
|
112
|
-
options: {
|
|
113
|
-
duration: 700,
|
|
114
|
-
easing: 'ease'
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
});
|
|
118
|
-
|
|
119
|
-
// then, attach it to an element:
|
|
120
|
-
const attachedAnim = linkTextHoverAnim.toAnimation({
|
|
121
|
-
target: document.querySelector('.link')
|
|
122
|
-
});
|
|
123
|
-
|
|
124
|
-
attachedAnim.play();
|
|
125
|
-
```
|
|
126
|
-
|
|
127
|
-
### Full reference
|
|
128
|
-
[See here.](/reference/index.md)
|
|
129
|
-
|
|
130
|
-
## Typing
|
|
131
|
-
|
|
132
|
-
This library is fully compatable with native JS, but it also has full spec-compliant type support, including declaration files and source maps.
|
|
133
|
-
|
|
134
|
-
## License
|
|
135
|
-
|
|
136
|
-
[MIT](https://github.com/benhatsor/KeyframeKit/blob/main/LICENSE)
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
[1]: https://developer.mozilla.org/en-US/docs/Web/API/Web_Animations_API
|
|
140
|
-
[2]: https://benhatsor.medium.com/99573ef4738b
|
|
141
|
-
[3]: https://developer.mozilla.org/en-US/docs/Web/API/Web_Animations_API/Using_the_Web_Animations_API
|
|
142
|
-
[4]: https://github.com/WICG/construct-stylesheets/issues/119#issuecomment-588352418
|
|
Binary file
|