@propellerads/badge 4.0.2 → 6.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 ADDED
@@ -0,0 +1,180 @@
1
+ # Badge
2
+
3
+ A short label that says something about the thing it stands next to — a
4
+ campaign's status, a format that is new, a draft — and does nothing itself. It
5
+ has no hover, no press and no focus ring.
6
+
7
+ Version 5 is built the way [Button](../Button/README.md) is: one stylesheet,
8
+ variants picked by [class-variance-authority](https://cva.style/), every colour,
9
+ gap, corner and font from [@propellerads/tokens](../Tokens/README.md). The
10
+ structure is [shadcn's Badge](https://ui.shadcn.com/docs/components/base/badge)
11
+ on Base UI's `useRender`.
12
+
13
+ ![](https://img.shields.io/npm/v/@propellerads/badge.svg?style=flat-square)
14
+
15
+ [NPM](https://www.npmjs.com/package/@propellerads/badge) |
16
+ [Github](https://github.com/propellerads/ui-components/tree/master/Components/Badge)
17
+
18
+ ## Installation
19
+
20
+ ```sh
21
+ bun add @propellerads/badge
22
+ ```
23
+
24
+ The built entry imports its own stylesheet. The tokens are the app's:
25
+
26
+ ```ts
27
+ import '@propellerads/tokens/common.css';
28
+ import '@propellerads/tokens/propellerads.css'; // or monetag / propush / zeydoo
29
+ ```
30
+
31
+ ## Use
32
+
33
+ ```tsx
34
+ import Badge from '@propellerads/badge';
35
+ import {CheckCircleOutline, Time} from '@propellerads/icon';
36
+
37
+ <Badge tone="success">Recommend</Badge>
38
+ <Badge variant="outline" tone="info" icon={Time}>Moderation</Badge>
39
+ <Badge tone="info" icon={CheckCircleOutline}>Approved</Badge>
40
+ ```
41
+
42
+ The badge is a `<span>`, 20px high and fully round, with the text in
43
+ `--font-description`. `render` swaps the element — `render={<div />}` — and the
44
+ badge writes `data-slot="badge"`, `data-variant`, `data-tone` and, when it is
45
+ set, `data-inverse` on it.
46
+
47
+ The text is written in sentence case whatever case it arrives in: `NEW` and
48
+ `new` both read `New`. Only the text the badge is handed directly is rewritten;
49
+ an element inside it keeps its own.
50
+
51
+ ## Form and tone
52
+
53
+ Two axes, as on the button, and every form takes every tone.
54
+
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.
57
+
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 |
63
+
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.
66
+
67
+ `tone` is the colour. Each tone is a token triple: the pad `-bg`, the text
68
+ `-text`, and the colour itself for the icon and the contour.
69
+
70
+ | Tone | Triple | For |
71
+ |---|---|---|
72
+ | `info` (default) | `--color-action-*` | something to know |
73
+ | `warning` | `--color-warning-*` | nothing is broken yet |
74
+ | `danger` | `--color-danger-*` | an error, or a state that cannot be taken back |
75
+ | `success` | `--color-success-*` | a scenario that worked |
76
+ | `quiet` | `--color-quiet-*` | no status: a draft, an archive, an inactive item |
77
+ | `highlight` | `--color-highlight-*` | no status: a promo or a new feature, in the brand's colour |
78
+
79
+ `-text` on its own pad stays under 4.5:1 for every tone in at least one theme —
80
+ `warning` and `highlight` the furthest — so a badge is a label, not a place for a
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.
107
+
108
+ ## Icon
109
+
110
+ `icon` takes an icon from [@propellerads/icon](../Icon), passed as its
111
+ component: `icon={Star}`, not `icon={<Star />}`. The badge draws it at 16px, 2px
112
+ clear of the top and the bottom, in the tone's graphic colour, and the start
113
+ padding drops to `--space-4` for it. An element or a custom SVG does not compile.
114
+
115
+ At 16px next to 12px text an icon outweighs the word easily, so pick it by
116
+ weight as well as by meaning:
117
+
118
+ - **A contour, not a solid shape.** `CheckCircleOutline` rather than
119
+ `CheckCircle`, `Star` rather than `ThumbUp`. A filled circle reads as a blot.
120
+ - **Ink on no more than a quarter of the 24-unit grid.** `CheckCircleOutline`,
121
+ `Time`, `Block`, `Stop`, `Pause`, `Star`, `Segment` and `BellEmpty` sit between
122
+ 12% and 26%; `CheckCircle`, `Cancel`, `ThumbUp` and `Verified` are near half.
123
+ - **Two units clear of the grid's edge**, or the icon runs into the round end.
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.
129
+ - **One family in a row:** circles with circles, plain glyphs with plain glyphs.
130
+ - **Not an icon with its colour baked in** — `LaughingSmile`, `SurveyOffer` and
131
+ the brand logos — which cannot take the tone's.
132
+
133
+ ## Colours of its own
134
+
135
+ A product may change the colours and nothing else:
136
+
137
+ ```tsx
138
+ <Badge bgColor="linear-gradient(258deg, #bfbfbf, #383649)" textColor="white">
139
+ Platinum
140
+ </Badge>
141
+ ```
142
+
143
+ `bgColor` is the pad — a colour or a gradient; an outline badge has none and
144
+ ignores it. `textColor` is the text, the icon and the contour. The badge takes
145
+ no `style`; `className` stays for placing it on the page. A look that recurs
146
+ belongs in a tone, not in a call site.
147
+
148
+ `badgeVariants` gives markup that is not a badge the look:
149
+
150
+ ```tsx
151
+ <span className={badgeVariants({variant: 'outline', tone: 'danger'})} />
152
+ ```
153
+
154
+ ## Deprecated
155
+
156
+ The props and names of earlier majors still compile and are on the way out:
157
+
158
+ | v4 | v5 |
159
+ |---|---|
160
+ | `label="Moderation"` | children |
161
+ | `type={BadgeType.Warning}` | `tone="warning"` (`BadgeType` is `BadgeTone`) |
162
+ | `type={BADGE_TYPE.WARNING}` (v2, v3) | `tone="warning"` (`BADGE_TYPE` keeps the capitalised keys) |
163
+ | `icon={<Star color={Color.White} />}` | `icon={Star}` |
164
+ | `customStyle={{background, color}}` | `bgColor`, `textColor` |
165
+
166
+ `customStyle` takes only `background`, `backgroundColor` and `color` now; its
167
+ other properties and nested selectors are ignored. An icon passed as an element
168
+ does not compile, and one that still arrives as an element is drawn as it is,
169
+ without the tone's colour.
170
+
171
+ ## Migrating from v4
172
+
173
+ - The look: a tinted pad with coloured text instead of white on the solid
174
+ colour, 20px high instead of 16px, round ends instead of 4px corners, 12px
175
+ sentence case instead of 10px capitals.
176
+ - The element is a `<span>`, not a `<div>`, so a badge inside a `<p>` is valid
177
+ markup.
178
+ - The text stays on one line; v4 let a long label wrap.
179
+ - `styled-components` and `@propellerads/stylevariables` are no longer
180
+ dependencies; `@propellerads/tokens` is an optional peer.
package/dist/index.css ADDED
@@ -0,0 +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
@@ -1,21 +1,157 @@
1
- import { ReactNode, FC } from 'react';
2
- import { CSSObject } from 'styled-components';
1
+ import * as react from 'react';
2
+ import { ComponentType, ReactNode, CSSProperties, ReactElement } from 'react';
3
+ import { useRender } from '@base-ui-components/react/use-render';
4
+ import * as class_variance_authority_types from 'class-variance-authority/types';
3
5
 
6
+ /** The form: how the badge is drawn. It says nothing about colour. */
7
+ declare const BadgeVariant: {
8
+ /** The tone's pad, `-bg`, under the text. */
9
+ readonly Fill: "fill";
10
+ /** No pad: a dotted contour in the tone's colour, on whatever is behind the badge. */
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";
14
+ };
15
+ type BadgeVariant = (typeof BadgeVariant)[keyof typeof BadgeVariant];
16
+ /**
17
+ * The tone: what the badge says about the thing it stands next to. Each one is
18
+ * a token triple — the `-bg` pad, the `-text` text, and the colour itself for
19
+ * an icon and for the contour. Every form takes every tone.
20
+ */
21
+ declare const BadgeTone: {
22
+ /** Something to know: the --color-action triple. */
23
+ readonly Info: "info";
24
+ /** Nothing is broken yet: the --color-warning triple. */
25
+ readonly Warning: "warning";
26
+ /** An error, or a state that cannot be taken back: the --color-danger triple. */
27
+ readonly Danger: "danger";
28
+ /** A scenario that worked: the --color-success triple. */
29
+ readonly Success: "success";
30
+ /** No status: a draft, an archive, an inactive item, on the grey --color-quiet triple. */
31
+ readonly Quiet: "quiet";
32
+ /** No status: a promo or a new feature, on the brand's --color-highlight triple. */
33
+ readonly Highlight: "highlight";
34
+ };
35
+ type BadgeTone = (typeof BadgeTone)[keyof typeof BadgeTone];
36
+ /** @deprecated Use `BadgeTone`: the values are the same. */
4
37
  declare const BadgeType: {
38
+ /** Something to know: the --color-action triple. */
5
39
  readonly Info: "info";
40
+ /** Nothing is broken yet: the --color-warning triple. */
6
41
  readonly Warning: "warning";
42
+ /** An error, or a state that cannot be taken back: the --color-danger triple. */
7
43
  readonly Danger: "danger";
44
+ /** A scenario that worked: the --color-success triple. */
8
45
  readonly Success: "success";
46
+ /** No status: a draft, an archive, an inactive item, on the grey --color-quiet triple. */
47
+ readonly Quiet: "quiet";
48
+ /** No status: a promo or a new feature, on the brand's --color-highlight triple. */
49
+ readonly Highlight: "highlight";
50
+ };
51
+ /** @deprecated Use `BadgeTone`. */
52
+ type BadgeType = BadgeTone;
53
+ /**
54
+ * @deprecated Use `BadgeTone`. The enum v2 and v3 exported, with its
55
+ * capitalised keys, kept so that code written against them still runs: 29 files
56
+ * in seven products read `BADGE_TYPE.INFO` and the like.
57
+ */
58
+ declare const BADGE_TYPE: {
59
+ readonly INFO: "info";
60
+ readonly WARNING: "warning";
61
+ readonly DANGER: "danger";
62
+ readonly SUCCESS: "success";
9
63
  };
10
- type BadgeType = (typeof BadgeType)[keyof typeof BadgeType];
11
- type BadgeProps = {
64
+ /** @deprecated Use `BadgeTone`. */
65
+ type BADGE_TYPE = (typeof BADGE_TYPE)[keyof typeof BADGE_TYPE];
66
+ /**
67
+ * An icon from `@propellerads/icon`, as its component: `Star`, not `<Star />`.
68
+ * The badge draws it, so an element — and a custom SVG with it — does not
69
+ * compile.
70
+ */
71
+ type BadgeIcon = ComponentType<{
72
+ color?: string;
73
+ }>;
74
+ /**
75
+ * The span's props without `style`: a badge's box is the system's, and of
76
+ * everything in it a product may change only the colours, through `bgColor`
77
+ * and `textColor`. `className` stays for placing the badge on the page.
78
+ */
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
+ } | {
97
+ /** How the badge is drawn. @default 'fill' */
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 & {
103
+ /** Which colour that form is worn in. @default 'info' */
104
+ tone?: BadgeTone;
105
+ /**
106
+ * The pad, in place of the tone's: a colour, or a gradient for a tier like
107
+ * Platinum. For a look the design system does not have. An outline badge has
108
+ * no pad and ignores it.
109
+ */
110
+ bgColor?: string;
111
+ /**
112
+ * The text and the icon — and an outline badge's contour — in place of the
113
+ * tone's. Set it with `bgColor`, so the two stay legible together.
114
+ */
115
+ textColor?: string;
116
+ /**
117
+ * The icon before the text: one from `@propellerads/icon`, passed as its
118
+ * component — `icon={Star}`. The badge draws it at 16px in the tone's
119
+ * graphic colour.
120
+ *
121
+ * Pick it by weight as well as by meaning: a contour rather than a solid
122
+ * shape (`CheckCircleOutline`, not `CheckCircle`), ink on no more than a
123
+ * quarter of the 24-unit grid, two units clear of its edge, and not one with
124
+ * its colour baked in (`LaughingSmile`, `SurveyOffer`, the brand logos).
125
+ */
126
+ icon?: BadgeIcon;
127
+ /** The text, written in sentence case whatever case it is passed in. */
128
+ children?: ReactNode;
129
+ /** @deprecated Pass the text as children. */
12
130
  label?: ReactNode;
131
+ /** @deprecated Use `tone`. */
13
132
  type?: BadgeType;
14
- customStyle?: CSSObject;
15
- icon?: ReactNode;
133
+ /**
134
+ * @deprecated Use `bgColor` and `textColor`. Only `background`,
135
+ * `backgroundColor` and `color` are taken from it; everything else is ignored.
136
+ */
137
+ customStyle?: CSSProperties;
16
138
  };
17
139
 
18
- declare const Badge: FC<BadgeProps>;
140
+ /**
141
+ * The class names the stylesheet defines. Form and tone are separate axes: the
142
+ * variant decides where the colour goes, the tone which colour it is, so every
143
+ * form can be worn in every tone.
144
+ *
145
+ * Exported so that markup which is not a badge can take its look:
146
+ * `<span className={badgeVariants({variant: 'outline', tone: 'danger'})} />`.
147
+ */
148
+ declare const badgeVariants: (props?: ({
149
+ variant?: "fill" | "outline" | "bright" | null | undefined;
150
+ tone?: "info" | "warning" | "danger" | "success" | "quiet" | "highlight" | null | undefined;
151
+ inverse?: boolean | null | undefined;
152
+ } & class_variance_authority_types.ClassProp) | undefined) => string;
153
+
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>>;
19
155
 
20
- export { BadgeType, Badge as default };
21
- export type { BadgeProps };
156
+ export { BADGE_TYPE, Badge, BadgeTone, BadgeType, BadgeVariant, badgeVariants, Badge as default };
157
+ export type { BadgeIcon, BadgeProps };
package/dist/index.js CHANGED
@@ -1,38 +1,94 @@
1
- import { jsxs as a, jsx as c } from "react/jsx-runtime";
2
- import o from "styled-components";
3
- import { spacing as s, white as l, successColor as d, errorColor as g, warningColor as p, actionColor as m, borderRadius as f } from "@propellerads/stylevariables";
4
- const n = {
5
- Info: "info",
6
- Warning: "warning",
7
- Danger: "danger",
8
- Success: "success"
9
- }, u = {
10
- [n.Info]: m,
11
- [n.Warning]: p,
12
- [n.Danger]: g,
13
- [n.Success]: d
14
- }, x = o.div`
15
- display: inline-flex;
16
- align-items: center;
17
- padding: 0 ${s}px;
18
- text-transform: uppercase;
19
- color: ${l};
20
- border-radius: ${f / 2}px;
21
- font-size: 10px;
22
- line-height: 1.5;
23
- min-height: 16px;
24
- background-color: ${(e) => u[e.$type]};
25
- ${(e) => e.$customStyle}
26
- `, y = o.span`
27
- margin-right: ${s}px;
28
- display: flex;
29
- align-items: center;
30
- `, C = ({ label: e = "", type: t = n.Info, customStyle: i = {}, icon: r }) => /* @__PURE__ */ a(x, { $type: t, $customStyle: i, children: [
31
- r && /* @__PURE__ */ c(y, { children: r }),
32
- e
33
- ] });
34
- export {
35
- n as BadgeType,
36
- C as default
1
+ import { mergeProps as e } from "@base-ui-components/react/merge-props";
2
+ import { useRender as t } from "@base-ui-components/react/use-render";
3
+ import { Children as n, createElement as r, isValidElement as i } from "react";
4
+ import { clsx as a } from "clsx";
5
+ import { cva as o } from "class-variance-authority";
6
+ import { Fragment as s, jsx as c, jsxs as l } from "react/jsx-runtime";
7
+ import './index.css';//#region src/cn.ts
8
+ var u = (...e) => a(e), d = {
9
+ Fill: "fill",
10
+ Outline: "outline",
11
+ Bright: "bright"
12
+ }, f = {
13
+ Info: "info",
14
+ Warning: "warning",
15
+ Danger: "danger",
16
+ Success: "success",
17
+ Quiet: "quiet",
18
+ Highlight: "highlight"
19
+ }, p = f, m = {
20
+ INFO: f.Info,
21
+ WARNING: f.Warning,
22
+ DANGER: f.Danger,
23
+ SUCCESS: f.Success
24
+ }, h = o("badge", {
25
+ variants: {
26
+ variant: {
27
+ fill: "badge--fill",
28
+ outline: "badge--outline",
29
+ bright: "badge--bright"
30
+ },
31
+ tone: {
32
+ info: "badge--tone-info",
33
+ warning: "badge--tone-warning",
34
+ danger: "badge--tone-danger",
35
+ success: "badge--tone-success",
36
+ quiet: "badge--tone-quiet",
37
+ highlight: "badge--tone-highlight"
38
+ },
39
+ inverse: {
40
+ true: "badge--inverse",
41
+ false: ""
42
+ }
43
+ },
44
+ defaultVariants: {
45
+ variant: "fill",
46
+ tone: "info",
47
+ inverse: !1
48
+ }
49
+ }), g = (e) => {
50
+ let t = !0;
51
+ return n.map(e, (e) => {
52
+ if (typeof e != "string" && typeof e != "number") return e;
53
+ let n = String(e).toLowerCase();
54
+ return !t || n.trim() === "" ? n : (t = !1, n.replace(/\S/, (e) => e.toUpperCase()));
55
+ });
56
+ }, _ = (e, t) => {
57
+ if (!(e === void 0 && t === void 0)) return {
58
+ ...e !== void 0 && { "--badge-bg": e },
59
+ ...t !== void 0 && {
60
+ "--badge-fg": t,
61
+ "--badge-icon": t
62
+ }
63
+ };
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);
66
+ return t({
67
+ defaultTagName: "span",
68
+ render: m,
69
+ ref: y,
70
+ props: e({
71
+ className: u(h({
72
+ variant: r,
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", {
78
+ className: "badge__icon",
79
+ "aria-hidden": !0,
80
+ children: v(S)
81
+ }), g(w ?? b)] })
82
+ }, T),
83
+ state: {
84
+ slot: "badge",
85
+ variant: r,
86
+ tone: E,
87
+ inverse: a
88
+ }
89
+ });
37
90
  };
38
- //# sourceMappingURL=index.js.map
91
+ //#endregion
92
+ export { m as BADGE_TYPE, y as Badge, y as default, f as BadgeTone, p as BadgeType, d as BadgeVariant, h as badgeVariants };
93
+
94
+ //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sources":["../src/types.ts","../src/style.tsx","../src/index.tsx"],"sourcesContent":["import type {ReactNode} from 'react';\nimport type {CSSObject} from 'styled-components';\n\nexport const BadgeType = {\n Info: 'info',\n Warning: 'warning',\n Danger: 'danger',\n Success: 'success',\n} as const;\nexport type BadgeType = (typeof BadgeType)[keyof typeof BadgeType];\n\nexport type BadgeProps = {\n label?: ReactNode;\n type?: BadgeType;\n customStyle?: CSSObject;\n icon?: ReactNode;\n};\n","import styled from 'styled-components';\nimport {\n white,\n borderRadius,\n actionColor,\n warningColor,\n errorColor,\n successColor,\n spacing,\n} from '@propellerads/stylevariables';\nimport {BadgeType, type BadgeProps} from './types';\n\ntype StyledBadgeContainerProps = {\n $type: BadgeType;\n $customStyle: NonNullable<BadgeProps['customStyle']>;\n};\n\nconst BADGE_COLORS = {\n [BadgeType.Info]: actionColor,\n [BadgeType.Warning]: warningColor,\n [BadgeType.Danger]: errorColor,\n [BadgeType.Success]: successColor,\n} as const satisfies Record<BadgeType, string>;\n\nconst StyledBadgeContainer = styled.div<StyledBadgeContainerProps>`\n display: inline-flex;\n align-items: center;\n padding: 0 ${spacing}px;\n text-transform: uppercase;\n color: ${white};\n border-radius: ${borderRadius / 2}px;\n font-size: 10px;\n line-height: 1.5;\n min-height: 16px;\n background-color: ${(props) => BADGE_COLORS[props.$type]};\n ${(props) => props.$customStyle}\n`;\n\nconst StyledBadgeIcon = styled.span`\n margin-right: ${spacing}px;\n display: flex;\n align-items: center;\n`;\n\nexport {StyledBadgeContainer, StyledBadgeIcon};\n","import {FC} from 'react';\nimport {StyledBadgeContainer, StyledBadgeIcon} from './style';\nimport {BadgeType, BadgeProps} from './types';\n\nconst Badge: FC<BadgeProps> = ({label = '', type = BadgeType.Info, customStyle = {}, icon}) => (\n <StyledBadgeContainer $type={type} $customStyle={customStyle}>\n {icon && <StyledBadgeIcon>{icon}</StyledBadgeIcon>}\n {label}\n </StyledBadgeContainer>\n);\n\nexport default Badge;\n\nexport {BadgeType, type BadgeProps};\n"],"names":["BadgeType","BADGE_COLORS","actionColor","warningColor","errorColor","successColor","StyledBadgeContainer","styled","spacing","white","borderRadius","props","StyledBadgeIcon","Badge","label","type","customStyle","icon","jsxs","jsx"],"mappings":";;;AAGO,MAAMA,IAAY;AAAA,EACvB,MAAM;AAAA,EACN,SAAS;AAAA,EACT,QAAQ;AAAA,EACR,SAAS;AACX,GCSMC,IAAe;AAAA,EACnB,CAACD,EAAU,IAAI,GAAGE;AAAA,EAClB,CAACF,EAAU,OAAO,GAAGG;AAAA,EACrB,CAACH,EAAU,MAAM,GAAGI;AAAA,EACpB,CAACJ,EAAU,OAAO,GAAGK;AACvB,GAEMC,IAAuBC,EAAO;AAAA;AAAA;AAAA,eAGrBC,CAAO;AAAA;AAAA,WAEXC,CAAK;AAAA,mBACGC,IAAe,CAAC;AAAA;AAAA;AAAA;AAAA,sBAIb,CAACC,MAAUV,EAAaU,EAAM,KAAK,CAAC;AAAA,IACtD,CAACA,MAAUA,EAAM,YAAY;AAAA,GAG3BC,IAAkBL,EAAO;AAAA,kBACbC,CAAO;AAAA;AAAA;AAAA,GCnCnBK,IAAwB,CAAC,EAAC,OAAAC,IAAQ,IAAI,MAAAC,IAAOf,EAAU,MAAM,aAAAgB,IAAc,CAAA,GAAI,MAAAC,QACnF,gBAAAC,EAACZ,KAAqB,OAAOS,GAAM,cAAcC,GAC9C,UAAA;AAAA,EAAAC,KAAQ,gBAAAE,EAACP,KAAiB,UAAAK,EAAA,CAAK;AAAA,EAC/BH;AAAA,EAAA,CACH;"}
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,14 +1,19 @@
1
1
  {
2
2
  "name": "@propellerads/badge",
3
- "version": "4.0.2",
3
+ "version": "6.0.0",
4
4
  "type": "module",
5
5
  "types": "dist/index.d.ts",
6
+ "style": "dist/index.css",
6
7
  "exports": {
7
8
  ".": {
8
9
  "types": "./dist/index.d.ts",
9
10
  "import": "./dist/index.js"
10
- }
11
+ },
12
+ "./index.css": "./dist/index.css"
11
13
  },
14
+ "sideEffects": [
15
+ "**/*.css"
16
+ ],
12
17
  "files": [
13
18
  "dist"
14
19
  ],
@@ -19,23 +24,33 @@
19
24
  "url": "https://github.com/propellerads/ui-components/issues"
20
25
  },
21
26
  "scripts": {
22
- "build": "tsc && vite build && rollup -c rollup.dts.config.mjs"
27
+ "build": "tsc && vite build && bunx rollup -c rollup.dts.config.mjs"
23
28
  },
24
29
  "homepage": "https://github.com/propellerads/ui-components#readme",
25
30
  "private": false,
26
31
  "dependencies": {
27
- "@propellerads/stylevariables": "^2.0.2"
32
+ "@base-ui-components/react": "1.0.0-rc.0",
33
+ "class-variance-authority": "^0.7.1",
34
+ "clsx": "^2.1.1"
28
35
  },
29
36
  "peerDependencies": {
37
+ "@propellerads/tokens": ">=0.3.0",
30
38
  "react": "^18.0.0 || ^19.0.0",
31
- "styled-components": "^5.1.1 || ^6.0.0"
39
+ "react-dom": "^18.0.0 || ^19.0.0"
40
+ },
41
+ "peerDependenciesMeta": {
42
+ "@propellerads/tokens": {
43
+ "optional": true
44
+ }
32
45
  },
33
46
  "devDependencies": {
47
+ "@propellerads/tokens": "workspace:^",
34
48
  "@types/react": "^19.2.14",
35
- "@vitejs/plugin-react": "^5.1.4",
36
- "rollup": "^4.58.0",
37
- "rollup-plugin-dts": "^6.3.0",
38
- "typescript": "^5.9.3",
39
- "vite": "^7.3.1"
49
+ "@vitejs/plugin-react": "^6.0.1",
50
+ "rollup": "^4.60.3",
51
+ "rollup-plugin-dts": "^6.4.1",
52
+ "typescript": "^6.0.3",
53
+ "vite": "^8.0.11",
54
+ "vite-plugin-lib-inject-css": "^2.2.2"
40
55
  }
41
56
  }