From e2ebf7a26cb19f5f8230c39b09aa62f7336b5752 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Xuan=20Huang=20=28=E9=BB=84=E7=8E=84=29?= Date: Wed, 9 Oct 2024 02:23:16 -0400 Subject: [PATCH] feat(theme-default): children, outline and fixes for Badge (#1451) --- .../docs/en/api/client-api/api-components.mdx | 6 +- ...-components.mdx => builtin-components.mdx} | 61 +++++++++++++++++-- .../en/guide/default-theme/components.mdx | 6 +- .../docs/zh/api/client-api/api-components.mdx | 4 +- ...-components.mdx => builtin-components.mdx} | 61 +++++++++++++++++-- .../zh/guide/default-theme/components.mdx | 4 +- .../src/components/Badge/index.module.scss | 44 ++++++++++--- .../src/components/Badge/index.tsx | 59 ++++++++++++++++-- 8 files changed, 212 insertions(+), 33 deletions(-) rename packages/document/docs/en/fragments/{internal-components.mdx => builtin-components.mdx} (85%) rename packages/document/docs/zh/fragments/{internal-components.mdx => builtin-components.mdx} (85%) diff --git a/packages/document/docs/en/api/client-api/api-components.mdx b/packages/document/docs/en/api/client-api/api-components.mdx index 73ee78c80..835f09735 100644 --- a/packages/document/docs/en/api/client-api/api-components.mdx +++ b/packages/document/docs/en/api/client-api/api-components.mdx @@ -1,5 +1,5 @@ -# Internal Components +# Built-in Components -import InternalComponents from '../../fragments/internal-components'; +import BuiltinComponents from '../../fragments/builtin-components'; - + diff --git a/packages/document/docs/en/fragments/internal-components.mdx b/packages/document/docs/en/fragments/builtin-components.mdx similarity index 85% rename from packages/document/docs/en/fragments/internal-components.mdx rename to packages/document/docs/en/fragments/builtin-components.mdx index 8f605f2c0..c84486f92 100644 --- a/packages/document/docs/en/fragments/internal-components.mdx +++ b/packages/document/docs/en/fragments/builtin-components.mdx @@ -6,7 +6,19 @@ The Badge component is used to display a badge. For example: import { Badge } from '@theme'; function App() { + // Using text prop return ; + + // Using custom children + return ( + + + Rspress + + ); } ``` @@ -14,18 +26,59 @@ The effect is as follows: import { Badge } from '@theme'; + + + +Custom children: + +import SearchSvg from '@theme-assets/github'; + + + + Rspress + + + + + Github + + +Inlined with text + +##### H5 + +#### H4 + +### H3 The types of props included are as follows: ```ts interface BadgeProps { - // Used to set the text of the badge - text: string; - // Used to set the type of the badge - type?: 'info' | 'warning' | 'danger'; + /** + * The content to display inside the badge. Can be a string or React nodes. + */ + children?: React.ReactNode; + /** + * The type of badge, which determines its color and style. + * @default 'tip' + */ + type?: 'tip' | 'info' | 'warning' | 'danger'; + /** + * The text content to display inside the badge (for backwards compatibility). + */ + text?: string; + /** + * Whether to display the badge with an outline style. + * @default false + */ + outline?: boolean; } ``` diff --git a/packages/document/docs/en/guide/default-theme/components.mdx b/packages/document/docs/en/guide/default-theme/components.mdx index 2a0cd00da..835f09735 100644 --- a/packages/document/docs/en/guide/default-theme/components.mdx +++ b/packages/document/docs/en/guide/default-theme/components.mdx @@ -1,5 +1,5 @@ -# Internal Components +# Built-in Components -import InternalComponents from '../../fragments/internal-components' +import BuiltinComponents from '../../fragments/builtin-components'; - + diff --git a/packages/document/docs/zh/api/client-api/api-components.mdx b/packages/document/docs/zh/api/client-api/api-components.mdx index f4a70a3f8..0c00df2f2 100644 --- a/packages/document/docs/zh/api/client-api/api-components.mdx +++ b/packages/document/docs/zh/api/client-api/api-components.mdx @@ -1,5 +1,5 @@ # 内置组件 -import InternalComponents from '../../fragments/internal-components'; +import BuiltinComponents from '../../fragments/builtin-components'; - + diff --git a/packages/document/docs/zh/fragments/internal-components.mdx b/packages/document/docs/zh/fragments/builtin-components.mdx similarity index 85% rename from packages/document/docs/zh/fragments/internal-components.mdx rename to packages/document/docs/zh/fragments/builtin-components.mdx index 586955906..1ed038b89 100644 --- a/packages/document/docs/zh/fragments/internal-components.mdx +++ b/packages/document/docs/zh/fragments/builtin-components.mdx @@ -6,7 +6,19 @@ Badge 组件用于展示状态的标记。使用方法如下: import { Badge } from '@theme'; function App() { + // 使用 text 属性 return ; + + // 使用自定义子元素 + return ( + + + Rspress + + ); } ``` @@ -14,18 +26,59 @@ function App() { import { Badge } from '@theme'; + + + +import SearchSvg from '@theme-assets/github'; + +自定义子元素: + + + + Rspress + + + + + Github + + +内联文本 + +##### H5 + +#### H4 + +### H3 其中包含的 props 类型如下: ```ts interface BadgeProps { - // 用于设置 badge 的文本 - text: string; - // 用于设置 badge 的类型 - type: 'info' | 'warning' | 'danger'; + /** + * 在徽章内显示的内容。可以是字符串或 React 节点。 + */ + children?: React.ReactNode; + /** + * 徽章的类型,决定其颜色和样式。 + * @default 'tip' + */ + type?: 'tip' | 'info' | 'warning' | 'danger'; + /** + * 在徽章内显示的文本内容(为了向后兼容)。 + */ + text?: string; + /** + * 是否以轮廓样式显示徽章。 + * @default false + */ + outline?: boolean; } ``` diff --git a/packages/document/docs/zh/guide/default-theme/components.mdx b/packages/document/docs/zh/guide/default-theme/components.mdx index 15132c978..0c00df2f2 100644 --- a/packages/document/docs/zh/guide/default-theme/components.mdx +++ b/packages/document/docs/zh/guide/default-theme/components.mdx @@ -1,5 +1,5 @@ # 内置组件 -import InternalComponents from '../../fragments/internal-components' +import BuiltinComponents from '../../fragments/builtin-components'; - + diff --git a/packages/theme-default/src/components/Badge/index.module.scss b/packages/theme-default/src/components/Badge/index.module.scss index 1ca8d0616..272c0f0dc 100644 --- a/packages/theme-default/src/components/Badge/index.module.scss +++ b/packages/theme-default/src/components/Badge/index.module.scss @@ -1,18 +1,44 @@ .badge { - padding: 0 10px; - line-height: 22px; - font-size: 12px; + font-weight: 500; + transition: color 0.25s; + + &.tip { + color: var(--rp-container-tip-text); + background-color: var(--rp-container-tip-bg); + } &.info { - color: #2e3440; - background-color: rgba(46, 52, 64, 0.16); + color: var(--rp-container-info-text); + background-color: var(--rp-container-info-bg); } + &.warning { - color: #ad850e; - background-color: rgba(255, 197, 23, 0.16); + color: var(--rp-container-warning-text); + background-color: var(--rp-container-warning-bg); } + &.danger { - color: #ab2131; - background-color: rgba(237, 60, 80, 0.16); + color: var(--rp-container-danger-text); + background-color: var(--rp-container-danger-bg); + } + + &.outline { + border: 1px solid; + + &.tip { + border-color: var(--rp-container-tip-border); + } + + &.info { + border-color: var(--rp-container-info-border); + } + + &.warning { + border-color: var(--rp-container-warning-border); + } + + &.danger { + border-color: var(--rp-container-danger-border); + } } } diff --git a/packages/theme-default/src/components/Badge/index.tsx b/packages/theme-default/src/components/Badge/index.tsx index 807d7d7a8..b4abc67af 100644 --- a/packages/theme-default/src/components/Badge/index.tsx +++ b/packages/theme-default/src/components/Badge/index.tsx @@ -1,17 +1,64 @@ import styles from './index.module.scss'; interface BadgeProps { - text: string; - type: 'info' | 'warning' | 'danger'; + /** + * The content to display inside the badge. Can be a string or React nodes. + */ + children?: React.ReactNode; + /** + * The type of badge, which determines its color and style. + * @default 'tip' + */ + type?: 'tip' | 'info' | 'warning' | 'danger'; + /** + * The text content to display inside the badge (for backwards compatibility). + */ + text?: string; + /** + * Whether to display the badge with an outline style. + * @default false + */ + outline?: boolean; } -export function Badge(props: BadgeProps) { - const { text, type = 'info' } = props; +/** + * A component that renders a styled badge with custom content. + * + * The Badge component displays a small, inline element with customizable content and appearance. + * It's useful for highlighting status, categories, or other short pieces of information. + * + * @param {BadgeProps} props - The properties for the Badge component. + * @returns {JSX.Element} A span element representing the badge. + * + * @example + * Using children: + * New + * Experimental + * Deprecated + * Pro Tip: Use custom elements + * + * Using text prop: + * + * + * + */ +export function Badge({ + children, + type = 'tip', + text, + outline = false, +}: BadgeProps) { + const content = children || text; + return ( - {text} + {content} ); }