From 894e8a61b6fea3618fc863bd77678385e9d021c2 Mon Sep 17 00:00:00 2001 From: Daniele Nicosia Date: Tue, 13 May 2025 14:44:23 +0200 Subject: [PATCH] feat(Badge): add `square` prop (#4008) Co-authored-by: Benjamin Canac --- playground/app/pages/components/badge.vue | 19 +++++++++++--- src/runtime/components/Badge.vue | 5 +++- src/theme/badge.ts | 23 +++++++++++++++++ test/components/Badge.spec.ts | 1 + .../__snapshots__/Badge-vue.spec.ts.snap | 25 +++++++++++-------- .../__snapshots__/Badge.spec.ts.snap | 25 +++++++++++-------- 6 files changed, 74 insertions(+), 24 deletions(-) diff --git a/playground/app/pages/components/badge.vue b/playground/app/pages/components/badge.vue index 8694c259..b85227ee 100644 --- a/playground/app/pages/components/badge.vue +++ b/playground/app/pages/components/badge.vue @@ -36,14 +36,27 @@ const variants = Object.keys(theme.variants.variant) as Array -
+
-
+
-
+
+
+ +
+
+ +
diff --git a/src/runtime/components/Badge.vue b/src/runtime/components/Badge.vue index 16f28b3f..bde5746e 100644 --- a/src/runtime/components/Badge.vue +++ b/src/runtime/components/Badge.vue @@ -26,6 +26,8 @@ export interface BadgeProps extends Omit(), { as: 'span' }) -defineSlots() +const slots = defineSlots() const appConfig = useAppConfig() as Badge['AppConfig'] const { orientation, size: buttonGroupSize } = useButtonGroup(props) @@ -60,6 +62,7 @@ const ui = computed(() => tv({ extend: tv(theme), ...(appConfig.ui?.badge || {}) color: props.color, variant: props.variant, size: buttonGroupSize.value || props.size, + square: props.square || (!slots.default && !props.label), buttonGroup: orientation.value })) diff --git a/src/theme/badge.ts b/src/theme/badge.ts index 22c184c3..62e65a73 100644 --- a/src/theme/badge.ts +++ b/src/theme/badge.ts @@ -53,6 +53,9 @@ export default (options: Required) => ({ leadingAvatarSize: '2xs', trailingIcon: 'size-6' } + }, + square: { + true: '' } }, compoundVariants: [...(options.theme.colors || []).map((color: string) => ({ @@ -87,6 +90,26 @@ export default (options: Required) => ({ color: 'neutral', variant: 'subtle', class: 'ring ring-inset ring-accented text-default bg-elevated' + }, { + size: 'xs', + square: true, + class: 'p-0.5' + }, { + size: 'sm', + square: true, + class: 'p-1' + }, { + size: 'md', + square: true, + class: 'p-1' + }, { + size: 'lg', + square: true, + class: 'p-1' + }, { + size: 'xl', + square: true, + class: 'p-1' }], defaultVariants: { color: 'primary', diff --git a/test/components/Badge.spec.ts b/test/components/Badge.spec.ts index 5378ee44..a9452e9e 100644 --- a/test/components/Badge.spec.ts +++ b/test/components/Badge.spec.ts @@ -21,6 +21,7 @@ describe('Badge', () => { ['with avatar', { props: { avatar: { src: 'https://github.com/benjamincanac.png' } } }], ['with avatar and leadingIcon', { props: { avatar: { src: 'https://github.com/benjamincanac.png' }, leadingIcon: 'i-lucide-arrow-left' } }], ['with avatar and trailingIcon', { props: { avatar: { src: 'https://github.com/benjamincanac.png' }, trailingIcon: 'i-lucide-arrow-right' } }], + ['with square', { props: { label: 'Badge', square: true } }], ['with as', { props: { label: 'Badge', as: 'div' } }], ['with class', { props: { label: 'Badge', class: 'rounded-full font-bold' } }], ['with ui', { props: { label: 'Badge', ui: { label: 'font-bold' } } }], diff --git a/test/components/__snapshots__/Badge-vue.spec.ts.snap b/test/components/__snapshots__/Badge-vue.spec.ts.snap index 3d754615..1d544baa 100644 --- a/test/components/__snapshots__/Badge-vue.spec.ts.snap +++ b/test/components/__snapshots__/Badge-vue.spec.ts.snap @@ -7,15 +7,15 @@ exports[`Badge > renders with as correctly 1`] = `
" `; -exports[`Badge > renders with avatar and leadingIcon correctly 1`] = `""`; +exports[`Badge > renders with avatar and leadingIcon correctly 1`] = `""`; exports[`Badge > renders with avatar and trailingIcon correctly 1`] = ` -" +" " `; exports[`Badge > renders with avatar correctly 1`] = ` -" +" " `; @@ -27,18 +27,18 @@ exports[`Badge > renders with class correctly 1`] = ` exports[`Badge > renders with default slot correctly 1`] = `"Default slot"`; -exports[`Badge > renders with icon correctly 1`] = `""`; +exports[`Badge > renders with icon correctly 1`] = `""`; exports[`Badge > renders with label correctly 1`] = ` "Badge " `; -exports[`Badge > renders with leading and icon correctly 1`] = `""`; +exports[`Badge > renders with leading and icon correctly 1`] = `""`; -exports[`Badge > renders with leading slot correctly 1`] = `"Leading slot"`; +exports[`Badge > renders with leading slot correctly 1`] = `"Leading slot"`; -exports[`Badge > renders with leadingIcon correctly 1`] = `""`; +exports[`Badge > renders with leadingIcon correctly 1`] = `""`; exports[`Badge > renders with neutral variant outline correctly 1`] = ` "Badge @@ -105,11 +105,16 @@ exports[`Badge > renders with size xs correctly 1`] = ` " `; -exports[`Badge > renders with trailing and icon correctly 1`] = `""`; +exports[`Badge > renders with square correctly 1`] = ` +"Badge +" +`; -exports[`Badge > renders with trailing slot correctly 1`] = `"Trailing slot"`; +exports[`Badge > renders with trailing and icon correctly 1`] = `""`; -exports[`Badge > renders with trailingIcon correctly 1`] = `""`; +exports[`Badge > renders with trailing slot correctly 1`] = `"Trailing slot"`; + +exports[`Badge > renders with trailingIcon correctly 1`] = `""`; exports[`Badge > renders with ui correctly 1`] = ` "Badge diff --git a/test/components/__snapshots__/Badge.spec.ts.snap b/test/components/__snapshots__/Badge.spec.ts.snap index 16e2f112..c53a59ef 100644 --- a/test/components/__snapshots__/Badge.spec.ts.snap +++ b/test/components/__snapshots__/Badge.spec.ts.snap @@ -8,18 +8,18 @@ exports[`Badge > renders with as correctly 1`] = ` `; exports[`Badge > renders with avatar and leadingIcon correctly 1`] = ` -" +" " `; exports[`Badge > renders with avatar and trailingIcon correctly 1`] = ` -" +" " `; exports[`Badge > renders with avatar correctly 1`] = ` -" +" " `; @@ -32,7 +32,7 @@ exports[`Badge > renders with class correctly 1`] = ` exports[`Badge > renders with default slot correctly 1`] = `"Default slot"`; exports[`Badge > renders with icon correctly 1`] = ` -" +" " `; @@ -43,15 +43,15 @@ exports[`Badge > renders with label correctly 1`] = ` `; exports[`Badge > renders with leading and icon correctly 1`] = ` -" +" " `; -exports[`Badge > renders with leading slot correctly 1`] = `"Leading slot"`; +exports[`Badge > renders with leading slot correctly 1`] = `"Leading slot"`; exports[`Badge > renders with leadingIcon correctly 1`] = ` -" +" " `; @@ -121,11 +121,16 @@ exports[`Badge > renders with size xs correctly 1`] = ` " `; -exports[`Badge > renders with trailing and icon correctly 1`] = `""`; +exports[`Badge > renders with square correctly 1`] = ` +"Badge +" +`; -exports[`Badge > renders with trailing slot correctly 1`] = `"Trailing slot"`; +exports[`Badge > renders with trailing and icon correctly 1`] = `""`; -exports[`Badge > renders with trailingIcon correctly 1`] = `""`; +exports[`Badge > renders with trailing slot correctly 1`] = `"Trailing slot"`; + +exports[`Badge > renders with trailingIcon correctly 1`] = `""`; exports[`Badge > renders with ui correctly 1`] = ` "Badge