@weser/keyframes 1.0.0 → 1.0.1

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 CHANGED
@@ -1,3 +1,3 @@
1
1
  # @weser/keyframes
2
2
 
3
- [Documentation](https://packages.weser.io/keyframes)
3
+ [Documentation](https://stack.weser.io/keyframes)
@@ -1,4 +1,3 @@
1
- /// <reference types="react" />
2
1
  export default function createStyleNode(animationName: string, css: string, nonce?: string): import("react").DetailedReactHTMLElement<{
3
2
  dangerouslySetInnerHTML: {
4
3
  __html: string;
@@ -0,0 +1,5 @@
1
+ import createStyleNode from './createStyleNode.js';
2
+ import { T_Keyframe } from './types.js';
3
+ type Node = ReturnType<typeof createStyleNode>;
4
+ export default function createKeyframe(style: T_Keyframe, nonce?: string): [string, Node];
5
+ export {};
@@ -0,0 +1,9 @@
1
+ import getValueFromCache from './getValueFromCache.js';
2
+ import createStyleNode from './createStyleNode.js';
3
+ import getAnimationName from './getAnimationName.js';
4
+ export default function createKeyframe(style, nonce) {
5
+ const animationName = getAnimationName(style);
6
+ const css = getValueFromCache(animationName, style);
7
+ const node = createStyleNode(animationName, css, nonce);
8
+ return [animationName, node];
9
+ }
@@ -0,0 +1,5 @@
1
+ import createStyleNode from './createStyleNode.js';
2
+ import { T_Keyframe } from './types.js';
3
+ type Node = ReturnType<typeof createStyleNode>;
4
+ export default function createKeyframes<T extends string>(keyframes: Record<T, T_Keyframe>, nonce?: string): [Record<T, string>, Node];
5
+ export {};
@@ -0,0 +1,14 @@
1
+ import getValueFromCache from './getValueFromCache.js';
2
+ import createStyleNode from './createStyleNode.js';
3
+ import getAnimationName from './getAnimationName.js';
4
+ export default function createKeyframes(keyframes, nonce) {
5
+ const animationNameMap = {};
6
+ let css = '';
7
+ for (const key in keyframes) {
8
+ const animationName = getAnimationName(keyframes[key]);
9
+ css += getValueFromCache(animationName, keyframes[key]);
10
+ animationNameMap[key] = animationName;
11
+ }
12
+ const node = createStyleNode(Object.keys(keyframes).join('-'), css, nonce);
13
+ return [animationNameMap, node];
14
+ }
@@ -0,0 +1,8 @@
1
+ export default function createStyleNode(animationName: string, css: string, nonce?: string): import("react").DetailedReactHTMLElement<{
2
+ dangerouslySetInnerHTML: {
3
+ __html: string;
4
+ };
5
+ precedence: string;
6
+ href: string;
7
+ nonce: string | undefined;
8
+ }, HTMLElement>;
@@ -0,0 +1,9 @@
1
+ import { createElement } from 'react';
2
+ export default function createStyleNode(animationName, css, nonce) {
3
+ return createElement('style', {
4
+ dangerouslySetInnerHTML: { __html: css },
5
+ precedence: 'low',
6
+ href: animationName,
7
+ nonce,
8
+ });
9
+ }
@@ -0,0 +1,2 @@
1
+ import { T_Keyframe } from './types.js';
2
+ export default function getAnimationName(style: T_Keyframe): string;
@@ -0,0 +1,4 @@
1
+ import hash from './hash.js';
2
+ export default function getAnimationName(style) {
3
+ return '_' + hash(JSON.stringify(style));
4
+ }
@@ -0,0 +1,2 @@
1
+ import { T_Keyframe } from './types.js';
2
+ export default function getValueFromCache(animationName: string, style: T_Keyframe): any;
@@ -0,0 +1,10 @@
1
+ import { cssifyObject } from 'css-in-js-utils';
2
+ const cache = new Map();
3
+ export default function getValueFromCache(animationName, style) {
4
+ if (!cache.has(animationName)) {
5
+ const keyframe = Object.entries(style).reduce((keyframe, [key, declaration = {}]) => keyframe + key + '{' + cssifyObject(declaration) + '}', '');
6
+ const css = `@keyframes ${animationName}{${keyframe}}`;
7
+ cache.set(animationName, css);
8
+ }
9
+ return cache.get(animationName);
10
+ }
@@ -0,0 +1 @@
1
+ export default function hash(str: string): string;
@@ -0,0 +1,16 @@
1
+ // copied from restyle
2
+ export default function hash(str) {
3
+ let h = 0 ^ 0x811c9dc5;
4
+ for (let index = 0; index < str.length; index++) {
5
+ h ^= str.charCodeAt(index);
6
+ h = (h * 0x01000193) >>> 0;
7
+ }
8
+ const letters = 'abcdefghijklmnopqrstuvwxyz';
9
+ const base36 = '0123456789' + letters;
10
+ let result = '';
11
+ do {
12
+ result = base36[h % 36] + result;
13
+ h = Math.floor(h / 36);
14
+ } while (h > 0);
15
+ return `x${result}`;
16
+ }
@@ -0,0 +1,3 @@
1
+ export { default as createKeyframe } from './createKeyframe.js';
2
+ export { default as createKeyframes } from './createKeyframes.js';
3
+ export { T_Keyframe } from './types.js';
@@ -0,0 +1,2 @@
1
+ export { default as createKeyframe } from './createKeyframe.js';
2
+ export { default as createKeyframes } from './createKeyframes.js';
@@ -0,0 +1,4 @@
1
+ import { CSSProperties } from 'react';
2
+ type Key = `${number}%` | 'from' | 'to';
3
+ export type T_Keyframe = Partial<Record<Key, CSSProperties>>;
4
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ {"root":["../vitest.config.ts","../src/createkeyframe.ts","../src/createkeyframes.ts","../src/createstylenode.ts","../src/getanimationname.ts","../src/getvaluefromcache.ts","../src/hash.ts","../src/index.ts","../src/types.ts"],"version":"5.9.3"}
@@ -0,0 +1,2 @@
1
+ declare const _default: import("vite").UserConfig;
2
+ export default _default;
@@ -0,0 +1,11 @@
1
+ import { defineConfig } from 'vitest/config';
2
+ export default defineConfig({
3
+ test: {
4
+ include: ['**/__tests__/**/*.{js,ts}'],
5
+ globals: false,
6
+ environment: 'jsdom',
7
+ },
8
+ esbuild: {
9
+ target: 'esnext',
10
+ },
11
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@weser/keyframes",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "Tiny helpers for CSS keyframes in React",
5
5
  "author": "Robin Weser <robin@weser.io>",
6
6
  "license": "MIT",
@@ -33,7 +33,7 @@
33
33
  "clean": "rimraf dist",
34
34
  "build": "tsc -b",
35
35
  "dev": "pnpm build -w",
36
- "test": "ava"
36
+ "test": "vitest run"
37
37
  },
38
38
  "keywords": [
39
39
  "error handling",
@@ -48,10 +48,11 @@
48
48
  },
49
49
  "devDependencies": {
50
50
  "@types/react": "^18.3.3",
51
- "ava": "^6.1.3",
51
+ "jsdom": "^25.0.1",
52
52
  "react": "canary",
53
53
  "rimraf": "^3.0.2",
54
- "typescript": "^5.4.5"
54
+ "typescript": "^5.4.5",
55
+ "vitest": "^2.1.8"
55
56
  },
56
- "gitHead": "afc0628ca4aeaba255eef9716652640fdc5b50c4"
57
+ "gitHead": "490b5b37e8da7f0cac2880543874914c6f6be5d6"
57
58
  }