funkwhale/front/src/components/ui/Card.vue

216 wiersze
5.3 KiB
Vue

<script setup lang="ts">
import { computed } from 'vue'
import { type RouterLinkProps, RouterLink } from 'vue-router';
import { type ColorProps, type DefaultProps, type PastelProps, type RaisedProps, type VariantProps, propsToColor } from '~/composables/colors'
import Pill from './Pill.vue'
import Alert from './Alert.vue'
import Layout from './Layout.vue';
import Spacer from './layout/Spacer.vue';
const props = defineProps<{
title: string
category?: true | "h1" | "h2" | "h3" | "h4" | "h5"
small?: true
tags?: string[]
image?: string | { src: string, style?: "withPadding" }
icon?: string
} & Partial<RouterLinkProps> & (PastelProps | ColorProps | DefaultProps) & RaisedProps & VariantProps>()
const image = typeof props.image === 'string' ? { src: props.image } : props.image
const isExternalLink = computed(() => {
return typeof props.to === 'string' && props.to.startsWith('http')
})
// const fallbackWidth =
</script>
<template>
<Layout stack
:class="{ [$style.card]: true, [$style['is-category']]: category }"
style="--gap:16px"
v-bind="propsToColor({...(props.to? { interactive: true, solid: true, default: true } : {}), ...props})"
>
<!-- Link -->
<a v-if="props.to && isExternalLink" :class="$style.covering" :href="to?.toString()" target="_blank" />
<RouterLink v-if="props.to && !isExternalLink" :class="$style.covering" :to="props.to" />
<!-- Image -->
<div v-if="$slots.image" :class="$style.image">
<slot name="image" :src="image" />
</div>
<img v-else-if="image" :src="image?.src"
:class="{ [$style.image]: true, [$style['with-padding']]: image?.style === 'withPadding' }" />
<Spacer v-else :size="props.small? 4 : 12" />
<!-- Icon -->
<i v-if="props.icon" :class="[$style.icon, 'bi', icon]" />
<!-- Title -->
<component :class="$style.title" :is="typeof category === 'string' ? category : 'h6'">{{ title }}</component>
<!-- Content -->
<Alert blue v-if="$slots.alert" :class="$style.alert">
<slot name="alert" />
</Alert>
<div v-if="tags" :class="$style.tags">
<Pill v-for="tag in tags" :key="tag">
#{{ tag }}
</Pill>
</div>
<Layout no-gap v-if="$slots.default" :class="$style.content">
<slot />
</Layout>
<!-- Footer and Action -->
<div v-if="$slots.footer" :class="$style.footer">
<slot name="footer" />
</div>
<div v-if="$slots.action" :class="$style.action">
<slot name="action" />
</div>
<Spacer v-if="!$slots.footer && !$slots.action" :size="props.small? 8 : 16" />
</Layout>
</template>
<style module>
.card {
/* Override --width with your preferred value */
--fw-card-width: var(--width, v-bind("props.small ? 'min-content' : '320px'"));
--fw-card-padding: v-bind("props.small ? '16px' : '24px'");
position: relative;
color: var(--fw-text-color);
background-color: var(--fw-bg-color);
box-shadow: 0 3px 12px 2px rgb(0 0 0 / 20%);
border-radius: var(--fw-border-radius);
font-size: 1rem;
width: var(--fw-card-width);
>.covering {
position: absolute;
inset: 0;
&~:is(.title, .content) {
pointer-events:none;
}
&:hover~:is(.content) {
text-decoration: underline
}
}
&:has(>.image) {
text-align: center;
}
>.image {
overflow: hidden;
border-radius: var(--fw-border-radius) var(--fw-border-radius) 0 0;
object-fit: cover;
width: 100%;
aspect-ratio: 1;
&.with-padding {
margin: var(--fw-card-padding) var(--fw-card-padding) calc(var(--fw-card-padding) / 2) var(--fw-card-padding);
width: calc(100% - 2 * var(--fw-card-padding));
border-radius: var(--fw-border-radius);
}
}
>.icon {
position: absolute;
top: var(--fw-card-padding);
right: var(--fw-card-padding);
font-size: 1.2rem;
}
>.title {
padding: 0 var(--fw-card-padding);
line-height: 1.3em;
font-size: 1.125em;
font-weight: bold;
text-overflow: ellipsis;
white-space: nowrap;
overflow: hidden;
flex-shrink: 0;
}
&.is-category>.title {
font-size: 1.75em;
padding-bottom: .25em;
}
>.alert {
padding-left: var(--fw-card-padding);
padding-right: var(--fw-card-padding);
}
>.tags {
/* Tags have an inherent padding which we offset here: */
padding: 0 calc(var(--fw-card-padding) - 12px);
}
>.content {
padding: 0 var(--fw-card-padding);
/* Consider making all line height, vertical paddings, margins and borders,
a multiple of a global vertical rhythm so that side-by-side lines coincide */
line-height: 24px;
}
>.footer {
padding: calc(var(--fw-card-padding) - 4px);
display: flex;
align-items: center;
font-size: 0.8125rem;
>.options-button {
margin-left: auto;
}
}
>.action {
display: flex;
background: color-mix(in oklab, var(--fw-bg-color) 80%, var(--fw-gray-500));
border-bottom-left-radius: var(--fw-border-radius);
border-bottom-right-radius: var(--fw-border-radius);
>*:not(.with-padding) {
margin: 0;
flex-grow: 1;
border-top-left-radius: 0;
border-top-right-radius: 0;
border: 8px solid transparent;
&:not(:first-child) {
border-bottom-left-radius: 0;
}
&:not(:last-child) {
border-bottom-right-radius: 0;
}
}
}
}
</style>