-
-
Notifications
You must be signed in to change notification settings - Fork 173
/
StoryVariantGridItem.vue
122 lines (108 loc) · 3.37 KB
/
StoryVariantGridItem.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<script lang="ts" setup>
import { PropType, ref, toRefs } from 'vue'
import { Icon } from '@iconify/vue'
import { useRouter } from 'vue-router'
import { useResizeObserver } from '@vueuse/core'
import GenericRenderStory from './GenericRenderStory.vue'
import { useCurrentVariantRoute } from '../../util/variant'
import type { Story, Variant } from '../../types'
import { useScrollOnActive } from '../../util/scroll'
import StoryVariantNewTab from '../toolbar/StoryVariantNewTab.vue'
const props = defineProps({
variant: {
type: Object as PropType<Variant>,
required: true,
},
story: {
type: Object as PropType<Story>,
required: true,
},
})
const emit = defineEmits({
// eslint-disable-next-line @typescript-eslint/no-unused-vars
resize: (width: number, height: number) => true,
})
const { variant } = toRefs(props)
const { isActive, targetRoute } = useCurrentVariantRoute(variant)
Object.assign(props.variant, {
previewReady: false,
})
function onReady () {
Object.assign(props.variant, {
previewReady: true,
})
}
const router = useRouter()
function selectVariant () {
router.push(targetRoute.value)
}
const el = ref<HTMLDivElement>()
const { autoScroll } = useScrollOnActive(isActive, el)
useResizeObserver(el, () => {
if (props.variant.previewReady) {
emit('resize', el.value!.clientWidth, el.value!.clientHeight)
if (isActive.value) {
autoScroll()
}
}
})
</script>
<template>
<div
ref="el"
class="htw-cursor-default htw-flex htw-flex-col htw-gap-y-1 htw-group"
>
<!-- Header -->
<div class="htw-flex-none htw-flex htw-items-center">
<RouterLink
v-tooltip="variant.title"
:to="targetRoute"
class="htw-rounded htw-w-max htw-px-2 htw-py-0.5 htw-min-w-16 htw-cursor-pointer htw-flex htw-items-center htw-gap-1 htw-flex-shrink"
:class="{
'hover:htw-bg-gray-200 htw-text-gray-500 dark:hover:htw-bg-gray-800': !isActive,
'htw-bg-primary-200 hover:htw-bg-primary-300 htw-text-primary-800 dark:htw-bg-primary-700 dark:hover:htw-bg-primary-800 dark:htw-text-primary-200': isActive,
}"
>
<Icon
:icon="variant.icon ?? 'carbon:cube'"
class="base-list-item-link-icon htw-w-4 htw-h-4 htw-opacity-50"
:class="{
'htw-text-gray-500': !isActive && !variant.iconColor,
'bind-icon-color': !isActive && variant.iconColor,
}"
/>
<span class="htw-truncate htw-flex-1">{{ variant.title }}</span>
</RouterLink>
<!-- Toolbar -->
<div class="htw-flex-none htw-ml-auto htw-hidden group-hover:htw-flex htw-items-center htw-gap-1">
<StoryVariantNewTab
:variant="variant"
:story="story"
/>
</div>
</div>
<!-- Body -->
<div
class="htw-border htw-bg-white dark:htw-bg-gray-700 htw-rounded htw-flex-1 htw-p-4"
:class="{
'htw-border-gray-100 dark:htw-border-gray-800': !isActive,
'htw-border-primary-200 dark:htw-border-primary-900': isActive,
}"
data-test-id="sandbox-render"
@click.stop="selectVariant()"
@keyup="selectVariant()"
>
<GenericRenderStory
:key="`${story.id}-${variant.id}`"
:variant="variant"
:story="story"
@ready="onReady"
/>
</div>
</div>
</template>
<style scoped>
.bind-icon-color {
color: v-bind('variant.iconColor');
}
</style>