|
| 1 | +--- |
| 2 | +title: Banner 组件用法 |
| 3 | +description: '如何使用通用的 Banner 组件来创建页面标题区域。' |
| 4 | +publishDate: 2025-10-17 19:45:00 |
| 5 | +slug: yex1 |
| 6 | +tags: ['组件', '建站'] |
| 7 | +--- |
| 8 | + |
| 9 | +import { MdxRepl, Aside } from '@/custom/components/user'; |
| 10 | +import Banner from '@/components/banner/Banner.astro'; |
| 11 | +import { Icon } from '@/custom/components/user'; |
| 12 | +import bannerImage from '@/assets/shelf/desk1.png'; |
| 13 | + |
| 14 | +`Banner` 是一个通用的页面横幅组件,用于快速创建一个包含左侧内容和右侧图片的标题区域。它通过 Astro 的 `slot` (插槽) 特性提供了极高的灵活性。 |
| 15 | + |
| 16 | +### 基础用法 |
| 17 | + |
| 18 | +`Banner` 组件接收两个主要属性:`image` 和 `imageAlt`。您需要将想要在左侧显示的任何内容放置在 `<Banner>` 和 `</Banner>` 标签之间。 |
| 19 | + |
| 20 | +<MdxRepl> |
| 21 | + <Banner image={bannerImage} imageAlt="示例图片"> |
| 22 | + <div class="flex items-center gap-3"> |
| 23 | + <Icon name="book" class="size-8" /> |
| 24 | + <h2 class="text-3xl font-bold">这是主标题</h2> |
| 25 | + </div> |
| 26 | + <p class="border-l-2 border-border pl-3 text-muted-foreground mt-2"> |
| 27 | + 这是副标题,您可以在这里写一些描述性文字。 |
| 28 | + </p> |
| 29 | + </Banner> |
| 30 | + <Fragment slot="desc"> |
| 31 | + ```jsx |
| 32 | + import Banner from '@/components/banner/Banner.astro'; |
| 33 | + import { Icon } from '@/custom/components/user'; |
| 34 | + import bannerImage from '@/assets/shelf/desk1.png'; |
| 35 | + |
| 36 | + <Banner image={bannerImage} imageAlt="示例图片"> |
| 37 | + <div class="flex items-center gap-3"> |
| 38 | + <Icon name="book" class="size-8" /> |
| 39 | + <h2 class="text-3xl font-bold">这是主标题</h2> |
| 40 | + </div> |
| 41 | + <p class="border-l-2 border-border pl-3 text-muted-foreground mt-2"> |
| 42 | + 这是副标题,您可以在这里写一些描述性文字。 |
| 43 | + </p> |
| 44 | + </Banner> |
| 45 | + ``` |
| 46 | + </Fragment> |
| 47 | +</MdxRepl> |
| 48 | + |
| 49 | +### MDX Syntax Example |
| 50 | + |
| 51 | +When using in an `.mdx` file, you can write Markdown directly in the slot, like this: |
| 52 | + |
| 53 | +<MdxRepl prose={true}> |
| 54 | + <Banner image={bannerImage} imageAlt="MDX Example Image"> |
| 55 | + > This is a Markdown Title |
| 56 | +
|
| 57 | + </Banner> |
| 58 | + <Fragment slot="desc"> |
| 59 | + ```mdx |
| 60 | + import Banner from '@/components/banner/Banner.astro'; |
| 61 | + import bannerImage from '@/assets/shelf/desk1.png'; |
| 62 | + |
| 63 | + {/* 显然这里不适合有太多的太复杂的内容 */} |
| 64 | + <Banner image={bannerImage} imageAlt="MDX Example Image"> |
| 65 | + > This is a Markdown Title |
| 66 | + |
| 67 | + </Banner> |
| 68 | + ``` |
| 69 | + </Fragment> |
| 70 | +</MdxRepl> |
| 71 | + |
| 72 | +### 组件属性 (Props) |
| 73 | + |
| 74 | +| 属性 | 类型 | 是否必需 | 描述 | |
| 75 | +|---|---|---|---| |
| 76 | +| `image` | `ImageMetadata` | 是 | 用于右侧展示的图片资源, 需要从 `src` 目录导入。 | |
| 77 | +| `imageAlt` | `string` | 是 | 图片的替代文本。 | |
| 78 | +| `class` | `string` | 否 | 为组件根元素添加额外的 CSS 类。 | |
| 79 | +| `loading` | `'lazy' \| 'eager'` | 否 | 图片的加载策略,对于首屏的 Banner,推荐设置为 `eager`。 | |
| 80 | + |
| 81 | + |
| 82 | +<Banner image={bannerImage} imageAlt="MDX Example Image"> |
| 83 | + ## This is a Markdown Title |
| 84 | + This is a paragraph written in **Markdown** syntax. |
| 85 | +</Banner> |
| 86 | + |
| 87 | +<Banner image={bannerImage} imageAlt="MDX Example Image"> |
| 88 | + > This is a Markdown Title |
| 89 | +
|
| 90 | +</Banner> |
| 91 | + |
| 92 | +### 插槽 (Slot) |
| 93 | + |
| 94 | +<Aside type="info"> |
| 95 | +`Banner` 组件的核心是它的默认插槽。您可以在组件标签内放置任何有效的 HTML 或 Astro 组件,它们将被渲染在 Banner 的左侧区域。 |
| 96 | +</Aside> |
| 97 | + |
| 98 | +<Aside type="tip" title="在 MDX 中使用"> |
| 99 | +正如本页面一样,当您在 `.mdx` 文件中使用 `Banner` 组件时,可以直接在插槽中编写 Markdown 语法,它会被自动解析。 |
| 100 | +```mdx |
| 101 | +<Banner image={...}> |
| 102 | + ### 我是一个 Markdown 标题 |
| 103 | + 这是用 **Markdown** 写的段落。 |
| 104 | +</Banner> |
| 105 | +``` |
| 106 | +</Aside> |
0 commit comments