@kmotion/animation 0.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.
- package/LICENSE +21 -0
- package/README.md +83 -0
- package/package.json +51 -0
- package/src/core.js +123 -0
- package/src/js.js +33 -0
- package/src/react.js +26 -0
- package/src/solid.js +33 -0
- package/src/svelte.js +42 -0
- package/src/vue.js +27 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 kmotion
|
|
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,83 @@
|
|
|
1
|
+
# @kmotion/animation
|
|
2
|
+
|
|
3
|
+
Embed any Kmotion template preview in **React, Vue, Svelte, Solid, or vanilla JS**.
|
|
4
|
+
|
|
5
|
+
```bash
|
|
6
|
+
npm install @kmotion/animation
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
Default origin is `http://localhost:5173` (local Kmotion gallery). After you deploy the site, call `setOrigin('https://your-domain.com')` once, or pass `origin` on each preview.
|
|
10
|
+
|
|
11
|
+
## React
|
|
12
|
+
|
|
13
|
+
```jsx
|
|
14
|
+
import { Preview } from '@kmotion/animation/react';
|
|
15
|
+
|
|
16
|
+
export function App() {
|
|
17
|
+
return <Preview id="heritage-grove" />;
|
|
18
|
+
}
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## Vue 3
|
|
22
|
+
|
|
23
|
+
```vue
|
|
24
|
+
<script setup>
|
|
25
|
+
import { Preview } from '@kmotion/animation/vue';
|
|
26
|
+
</script>
|
|
27
|
+
|
|
28
|
+
<template>
|
|
29
|
+
<Preview id="heritage-grove" />
|
|
30
|
+
</template>
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## Svelte
|
|
34
|
+
|
|
35
|
+
```svelte
|
|
36
|
+
<script>
|
|
37
|
+
import { preview } from '@kmotion/animation/svelte';
|
|
38
|
+
</script>
|
|
39
|
+
|
|
40
|
+
<div use:preview={{ id: 'heritage-grove' }}></div>
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## Solid
|
|
44
|
+
|
|
45
|
+
```jsx
|
|
46
|
+
import { Preview } from '@kmotion/animation/solid';
|
|
47
|
+
|
|
48
|
+
export function App() {
|
|
49
|
+
return Preview({ id: 'heritage-grove' });
|
|
50
|
+
}
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
## Vanilla JS
|
|
54
|
+
|
|
55
|
+
```js
|
|
56
|
+
import { Preview } from '@kmotion/animation';
|
|
57
|
+
|
|
58
|
+
Preview(document.querySelector('#app'), { id: 'heritage-grove' });
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
Or the web component after any import:
|
|
62
|
+
|
|
63
|
+
```html
|
|
64
|
+
<kmotion-preview template="heritage-grove" height="640px"></kmotion-preview>
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
## Props
|
|
68
|
+
|
|
69
|
+
| Prop | Type | Default | Description |
|
|
70
|
+
| --- | --- | --- | --- |
|
|
71
|
+
| `id` | string | required | Template id, e.g. `heritage-grove` |
|
|
72
|
+
| `origin` | string | `http://localhost:5173` | Kmotion site origin |
|
|
73
|
+
| `height` | string | `640px` | Preview height |
|
|
74
|
+
| `title` | string | `Kmotion preview` | iframe title |
|
|
75
|
+
|
|
76
|
+
```js
|
|
77
|
+
import { setOrigin, TEMPLATES } from '@kmotion/animation';
|
|
78
|
+
|
|
79
|
+
setOrigin('https://your-kmotion-site.com');
|
|
80
|
+
console.log(TEMPLATES);
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
Ids: `3d-portfolio`, `prompt`, `neo-museum`, `portfolio-cosmic`, `adam-roberts`, `lumina`, `heritage-grove`.
|
package/package.json
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@kmotion/animation",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Embed any Kmotion template preview in React, Vue, Svelte, Solid, or vanilla JS.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"author": "kmotion",
|
|
8
|
+
"keywords": [
|
|
9
|
+
"kmotion",
|
|
10
|
+
"animation",
|
|
11
|
+
"preview",
|
|
12
|
+
"embed",
|
|
13
|
+
"react",
|
|
14
|
+
"vue",
|
|
15
|
+
"svelte",
|
|
16
|
+
"solid"
|
|
17
|
+
],
|
|
18
|
+
"sideEffects": [
|
|
19
|
+
"./src/core.js"
|
|
20
|
+
],
|
|
21
|
+
"exports": {
|
|
22
|
+
".": "./src/js.js",
|
|
23
|
+
"./react": "./src/react.js",
|
|
24
|
+
"./vue": "./src/vue.js",
|
|
25
|
+
"./svelte": "./src/svelte.js",
|
|
26
|
+
"./solid": "./src/solid.js",
|
|
27
|
+
"./js": "./src/js.js",
|
|
28
|
+
"./core": "./src/core.js"
|
|
29
|
+
},
|
|
30
|
+
"files": [
|
|
31
|
+
"src",
|
|
32
|
+
"README.md",
|
|
33
|
+
"LICENSE"
|
|
34
|
+
],
|
|
35
|
+
"peerDependencies": {
|
|
36
|
+
"react": ">=17",
|
|
37
|
+
"solid-js": ">=1",
|
|
38
|
+
"vue": ">=3"
|
|
39
|
+
},
|
|
40
|
+
"peerDependenciesMeta": {
|
|
41
|
+
"react": {
|
|
42
|
+
"optional": true
|
|
43
|
+
},
|
|
44
|
+
"solid-js": {
|
|
45
|
+
"optional": true
|
|
46
|
+
},
|
|
47
|
+
"vue": {
|
|
48
|
+
"optional": true
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
package/src/core.js
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
const TAG = 'kmotion-preview';
|
|
2
|
+
|
|
3
|
+
/** Known live template ids on the Kmotion site */
|
|
4
|
+
export const TEMPLATES = [
|
|
5
|
+
'3d-portfolio',
|
|
6
|
+
'prompt',
|
|
7
|
+
'neo-museum',
|
|
8
|
+
'portfolio-cosmic',
|
|
9
|
+
'adam-roberts',
|
|
10
|
+
'lumina',
|
|
11
|
+
'heritage-grove',
|
|
12
|
+
];
|
|
13
|
+
|
|
14
|
+
const LOCAL_ORIGIN = 'http://localhost:5173';
|
|
15
|
+
|
|
16
|
+
function trimSlash(url) {
|
|
17
|
+
return String(url || '').replace(/\/+$/, '');
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function getOrigin(override) {
|
|
21
|
+
if (override) return trimSlash(override);
|
|
22
|
+
if (typeof globalThis !== 'undefined' && globalThis.KMOTION_ORIGIN) {
|
|
23
|
+
return trimSlash(globalThis.KMOTION_ORIGIN);
|
|
24
|
+
}
|
|
25
|
+
return LOCAL_ORIGIN;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** Set the Kmotion site once (after you deploy, pass the public HTTPS origin). */
|
|
29
|
+
export function setOrigin(url) {
|
|
30
|
+
if (typeof globalThis === 'undefined') return;
|
|
31
|
+
globalThis.KMOTION_ORIGIN = trimSlash(url);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function embedUrl(id, origin) {
|
|
35
|
+
const templateId = String(id || '').trim();
|
|
36
|
+
if (!templateId) {
|
|
37
|
+
throw new Error('kmotion: Preview needs an id, e.g. "heritage-grove"');
|
|
38
|
+
}
|
|
39
|
+
return `${getOrigin(origin)}/p/${encodeURIComponent(templateId)}?embed=1`;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function escapeAttr(value) {
|
|
43
|
+
return String(value ?? '')
|
|
44
|
+
.replace(/&/g, '&')
|
|
45
|
+
.replace(/"/g, '"')
|
|
46
|
+
.replace(/</g, '<');
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
class KmotionPreview extends HTMLElement {
|
|
50
|
+
static get observedAttributes() {
|
|
51
|
+
return ['template', 'id', 'origin', 'height', 'title'];
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
connectedCallback() {
|
|
55
|
+
this.render();
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
attributeChangedCallback() {
|
|
59
|
+
this.render();
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
getTemplateId() {
|
|
63
|
+
return this.getAttribute('template') || this.getAttribute('id') || '';
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
render() {
|
|
67
|
+
if (!this.shadowRoot) this.attachShadow({ mode: 'open' });
|
|
68
|
+
|
|
69
|
+
const templateId = this.getTemplateId();
|
|
70
|
+
const origin = this.getAttribute('origin') || '';
|
|
71
|
+
const height = this.getAttribute('height') || '640px';
|
|
72
|
+
const title = this.getAttribute('title') || 'Kmotion preview';
|
|
73
|
+
|
|
74
|
+
let src = '';
|
|
75
|
+
let error = '';
|
|
76
|
+
try {
|
|
77
|
+
src = embedUrl(templateId, origin);
|
|
78
|
+
} catch (err) {
|
|
79
|
+
error = err instanceof Error ? err.message : 'kmotion: invalid preview';
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
this.shadowRoot.innerHTML = `
|
|
83
|
+
<style>
|
|
84
|
+
:host {
|
|
85
|
+
display: block;
|
|
86
|
+
width: 100%;
|
|
87
|
+
height: ${escapeAttr(height)};
|
|
88
|
+
}
|
|
89
|
+
iframe, .kmotion-msg {
|
|
90
|
+
width: 100%;
|
|
91
|
+
height: 100%;
|
|
92
|
+
border: 0;
|
|
93
|
+
display: block;
|
|
94
|
+
background: #0c0c0c;
|
|
95
|
+
}
|
|
96
|
+
.kmotion-msg {
|
|
97
|
+
box-sizing: border-box;
|
|
98
|
+
padding: 24px;
|
|
99
|
+
color: #a1a1aa;
|
|
100
|
+
font: 14px/1.5 ui-sans-serif, system-ui, sans-serif;
|
|
101
|
+
}
|
|
102
|
+
</style>
|
|
103
|
+
${
|
|
104
|
+
error
|
|
105
|
+
? `<div class="kmotion-msg">${escapeAttr(error)}</div>`
|
|
106
|
+
: `<iframe
|
|
107
|
+
src="${escapeAttr(src)}"
|
|
108
|
+
title="${escapeAttr(title)}"
|
|
109
|
+
loading="lazy"
|
|
110
|
+
allow="autoplay; fullscreen"
|
|
111
|
+
></iframe>`
|
|
112
|
+
}
|
|
113
|
+
`;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
export function definePreview() {
|
|
118
|
+
if (typeof customElements === 'undefined') return TAG;
|
|
119
|
+
if (!customElements.get(TAG)) customElements.define(TAG, KmotionPreview);
|
|
120
|
+
return TAG;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
definePreview();
|
package/src/js.js
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { definePreview, embedUrl } from './core.js';
|
|
2
|
+
|
|
3
|
+
definePreview();
|
|
4
|
+
|
|
5
|
+
export { TEMPLATES, embedUrl, setOrigin, getOrigin, definePreview } from './core.js';
|
|
6
|
+
|
|
7
|
+
function applyProps(el, { id, origin, height = '640px', title = 'Kmotion preview' }) {
|
|
8
|
+
el.setAttribute('template', id);
|
|
9
|
+
if (origin) el.setAttribute('origin', origin);
|
|
10
|
+
else el.removeAttribute('origin');
|
|
11
|
+
el.setAttribute('height', height);
|
|
12
|
+
el.setAttribute('title', title);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function Preview(target, props) {
|
|
16
|
+
if (!target) throw new Error('kmotion: Preview needs a DOM node');
|
|
17
|
+
definePreview();
|
|
18
|
+
const el = document.createElement('kmotion-preview');
|
|
19
|
+
applyProps(el, props || {});
|
|
20
|
+
target.appendChild(el);
|
|
21
|
+
return {
|
|
22
|
+
el,
|
|
23
|
+
src: embedUrl(props?.id, props?.origin),
|
|
24
|
+
update(next) {
|
|
25
|
+
applyProps(el, next || {});
|
|
26
|
+
},
|
|
27
|
+
destroy() {
|
|
28
|
+
el.remove();
|
|
29
|
+
},
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export default Preview;
|
package/src/react.js
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { createElement } from 'react';
|
|
2
|
+
import { definePreview } from './core.js';
|
|
3
|
+
|
|
4
|
+
definePreview();
|
|
5
|
+
|
|
6
|
+
export { TEMPLATES, embedUrl, setOrigin, getOrigin, definePreview } from './core.js';
|
|
7
|
+
|
|
8
|
+
export function Preview({
|
|
9
|
+
id,
|
|
10
|
+
origin,
|
|
11
|
+
height = '640px',
|
|
12
|
+
title = 'Kmotion preview',
|
|
13
|
+
className,
|
|
14
|
+
style,
|
|
15
|
+
}) {
|
|
16
|
+
return createElement('kmotion-preview', {
|
|
17
|
+
template: id,
|
|
18
|
+
origin,
|
|
19
|
+
height,
|
|
20
|
+
title,
|
|
21
|
+
className,
|
|
22
|
+
style,
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export default Preview;
|
package/src/solid.js
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { createEffect, onCleanup } from 'solid-js';
|
|
2
|
+
import { definePreview } from './core.js';
|
|
3
|
+
|
|
4
|
+
definePreview();
|
|
5
|
+
|
|
6
|
+
export { TEMPLATES, embedUrl, setOrigin, getOrigin, definePreview } from './core.js';
|
|
7
|
+
|
|
8
|
+
function applyProps(el, props) {
|
|
9
|
+
el.setAttribute('template', props.id || '');
|
|
10
|
+
if (props.origin) el.setAttribute('origin', props.origin);
|
|
11
|
+
else el.removeAttribute('origin');
|
|
12
|
+
el.setAttribute('height', props.height || '640px');
|
|
13
|
+
el.setAttribute('title', props.title || 'Kmotion preview');
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/** Solid: const el = Preview({ id: 'heritage-grove' }); */
|
|
17
|
+
export function Preview(props) {
|
|
18
|
+
definePreview();
|
|
19
|
+
const el = document.createElement('kmotion-preview');
|
|
20
|
+
applyProps(el, props || {});
|
|
21
|
+
|
|
22
|
+
createEffect(() => {
|
|
23
|
+
applyProps(el, props || {});
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
onCleanup(() => {
|
|
27
|
+
el.remove();
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
return el;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export default Preview;
|
package/src/svelte.js
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { definePreview, embedUrl } from './core.js';
|
|
2
|
+
|
|
3
|
+
definePreview();
|
|
4
|
+
|
|
5
|
+
export { TEMPLATES, embedUrl, setOrigin, getOrigin, definePreview } from './core.js';
|
|
6
|
+
|
|
7
|
+
function applyProps(el, { id, origin, height = '640px', title = 'Kmotion preview' }) {
|
|
8
|
+
el.setAttribute('template', id);
|
|
9
|
+
if (origin) el.setAttribute('origin', origin);
|
|
10
|
+
else el.removeAttribute('origin');
|
|
11
|
+
el.setAttribute('height', height);
|
|
12
|
+
el.setAttribute('title', title);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/** Svelte action: <div use:preview={{ id: 'heritage-grove' }}></div> */
|
|
16
|
+
export function preview(node, props) {
|
|
17
|
+
definePreview();
|
|
18
|
+
const el = document.createElement('kmotion-preview');
|
|
19
|
+
applyProps(el, props || {});
|
|
20
|
+
node.appendChild(el);
|
|
21
|
+
|
|
22
|
+
return {
|
|
23
|
+
update(next) {
|
|
24
|
+
applyProps(el, next || {});
|
|
25
|
+
},
|
|
26
|
+
destroy() {
|
|
27
|
+
el.remove();
|
|
28
|
+
},
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function Preview(target, props) {
|
|
33
|
+
if (!target) throw new Error('kmotion: Preview needs a DOM node');
|
|
34
|
+
const action = preview(target, props);
|
|
35
|
+
return {
|
|
36
|
+
$set: action.update,
|
|
37
|
+
$destroy: action.destroy,
|
|
38
|
+
src: embedUrl(props?.id, props?.origin),
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export default Preview;
|
package/src/vue.js
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { defineComponent, h } from 'vue';
|
|
2
|
+
import { definePreview } from './core.js';
|
|
3
|
+
|
|
4
|
+
definePreview();
|
|
5
|
+
|
|
6
|
+
export { TEMPLATES, embedUrl, setOrigin, getOrigin, definePreview } from './core.js';
|
|
7
|
+
|
|
8
|
+
export const Preview = defineComponent({
|
|
9
|
+
name: 'KmotionPreview',
|
|
10
|
+
props: {
|
|
11
|
+
id: { type: String, required: true },
|
|
12
|
+
origin: { type: String, default: '' },
|
|
13
|
+
height: { type: String, default: '640px' },
|
|
14
|
+
title: { type: String, default: 'Kmotion preview' },
|
|
15
|
+
},
|
|
16
|
+
setup(props) {
|
|
17
|
+
return () =>
|
|
18
|
+
h('kmotion-preview', {
|
|
19
|
+
template: props.id,
|
|
20
|
+
origin: props.origin || undefined,
|
|
21
|
+
height: props.height,
|
|
22
|
+
title: props.title,
|
|
23
|
+
});
|
|
24
|
+
},
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
export default Preview;
|