refactor: defining default values for Props using withDefaults. (#2498)

pull/2825/head
丶远方 1 year ago committed by GitHub
parent 5836facc61
commit ff86c73175
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1,12 +1,15 @@
<script setup lang="ts"> <script setup lang="ts">
defineProps<{ interface Props {
text?: string text?: string
type?: 'info' | 'tip' | 'warning' | 'danger' type?: 'info' | 'tip' | 'warning' | 'danger'
}>() }
withDefaults(defineProps<Props>(), {
type: 'tip'
})
</script> </script>
<template> <template>
<span class="VPBadge" :class="type ?? 'tip'"> <span class="VPBadge" :class="type">
<slot>{{ text }}</slot> <slot>{{ text }}</slot>
</span> </span>
</template> </template>

@ -3,27 +3,24 @@ import { computed } from 'vue'
import { normalizeLink } from '../support/utils' import { normalizeLink } from '../support/utils'
import { EXTERNAL_URL_RE } from '../../shared' import { EXTERNAL_URL_RE } from '../../shared'
const props = defineProps<{ interface Props {
tag?: string tag?: string
size?: 'medium' | 'big' size?: 'medium' | 'big'
theme?: 'brand' | 'alt' | 'sponsor' theme?: 'brand' | 'alt' | 'sponsor'
text: string text: string
href?: string href?: string
}>() }
const props = withDefaults(defineProps<Props>(), {
const classes = computed(() => [ size: 'medium',
props.size ?? 'medium', theme: 'brand'
props.theme ?? 'brand' })
])
const isExternal = computed(() => props.href && EXTERNAL_URL_RE.test(props.href)) const isExternal = computed(
() => props.href && EXTERNAL_URL_RE.test(props.href)
)
const component = computed(() => { const component = computed(() => {
if (props.tag) { return props.tag || props.href ? 'a' : 'button'
return props.tag
}
return props.href ? 'a' : 'button'
}) })
</script> </script>
@ -31,7 +28,7 @@ const component = computed(() => {
<component <component
:is="component" :is="component"
class="VPButton" class="VPButton"
:class="classes" :class="[size, theme]"
:href="href ? normalizeLink(href) : undefined" :href="href ? normalizeLink(href) : undefined"
:target="isExternal ? '_blank' : undefined" :target="isExternal ? '_blank' : undefined"
:rel="isExternal ? 'noreferrer' : undefined" :rel="isExternal ? 'noreferrer' : undefined"

@ -14,13 +14,16 @@ export interface Sponsor {
img: string img: string
url: string url: string
} }
interface Props {
defineProps<{
message?: string message?: string
actionText?: string actionText?: string
actionLink?: string actionLink?: string
data: Sponsors[] data: Sponsors[]
}>() }
withDefaults(defineProps<Props>(), {
actionText: 'Become a sponsor'
})
</script> </script>
<template> <template>
@ -36,11 +39,7 @@ defineProps<{
</div> </div>
<div v-if="actionLink" class="action"> <div v-if="actionLink" class="action">
<VPButton <VPButton theme="sponsor" :text="actionText" :href="actionLink" />
theme="sponsor"
:text="actionText ?? 'Become a sponsor'"
:href="actionLink"
/>
</div> </div>
</div> </div>
</section> </section>

@ -9,13 +9,15 @@ export interface Sponsors {
size?: GridSize size?: GridSize
items: Sponsor[] items: Sponsor[]
} }
interface Props {
const props = defineProps<{
mode?: 'normal' | 'aside' mode?: 'normal' | 'aside'
tier?: string tier?: string
size?: GridSize size?: GridSize
data: Sponsors[] | Sponsor[] data: Sponsors[] | Sponsor[]
}>() }
const props = withDefaults(defineProps<Props>(), {
mode: 'normal'
})
const sponsors = computed(() => { const sponsors = computed(() => {
const isSponsors = props.data.some((s) => { const isSponsors = props.data.some((s) => {
@ -33,7 +35,7 @@ const sponsors = computed(() => {
</script> </script>
<template> <template>
<div class="VPSponsors vp-sponsor" :class="[mode ?? 'normal']"> <div class="VPSponsors vp-sponsor" :class="[mode]">
<section <section
v-for="(sponsor, index) in sponsors" v-for="(sponsor, index) in sponsors"
:key="index" :key="index"

@ -8,11 +8,13 @@ export interface Sponsor {
img: string img: string
url: string url: string
} }
interface Props {
const props = defineProps<{
size?: GridSize size?: GridSize
data: Sponsor[] data: Sponsor[]
}>() }
const props = withDefaults(defineProps<Props>(), {
size: 'medium'
})
const el = ref(null) const el = ref(null)
@ -20,11 +22,7 @@ useSponsorsGrid({ el, size: props.size })
</script> </script>
<template> <template>
<div <div class="VPSponsorsGrid vp-sponsor-grid" :class="[size]" ref="el">
class="VPSponsorsGrid vp-sponsor-grid"
:class="[props.size ?? 'medium']"
ref="el"
>
<div <div
v-for="sponsor in data" v-for="sponsor in data"
:key="sponsor.name" :key="sponsor.name"

@ -3,15 +3,16 @@ import type { DefaultTheme } from 'vitepress/theme'
import { computed } from 'vue' import { computed } from 'vue'
import VPTeamMembersItem from './VPTeamMembersItem.vue' import VPTeamMembersItem from './VPTeamMembersItem.vue'
const props = defineProps<{ interface Props {
size?: 'small' | 'medium' size?: 'small' | 'medium'
members: DefaultTheme.TeamMember[] members: DefaultTheme.TeamMember[]
}>() }
const props = withDefaults(defineProps<Props>(), {
size: 'medium'
})
const classes = computed(() => [ const classes = computed(() => [props.size, `count-${props.members.length}`])
props.size ?? 'medium',
`count-${props.members.length}`
])
</script> </script>
<template> <template>
@ -29,9 +30,15 @@ const classes = computed(() => [
grid-template-columns: repeat(auto-fit, minmax(224px, 1fr)); grid-template-columns: repeat(auto-fit, minmax(224px, 1fr));
} }
.VPTeamMembers.small.count-1 .container { max-width: 276px; } .VPTeamMembers.small.count-1 .container {
.VPTeamMembers.small.count-2 .container { max-width: calc(276px * 2 + 24px); } max-width: 276px;
.VPTeamMembers.small.count-3 .container { max-width: calc(276px * 3 + 24px * 2); } }
.VPTeamMembers.small.count-2 .container {
max-width: calc(276px * 2 + 24px);
}
.VPTeamMembers.small.count-3 .container {
max-width: calc(276px * 3 + 24px * 2);
}
.VPTeamMembers.medium .container { .VPTeamMembers.medium .container {
grid-template-columns: repeat(auto-fit, minmax(256px, 1fr)); grid-template-columns: repeat(auto-fit, minmax(256px, 1fr));
@ -43,8 +50,12 @@ const classes = computed(() => [
} }
} }
.VPTeamMembers.medium.count-1 .container { max-width: 368px; } .VPTeamMembers.medium.count-1 .container {
.VPTeamMembers.medium.count-2 .container { max-width: calc(368px * 2 + 24px); } max-width: 368px;
}
.VPTeamMembers.medium.count-2 .container {
max-width: calc(368px * 2 + 24px);
}
.container { .container {
display: grid; display: grid;

@ -4,17 +4,21 @@ import VPIconHeart from './icons/VPIconHeart.vue'
import VPLink from './VPLink.vue' import VPLink from './VPLink.vue'
import VPSocialLinks from './VPSocialLinks.vue' import VPSocialLinks from './VPSocialLinks.vue'
defineProps<{ interface Props {
size?: 'small' | 'medium' size?: 'small' | 'medium'
member: DefaultTheme.TeamMember member: DefaultTheme.TeamMember
}>() }
withDefaults(defineProps<Props>(), {
size: 'medium'
})
</script> </script>
<template> <template>
<article class="VPTeamMembersItem" :class="[size ?? 'medium']"> <article class="VPTeamMembersItem" :class="[size]">
<div class="profile"> <div class="profile">
<figure class="avatar"> <figure class="avatar">
<img class="avatar-img" :src="member.avatar" :alt="member.name"> <img class="avatar-img" :src="member.avatar" :alt="member.name" />
</figure> </figure>
<div class="data"> <div class="data">
<h1 class="name"> <h1 class="name">
@ -24,14 +28,18 @@ defineProps<{
<span v-if="member.title" class="title"> <span v-if="member.title" class="title">
{{ member.title }} {{ member.title }}
</span> </span>
<span v-if="member.title && member.org" class="at"> <span v-if="member.title && member.org" class="at"> @ </span>
@ <VPLink
</span> v-if="member.org"
<VPLink v-if="member.org" class="org" :class="{ link: member.orgLink }" :href="member.orgLink" no-icon> class="org"
:class="{ link: member.orgLink }"
:href="member.orgLink"
no-icon
>
{{ member.org }} {{ member.org }}
</VPLink> </VPLink>
</p> </p>
<p v-if="member.desc" class="desc" v-html="member.desc"/> <p v-if="member.desc" class="desc" v-html="member.desc" />
<div v-if="member.links" class="links"> <div v-if="member.links" class="links">
<VPSocialLinks :links="member.links" /> <VPSocialLinks :links="member.links" />
</div> </div>
@ -155,12 +163,12 @@ defineProps<{
} }
.name { .name {
margin: 0; margin: 0;
font-weight: 600; font-weight: 600;
} }
.affiliation { .affiliation {
margin: 0; margin: 0;
font-weight: 500; font-weight: 500;
color: var(--vp-c-text-2); color: var(--vp-c-text-2);
} }

Loading…
Cancel
Save