@propellerads/badge 4.0.1 → 5.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 +148 -0
- package/dist/index.css +1 -0
- package/dist/index.d.ts +121 -9
- package/dist/index.js +84 -37
- package/dist/index.js.map +1 -1
- package/package.json +25 -10
package/README.md
ADDED
|
@@ -0,0 +1,148 @@
|
|
|
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
|
+

|
|
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` and `data-tone` on it.
|
|
45
|
+
|
|
46
|
+
The text is written in sentence case whatever case it arrives in: `NEW` and
|
|
47
|
+
`new` both read `New`. Only the text the badge is handed directly is rewritten;
|
|
48
|
+
an element inside it keeps its own.
|
|
49
|
+
|
|
50
|
+
## Form and tone
|
|
51
|
+
|
|
52
|
+
Two axes, as on the button, and every form takes every tone.
|
|
53
|
+
|
|
54
|
+
`variant` is the form:
|
|
55
|
+
|
|
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 |
|
|
60
|
+
|
|
61
|
+
Every badge carries the 1px border, transparent on `fill`, so both forms are the
|
|
62
|
+
same size and the text sits in the same place.
|
|
63
|
+
|
|
64
|
+
`tone` is the colour. Each tone is a token triple: the pad `-bg`, the text
|
|
65
|
+
`-text`, and the colour itself for the icon and the contour.
|
|
66
|
+
|
|
67
|
+
| Tone | Triple | For |
|
|
68
|
+
|---|---|---|
|
|
69
|
+
| `info` (default) | `--color-action-*` | something to know |
|
|
70
|
+
| `warning` | `--color-warning-*` | nothing is broken yet |
|
|
71
|
+
| `danger` | `--color-danger-*` | an error, or a state that cannot be taken back |
|
|
72
|
+
| `success` | `--color-success-*` | a scenario that worked |
|
|
73
|
+
| `quiet` | `--color-quiet-*` | no status: a draft, an archive, an inactive item |
|
|
74
|
+
| `highlight` | `--color-highlight-*` | no status: a promo or a new feature, in the brand's colour |
|
|
75
|
+
|
|
76
|
+
`-text` on its own pad stays under 4.5:1 for every tone in at least one theme —
|
|
77
|
+
`warning` and `highlight` the furthest — so a badge is a label, not a place for a
|
|
78
|
+
sentence.
|
|
79
|
+
|
|
80
|
+
## Icon
|
|
81
|
+
|
|
82
|
+
`icon` takes an icon from [@propellerads/icon](../Icon), passed as its
|
|
83
|
+
component: `icon={Star}`, not `icon={<Star />}`. The badge draws it at 16px, 2px
|
|
84
|
+
clear of the top and the bottom, in the tone's graphic colour, and the start
|
|
85
|
+
padding drops to `--space-4` for it. An element or a custom SVG does not compile.
|
|
86
|
+
|
|
87
|
+
At 16px next to 12px text an icon outweighs the word easily, so pick it by
|
|
88
|
+
weight as well as by meaning:
|
|
89
|
+
|
|
90
|
+
- **A contour, not a solid shape.** `CheckCircleOutline` rather than
|
|
91
|
+
`CheckCircle`, `Star` rather than `ThumbUp`. A filled circle reads as a blot.
|
|
92
|
+
- **Ink on no more than a quarter of the 24-unit grid.** `CheckCircleOutline`,
|
|
93
|
+
`Time`, `Block`, `Stop`, `Pause`, `Edit`, `Star` and `Offer` sit between 18% and
|
|
94
|
+
25%; `CheckCircle`, `Cancel`, `ThumbUp` and `Verified` are near half.
|
|
95
|
+
- **Two units clear of the grid's edge**, or the icon runs into the round end.
|
|
96
|
+
`ThumbUp`, `Lamp`, `MagicWand` and `Eye` come closer than that.
|
|
97
|
+
- **One family in a row:** circles with circles, plain glyphs with plain glyphs.
|
|
98
|
+
- **Not an icon with its colour baked in** — `LaughingSmile`, `SurveyOffer` and
|
|
99
|
+
the brand logos — which cannot take the tone's.
|
|
100
|
+
|
|
101
|
+
## Colours of its own
|
|
102
|
+
|
|
103
|
+
A product may change the colours and nothing else:
|
|
104
|
+
|
|
105
|
+
```tsx
|
|
106
|
+
<Badge bgColor="linear-gradient(258deg, #bfbfbf, #383649)" textColor="white">
|
|
107
|
+
Platinum
|
|
108
|
+
</Badge>
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
`bgColor` is the pad — a colour or a gradient; an outline badge has none and
|
|
112
|
+
ignores it. `textColor` is the text, the icon and the contour. The badge takes
|
|
113
|
+
no `style`; `className` stays for placing it on the page. A look that recurs
|
|
114
|
+
belongs in a tone, not in a call site.
|
|
115
|
+
|
|
116
|
+
`badgeVariants` gives markup that is not a badge the look:
|
|
117
|
+
|
|
118
|
+
```tsx
|
|
119
|
+
<span className={badgeVariants({variant: 'outline', tone: 'danger'})} />
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
## Deprecated
|
|
123
|
+
|
|
124
|
+
The props and names of earlier majors still compile and are on the way out:
|
|
125
|
+
|
|
126
|
+
| v4 | v5 |
|
|
127
|
+
|---|---|
|
|
128
|
+
| `label="Moderation"` | children |
|
|
129
|
+
| `type={BadgeType.Warning}` | `tone="warning"` (`BadgeType` is `BadgeTone`) |
|
|
130
|
+
| `type={BADGE_TYPE.WARNING}` (v2, v3) | `tone="warning"` (`BADGE_TYPE` keeps the capitalised keys) |
|
|
131
|
+
| `icon={<Star color={Color.White} />}` | `icon={Star}` |
|
|
132
|
+
| `customStyle={{background, color}}` | `bgColor`, `textColor` |
|
|
133
|
+
|
|
134
|
+
`customStyle` takes only `background`, `backgroundColor` and `color` now; its
|
|
135
|
+
other properties and nested selectors are ignored. An icon passed as an element
|
|
136
|
+
does not compile, and one that still arrives as an element is drawn as it is,
|
|
137
|
+
without the tone's colour.
|
|
138
|
+
|
|
139
|
+
## Migrating from v4
|
|
140
|
+
|
|
141
|
+
- The look: a tinted pad with coloured text instead of white on the solid
|
|
142
|
+
colour, 20px high instead of 16px, round ends instead of 4px corners, 12px
|
|
143
|
+
sentence case instead of 10px capitals.
|
|
144
|
+
- The element is a `<span>`, not a `<div>`, so a badge inside a `<p>` is valid
|
|
145
|
+
markup.
|
|
146
|
+
- The text stays on one line; v4 let a long label wrap.
|
|
147
|
+
- `styled-components` and `@propellerads/stylevariables` are no longer
|
|
148
|
+
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__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,133 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
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
|
+
};
|
|
13
|
+
type BadgeVariant = (typeof BadgeVariant)[keyof typeof BadgeVariant];
|
|
14
|
+
/**
|
|
15
|
+
* The tone: what the badge says about the thing it stands next to. Each one is
|
|
16
|
+
* a token triple — the `-bg` pad, the `-text` text, and the colour itself for
|
|
17
|
+
* an icon and for the contour. Every form takes every tone.
|
|
18
|
+
*/
|
|
19
|
+
declare const BadgeTone: {
|
|
20
|
+
/** Something to know: the --color-action triple. */
|
|
21
|
+
readonly Info: "info";
|
|
22
|
+
/** Nothing is broken yet: the --color-warning triple. */
|
|
23
|
+
readonly Warning: "warning";
|
|
24
|
+
/** An error, or a state that cannot be taken back: the --color-danger triple. */
|
|
25
|
+
readonly Danger: "danger";
|
|
26
|
+
/** A scenario that worked: the --color-success triple. */
|
|
27
|
+
readonly Success: "success";
|
|
28
|
+
/** No status: a draft, an archive, an inactive item, on the grey --color-quiet triple. */
|
|
29
|
+
readonly Quiet: "quiet";
|
|
30
|
+
/** No status: a promo or a new feature, on the brand's --color-highlight triple. */
|
|
31
|
+
readonly Highlight: "highlight";
|
|
32
|
+
};
|
|
33
|
+
type BadgeTone = (typeof BadgeTone)[keyof typeof BadgeTone];
|
|
34
|
+
/** @deprecated Use `BadgeTone`: the values are the same. */
|
|
4
35
|
declare const BadgeType: {
|
|
36
|
+
/** Something to know: the --color-action triple. */
|
|
5
37
|
readonly Info: "info";
|
|
38
|
+
/** Nothing is broken yet: the --color-warning triple. */
|
|
6
39
|
readonly Warning: "warning";
|
|
40
|
+
/** An error, or a state that cannot be taken back: the --color-danger triple. */
|
|
7
41
|
readonly Danger: "danger";
|
|
42
|
+
/** A scenario that worked: the --color-success triple. */
|
|
8
43
|
readonly Success: "success";
|
|
44
|
+
/** No status: a draft, an archive, an inactive item, on the grey --color-quiet triple. */
|
|
45
|
+
readonly Quiet: "quiet";
|
|
46
|
+
/** No status: a promo or a new feature, on the brand's --color-highlight triple. */
|
|
47
|
+
readonly Highlight: "highlight";
|
|
9
48
|
};
|
|
10
|
-
|
|
11
|
-
type
|
|
49
|
+
/** @deprecated Use `BadgeTone`. */
|
|
50
|
+
type BadgeType = BadgeTone;
|
|
51
|
+
/**
|
|
52
|
+
* @deprecated Use `BadgeTone`. The enum v2 and v3 exported, with its
|
|
53
|
+
* capitalised keys, kept so that code written against them still runs: 29 files
|
|
54
|
+
* in seven products read `BADGE_TYPE.INFO` and the like.
|
|
55
|
+
*/
|
|
56
|
+
declare const BADGE_TYPE: {
|
|
57
|
+
readonly INFO: "info";
|
|
58
|
+
readonly WARNING: "warning";
|
|
59
|
+
readonly DANGER: "danger";
|
|
60
|
+
readonly SUCCESS: "success";
|
|
61
|
+
};
|
|
62
|
+
/** @deprecated Use `BadgeTone`. */
|
|
63
|
+
type BADGE_TYPE = (typeof BADGE_TYPE)[keyof typeof BADGE_TYPE];
|
|
64
|
+
/**
|
|
65
|
+
* An icon from `@propellerads/icon`, as its component: `Star`, not `<Star />`.
|
|
66
|
+
* The badge draws it, so an element — and a custom SVG with it — does not
|
|
67
|
+
* compile.
|
|
68
|
+
*/
|
|
69
|
+
type BadgeIcon = ComponentType<{
|
|
70
|
+
color?: string;
|
|
71
|
+
}>;
|
|
72
|
+
/**
|
|
73
|
+
* The span's props without `style`: a badge's box is the system's, and of
|
|
74
|
+
* everything in it a product may change only the colours, through `bgColor`
|
|
75
|
+
* and `textColor`. `className` stays for placing the badge on the page.
|
|
76
|
+
*/
|
|
77
|
+
type BadgeProps = Omit<useRender.ComponentProps<'span'>, 'style'> & {
|
|
78
|
+
/** How the badge is drawn. @default 'fill' */
|
|
79
|
+
variant?: BadgeVariant;
|
|
80
|
+
/** Which colour that form is worn in. @default 'info' */
|
|
81
|
+
tone?: BadgeTone;
|
|
82
|
+
/**
|
|
83
|
+
* The pad, in place of the tone's: a colour, or a gradient for a tier like
|
|
84
|
+
* Platinum. For a look the design system does not have. An outline badge has
|
|
85
|
+
* no pad and ignores it.
|
|
86
|
+
*/
|
|
87
|
+
bgColor?: string;
|
|
88
|
+
/**
|
|
89
|
+
* The text and the icon — and an outline badge's contour — in place of the
|
|
90
|
+
* tone's. Set it with `bgColor`, so the two stay legible together.
|
|
91
|
+
*/
|
|
92
|
+
textColor?: string;
|
|
93
|
+
/**
|
|
94
|
+
* The icon before the text: one from `@propellerads/icon`, passed as its
|
|
95
|
+
* component — `icon={Star}`. The badge draws it at 16px in the tone's
|
|
96
|
+
* graphic colour.
|
|
97
|
+
*
|
|
98
|
+
* Pick it by weight as well as by meaning: a contour rather than a solid
|
|
99
|
+
* shape (`CheckCircleOutline`, not `CheckCircle`), ink on no more than a
|
|
100
|
+
* quarter of the 24-unit grid, two units clear of its edge, and not one with
|
|
101
|
+
* its colour baked in (`LaughingSmile`, `SurveyOffer`, the brand logos).
|
|
102
|
+
*/
|
|
103
|
+
icon?: BadgeIcon;
|
|
104
|
+
/** The text, written in sentence case whatever case it is passed in. */
|
|
105
|
+
children?: ReactNode;
|
|
106
|
+
/** @deprecated Pass the text as children. */
|
|
12
107
|
label?: ReactNode;
|
|
108
|
+
/** @deprecated Use `tone`. */
|
|
13
109
|
type?: BadgeType;
|
|
14
|
-
|
|
15
|
-
|
|
110
|
+
/**
|
|
111
|
+
* @deprecated Use `bgColor` and `textColor`. Only `background`,
|
|
112
|
+
* `backgroundColor` and `color` are taken from it; everything else is ignored.
|
|
113
|
+
*/
|
|
114
|
+
customStyle?: CSSProperties;
|
|
16
115
|
};
|
|
17
116
|
|
|
18
|
-
|
|
117
|
+
/**
|
|
118
|
+
* The class names the stylesheet defines. Form and tone are separate axes: the
|
|
119
|
+
* variant decides where the colour goes, the tone which colour it is, so every
|
|
120
|
+
* form can be worn in every tone.
|
|
121
|
+
*
|
|
122
|
+
* Exported so that markup which is not a badge can take its look:
|
|
123
|
+
* `<span className={badgeVariants({variant: 'outline', tone: 'danger'})} />`.
|
|
124
|
+
*/
|
|
125
|
+
declare const badgeVariants: (props?: ({
|
|
126
|
+
variant?: "fill" | "outline" | null | undefined;
|
|
127
|
+
tone?: "info" | "warning" | "danger" | "success" | "quiet" | "highlight" | null | undefined;
|
|
128
|
+
} & class_variance_authority_types.ClassProp) | undefined) => string;
|
|
129
|
+
|
|
130
|
+
declare const Badge: ({ className, variant, tone, bgColor, textColor, render, ref, label, type, icon, customStyle, children, ...props }: BadgeProps) => ReactElement<unknown, string | react.JSXElementConstructor<any>>;
|
|
19
131
|
|
|
20
|
-
export { BadgeType, Badge as default };
|
|
21
|
-
export type { BadgeProps };
|
|
132
|
+
export { BADGE_TYPE, Badge, BadgeTone, BadgeType, BadgeVariant, badgeVariants, Badge as default };
|
|
133
|
+
export type { BadgeIcon, BadgeProps };
|
package/dist/index.js
CHANGED
|
@@ -1,38 +1,85 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
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
|
+
}, f = {
|
|
12
|
+
Info: "info",
|
|
13
|
+
Warning: "warning",
|
|
14
|
+
Danger: "danger",
|
|
15
|
+
Success: "success",
|
|
16
|
+
Quiet: "quiet",
|
|
17
|
+
Highlight: "highlight"
|
|
18
|
+
}, p = f, m = {
|
|
19
|
+
INFO: f.Info,
|
|
20
|
+
WARNING: f.Warning,
|
|
21
|
+
DANGER: f.Danger,
|
|
22
|
+
SUCCESS: f.Success
|
|
23
|
+
}, h = o("badge", {
|
|
24
|
+
variants: {
|
|
25
|
+
variant: {
|
|
26
|
+
fill: "badge--fill",
|
|
27
|
+
outline: "badge--outline"
|
|
28
|
+
},
|
|
29
|
+
tone: {
|
|
30
|
+
info: "badge--tone-info",
|
|
31
|
+
warning: "badge--tone-warning",
|
|
32
|
+
danger: "badge--tone-danger",
|
|
33
|
+
success: "badge--tone-success",
|
|
34
|
+
quiet: "badge--tone-quiet",
|
|
35
|
+
highlight: "badge--tone-highlight"
|
|
36
|
+
}
|
|
37
|
+
},
|
|
38
|
+
defaultVariants: {
|
|
39
|
+
variant: "fill",
|
|
40
|
+
tone: "info"
|
|
41
|
+
}
|
|
42
|
+
}), g = (e) => {
|
|
43
|
+
let t = !0;
|
|
44
|
+
return n.map(e, (e) => {
|
|
45
|
+
if (typeof e != "string" && typeof e != "number") return e;
|
|
46
|
+
let n = String(e).toLowerCase();
|
|
47
|
+
return !t || n.trim() === "" ? n : (t = !1, n.replace(/\S/, (e) => e.toUpperCase()));
|
|
48
|
+
});
|
|
49
|
+
}, _ = (e, t) => {
|
|
50
|
+
if (!(e === void 0 && t === void 0)) return {
|
|
51
|
+
...e !== void 0 && { "--badge-bg": e },
|
|
52
|
+
...t !== void 0 && {
|
|
53
|
+
"--badge-fg": t,
|
|
54
|
+
"--badge-icon": t
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
}, v = (e) => i(e) ? e : r(e, { color: "currentColor" }), y = ({ className: n, variant: r = d.Fill, tone: i, bgColor: a, textColor: o, render: p, ref: m, label: y, type: b, icon: x, customStyle: S, children: C, ...w }) => {
|
|
58
|
+
let T = i ?? b ?? f.Info, E = _(a ?? S?.background ?? S?.backgroundColor, o ?? S?.color);
|
|
59
|
+
return t({
|
|
60
|
+
defaultTagName: "span",
|
|
61
|
+
render: p,
|
|
62
|
+
ref: m,
|
|
63
|
+
props: e({
|
|
64
|
+
className: u(h({
|
|
65
|
+
variant: r,
|
|
66
|
+
tone: T
|
|
67
|
+
}), x !== void 0 && "badge--icon", n),
|
|
68
|
+
style: E,
|
|
69
|
+
children: /* @__PURE__ */ l(s, { children: [x !== void 0 && /* @__PURE__ */ c("span", {
|
|
70
|
+
className: "badge__icon",
|
|
71
|
+
"aria-hidden": !0,
|
|
72
|
+
children: v(x)
|
|
73
|
+
}), g(C ?? y)] })
|
|
74
|
+
}, w),
|
|
75
|
+
state: {
|
|
76
|
+
slot: "badge",
|
|
77
|
+
variant: r,
|
|
78
|
+
tone: T
|
|
79
|
+
}
|
|
80
|
+
});
|
|
37
81
|
};
|
|
38
|
-
//#
|
|
82
|
+
//#endregion
|
|
83
|
+
export { m as BADGE_TYPE, y as Badge, y as default, f as BadgeTone, p as BadgeType, d as BadgeVariant, h as badgeVariants };
|
|
84
|
+
|
|
85
|
+
//# 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} 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"}
|
package/package.json
CHANGED
|
@@ -1,14 +1,19 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@propellerads/badge",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "5.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
|
-
"@
|
|
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.2.0",
|
|
30
38
|
"react": "^18.0.0 || ^19.0.0",
|
|
31
|
-
"
|
|
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": "^
|
|
36
|
-
"rollup": "^4.
|
|
37
|
-
"rollup-plugin-dts": "^6.
|
|
38
|
-
"typescript": "^
|
|
39
|
-
"vite": "^
|
|
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.10",
|
|
54
|
+
"vite-plugin-lib-inject-css": "^2.2.2"
|
|
40
55
|
}
|
|
41
56
|
}
|