docs/.vitepress/theme/components/themerepo/ThemeRow.vue

233 lines · 5577 bytes

  1<script setup lang="ts">
  2import { computed } from 'vue'
  3import { IDENTITY_COLORS } from '../../../../ext/themeRepos/defaults'
  4import type { ManaTheme } from '../../../../ext/themeRepos/types'
  5import { useCopy } from '../extrepo/useCopy'
  6
  7const props = withDefaults(
  8  defineProps<{
  9    theme: ManaTheme
 10    selected: boolean
 11    mainOnly?: boolean
 12    bigSwatches?: boolean
 13  }>(),
 14  { mainOnly: false, bigSwatches: false }
 15)
 16defineEmits<{ select: [] }>()
 17
 18const { copiedKey, copy } = useCopy()
 19
 20const addThemeLink = computed(() =>
 21  props.theme.raw
 22    ? `mana://add-theme?url=${encodeURIComponent(props.theme.raw)}`
 23    : undefined
 24)
 25
 26function formatColor(color: string): string {
 27  return /^#[0-9a-f]{6}ff$/i.test(color) ? color.slice(0, 7) : color
 28}
 29
 30const swatches = computed(() => {
 31  const colors = props.theme.colors
 32  if (props.mainOnly) {
 33    return IDENTITY_COLORS
 34      .filter(key => colors[key])
 35      .map(key => ({ key, color: formatColor(colors[key]) }))
 36  }
 37  return Object.entries(colors)
 38    .sort(([a], [b]) => a.localeCompare(b))
 39    .map(([key, color]) => ({ key, color: formatColor(color) }))
 40})
 41</script>
 42
 43<template>
 44  <div
 45    class="theme-row"
 46    :class="{ 'theme-row-selected': selected }"
 47    role="button"
 48    tabindex="0"
 49    :aria-pressed="selected"
 50    @click="$emit('select')"
 51    @keydown.enter.self.prevent="$emit('select')"
 52    @keydown.space.self.prevent="$emit('select')"
 53  >
 54    <div class="theme-row-body">
 55      <button
 56        type="button"
 57        class="theme-row-name"
 58        :aria-pressed="selected"
 59        :title="selected
 60        ? 'Show default preview'
 61        : `Preview ${theme.name}`"
 62      >
 63        {{ theme.name }}
 64      </button>
 65      <button
 66        type="button"
 67        class="theme-shades"
 68        :class="{ 'theme-shades-big': bigSwatches }"
 69        :aria-pressed="selected"
 70        :aria-label="`Preview ${theme.name}`"
 71      >
 72        <span
 73          v-for="swatch in swatches"
 74          :key="swatch.key"
 75          class="theme-shade"
 76          :class="{ 'theme-shade-big': bigSwatches }"
 77          :style="bigSwatches
 78          ? undefined
 79          : { backgroundColor: swatch.color }"
 80          :title="`${swatch.key}: ${swatch.color}`"
 81        >
 82          <template v-if="bigSwatches">
 83            <span
 84              class="theme-shade-fill"
 85              :style="{ backgroundColor: swatch.color }"
 86            />
 87            <span class="theme-shade-hex">{{
 88              swatch.color.replace('#', '')
 89            }}</span>
 90          </template>
 91        </span>
 92      </button>
 93    </div>
 94
 95    <div class="theme-row-actions">
 96      <a
 97        v-if="addThemeLink"
 98        class="ext-btn ext-btn-install"
 99        :href="addThemeLink"
100        :aria-label="`Add ${theme.name} to Mana`"
101        :title="`Add ${theme.name} to Mana`"
102        @click.stop
103      >
104        <span class="i-lucide:download" />
105        <span class="ext-btn-install-label">Install</span>
106      </a>
107
108      <button
109        v-if="theme.raw"
110        type="button"
111        class="ext-btn ext-btn-copy"
112        :class="{ 'theme-copy-done': copiedKey === theme.raw }"
113        :aria-label="copiedKey === theme.raw
114        ? `Copied ${theme.name} URL`
115        : `Copy ${theme.name} URL`"
116        :title="copiedKey === theme.raw
117        ? 'Copied!'
118        : `Copy ${theme.name} JSON URL`"
119        @click.stop="copy(theme.raw, theme.raw)"
120      >
121        <span v-if="copiedKey === theme.raw" class="i-lucide:check" />
122        <span v-else class="i-lucide:copy" />
123      </button>
124    </div>
125  </div>
126</template>
127
128<style scoped src="../extrepo/repoRowShared.css"></style>
129
130<style scoped>
131.theme-row {
132  display: flex;
133  align-items: center;
134  gap: 10px;
135  padding: 10px 12px;
136  border-left: 2px solid transparent;
137  cursor: pointer;
138  transition: background-color 0.15s, border-color 0.15s;
139}
140.theme-row:hover {
141  background-color: var(--vp-c-bg-soft);
142}
143.theme-row:focus-visible {
144  outline: 2px solid var(--vp-c-brand-1);
145  outline-offset: -2px;
146}
147.theme-row-selected {
148  border-left-color: var(--vp-c-brand-1);
149  background-color: var(--vp-c-bg-soft);
150}
151.theme-row-body {
152  flex: 1;
153  min-width: 0;
154  display: flex;
155  flex-direction: column;
156  gap: 6px;
157}
158.theme-row-actions {
159  display: flex;
160  align-items: center;
161  gap: 6px;
162  flex-shrink: 0;
163}
164.theme-row-name {
165  min-width: 0;
166  padding: 0;
167  border: none;
168  background: transparent;
169  font: inherit;
170  font-size: 14px;
171  font-weight: 600;
172  line-height: 1.3;
173  color: var(--vp-c-text-1);
174  text-align: left;
175  white-space: nowrap;
176  overflow: hidden;
177  text-overflow: ellipsis;
178  cursor: pointer;
179}
180.theme-shades {
181  width: 100%;
182  display: flex;
183  height: 10px;
184  padding: 0;
185  border: none;
186  border-radius: 4px;
187  overflow: hidden;
188  background: transparent;
189  cursor: pointer;
190}
191.theme-shade {
192  flex: 1;
193  height: 100%;
194}
195.theme-shades-big {
196  height: auto;
197  gap: 6px;
198  border-radius: 0;
199  overflow-x: auto;
200  overflow-y: hidden;
201}
202.theme-shade-big {
203  flex: 1 0 48px;
204  min-width: 48px;
205  height: auto;
206  display: flex;
207  flex-direction: column;
208  border-radius: 6px;
209  overflow: hidden;
210  border: 1px solid var(--vp-c-divider);
211  background-color: var(--vp-c-bg);
212}
213.theme-shade-fill {
214  width: 100%;
215  height: 42px;
216}
217.theme-shade-hex {
218  display: block;
219  padding: 4px 2px;
220  font-size: 10px;
221  line-height: 1.2;
222  text-align: center;
223  color: var(--vp-c-text-2);
224  font-variant-numeric: tabular-nums;
225  white-space: nowrap;
226  overflow: hidden;
227  text-overflow: ellipsis;
228}
229.theme-copy-done {
230  border-color: var(--vp-c-brand-1);
231  color: var(--vp-c-brand-1);
232}
233</style>