docs/.vitepress/theme/components/extrepo/SiteGrid.vue

108 lines · 2660 bytes

  1<script setup lang="ts">
  2import { computed, ref, watch } from 'vue'
  3import { SITE_GRID_PAGE_SIZE } from './constants'
  4import { groupSites } from './groupSites'
  5import SiteCell from './SiteCell.vue'
  6import SiteGroupCell from './SiteGroupCell.vue'
  7import type { RepoSite } from './types'
  8
  9const props = defineProps<{
 10  sites: (RepoSite & { repoName?: string })[]
 11  emptyText: string
 12  scheme?: string
 13}>()
 14
 15const groups = computed(() => groupSites(props.sites))
 16
 17const openGroups = ref<Set<string>>(new Set())
 18
 19function toggleGroup(key: string) {
 20  const next = new Set(openGroups.value)
 21  if (next.has(key)) next.delete(key)
 22  else next.add(key)
 23  openGroups.value = next
 24}
 25
 26const visibleCount = ref(SITE_GRID_PAGE_SIZE)
 27watch(() => props.sites, () => {
 28  visibleCount.value = SITE_GRID_PAGE_SIZE
 29})
 30
 31const visibleGroups = computed(() => groups.value.slice(0, visibleCount.value))
 32const remaining = computed(() =>
 33  groups.value.length - visibleGroups.value.length
 34)
 35
 36function showMore() {
 37  visibleCount.value += SITE_GRID_PAGE_SIZE
 38}
 39</script>
 40
 41<template>
 42  <div class="ext-site-grid">
 43    <div v-if="sites.length === 0" class="ext-site-empty">
 44      {{ emptyText }}
 45    </div>
 46    <template v-for="group in visibleGroups" :key="group.key">
 47      <SiteCell
 48        v-if="group.sites.length === 1"
 49        :site="group.sites[0]"
 50        :scheme="scheme"
 51      />
 52      <template v-else>
 53        <SiteGroupCell
 54          :sites="group.sites"
 55          :open="openGroups.has(group.key)"
 56          @toggle="toggleGroup(group.key)"
 57        />
 58        <template v-if="openGroups.has(group.key)">
 59          <SiteCell
 60            v-for="(site, j) in group.sites"
 61            :key="`${group.key}::${site.lang}::${j}`"
 62            :site="site"
 63            :scheme="scheme"
 64          />
 65        </template>
 66      </template>
 67    </template>
 68    <button
 69      v-if="remaining > 0"
 70      type="button"
 71      class="ext-site-more"
 72      @click="showMore"
 73    >
 74      Show more ({{ remaining }} remaining)
 75    </button>
 76  </div>
 77</template>
 78
 79<style scoped>
 80.ext-site-grid {
 81  display: grid;
 82  grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
 83  gap: 6px;
 84  padding: 10px;
 85  max-height: 340px;
 86  overflow-y: auto;
 87  background-color: var(--vp-c-bg);
 88}
 89.ext-site-empty {
 90  grid-column: 1 / -1;
 91  color: var(--vp-c-text-2);
 92  font-size: 13px;
 93  padding: 8px 4px;
 94}
 95.ext-site-more {
 96  grid-column: 1 / -1;
 97  padding: 8px;
 98  border-radius: 8px;
 99  border: 1px solid var(--vp-c-divider);
100  background-color: var(--vp-c-bg-soft);
101  color: var(--vp-c-text-1);
102  font-size: 13px;
103  cursor: pointer;
104}
105.ext-site-more:hover {
106  border-color: var(--vp-c-brand-1);
107}
108</style>