Skip to content

Accessing the Runtime Instance in Components

inject(PTAH_INSTANCE)

PtahJs provides the runtime instance through Vue dependency injection inside <DesignerRoot> and <Preview>.

js
import { inject } from 'vue';
import { PTAH_INSTANCE } from '@ptahjs/ui-vue';

const app = inject(PTAH_INSTANCE);

useRuntimeApp()

Creates a Runtime instance for preview or runtime mode.

js
import { useRuntimeApp } from '@ptahjs/ui-vue';

const runtime = useRuntimeApp({ id: 've1' });
  • Parameters: { id: string }
  • Returns: Runtime

useDesignerApp()

Creates a Runtime instance with runtime.designer.

js
import { useDesignerApp } from '@ptahjs/ui-vue';

const runtime = useDesignerApp({ id: 've1' });
const designer = runtime.designer;
  • Parameters: { id: string }
  • Returns: Runtime

Accessing app.$state

vue
<script setup>
import { inject, computed } from 'vue';
import { PTAH_INSTANCE } from '@ptahjs/ui-vue';

const app = inject(PTAH_INSTANCE);
const currentScene = computed(() => app.$state.currentScene);
const currentModule = computed(() => app.$state.currentModule);
</script>

Available State Keys on app.$state

KeyTypeDescription
scenesByIdMap<string, Scene>All scenes by ID
modulesByIdMap<string, Module>All modules by ID
eventsByIdMap<string, Event>All events by ID
childrenIdsByParentIdMap<string, string[]>Children lookup
sceneIdOrderstring[]Ordered scene IDs
currentSceneIdstringActive scene ID
currentModuleIdstringSelected module ID
currentSceneScene | nullActive scene
currentModuleModule | nullSelected module

Example: Custom Module Component

vue
<template>
    <div :id="id" class="ui-button">
        <button>{{ module?.props?.label }}</button>
    </div>
</template>

<script setup>
import { inject, computed } from 'vue';
import { PTAH_INSTANCE } from '@ptahjs/ui-vue';

const props = defineProps({ id: String });
const app = inject(PTAH_INSTANCE);
const module = computed(() => app.$state.modulesById.get(props.id));
</script>

Example: Custom Parameter Component

jsx
export default {
    name: 'PropString',
    props: {
        modelValue: String,
        node: Object,
    },
    emits: ['update:modelValue'],
    setup(props, { emit }) {
        return () => (
            <input
                type="text"
                value={props.modelValue ?? props.node.defaultValue ?? ''}
                onInput={(e) => emit('update:modelValue', e.target.value)}
                placeholder={props.node.label}
            />
        );
    },
};