Skip to content

Commit 6c2dd9a

Browse files
committed
feat: badge component
1 parent 15be1d3 commit 6c2dd9a

45 files changed

Lines changed: 2022 additions & 0 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
2+
// SPDX-License-Identifier: BSD-3-Clause-Clear
3+
4+
import {booleanDataAttr} from "@qualcomm-ui/utils/attributes"
5+
import type {PropNormalizer} from "@qualcomm-ui/utils/machine"
6+
7+
import {badgeClasses} from "./badge.classes"
8+
import type {
9+
QdsBadgeApi,
10+
QdsBadgeApiProps,
11+
QdsBadgeCountProps,
12+
QdsBadgeIconBindings,
13+
QdsBadgeIconProps,
14+
QdsBadgeRootBindings,
15+
QdsBadgeStatusProps,
16+
QdsBadgeTextProps,
17+
} from "./badge.types"
18+
19+
export function createQdsBadgeApi(
20+
props: QdsBadgeApiProps,
21+
normalize: PropNormalizer,
22+
): QdsBadgeApi {
23+
const type = props.type || "text"
24+
const size = props.size || "md"
25+
26+
let emphasis: string | undefined
27+
let variant: string | undefined
28+
let overflow = false
29+
let displayCount: number | string | null = null
30+
31+
if (type === "count") {
32+
const countProps = props as QdsBadgeCountProps
33+
variant = countProps.variant || "neutral"
34+
if (countProps.count != null) {
35+
const max = countProps.max ?? 99
36+
overflow = countProps.count > max
37+
displayCount = overflow ? `${max}+` : countProps.count
38+
}
39+
} else if (type === "status") {
40+
emphasis = (props as QdsBadgeStatusProps).emphasis || "neutral"
41+
variant = (props as QdsBadgeStatusProps).variant || "filled"
42+
} else if (type === "icon") {
43+
emphasis = (props as QdsBadgeIconProps).emphasis || "neutral"
44+
variant = (props as QdsBadgeIconProps).variant || "default"
45+
} else {
46+
emphasis = (props as QdsBadgeTextProps).emphasis || "neutral"
47+
variant = (props as QdsBadgeTextProps).variant || "default"
48+
}
49+
50+
return {
51+
getDisplayCount(): number | string | null {
52+
return displayCount
53+
},
54+
getIconBindings(): QdsBadgeIconBindings {
55+
return normalize.element({
56+
className: badgeClasses.icon,
57+
"data-part": "icon",
58+
"data-scope": "badge",
59+
"data-size": size,
60+
})
61+
},
62+
getRootBindings(): QdsBadgeRootBindings {
63+
return normalize.element({
64+
className: badgeClasses.root,
65+
"data-disabled": booleanDataAttr(props.disabled),
66+
"data-emphasis": emphasis,
67+
"data-overflow": booleanDataAttr(overflow),
68+
"data-part": "root",
69+
"data-scope": "badge",
70+
"data-size": size,
71+
"data-type": type,
72+
"data-variant": variant,
73+
})
74+
},
75+
type,
76+
}
77+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
2+
// SPDX-License-Identifier: BSD-3-Clause-Clear
3+
4+
export const badgeClasses = {
5+
icon: "qui-badge__icon",
6+
root: "qui-badge__root",
7+
} as const
Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
1+
// Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries.
2+
// SPDX-License-Identifier: BSD-3-Clause-Clear
3+
4+
import type {BooleanDataAttr} from "@qualcomm-ui/utils/attributes"
5+
6+
import type {badgeClasses} from "./badge.classes"
7+
8+
export type QdsBadgeType = "count" | "icon" | "text" | "status"
9+
10+
export type QdsBadgeBasicSize = "sm" | "md" | "lg"
11+
export type QdsBadgeExtendedSize = "xs" | QdsBadgeBasicSize | "xl"
12+
export type QdsBadgeIconSize = "xxs" | QdsBadgeExtendedSize
13+
14+
export type QdsBadgeSize =
15+
| QdsBadgeBasicSize
16+
| QdsBadgeExtendedSize
17+
| QdsBadgeIconSize
18+
19+
export type QdsBadgeSemanticEmphasis =
20+
| "neutral"
21+
| "brand"
22+
| "info"
23+
| "success"
24+
| "warning"
25+
| "danger"
26+
27+
export type QdsBadgeCategoryEmphasis =
28+
| "cat-1"
29+
| "cat-2"
30+
| "cat-3"
31+
| "cat-4"
32+
| "cat-5"
33+
| "cat-6"
34+
| "cat-7"
35+
| "cat-8"
36+
| "cat-9"
37+
| "cat-10"
38+
39+
export type QdsBadgeCountVariant =
40+
| "neutral"
41+
| "neutral-outline"
42+
| "brand"
43+
| "brand-outline"
44+
| "info"
45+
| "success"
46+
| "warning"
47+
| "danger"
48+
| "persistent-black"
49+
| "persistent-white"
50+
51+
export type QdsBadgeStatusEmphasis = QdsBadgeSemanticEmphasis
52+
export type QdsBadgeStatusVariant = "filled" | "outlined"
53+
54+
export type QdsBadgeIconTextEmphasis =
55+
| QdsBadgeSemanticEmphasis
56+
| QdsBadgeCategoryEmphasis
57+
export type QdsBadgeIconTextVariant = "default" | "subtle"
58+
59+
interface QdsBadgeBaseProps {
60+
/**
61+
* The badge disabled state.
62+
*/
63+
disabled?: boolean
64+
}
65+
66+
export interface QdsBadgeCountProps extends QdsBadgeBaseProps {
67+
/**
68+
* The numeric count to display.
69+
*/
70+
count?: number
71+
72+
/**
73+
* Maximum count to display.
74+
* @default 99
75+
*/
76+
max?: number
77+
78+
/**
79+
* Governs the size of the badge.
80+
* @default 'md'
81+
*/
82+
size?: QdsBadgeBasicSize
83+
84+
/**
85+
* The badge type.
86+
*/
87+
type: "count"
88+
89+
/**
90+
* Governs the color and style of the count badge.
91+
* @default 'neutral'
92+
*/
93+
variant?: QdsBadgeCountVariant
94+
}
95+
96+
export interface QdsBadgeStatusProps extends QdsBadgeBaseProps {
97+
/**
98+
* Governs the color of the status badge.
99+
* @default 'neutral'
100+
*/
101+
emphasis?: QdsBadgeStatusEmphasis
102+
103+
/**
104+
* Governs the size of the badge.
105+
* @default 'md'
106+
*/
107+
size?: QdsBadgeExtendedSize
108+
109+
/**
110+
* The badge type.
111+
*/
112+
type: "status"
113+
114+
/**
115+
* Governs the style of the status badge.
116+
* @default 'filled'
117+
*/
118+
variant?: QdsBadgeStatusVariant
119+
}
120+
121+
export interface QdsBadgeIconProps extends QdsBadgeBaseProps {
122+
/**
123+
* Governs the color of the icon badge.
124+
* @default 'neutral'
125+
*/
126+
emphasis?: QdsBadgeIconTextEmphasis
127+
128+
/**
129+
* Governs the size of the badge.
130+
* @default 'md'
131+
*/
132+
size?: QdsBadgeIconSize
133+
134+
/**
135+
* The badge type.
136+
*/
137+
type: "icon"
138+
139+
/**
140+
* Governs the style of the icon badge.
141+
* @default 'default'
142+
*/
143+
variant?: QdsBadgeIconTextVariant
144+
}
145+
146+
export interface QdsBadgeTextProps extends QdsBadgeBaseProps {
147+
/**
148+
* Governs the color of the text badge.
149+
* @default 'neutral'
150+
*/
151+
emphasis?: QdsBadgeIconTextEmphasis
152+
153+
/**
154+
* Governs the size of the badge.
155+
* @default 'md'
156+
*/
157+
size?: QdsBadgeBasicSize
158+
159+
/**
160+
* The badge type.
161+
*/
162+
type?: "text"
163+
164+
/**
165+
* Governs the style of the text badge.
166+
* @default 'default'
167+
*/
168+
variant?: QdsBadgeIconTextVariant
169+
}
170+
171+
export type QdsBadgeApiProps =
172+
| QdsBadgeCountProps
173+
| QdsBadgeStatusProps
174+
| QdsBadgeIconProps
175+
| QdsBadgeTextProps
176+
177+
type BadgeClasses = typeof badgeClasses
178+
179+
export interface QdsBadgeRootBindings {
180+
className: BadgeClasses["root"]
181+
"data-disabled": BooleanDataAttr
182+
"data-emphasis"?: string
183+
"data-overflow": BooleanDataAttr
184+
"data-part": "root"
185+
"data-scope": "badge"
186+
"data-size": QdsBadgeSize
187+
"data-type": QdsBadgeType
188+
"data-variant"?: string
189+
}
190+
191+
export interface QdsBadgeIconBindings {
192+
className: BadgeClasses["icon"]
193+
"data-part": "icon"
194+
"data-scope": "badge"
195+
"data-size": QdsBadgeSize
196+
}
197+
198+
export interface QdsBadgeApi {
199+
getDisplayCount(): number | string | null
200+
getIconBindings(): QdsBadgeIconBindings
201+
getRootBindings(): QdsBadgeRootBindings
202+
type: QdsBadgeType
203+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export * from "./badge.api"
2+
export * from "./badge.classes"
3+
export type * from "./badge.types"

0 commit comments

Comments
 (0)