-
Notifications
You must be signed in to change notification settings - Fork 130
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(theme-default): children, outline and fixes for Badge
Improve built-in `<Badge>` components: - Custom children: this is particularly useful for adding icons. - `outline` support: more variations! - Style tweak: - use CSS variables from containers to look consistent. - fixes default badge being invisible in Dark mode. - better vertically aligned with different headings. - Changes to `type` props: - add `info` type to be consistent with containers. - `tip` is added as the new default. Do note that even though this kept untyped `<Badge>` unchanged, it does introduce a **benign** breaking change to `<Badge type="info">`. Changes to Docs: - Badges docs are updated to reflect the new usages and new types. - This MR correct "Internal Components" to be "Built-in Components". (does not involve URL changes due to the use of fragments)
- Loading branch information
Showing
8 changed files
with
212 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
# Internal Components | ||
# Built-in Components | ||
|
||
import InternalComponents from '../../fragments/internal-components'; | ||
import BuiltinComponents from '../../fragments/builtin-components'; | ||
|
||
<InternalComponents /> | ||
<BuiltinComponents /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
# Internal Components | ||
# Built-in Components | ||
|
||
import InternalComponents from '../../fragments/internal-components' | ||
import BuiltinComponents from '../../fragments/builtin-components'; | ||
|
||
<InternalComponents /> | ||
<BuiltinComponents /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
# 内置组件 | ||
|
||
import InternalComponents from '../../fragments/internal-components'; | ||
import BuiltinComponents from '../../fragments/builtin-components'; | ||
|
||
<InternalComponents /> | ||
<BuiltinComponents /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
# 内置组件 | ||
|
||
import InternalComponents from '../../fragments/internal-components' | ||
import BuiltinComponents from '../../fragments/builtin-components'; | ||
|
||
<InternalComponents /> | ||
<BuiltinComponents /> |
44 changes: 35 additions & 9 deletions
44
packages/theme-default/src/components/Badge/index.module.scss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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: | ||
* <Badge type="info">New</Badge> | ||
* <Badge type="warning" outline>Experimental</Badge> | ||
* <Badge type="danger">Deprecated</Badge> | ||
* <Badge type="tip" outline><strong>Pro Tip:</strong> Use custom elements</Badge> | ||
* | ||
* Using text prop: | ||
* <Badge text="New" type="info" /> | ||
* <Badge text="Experimental" type="warning" outline /> | ||
* <Badge text="Deprecated" type="danger" /> | ||
*/ | ||
export function Badge({ | ||
children, | ||
type = 'tip', | ||
text, | ||
outline = false, | ||
}: BadgeProps) { | ||
const content = children || text; | ||
|
||
return ( | ||
<span | ||
className={`inline-block rounded-full border border-solid border-transparent font-medium ${styles.badge} ${styles[type]}`} | ||
className={`inline-flex items-center justify-center rounded-full border border-solid ${ | ||
outline ? 'border-current' : 'border-transparent' | ||
} font-semibold align-middle px-2.5 h-6 gap-1 text-xs ${styles.badge} ${styles[type]} ${ | ||
outline ? styles.outline : '' | ||
}`} | ||
> | ||
{text} | ||
{content} | ||
</span> | ||
); | ||
} |