Profectus/src/features/clickables/Clickable.vue

104 lines
2.7 KiB
Vue
Raw Normal View History

2021-06-11 23:38:16 -05:00
<template>
<button
v-if="isVisible(visibility)"
:style="[
{ visibility: isHidden(visibility) ? 'hidden' : undefined },
unref(style) ?? []
]"
@click="onClick"
@mousedown="start"
@mouseleave="stop"
@mouseup="stop"
2022-05-02 21:10:18 -05:00
@touchstart.passive="start"
@touchend.passive="stop"
@touchcancel.passive="stop"
:class="{
feature: true,
clickable: true,
can: unref(canClick),
locked: !unref(canClick),
small,
...unref(classes)
}"
>
<component v-if="unref(comp)" :is="unref(comp)" />
<MarkNode :mark="unref(mark)" />
<Node :id="id" />
</button>
2021-06-11 23:38:16 -05:00
</template>
<script setup lang="tsx">
2022-03-03 21:39:48 -06:00
import "components/common/features.css";
import MarkNode from "components/MarkNode.vue";
2022-06-26 19:17:22 -05:00
import Node from "components/Node.vue";
import type { GenericClickable } from "features/clickables/clickable";
import type { StyleValue } from "features/feature";
import { isHidden, isVisible, jsx, Visibility } from "features/feature";
import {
coerceComponent,
isCoercableComponent,
setupHoldToClick
2022-03-03 21:39:48 -06:00
} from "util/vue";
import type { Component, UnwrapRef } from "vue";
import { shallowRef, toRef, unref, watchEffect } from "vue";
2022-01-13 22:25:47 -06:00
const props = defineProps<{
display: UnwrapRef<GenericClickable["display"]>;
visibility: Visibility | boolean;
style?: StyleValue;
classes?: Record<string, boolean>;
onClick?: (e?: MouseEvent | TouchEvent) => void;
onHold?: VoidFunction;
canClick: boolean;
small?: boolean;
mark?: boolean | string;
id: string;
}>();
2021-06-11 23:38:16 -05:00
const comp = shallowRef<Component | string>("");
watchEffect(() => {
const currDisplay = props.display;
if (currDisplay == null) {
comp.value = "";
return;
}
if (isCoercableComponent(currDisplay)) {
comp.value = coerceComponent(currDisplay);
return;
}
const Title = coerceComponent(currDisplay.title ?? "", "h3");
const Description = coerceComponent(currDisplay.description, "div");
comp.value = coerceComponent(
jsx(() => (
<span>
{currDisplay.title != null ? (
<div>
<Title />
</div>
) : null}
<Description />
</span>
))
);
});
const { start, stop } = setupHoldToClick(toRef(props, "onClick"), toRef(props, "onHold"));
2021-06-11 23:38:16 -05:00
</script>
<style scoped>
2021-06-24 22:15:10 -05:00
.clickable {
min-height: 120px;
width: 120px;
font-size: 10px;
}
.clickable.small {
min-height: unset;
}
.clickable > * {
pointer-events: none;
}
2021-06-11 23:38:16 -05:00
</style>