Profectus/src/features/clickables/Clickable.vue

143 lines
3.7 KiB
Vue
Raw Normal View History

2021-06-11 23:38:16 -05:00
<template>
<div
v-if="unref(visibility) !== Visibility.None"
:style="{ visibility: unref(visibility) === Visibility.Hidden ? 'hidden' : undefined }"
>
<button
:style="unref(style)"
2022-01-13 22:25:47 -06:00
@click="onClick"
@mousedown="start"
@mouseleave="stop"
@mouseup="stop"
@touchstart="start"
@touchend="stop"
@touchcancel="stop"
:class="{
feature: true,
clickable: true,
can: unref(canClick),
locked: !unref(canClick),
2022-01-13 22:25:47 -06:00
small,
...unref(classes)
}"
>
<component v-if="unref(comp)" :is="unref(comp)" />
<MarkNode :mark="unref(mark)" />
2022-01-13 22:25:47 -06:00
<LinkNode :id="id" />
</button>
</div>
2021-06-11 23:38:16 -05:00
</template>
2022-01-24 22:25:34 -06:00
<script lang="tsx">
2022-03-03 21:39:48 -06:00
import "components/common/features.css";
import LinkNode from "components/links/LinkNode.vue";
import MarkNode from "components/MarkNode.vue";
import { GenericClickable } from "features/clickables/clickable";
import { jsx, StyleValue, Visibility } from "features/feature";
import {
coerceComponent,
isCoercableComponent,
processedPropType,
setupHoldToClick,
unwrapRef
2022-03-03 21:39:48 -06:00
} from "util/vue";
import {
Component,
defineComponent,
PropType,
shallowRef,
toRefs,
unref,
UnwrapRef,
watchEffect
} from "vue";
2022-01-13 22:25:47 -06:00
2022-01-24 22:25:34 -06:00
export default defineComponent({
props: {
display: {
type: processedPropType<UnwrapRef<GenericClickable["display"]>>(
Object,
String,
Function
),
2022-01-24 22:25:34 -06:00
required: true
},
visibility: {
type: processedPropType<Visibility>(Number),
2022-01-24 22:25:34 -06:00
required: true
},
style: processedPropType<StyleValue>(Object, String, Array),
classes: processedPropType<Record<string, boolean>>(Object),
2022-01-24 22:25:34 -06:00
onClick: Function as PropType<VoidFunction>,
onHold: Function as PropType<VoidFunction>,
canClick: {
type: processedPropType<boolean>(Boolean),
2022-01-24 22:25:34 -06:00
required: true
},
small: Boolean,
mark: processedPropType<boolean | string>(Boolean, String),
2022-01-24 22:25:34 -06:00
id: {
type: String,
required: true
}
},
components: {
LinkNode,
MarkNode
},
2022-01-24 22:25:34 -06:00
setup(props) {
const { display, onClick, onHold } = toRefs(props);
2021-06-11 23:38:16 -05:00
const comp = shallowRef<Component | string>("");
watchEffect(() => {
const currDisplay = unwrapRef(display);
2022-01-24 22:25:34 -06:00
if (currDisplay == null) {
comp.value = "";
return;
2022-01-24 22:25:34 -06:00
}
if (isCoercableComponent(currDisplay)) {
comp.value = coerceComponent(currDisplay);
return;
2022-01-24 22:25:34 -06:00
}
const Title = coerceComponent(currDisplay.title || "", "h3");
const Description = coerceComponent(currDisplay.description, "div");
comp.value = coerceComponent(
jsx(() => (
<span>
{currDisplay.title ? (
<div>
<Title />
</div>
) : null}
<Description />
</span>
))
2022-01-24 22:25:34 -06:00
);
});
const { start, stop } = setupHoldToClick(onClick, onHold);
return {
start,
stop,
comp,
Visibility,
unref
2022-01-24 22:25:34 -06:00
};
}
});
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;
}
2021-06-11 23:38:16 -05:00
</style>