Skip to content

Commit

Permalink
Keep all condition overlays in the DOM to prevent flickering
Browse files Browse the repository at this point in the history
  • Loading branch information
porst17 committed May 2, 2024
1 parent c6988e7 commit e2e058c
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/vue/components/ConditionalLayer.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<script setup lang="ts">
import type { DeepReadonly } from 'ts-essentials';
import type { Ref } from 'vue';
import { computed } from 'vue';
Expand All @@ -11,7 +12,7 @@ import { useModelStore } from '../../ts/stores/model';
import { compile } from '../../ts/util/record-condition';
const props = defineProps<{
layerConfig: DeepReadonly<ConditionalLayerConfig>;
readonly layerConfig: DeepReadonly<ConditionalLayerConfig>;
}>();
const { extractAssetPosition, toAssetUrl } = useConfigStore();
Expand All @@ -20,34 +21,38 @@ const { record } = useModelStore();
function compileLayer({ condition, url }: ConditionalLayerConfig): {
checkCondition: Condition<typeof record>;
condition: string;
active: Ref<boolean>;
url: URL;
x: number;
y: number;
} {
const resolvedUrl = toAssetUrl(url);
const { x, y } = extractAssetPosition(resolvedUrl);
const checkCondition = compile(condition);
const active = computed(() => checkCondition(record));
return {
checkCondition,
condition,
active,
url: resolvedUrl,
x,
y,
};
}
const compiledLayer = computed(() => compileLayer(props.layerConfig));
const compiledLayer = compileLayer(props.layerConfig);
</script>

<template>
<img
:src="compiledLayer.url.href"
v-if="compiledLayer.checkCondition(record)"
class="positioned-layer"
:class="{ inactive: !compiledLayer.active.value }"
:style="{
'--layer-x': compiledLayer.x,
'--layer-y': compiledLayer.y,
}"
:alt="`${compiledLayer.condition} -> ${compiledLayer.url}`"
/>
</template>

Expand All @@ -57,4 +62,8 @@ const compiledLayer = computed(() => compileLayer(props.layerConfig));
top: calc(1px * var(--layer-y));
left: calc(1px * var(--layer-x));
}
.inactive {
display: none;
}
</style>

0 comments on commit e2e058c

Please sign in to comment.