@propellerads/badge 5.0.0 → 7.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +43 -11
- package/dist/index.css +1 -1
- package/dist/index.d.ts +28 -4
- package/dist/index.js +24 -15
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -41,7 +41,8 @@ import {CheckCircleOutline, Time} from '@propellerads/icon';
|
|
|
41
41
|
|
|
42
42
|
The badge is a `<span>`, 20px high and fully round, with the text in
|
|
43
43
|
`--font-description`. `render` swaps the element — `render={<div />}` — and the
|
|
44
|
-
badge writes `data-slot="badge"`, `data-variant
|
|
44
|
+
badge writes `data-slot="badge"`, `data-variant`, `data-tone` and, when it is
|
|
45
|
+
set, `data-inverse` on it.
|
|
45
46
|
|
|
46
47
|
The text is written in sentence case whatever case it arrives in: `NEW` and
|
|
47
48
|
`new` both read `New`. Only the text the badge is handed directly is rewritten;
|
|
@@ -51,15 +52,17 @@ an element inside it keeps its own.
|
|
|
51
52
|
|
|
52
53
|
Two axes, as on the button, and every form takes every tone.
|
|
53
54
|
|
|
54
|
-
`variant` is the form
|
|
55
|
+
`variant` is the form. `fill` and `outline` are the defaults, what a status
|
|
56
|
+
badge is on the page background; `bright` is for a special badge: a promo, a tier.
|
|
55
57
|
|
|
56
|
-
| Variant | Draws |
|
|
57
|
-
|
|
58
|
-
| `fill` (default) | the tone's pad under the text |
|
|
59
|
-
| `outline` | no pad; a 1px dotted contour in the tone's colour |
|
|
58
|
+
| Variant | Draws | For |
|
|
59
|
+
|---|---|---|
|
|
60
|
+
| `fill` (default) | the tone's pad under the text | a settled status |
|
|
61
|
+
| `outline` | no pad; a 1px dotted contour in the tone's colour | a state on the way somewhere |
|
|
62
|
+
| `bright` | the tone's colour itself as the pad, with a sheen of `--color-light` laid on its left end in `overlay`; text and icon in `--color-light-inversion` | a special badge: a promo, a tier |
|
|
60
63
|
|
|
61
|
-
Every badge carries the 1px border, transparent on `
|
|
62
|
-
same size and the text sits in the same place.
|
|
64
|
+
Every badge carries the 1px border, transparent on all but `outline`, so every
|
|
65
|
+
form is the same size and the text sits in the same place.
|
|
63
66
|
|
|
64
67
|
`tone` is the colour. Each tone is a token triple: the pad `-bg`, the text
|
|
65
68
|
`-text`, and the colour itself for the icon and the contour.
|
|
@@ -75,7 +78,32 @@ same size and the text sits in the same place.
|
|
|
75
78
|
|
|
76
79
|
`-text` on its own pad stays under 4.5:1 for every tone in at least one theme —
|
|
77
80
|
`warning` and `highlight` the furthest — so a badge is a label, not a place for a
|
|
78
|
-
sentence.
|
|
81
|
+
sentence. White on `bright`'s `warning`, `highlight` and `quiet` stays well under
|
|
82
|
+
3:1 in the light theme.
|
|
83
|
+
|
|
84
|
+
## On a busy surface
|
|
85
|
+
|
|
86
|
+
`inverse` is not a form but where the badge stands: a surface the design system
|
|
87
|
+
does not control — a photo, a coloured pad, a banner — where neither a tinted
|
|
88
|
+
pad nor the tone on its own stays legible.
|
|
89
|
+
|
|
90
|
+
```tsx
|
|
91
|
+
<Badge inverse tone="success">Recommend</Badge>
|
|
92
|
+
<Badge inverse variant="outline" tone="info" icon={Time}>Moderation</Badge>
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
Each form turns over in `--color-light`, the end of the scale that stays light
|
|
96
|
+
whatever the theme is — the surface behind the badge is not the page's, so the
|
|
97
|
+
badge does not follow the page's theme:
|
|
98
|
+
|
|
99
|
+
| Variant | With `inverse` |
|
|
100
|
+
|---|---|
|
|
101
|
+
| `fill` | the pad in that colour, the text and the icon in the tone |
|
|
102
|
+
| `outline` | still no pad; the dotted contour, the text and the icon in that colour |
|
|
103
|
+
| `bright` | does not compile: it already has a pad of its own |
|
|
104
|
+
|
|
105
|
+
An inverse outline is the same in every tone; when the tone matters on a busy
|
|
106
|
+
surface, use the fill.
|
|
79
107
|
|
|
80
108
|
## Icon
|
|
81
109
|
|
|
@@ -90,10 +118,14 @@ weight as well as by meaning:
|
|
|
90
118
|
- **A contour, not a solid shape.** `CheckCircleOutline` rather than
|
|
91
119
|
`CheckCircle`, `Star` rather than `ThumbUp`. A filled circle reads as a blot.
|
|
92
120
|
- **Ink on no more than a quarter of the 24-unit grid.** `CheckCircleOutline`,
|
|
93
|
-
`Time`, `Block`, `Stop`, `Pause`, `
|
|
94
|
-
|
|
121
|
+
`Time`, `Block`, `Stop`, `Pause`, `Star`, `Segment` and `BellEmpty` sit between
|
|
122
|
+
12% and 26%; `CheckCircle`, `Cancel`, `ThumbUp` and `Verified` are near half.
|
|
95
123
|
- **Two units clear of the grid's edge**, or the icon runs into the round end.
|
|
96
124
|
`ThumbUp`, `Lamp`, `MagicWand` and `Eye` come closer than that.
|
|
125
|
+
- **Nothing out in the corners of the grid**, which the round end cuts off. A
|
|
126
|
+
diagonal glyph passes the edge rule and still pokes out: `Edit`'s nib and
|
|
127
|
+
`Offer`'s tag corner reach 12.5 and 13.3 units from the centre, where the set
|
|
128
|
+
above stays within 10.6.
|
|
97
129
|
- **One family in a row:** circles with circles, plain glyphs with plain glyphs.
|
|
98
130
|
- **Not an icon with its colour baked in** — `LaughingSmile`, `SurveyOffer` and
|
|
99
131
|
the brand logos — which cannot take the tone's.
|
package/dist/index.css
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
.badge{--badge-accent-bg:var(--color-action-bg);--badge-accent-text:var(--color-action-text);--badge-accent:var(--color-action);--badge-bg:var(--badge-accent-bg);--badge-fg:var(--badge-accent-text);--badge-icon:var(--badge-accent);--badge-contour:transparent;box-sizing:border-box;justify-content:center;align-items:center;gap:var(--space-4);width:fit-content;min-height:20px;margin:var(--space-0);padding:var(--space-0) calc(var(--space-8) - var(--space-1));border-radius:var(--radius-40);background:var(--badge-bg);border:var(--space-1) solid var(--badge-contour);color:var(--badge-fg);font:var(--font-description);white-space:nowrap;vertical-align:middle;background-origin:border-box;flex-shrink:0;display:inline-flex;overflow:hidden}.badge--tone-info{--badge-accent-bg:var(--color-action-bg);--badge-accent-text:var(--color-action-text);--badge-accent:var(--color-action)}.badge--tone-warning{--badge-accent-bg:var(--color-warning-bg);--badge-accent-text:var(--color-warning-text);--badge-accent:var(--color-warning)}.badge--tone-danger{--badge-accent-bg:var(--color-danger-bg);--badge-accent-text:var(--color-danger-text);--badge-accent:var(--color-danger)}.badge--tone-success{--badge-accent-bg:var(--color-success-bg);--badge-accent-text:var(--color-success-text);--badge-accent:var(--color-success)}.badge--tone-quiet{--badge-accent-bg:var(--color-quiet-bg);--badge-accent-text:var(--color-quiet-text);--badge-accent:var(--color-quiet)}.badge--tone-highlight{--badge-accent-bg:var(--color-highlight-bg);--badge-accent-text:var(--color-highlight-text);--badge-accent:var(--color-highlight)}.badge--outline{--badge-contour:var(--badge-icon);background:0 0;border-style:dotted}.badge__icon{color:var(--badge-icon);flex:none;align-items:center;display:inline-flex}.badge__icon>svg{width:16px;height:16px}.badge--icon{padding-inline-start:calc(var(--space-4) - var(--space-1))}
|
|
1
|
+
.badge{--badge-accent-bg:var(--color-action-bg);--badge-accent-text:var(--color-action-text);--badge-accent:var(--color-action);--badge-bg:var(--badge-accent-bg);--badge-fg:var(--badge-accent-text);--badge-icon:var(--badge-accent);--badge-contour:transparent;box-sizing:border-box;justify-content:center;align-items:center;gap:var(--space-4);width:fit-content;min-height:20px;margin:var(--space-0);padding:var(--space-0) calc(var(--space-8) - var(--space-1));border-radius:var(--radius-40);background:var(--badge-bg);border:var(--space-1) solid var(--badge-contour);color:var(--badge-fg);font:var(--font-description);white-space:nowrap;vertical-align:middle;background-origin:border-box;flex-shrink:0;display:inline-flex;overflow:hidden}.badge--tone-info{--badge-accent-bg:var(--color-action-bg);--badge-accent-text:var(--color-action-text);--badge-accent:var(--color-action)}.badge--tone-warning{--badge-accent-bg:var(--color-warning-bg);--badge-accent-text:var(--color-warning-text);--badge-accent:var(--color-warning)}.badge--tone-danger{--badge-accent-bg:var(--color-danger-bg);--badge-accent-text:var(--color-danger-text);--badge-accent:var(--color-danger)}.badge--tone-success{--badge-accent-bg:var(--color-success-bg);--badge-accent-text:var(--color-success-text);--badge-accent:var(--color-success)}.badge--tone-quiet{--badge-accent-bg:var(--color-quiet-bg);--badge-accent-text:var(--color-quiet-text);--badge-accent:var(--color-quiet)}.badge--tone-highlight{--badge-accent-bg:var(--color-highlight-bg);--badge-accent-text:var(--color-highlight-text);--badge-accent:var(--color-highlight)}.badge--outline{--badge-contour:var(--badge-icon);background:0 0;border-style:dotted}.badge--bright{--badge-bg:linear-gradient(102deg, color-mix(in srgb, var(--color-light) 50%, transparent), transparent 80%), var(--badge-accent);--badge-fg:var(--color-light-inversion);--badge-icon:var(--color-light-inversion);background-blend-mode:overlay, normal}.badge--inverse.badge--fill{--badge-bg:var(--color-light)}.badge--inverse.badge--outline{--badge-fg:var(--color-light);--badge-icon:var(--color-light)}.badge__icon{color:var(--badge-icon);flex:none;align-items:center;display:inline-flex}.badge__icon>svg{width:16px;height:16px}.badge--icon{padding-inline-start:calc(var(--space-4) - var(--space-1))}
|
package/dist/index.d.ts
CHANGED
|
@@ -9,6 +9,8 @@ declare const BadgeVariant: {
|
|
|
9
9
|
readonly Fill: "fill";
|
|
10
10
|
/** No pad: a dotted contour in the tone's colour, on whatever is behind the badge. */
|
|
11
11
|
readonly Outline: "outline";
|
|
12
|
+
/** The tone's colour itself as the pad, the text and icon on it in the page surface's colour. */
|
|
13
|
+
readonly Bright: "bright";
|
|
12
14
|
};
|
|
13
15
|
type BadgeVariant = (typeof BadgeVariant)[keyof typeof BadgeVariant];
|
|
14
16
|
/**
|
|
@@ -74,9 +76,30 @@ type BadgeIcon = ComponentType<{
|
|
|
74
76
|
* everything in it a product may change only the colours, through `bgColor`
|
|
75
77
|
* and `textColor`. `className` stays for placing the badge on the page.
|
|
76
78
|
*/
|
|
77
|
-
|
|
79
|
+
/**
|
|
80
|
+
* The form, and whether it is inverse. Only `fill` and `outline` turn over:
|
|
81
|
+
* `bright` already has a pad of its own, so `variant="bright"` with `inverse`
|
|
82
|
+
* does not compile rather than compiling to nothing.
|
|
83
|
+
*/
|
|
84
|
+
type BadgeForm = {
|
|
85
|
+
/** How the badge is drawn. @default 'fill' */
|
|
86
|
+
variant?: Exclude<BadgeVariant, typeof BadgeVariant.Bright>;
|
|
87
|
+
/**
|
|
88
|
+
* For a surface the design system does not control — a photo, a coloured
|
|
89
|
+
* pad, a banner. Each form turns over in --color-light, which stays light
|
|
90
|
+
* in both themes: `fill` takes it as its pad and keeps its text and icon
|
|
91
|
+
* in the tone, `outline` stays without a pad and draws its contour, text
|
|
92
|
+
* and icon in it.
|
|
93
|
+
* @default false
|
|
94
|
+
*/
|
|
95
|
+
inverse?: boolean;
|
|
96
|
+
} | {
|
|
78
97
|
/** How the badge is drawn. @default 'fill' */
|
|
79
|
-
variant
|
|
98
|
+
variant: typeof BadgeVariant.Bright;
|
|
99
|
+
/** Not for `bright`: it already has a pad of its own. */
|
|
100
|
+
inverse?: never;
|
|
101
|
+
};
|
|
102
|
+
type BadgeProps = Omit<useRender.ComponentProps<'span'>, 'style'> & BadgeForm & {
|
|
80
103
|
/** Which colour that form is worn in. @default 'info' */
|
|
81
104
|
tone?: BadgeTone;
|
|
82
105
|
/**
|
|
@@ -123,11 +146,12 @@ type BadgeProps = Omit<useRender.ComponentProps<'span'>, 'style'> & {
|
|
|
123
146
|
* `<span className={badgeVariants({variant: 'outline', tone: 'danger'})} />`.
|
|
124
147
|
*/
|
|
125
148
|
declare const badgeVariants: (props?: ({
|
|
126
|
-
variant?: "fill" | "outline" | null | undefined;
|
|
149
|
+
variant?: "fill" | "outline" | "bright" | null | undefined;
|
|
127
150
|
tone?: "info" | "warning" | "danger" | "success" | "quiet" | "highlight" | null | undefined;
|
|
151
|
+
inverse?: boolean | null | undefined;
|
|
128
152
|
} & class_variance_authority_types.ClassProp) | undefined) => string;
|
|
129
153
|
|
|
130
|
-
declare const Badge: ({ className, variant, tone, bgColor, textColor, render, ref, label, type, icon, customStyle, children, ...props }: BadgeProps) => ReactElement<unknown, string | react.JSXElementConstructor<any>>;
|
|
154
|
+
declare const Badge: ({ className, variant, tone, inverse, bgColor, textColor, render, ref, label, type, icon, customStyle, children, ...props }: BadgeProps) => ReactElement<unknown, string | react.JSXElementConstructor<any>>;
|
|
131
155
|
|
|
132
156
|
export { BADGE_TYPE, Badge, BadgeTone, BadgeType, BadgeVariant, badgeVariants, Badge as default };
|
|
133
157
|
export type { BadgeIcon, BadgeProps };
|
package/dist/index.js
CHANGED
|
@@ -7,7 +7,8 @@ import { Fragment as s, jsx as c, jsxs as l } from "react/jsx-runtime";
|
|
|
7
7
|
import './index.css';//#region src/cn.ts
|
|
8
8
|
var u = (...e) => a(e), d = {
|
|
9
9
|
Fill: "fill",
|
|
10
|
-
Outline: "outline"
|
|
10
|
+
Outline: "outline",
|
|
11
|
+
Bright: "bright"
|
|
11
12
|
}, f = {
|
|
12
13
|
Info: "info",
|
|
13
14
|
Warning: "warning",
|
|
@@ -24,7 +25,8 @@ var u = (...e) => a(e), d = {
|
|
|
24
25
|
variants: {
|
|
25
26
|
variant: {
|
|
26
27
|
fill: "badge--fill",
|
|
27
|
-
outline: "badge--outline"
|
|
28
|
+
outline: "badge--outline",
|
|
29
|
+
bright: "badge--bright"
|
|
28
30
|
},
|
|
29
31
|
tone: {
|
|
30
32
|
info: "badge--tone-info",
|
|
@@ -33,11 +35,16 @@ var u = (...e) => a(e), d = {
|
|
|
33
35
|
success: "badge--tone-success",
|
|
34
36
|
quiet: "badge--tone-quiet",
|
|
35
37
|
highlight: "badge--tone-highlight"
|
|
38
|
+
},
|
|
39
|
+
inverse: {
|
|
40
|
+
true: "badge--inverse",
|
|
41
|
+
false: ""
|
|
36
42
|
}
|
|
37
43
|
},
|
|
38
44
|
defaultVariants: {
|
|
39
45
|
variant: "fill",
|
|
40
|
-
tone: "info"
|
|
46
|
+
tone: "info",
|
|
47
|
+
inverse: !1
|
|
41
48
|
}
|
|
42
49
|
}), g = (e) => {
|
|
43
50
|
let t = !0;
|
|
@@ -54,28 +61,30 @@ var u = (...e) => a(e), d = {
|
|
|
54
61
|
"--badge-icon": t
|
|
55
62
|
}
|
|
56
63
|
};
|
|
57
|
-
}, v = (e) => i(e) ? e : r(e, { color: "currentColor" }), y = ({ className: n, variant: r = d.Fill, tone: i,
|
|
58
|
-
let
|
|
64
|
+
}, v = (e) => i(e) ? e : r(e, { color: "currentColor" }), y = ({ className: n, variant: r = d.Fill, tone: i, inverse: a = !1, bgColor: o, textColor: p, render: m, ref: y, label: b, type: x, icon: S, customStyle: C, children: w, ...T }) => {
|
|
65
|
+
let E = i ?? x ?? f.Info, D = _(o ?? C?.background ?? C?.backgroundColor, p ?? C?.color);
|
|
59
66
|
return t({
|
|
60
67
|
defaultTagName: "span",
|
|
61
|
-
render:
|
|
62
|
-
ref:
|
|
68
|
+
render: m,
|
|
69
|
+
ref: y,
|
|
63
70
|
props: e({
|
|
64
71
|
className: u(h({
|
|
65
72
|
variant: r,
|
|
66
|
-
tone:
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
73
|
+
tone: E,
|
|
74
|
+
inverse: a
|
|
75
|
+
}), S !== void 0 && "badge--icon", n),
|
|
76
|
+
style: D,
|
|
77
|
+
children: /* @__PURE__ */ l(s, { children: [S !== void 0 && /* @__PURE__ */ c("span", {
|
|
70
78
|
className: "badge__icon",
|
|
71
79
|
"aria-hidden": !0,
|
|
72
|
-
children: v(
|
|
73
|
-
}), g(
|
|
74
|
-
},
|
|
80
|
+
children: v(S)
|
|
81
|
+
}), g(w ?? b)] })
|
|
82
|
+
}, T),
|
|
75
83
|
state: {
|
|
76
84
|
slot: "badge",
|
|
77
85
|
variant: r,
|
|
78
|
-
tone:
|
|
86
|
+
tone: E,
|
|
87
|
+
inverse: a
|
|
79
88
|
}
|
|
80
89
|
});
|
|
81
90
|
};
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","names":[],"sources":["../src/cn.ts","../src/types.ts","../src/variants.ts","../src/index.tsx"],"sourcesContent":["import {type ClassValue, clsx} from 'clsx';\n\n/**\n * Joins class names. There is no tailwind-merge here on purpose: the badge\n * ships its own stylesheet instead of utility classes, so there is nothing to\n * deduplicate.\n */\nconst cn = (...inputs: ClassValue[]): string => clsx(inputs);\n\nexport {cn};\n","import type {useRender} from '@base-ui-components/react/use-render';\nimport type {ComponentType, CSSProperties, ReactNode} from 'react';\n\n/** The form: how the badge is drawn. It says nothing about colour. */\nexport const BadgeVariant = {\n /** The tone's pad, `-bg`, under the text. */\n Fill: 'fill',\n /** No pad: a dotted contour in the tone's colour, on whatever is behind the badge. */\n Outline: 'outline',\n} as const;\nexport type BadgeVariant = (typeof BadgeVariant)[keyof typeof BadgeVariant];\n\n/**\n * The tone: what the badge says about the thing it stands next to. Each one is\n * a token triple — the `-bg` pad, the `-text` text, and the colour itself for\n * an icon and for the contour. Every form takes every tone.\n */\nexport const BadgeTone = {\n /** Something to know: the --color-action triple. */\n Info: 'info',\n /** Nothing is broken yet: the --color-warning triple. */\n Warning: 'warning',\n /** An error, or a state that cannot be taken back: the --color-danger triple. */\n Danger: 'danger',\n /** A scenario that worked: the --color-success triple. */\n Success: 'success',\n /** No status: a draft, an archive, an inactive item, on the grey --color-quiet triple. */\n Quiet: 'quiet',\n /** No status: a promo or a new feature, on the brand's --color-highlight triple. */\n Highlight: 'highlight',\n} as const;\nexport type BadgeTone = (typeof BadgeTone)[keyof typeof BadgeTone];\n\n/** @deprecated Use `BadgeTone`: the values are the same. */\nexport const BadgeType = BadgeTone;\n/** @deprecated Use `BadgeTone`. */\nexport type BadgeType = BadgeTone;\n\n/**\n * @deprecated Use `BadgeTone`. The enum v2 and v3 exported, with its\n * capitalised keys, kept so that code written against them still runs: 29 files\n * in seven products read `BADGE_TYPE.INFO` and the like.\n */\nexport const BADGE_TYPE = {\n INFO: BadgeTone.Info,\n WARNING: BadgeTone.Warning,\n DANGER: BadgeTone.Danger,\n SUCCESS: BadgeTone.Success,\n} as const;\n/** @deprecated Use `BadgeTone`. */\nexport type BADGE_TYPE = (typeof BADGE_TYPE)[keyof typeof BADGE_TYPE];\n\n/**\n * An icon from `@propellerads/icon`, as its component: `Star`, not `<Star />`.\n * The badge draws it, so an element — and a custom SVG with it — does not\n * compile.\n */\nexport type BadgeIcon = ComponentType<{color?: string}>;\n\n/**\n * The span's props without `style`: a badge's box is the system's, and of\n * everything in it a product may change only the colours, through `bgColor`\n * and `textColor`. `className` stays for placing the badge on the page.\n */\nexport type BadgeProps = Omit<useRender.ComponentProps<'span'>, 'style'> & {\n /** How the badge is drawn. @default 'fill' */\n variant?: BadgeVariant;\n /** Which colour that form is worn in. @default 'info' */\n tone?: BadgeTone;\n /**\n * The pad, in place of the tone's: a colour, or a gradient for a tier like\n * Platinum. For a look the design system does not have. An outline badge has\n * no pad and ignores it.\n */\n bgColor?: string;\n /**\n * The text and the icon — and an outline badge's contour — in place of the\n * tone's. Set it with `bgColor`, so the two stay legible together.\n */\n textColor?: string;\n /**\n * The icon before the text: one from `@propellerads/icon`, passed as its\n * component — `icon={Star}`. The badge draws it at 16px in the tone's\n * graphic colour.\n *\n * Pick it by weight as well as by meaning: a contour rather than a solid\n * shape (`CheckCircleOutline`, not `CheckCircle`), ink on no more than a\n * quarter of the 24-unit grid, two units clear of its edge, and not one with\n * its colour baked in (`LaughingSmile`, `SurveyOffer`, the brand logos).\n */\n icon?: BadgeIcon;\n /** The text, written in sentence case whatever case it is passed in. */\n children?: ReactNode;\n /** @deprecated Pass the text as children. */\n label?: ReactNode;\n /** @deprecated Use `tone`. */\n type?: BadgeType;\n /**\n * @deprecated Use `bgColor` and `textColor`. Only `background`,\n * `backgroundColor` and `color` are taken from it; everything else is ignored.\n */\n customStyle?: CSSProperties;\n};\n","import {cva} from 'class-variance-authority';\n\n/**\n * The class names the stylesheet defines. Form and tone are separate axes: the\n * variant decides where the colour goes, the tone which colour it is, so every\n * form can be worn in every tone.\n *\n * Exported so that markup which is not a badge can take its look:\n * `<span className={badgeVariants({variant: 'outline', tone: 'danger'})} />`.\n */\nconst badgeVariants = cva('badge', {\n variants: {\n variant: {\n fill: 'badge--fill',\n outline: 'badge--outline',\n },\n tone: {\n info: 'badge--tone-info',\n warning: 'badge--tone-warning',\n danger: 'badge--tone-danger',\n success: 'badge--tone-success',\n quiet: 'badge--tone-quiet',\n highlight: 'badge--tone-highlight',\n },\n },\n defaultVariants: {\n variant: 'fill',\n tone: 'info',\n },\n});\n\nexport {badgeVariants};\n","import {mergeProps} from '@base-ui-components/react/merge-props';\nimport {useRender} from '@base-ui-components/react/use-render';\nimport {Children, type CSSProperties, createElement, isValidElement, type ReactElement, type ReactNode} from 'react';\nimport './badge.css';\nimport {cn} from './cn';\nimport {BADGE_TYPE, type BadgeIcon, type BadgeProps, BadgeTone, BadgeType, BadgeVariant} from './types';\nimport {badgeVariants} from './variants';\n\n/**\n * Writes the text in sentence case — the first letter a capital, the rest small\n * — whatever case it arrived in: `NEW` and `new` both read `New`. CSS cannot do\n * it here: `::first-letter` does not apply to a flex container. Only the text\n * the badge is handed directly is rewritten; an element inside it keeps its own.\n */\nconst toSentenceCase = (children: ReactNode): ReactNode => {\n let isFirst = true;\n\n return Children.map(children, (child) => {\n if (typeof child !== 'string' && typeof child !== 'number') {\n return child;\n }\n\n const text = String(child).toLowerCase();\n\n if (!isFirst || text.trim() === '') {\n return text;\n }\n\n isFirst = false;\n\n return text.replace(/\\S/, (letter) => letter.toUpperCase());\n });\n};\n\n/**\n * The colours a product may change, as the custom properties the stylesheet\n * reads. The icon follows the text: a pad the system did not pick has no\n * graphic colour of its own to offer.\n */\nconst toColors = (bgColor: string | undefined, textColor: string | undefined): CSSProperties | undefined => {\n if (bgColor === undefined && textColor === undefined) {\n return undefined;\n }\n\n return {\n ...(bgColor !== undefined && {'--badge-bg': bgColor}),\n ...(textColor !== undefined && {'--badge-fg': textColor, '--badge-icon': textColor}),\n } as CSSProperties;\n};\n\n/**\n * The pack paints its icons black unless told otherwise, so the badge draws one\n * in `currentColor`, which it sets to the variant's graphic colour.\n *\n * v4 took the icon as an element, `icon={<Star color={Color.White} />}`. The\n * type no longer allows it, and a call that still passes one is drawn as it is\n * rather than broken.\n */\nconst renderIcon = (icon: BadgeIcon | ReactElement): ReactNode =>\n isValidElement(icon) ? icon : createElement(icon, {color: 'currentColor'});\n\n/*\n * shadcn's Badge on Base UI: a `<span>` by default, any element through\n * `render`, the form and the tone written out as `data-variant` and\n * `data-tone` next to `data-slot`.\n */\nconst Badge = ({\n className,\n variant = BadgeVariant.Fill,\n tone,\n bgColor,\n textColor,\n render,\n ref,\n label,\n type,\n icon,\n customStyle,\n children,\n ...props\n}: BadgeProps) => {\n const resolvedTone = tone ?? type ?? BadgeTone.Info;\n const colors = toColors(\n bgColor ?? (customStyle?.background as string | undefined) ?? customStyle?.backgroundColor,\n textColor ?? customStyle?.color,\n );\n\n return useRender({\n defaultTagName: 'span',\n render,\n ref,\n props: mergeProps<'span'>(\n {\n className: cn(badgeVariants({variant, tone: resolvedTone}), icon !== undefined && 'badge--icon', className),\n style: colors,\n children: (\n <>\n {icon !== undefined && (\n <span className=\"badge__icon\" aria-hidden>\n {renderIcon(icon)}\n </span>\n )}\n {toSentenceCase(children ?? label)}\n </>\n ),\n },\n props,\n ),\n state: {\n slot: 'badge',\n variant,\n tone: resolvedTone,\n },\n });\n};\n\nexport default Badge;\n\nexport type {BadgeIcon, BadgeProps};\nexport {BADGE_TYPE, Badge, BadgeTone, BadgeType, BadgeVariant, badgeVariants};\n"],"mappings":";;;;;;;AAOA,IAAM,KAAM,GAAG,MAAiC,EAAK,EAAO,ECH/C,IAAe;CAE1B,MAAM;CAEN,SAAS;CACV,EAQY,IAAY;CAEvB,MAAM;CAEN,SAAS;CAET,QAAQ;CAER,SAAS;CAET,OAAO;CAEP,WAAW;CACZ,EAIY,IAAY,GASZ,IAAa;CACxB,MAAM,EAAU;CAChB,SAAS,EAAU;CACnB,QAAQ,EAAU;CAClB,SAAS,EAAU;CACpB,ECtCK,IAAgB,EAAI,SAAS;CACjC,UAAU;EACR,SAAS;GACP,MAAM;GACN,SAAS;GACV;EACD,MAAM;GACJ,MAAM;GACN,SAAS;GACT,QAAQ;GACR,SAAS;GACT,OAAO;GACP,WAAW;GACZ;EACF;CACD,iBAAiB;EACf,SAAS;EACT,MAAM;EACP;CACF,CAAC,ECfI,KAAkB,MAAmC;CACzD,IAAI,IAAU;CAEd,OAAO,EAAS,IAAI,IAAW,MAAU;EACvC,IAAI,OAAO,KAAU,YAAY,OAAO,KAAU,UAChD,OAAO;EAGT,IAAM,IAAO,OAAO,EAAM,CAAC,aAAa;EAQxC,OANI,CAAC,KAAW,EAAK,MAAM,KAAK,KACvB,KAGT,IAAU,IAEH,EAAK,QAAQ,OAAO,MAAW,EAAO,aAAa,CAAC;GAC3D;GAQE,KAAY,GAA6B,MAA6D;CACtG,YAAY,KAAA,KAAa,MAAc,KAAA,IAI3C,OAAO;EACL,GAAI,MAAY,KAAA,KAAa,EAAC,cAAc,GAAQ;EACpD,GAAI,MAAc,KAAA,KAAa;GAAC,cAAc;GAAW,gBAAgB;GAAU;EACpF;GAWG,KAAc,MAClB,EAAe,EAAK,GAAG,IAAO,EAAc,GAAM,EAAC,OAAO,gBAAe,CAAC,EAOtE,KAAS,EACb,cACA,aAAU,EAAa,MACvB,SACA,YACA,cACA,WACA,QACA,UACA,SACA,SACA,gBACA,aACA,GAAG,QACa;CAChB,IAAM,IAAe,KAAQ,KAAQ,EAAU,MACzC,IAAS,EACb,KAAY,GAAa,cAAqC,GAAa,iBAC3E,KAAa,GAAa,MAC3B;CAED,OAAO,EAAU;EACf,gBAAgB;EAChB;EACA;EACA,OAAO,EACL;GACE,WAAW,EAAG,EAAc;IAAC;IAAS,MAAM;IAAa,CAAC,EAAE,MAAS,KAAA,KAAa,eAAe,EAAU;GAC3G,OAAO;GACP,UACE,kBAAA,GAAA,EAAA,UAAA,CACG,MAAS,KAAA,KACR,kBAAC,QAAD;IAAM,WAAU;IAAc,eAAA;cAC3B,EAAW,EAAK;IACZ,CAAA,EAER,EAAe,KAAY,EAAM,CACjC,EAAA,CAAA;GAEN,EACD,EACD;EACD,OAAO;GACL,MAAM;GACN;GACA,MAAM;GACP;EACF,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../src/cn.ts","../src/types.ts","../src/variants.ts","../src/index.tsx"],"sourcesContent":["import {type ClassValue, clsx} from 'clsx';\n\n/**\n * Joins class names. There is no tailwind-merge here on purpose: the badge\n * ships its own stylesheet instead of utility classes, so there is nothing to\n * deduplicate.\n */\nconst cn = (...inputs: ClassValue[]): string => clsx(inputs);\n\nexport {cn};\n","import type {useRender} from '@base-ui-components/react/use-render';\nimport type {ComponentType, CSSProperties, ReactNode} from 'react';\n\n/** The form: how the badge is drawn. It says nothing about colour. */\nexport const BadgeVariant = {\n /** The tone's pad, `-bg`, under the text. */\n Fill: 'fill',\n /** No pad: a dotted contour in the tone's colour, on whatever is behind the badge. */\n Outline: 'outline',\n /** The tone's colour itself as the pad, the text and icon on it in the page surface's colour. */\n Bright: 'bright',\n} as const;\nexport type BadgeVariant = (typeof BadgeVariant)[keyof typeof BadgeVariant];\n\n/**\n * The tone: what the badge says about the thing it stands next to. Each one is\n * a token triple — the `-bg` pad, the `-text` text, and the colour itself for\n * an icon and for the contour. Every form takes every tone.\n */\nexport const BadgeTone = {\n /** Something to know: the --color-action triple. */\n Info: 'info',\n /** Nothing is broken yet: the --color-warning triple. */\n Warning: 'warning',\n /** An error, or a state that cannot be taken back: the --color-danger triple. */\n Danger: 'danger',\n /** A scenario that worked: the --color-success triple. */\n Success: 'success',\n /** No status: a draft, an archive, an inactive item, on the grey --color-quiet triple. */\n Quiet: 'quiet',\n /** No status: a promo or a new feature, on the brand's --color-highlight triple. */\n Highlight: 'highlight',\n} as const;\nexport type BadgeTone = (typeof BadgeTone)[keyof typeof BadgeTone];\n\n/** @deprecated Use `BadgeTone`: the values are the same. */\nexport const BadgeType = BadgeTone;\n/** @deprecated Use `BadgeTone`. */\nexport type BadgeType = BadgeTone;\n\n/**\n * @deprecated Use `BadgeTone`. The enum v2 and v3 exported, with its\n * capitalised keys, kept so that code written against them still runs: 29 files\n * in seven products read `BADGE_TYPE.INFO` and the like.\n */\nexport const BADGE_TYPE = {\n INFO: BadgeTone.Info,\n WARNING: BadgeTone.Warning,\n DANGER: BadgeTone.Danger,\n SUCCESS: BadgeTone.Success,\n} as const;\n/** @deprecated Use `BadgeTone`. */\nexport type BADGE_TYPE = (typeof BADGE_TYPE)[keyof typeof BADGE_TYPE];\n\n/**\n * An icon from `@propellerads/icon`, as its component: `Star`, not `<Star />`.\n * The badge draws it, so an element — and a custom SVG with it — does not\n * compile.\n */\nexport type BadgeIcon = ComponentType<{color?: string}>;\n\n/**\n * The span's props without `style`: a badge's box is the system's, and of\n * everything in it a product may change only the colours, through `bgColor`\n * and `textColor`. `className` stays for placing the badge on the page.\n */\n/**\n * The form, and whether it is inverse. Only `fill` and `outline` turn over:\n * `bright` already has a pad of its own, so `variant=\"bright\"` with `inverse`\n * does not compile rather than compiling to nothing.\n */\ntype BadgeForm =\n | {\n /** How the badge is drawn. @default 'fill' */\n variant?: Exclude<BadgeVariant, typeof BadgeVariant.Bright>;\n /**\n * For a surface the design system does not control — a photo, a coloured\n * pad, a banner. Each form turns over in --color-light, which stays light\n * in both themes: `fill` takes it as its pad and keeps its text and icon\n * in the tone, `outline` stays without a pad and draws its contour, text\n * and icon in it.\n * @default false\n */\n inverse?: boolean;\n }\n | {\n /** How the badge is drawn. @default 'fill' */\n variant: typeof BadgeVariant.Bright;\n /** Not for `bright`: it already has a pad of its own. */\n inverse?: never;\n };\n\nexport type BadgeProps = Omit<useRender.ComponentProps<'span'>, 'style'> &\n BadgeForm & {\n /** Which colour that form is worn in. @default 'info' */\n tone?: BadgeTone;\n /**\n * The pad, in place of the tone's: a colour, or a gradient for a tier like\n * Platinum. For a look the design system does not have. An outline badge has\n * no pad and ignores it.\n */\n bgColor?: string;\n /**\n * The text and the icon — and an outline badge's contour — in place of the\n * tone's. Set it with `bgColor`, so the two stay legible together.\n */\n textColor?: string;\n /**\n * The icon before the text: one from `@propellerads/icon`, passed as its\n * component — `icon={Star}`. The badge draws it at 16px in the tone's\n * graphic colour.\n *\n * Pick it by weight as well as by meaning: a contour rather than a solid\n * shape (`CheckCircleOutline`, not `CheckCircle`), ink on no more than a\n * quarter of the 24-unit grid, two units clear of its edge, and not one with\n * its colour baked in (`LaughingSmile`, `SurveyOffer`, the brand logos).\n */\n icon?: BadgeIcon;\n /** The text, written in sentence case whatever case it is passed in. */\n children?: ReactNode;\n /** @deprecated Pass the text as children. */\n label?: ReactNode;\n /** @deprecated Use `tone`. */\n type?: BadgeType;\n /**\n * @deprecated Use `bgColor` and `textColor`. Only `background`,\n * `backgroundColor` and `color` are taken from it; everything else is ignored.\n */\n customStyle?: CSSProperties;\n };\n","import {cva} from 'class-variance-authority';\n\n/**\n * The class names the stylesheet defines. Form and tone are separate axes: the\n * variant decides where the colour goes, the tone which colour it is, so every\n * form can be worn in every tone.\n *\n * Exported so that markup which is not a badge can take its look:\n * `<span className={badgeVariants({variant: 'outline', tone: 'danger'})} />`.\n */\nconst badgeVariants = cva('badge', {\n variants: {\n variant: {\n fill: 'badge--fill',\n outline: 'badge--outline',\n bright: 'badge--bright',\n },\n tone: {\n info: 'badge--tone-info',\n warning: 'badge--tone-warning',\n danger: 'badge--tone-danger',\n success: 'badge--tone-success',\n quiet: 'badge--tone-quiet',\n highlight: 'badge--tone-highlight',\n },\n inverse: {\n true: 'badge--inverse',\n false: '',\n },\n },\n defaultVariants: {\n variant: 'fill',\n tone: 'info',\n inverse: false,\n },\n});\n\nexport {badgeVariants};\n","import {mergeProps} from '@base-ui-components/react/merge-props';\nimport {useRender} from '@base-ui-components/react/use-render';\nimport {Children, type CSSProperties, createElement, isValidElement, type ReactElement, type ReactNode} from 'react';\nimport './badge.css';\nimport {cn} from './cn';\nimport {BADGE_TYPE, type BadgeIcon, type BadgeProps, BadgeTone, BadgeType, BadgeVariant} from './types';\nimport {badgeVariants} from './variants';\n\n/**\n * Writes the text in sentence case — the first letter a capital, the rest small\n * — whatever case it arrived in: `NEW` and `new` both read `New`. CSS cannot do\n * it here: `::first-letter` does not apply to a flex container. Only the text\n * the badge is handed directly is rewritten; an element inside it keeps its own.\n */\nconst toSentenceCase = (children: ReactNode): ReactNode => {\n let isFirst = true;\n\n return Children.map(children, (child) => {\n if (typeof child !== 'string' && typeof child !== 'number') {\n return child;\n }\n\n const text = String(child).toLowerCase();\n\n if (!isFirst || text.trim() === '') {\n return text;\n }\n\n isFirst = false;\n\n return text.replace(/\\S/, (letter) => letter.toUpperCase());\n });\n};\n\n/**\n * The colours a product may change, as the custom properties the stylesheet\n * reads. The icon follows the text: a pad the system did not pick has no\n * graphic colour of its own to offer.\n */\nconst toColors = (bgColor: string | undefined, textColor: string | undefined): CSSProperties | undefined => {\n if (bgColor === undefined && textColor === undefined) {\n return undefined;\n }\n\n return {\n ...(bgColor !== undefined && {'--badge-bg': bgColor}),\n ...(textColor !== undefined && {'--badge-fg': textColor, '--badge-icon': textColor}),\n } as CSSProperties;\n};\n\n/**\n * The pack paints its icons black unless told otherwise, so the badge draws one\n * in `currentColor`, which it sets to the variant's graphic colour.\n *\n * v4 took the icon as an element, `icon={<Star color={Color.White} />}`. The\n * type no longer allows it, and a call that still passes one is drawn as it is\n * rather than broken.\n */\nconst renderIcon = (icon: BadgeIcon | ReactElement): ReactNode =>\n isValidElement(icon) ? icon : createElement(icon, {color: 'currentColor'});\n\n/*\n * shadcn's Badge on Base UI: a `<span>` by default, any element through\n * `render`, the form and the tone written out as `data-variant` and\n * `data-tone` next to `data-slot`.\n */\nconst Badge = ({\n className,\n variant = BadgeVariant.Fill,\n tone,\n inverse = false,\n bgColor,\n textColor,\n render,\n ref,\n label,\n type,\n icon,\n customStyle,\n children,\n ...props\n}: BadgeProps) => {\n const resolvedTone = tone ?? type ?? BadgeTone.Info;\n const colors = toColors(\n bgColor ?? (customStyle?.background as string | undefined) ?? customStyle?.backgroundColor,\n textColor ?? customStyle?.color,\n );\n\n return useRender({\n defaultTagName: 'span',\n render,\n ref,\n props: mergeProps<'span'>(\n {\n className: cn(\n badgeVariants({variant, tone: resolvedTone, inverse}),\n icon !== undefined && 'badge--icon',\n className,\n ),\n style: colors,\n children: (\n <>\n {icon !== undefined && (\n <span className=\"badge__icon\" aria-hidden>\n {renderIcon(icon)}\n </span>\n )}\n {toSentenceCase(children ?? label)}\n </>\n ),\n },\n props,\n ),\n state: {\n slot: 'badge',\n variant,\n tone: resolvedTone,\n inverse,\n },\n });\n};\n\nexport default Badge;\n\nexport type {BadgeIcon, BadgeProps};\nexport {BADGE_TYPE, Badge, BadgeTone, BadgeType, BadgeVariant, badgeVariants};\n"],"mappings":";;;;;;;AAOA,IAAM,KAAM,GAAG,MAAiC,EAAK,EAAO,ECH/C,IAAe;CAE1B,MAAM;CAEN,SAAS;CAET,QAAQ;CACT,EAQY,IAAY;CAEvB,MAAM;CAEN,SAAS;CAET,QAAQ;CAER,SAAS;CAET,OAAO;CAEP,WAAW;CACZ,EAIY,IAAY,GASZ,IAAa;CACxB,MAAM,EAAU;CAChB,SAAS,EAAU;CACnB,QAAQ,EAAU;CAClB,SAAS,EAAU;CACpB,ECxCK,IAAgB,EAAI,SAAS;CACjC,UAAU;EACR,SAAS;GACP,MAAM;GACN,SAAS;GACT,QAAQ;GACT;EACD,MAAM;GACJ,MAAM;GACN,SAAS;GACT,QAAQ;GACR,SAAS;GACT,OAAO;GACP,WAAW;GACZ;EACD,SAAS;GACP,MAAM;GACN,OAAO;GACR;EACF;CACD,iBAAiB;EACf,SAAS;EACT,MAAM;EACN,SAAS;EACV;CACF,CAAC,ECrBI,KAAkB,MAAmC;CACzD,IAAI,IAAU;CAEd,OAAO,EAAS,IAAI,IAAW,MAAU;EACvC,IAAI,OAAO,KAAU,YAAY,OAAO,KAAU,UAChD,OAAO;EAGT,IAAM,IAAO,OAAO,EAAM,CAAC,aAAa;EAQxC,OANI,CAAC,KAAW,EAAK,MAAM,KAAK,KACvB,KAGT,IAAU,IAEH,EAAK,QAAQ,OAAO,MAAW,EAAO,aAAa,CAAC;GAC3D;GAQE,KAAY,GAA6B,MAA6D;CACtG,YAAY,KAAA,KAAa,MAAc,KAAA,IAI3C,OAAO;EACL,GAAI,MAAY,KAAA,KAAa,EAAC,cAAc,GAAQ;EACpD,GAAI,MAAc,KAAA,KAAa;GAAC,cAAc;GAAW,gBAAgB;GAAU;EACpF;GAWG,KAAc,MAClB,EAAe,EAAK,GAAG,IAAO,EAAc,GAAM,EAAC,OAAO,gBAAe,CAAC,EAOtE,KAAS,EACb,cACA,aAAU,EAAa,MACvB,SACA,aAAU,IACV,YACA,cACA,WACA,QACA,UACA,SACA,SACA,gBACA,aACA,GAAG,QACa;CAChB,IAAM,IAAe,KAAQ,KAAQ,EAAU,MACzC,IAAS,EACb,KAAY,GAAa,cAAqC,GAAa,iBAC3E,KAAa,GAAa,MAC3B;CAED,OAAO,EAAU;EACf,gBAAgB;EAChB;EACA;EACA,OAAO,EACL;GACE,WAAW,EACT,EAAc;IAAC;IAAS,MAAM;IAAc;IAAQ,CAAC,EACrD,MAAS,KAAA,KAAa,eACtB,EACD;GACD,OAAO;GACP,UACE,kBAAA,GAAA,EAAA,UAAA,CACG,MAAS,KAAA,KACR,kBAAC,QAAD;IAAM,WAAU;IAAc,eAAA;cAC3B,EAAW,EAAK;IACZ,CAAA,EAER,EAAe,KAAY,EAAM,CACjC,EAAA,CAAA;GAEN,EACD,EACD;EACD,OAAO;GACL,MAAM;GACN;GACA,MAAM;GACN;GACD;EACF,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@propellerads/badge",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "7.0.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"types": "dist/index.d.ts",
|
|
6
6
|
"style": "dist/index.css",
|
|
@@ -34,7 +34,7 @@
|
|
|
34
34
|
"clsx": "^2.1.1"
|
|
35
35
|
},
|
|
36
36
|
"peerDependencies": {
|
|
37
|
-
"@propellerads/tokens": ">=0.
|
|
37
|
+
"@propellerads/tokens": ">=0.4.0",
|
|
38
38
|
"react": "^18.0.0 || ^19.0.0",
|
|
39
39
|
"react-dom": "^18.0.0 || ^19.0.0"
|
|
40
40
|
},
|
|
@@ -50,7 +50,7 @@
|
|
|
50
50
|
"rollup": "^4.60.3",
|
|
51
51
|
"rollup-plugin-dts": "^6.4.1",
|
|
52
52
|
"typescript": "^6.0.3",
|
|
53
|
-
"vite": "^8.0.
|
|
53
|
+
"vite": "^8.0.11",
|
|
54
54
|
"vite-plugin-lib-inject-css": "^2.2.2"
|
|
55
55
|
}
|
|
56
56
|
}
|