Skip to content

Commit

Permalink
feat(theme): add icon support for home hero actions (#456)
Browse files Browse the repository at this point in the history
  • Loading branch information
pengzhanbo authored Feb 8, 2025
1 parent 3017bd7 commit 90db034
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 7 deletions.
4 changes: 3 additions & 1 deletion docs/notes/theme/guide/自定义首页.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,11 @@ interface PlumeThemeHomeHero extends PlumeHomeConfigBase {
tagline?: string
text?: string
actions?: {
theme?: 'brand' | 'alt'
theme?: 'brand' | 'alt' | 'sponsor'
text: string
link?: string
icon?: string // 文本左侧图标
suffixIcon?: string // 文本右侧图标
target?: '_blank' | '_self' | string
rel?: string
}
Expand Down
10 changes: 9 additions & 1 deletion theme/src/client/components/Home/VPHomeHero.vue
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ useHomeHeroTintPlate(
:href="action.link"
:target="action.target"
:rel="action.rel"
:icon="action.icon"
:suffix-icon="action.suffixIcon"
/>
</div>
</div>
Expand Down Expand Up @@ -168,8 +170,14 @@ useHomeHeroTintPlate(
margin: 30px 0 0;
}
.action {
display: flex;
gap: 24px;
align-items: center;
}
.action :deep(.vp-button) {
margin-right: 24px;
margin-left: 0;
}
.action :deep(.vp-button:last-of-type) {
Expand Down
27 changes: 25 additions & 2 deletions theme/src/client/components/VPButton.vue
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<script setup lang="ts">
import VPIcon from '@theme/VPIcon.vue'
import { computed, toRef } from 'vue'
import { useRouter, withBase } from 'vuepress/client'
import { useLink } from '../composables/index.js'
Expand All @@ -7,14 +8,17 @@ interface Props {
tag?: string
size?: 'medium' | 'big'
theme?: 'brand' | 'alt' | 'sponsor'
text: string
text?: string
href?: string
target?: string
rel?: string
icon?: string
suffixIcon?: string
}
const props = withDefaults(defineProps<Props>(), {
size: 'medium',
theme: 'brand',
text: '',
tag: undefined,
href: undefined,
target: undefined,
Expand Down Expand Up @@ -47,7 +51,11 @@ function linkTo(e: Event) {
:rel="rel ?? (isExternal ? 'noreferrer' : undefined)"
@click="linkTo($event)"
>
{{ text }}
<span class="button-content">
<VPIcon v-if="icon" :name="icon" />
<slot><span>{{ text }}</span></slot>
<VPIcon v-if="suffixIcon" :name="suffixIcon" />
</span>
</Component>
</template>

Expand Down Expand Up @@ -136,4 +144,19 @@ function linkTo(e: Event) {
background-color: var(--vp-button-sponsor-active-bg);
border-color: var(--vp-button-sponsor-active-border);
}
.vp-button .button-content {
display: flex;
align-items: center;
justify-content: center;
}
.vp-button .button-content :deep(.vp-icon) {
width: 1.2em;
height: 1.2em;
}
.vp-button + .vp-button {
margin-left: 1em;
}
</style>
3 changes: 3 additions & 0 deletions theme/src/client/globalComponents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import VPCardMasonry from '@theme/global/VPCardMasonry.vue'
import VPImageCard from '@theme/global/VPImageCard.vue'
import VPLinkCard from '@theme/global/VPLinkCard.vue'
import VPHomeBox from '@theme/Home/VPHomeBox.vue'
import VPButton from '@theme/VPButton.vue'
import VPIcon from '@theme/VPIcon.vue'
import { hasGlobalComponent } from '@vuepress/helper/client'
import { type App, h, resolveComponent } from 'vue'
Expand All @@ -31,6 +32,8 @@ export function globalComponents(app: App) {
app.component('Icon', VPIcon)
app.component('VPIcon', VPIcon)

app.component('VPButton', VPButton)

app.component('HomeBox', VPHomeBox)
app.component('VPHomeBox', VPHomeBox)

Expand Down
12 changes: 10 additions & 2 deletions theme/src/node/prepare/prepareIcons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ interface IconData {
type CollectMap = Record<string, string[]>
type IconDataMap = Record<string, IconData>

const ICON_REGEXP = /<(?:VP)?(Icon|Card|LinkCard)([^>]*)>/g
const ICON_NAME_REGEXP = /(?:name|icon)="([^"]+)"/
const ICON_REGEXP = /<(?:VP)?(Icon|Card|LinkCard|Button)([^>]*)>/g
const ICON_NAME_REGEXP = /(?:name|icon|suffix-icon)="([^"]+)"/
const URL_CONTENT_REGEXP = /(url\([\s\S]+\))/
const ICONIFY_NAME = /^[\w-]+:[\w-]+$/
const JS_FILENAME = 'internal/iconify.js'
Expand Down Expand Up @@ -124,6 +124,14 @@ function getIconsWithPage(page: Page): string[] {
list.push(feature.icon)
}
}
if (config.type === 'hero' && config.hero?.actions?.length) {
for (const action of config.hero.actions) {
if (action.icon && isIconify(action.icon))
list.push(action.icon)
if (action.suffixIcon && isIconify(action.suffixIcon))
list.push(action.suffixIcon)
}
}
}
}

Expand Down
4 changes: 3 additions & 1 deletion theme/src/shared/frontmatter/home.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export interface PlumeThemeHomeFrontmatter extends PlumeNormalFrontmatter, Omit<
config?: PlumeThemeHomeConfig[]
}

export type PlumeThemeHomeConfig = PlumeThemeHomeBanner | PlumeThemeHomeTextImage | PlumeThemeHomeFeatures | PlumeThemeHomeProfile
export type PlumeThemeHomeConfig = PlumeThemeHomeBanner | PlumeThemeHomeTextImage | PlumeThemeHomeFeatures | PlumeThemeHomeProfile | PlumeThemeHomeHero

export interface PlumeThemeHero {
name: string
Expand All @@ -22,6 +22,8 @@ export interface PlumeThemeHeroAction {
link?: string
target?: string
rel?: string
icon?: string
suffixIcon?: string
}

export interface PlumeHomeConfigBase {
Expand Down

0 comments on commit 90db034

Please sign in to comment.