@stackshift-ui/badge 6.0.11 → 7.0.0-beta.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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@stackshift-ui/badge",
3
3
  "description": "",
4
- "version": "6.0.11",
4
+ "version": "7.0.0-beta.0",
5
5
  "private": false,
6
6
  "sideEffects": false,
7
7
  "main": "./dist/index.js",
@@ -29,21 +29,22 @@
29
29
  "typescript": "^5.6.2",
30
30
  "vite-tsconfig-paths": "^5.0.1",
31
31
  "vitest": "^2.1.1",
32
- "@stackshift-ui/eslint-config": "6.0.10",
33
- "@stackshift-ui/typescript-config": "6.0.10"
32
+ "@stackshift-ui/typescript-config": "6.0.10",
33
+ "@stackshift-ui/eslint-config": "6.0.10"
34
34
  },
35
35
  "dependencies": {
36
+ "class-variance-authority": "^0.7.1",
36
37
  "classnames": "^2.5.1",
37
- "@stackshift-ui/scripts": "6.0.10",
38
- "@stackshift-ui/system": "6.0.11",
39
- "@stackshift-ui/flex": "6.0.11"
38
+ "@stackshift-ui/scripts": "6.1.0-beta.0",
39
+ "@stackshift-ui/system": "6.1.0-beta.0",
40
+ "@stackshift-ui/flex": "7.0.0-beta.0"
40
41
  },
41
42
  "peerDependencies": {
43
+ "@stackshift-ui/system": ">=6.1.0-beta.0",
42
44
  "@types/react": "16.8 - 19",
43
45
  "next": "10 - 14",
44
46
  "react": "16.8 - 19",
45
- "react-dom": "16.8 - 19",
46
- "@stackshift-ui/system": ">=6.0.11"
47
+ "react-dom": "16.8 - 19"
47
48
  },
48
49
  "peerDependenciesMeta": {
49
50
  "next": {
@@ -6,12 +6,38 @@ describe.concurrent("badge", () => {
6
6
  afterEach(cleanup);
7
7
 
8
8
  test("Common: Badge - test if renders without errors", ({ expect }) => {
9
- const clx = "badge-class";
10
- render(<Badge className={clx} />);
9
+ const { unmount } = render(<Badge data-testid="default-badge" />);
11
10
 
12
- const elements = screen.getAllByTestId("div");
11
+ const elements = screen.getAllByTestId("default-badge");
13
12
  elements.forEach(element => {
14
13
  expect(element.classList).toBeDefined();
15
14
  });
15
+ unmount();
16
+ });
17
+
18
+ test("Common: Badge - test if renders with correct variant", ({ expect }) => {
19
+ const { unmount } = render(<Badge data-testid="badge-secondary" variant="secondary" />);
20
+
21
+ const elements = screen.getAllByTestId("badge-secondary");
22
+ elements.forEach(element => {
23
+ expect(element.classList).toContain("bg-secondary");
24
+ });
25
+ unmount();
26
+ });
27
+
28
+ test("Common: Badge - test if renders with correct text", ({ expect }) => {
29
+ const { unmount } = render(
30
+ <>
31
+ <Badge data-testid="default-badge-2">Test</Badge>
32
+ <Badge data-testid="destructive-badge" variant="destructive">
33
+ Destructive
34
+ </Badge>
35
+ ,
36
+ </>,
37
+ );
38
+
39
+ expect(screen.getByTestId("default-badge-2").textContent).toBe("Test");
40
+ expect(screen.getByTestId("destructive-badge").textContent).toBe("Destructive");
41
+ unmount();
16
42
  });
17
43
  });
package/src/badge.tsx CHANGED
@@ -1,27 +1,39 @@
1
- import { DefaultComponent, useStackShiftUIComponents } from "@stackshift-ui/system";
2
- import type { ElementType, HTMLProps, ReactNode } from "react";
3
- import { Flex } from "@stackshift-ui/flex";
4
- import cn from "classnames";
5
-
6
- export interface BadgeProps extends Omit<HTMLProps<HTMLElement>, "as"> {
7
- children?: ReactNode;
8
- className?: string;
9
- as?: ElementType;
10
- }
1
+ import { cva, type VariantProps } from "class-variance-authority";
2
+ import * as React from "react";
3
+
4
+ import { cn, DefaultComponent, useStackShiftUIComponents } from "@stackshift-ui/system";
11
5
 
12
6
  const displayName = "Badge";
13
7
 
14
- export const Badge: React.FC<BadgeProps> = ({ children, className, as, ...props }) => {
8
+ const badgeVariants = cva(
9
+ "inline-flex items-center rounded-full border px-2.5 py-0.5 text-xs font-semibold transition-colors focus:outline-none focus:ring-2 focus:ring-ring focus:ring-offset-2",
10
+ {
11
+ variants: {
12
+ variant: {
13
+ default: "border-transparent bg-primary text-primary-foreground hover:bg-primary/80",
14
+ secondary:
15
+ "border-transparent bg-secondary text-secondary-foreground hover:bg-secondary/80",
16
+ destructive:
17
+ "border-transparent bg-destructive text-destructive-foreground hover:bg-destructive/80",
18
+ outline: "text-foreground",
19
+ },
20
+ },
21
+ defaultVariants: {
22
+ variant: "default",
23
+ },
24
+ },
25
+ );
26
+
27
+ export interface BadgeProps
28
+ extends React.HTMLAttributes<HTMLDivElement>,
29
+ VariantProps<typeof badgeVariants> {}
30
+
31
+ function Badge({ className, variant, ...props }: BadgeProps) {
15
32
  const { [displayName]: Component = DefaultComponent } = useStackShiftUIComponents();
16
- const defaultClass = "px-3 py-1 text-sm font-bold uppercase rounded-full";
17
33
 
18
- return (
19
- <Component className={className} {...props} data-testid={displayName}>
20
- <Flex>
21
- <div className={cn(defaultClass, className)}>{children}</div>
22
- </Flex>
23
- </Component>
24
- );
25
- };
34
+ return <Component className={cn(badgeVariants({ variant }), className)} {...props} />;
35
+ }
26
36
 
27
37
  Badge.displayName = displayName;
38
+
39
+ export { Badge, badgeVariants };