@coopdigital/react 0.3.0 → 0.4.1

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 CHANGED
@@ -16,31 +16,32 @@
16
16
  <a href="https://static.coop.co.uk/experience-kit-storybook-react"><img src="https://img.shields.io/badge/storybook-react-F85792" alt="storybook" /></a>
17
17
  [![All Contributors][all_contributors_badge]](#contributors)
18
18
 
19
+ > React components for the Experience Library design system.
20
+
21
+ You can see the full range of components and documentation on the [Experience Kit Storybook](https://static.coop.co.uk/experience-kit-storybook-react)
22
+
19
23
  ## Getting started
20
24
 
21
- Install the package from NPM:
25
+ Install the react package and its companion [styles package](https://www.npmjs.com/package/@coopdigital/styles) from NPM:
22
26
  ```
23
- npm install @coopdigital/react
27
+ npm install @coopdigital/react @coopdigital/styles
24
28
  ```
29
+ *Note: In some edge cases you may want to install the packages independently*
25
30
 
26
- Import the components that you need:
31
+ ## Usage
32
+
33
+ Import the components that you need, along with the main stylesheet and the corresponding component styles:
27
34
  ```
28
- import { Button } from "@coopdigital/react"
35
+ import { Pill } from "@coopdigital/react"
36
+ import "@coopdigital/styles/dist/main.css"
37
+ import "@coopdigital/styles/dist/components/Pill.css"
29
38
  ```
30
39
 
31
- ### Package Scripts
32
-
33
- The following scripts should be run inside the react package folder.
34
-
35
- | Script | Action |
36
- | -------------------- | ---------------------------------------- |
37
- | `npm run test` | To run vitest |
38
- | `npm run test:coverage` | To run vitest test coverage |
39
- | `npm run test:e2e` | To run playwright in headless mode |
40
- | `npm run test:e2e:ui` | To run playwright with UI |
41
- | `npm run build` | To run build script |
42
- | `npm run build:storybook` | To build storybook |
43
- | `npm run storybook` | To run storybook |
40
+ Alternatively if your project uses SASS you can import the source stylesheets:
41
+ ```
42
+ @use "@coopdigital/styles/src/main.scss"
43
+ @use "@coopdigital/styles/src/components/Pill.scss"
44
+ ```
44
45
 
45
46
  ## Contributors ✨
46
47
 
@@ -140,3 +141,27 @@ Thanks goes to these wonderful people:
140
141
  <!-- ALL-CONTRIBUTORS-LIST:END -->
141
142
 
142
143
  This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification.
144
+
145
+ ## License
146
+
147
+ MIT License
148
+
149
+ Copyright (c) Co-operative Group Limited
150
+
151
+ Permission is hereby granted, free of charge, to any person obtaining a copy
152
+ of this software and associated documentation files (the "Software"), to deal
153
+ in the Software without restriction, including without limitation the rights
154
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
155
+ copies of the Software, and to permit persons to whom the Software is
156
+ furnished to do so, subject to the following conditions:
157
+
158
+ The above copyright notice and this permission notice shall be included in all
159
+ copies or substantial portions of the Software.
160
+
161
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
162
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
163
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
164
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
165
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
166
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
167
+ SOFTWARE.
@@ -1,5 +1,6 @@
1
- import React from "react";
1
+ import React, { AnchorHTMLAttributes, ForwardRefExoticComponent } from "react";
2
2
  export interface PillProps {
3
+ as?: React.FC<AnchorHTMLAttributes<HTMLElement>> | ForwardRefExoticComponent<any> | string;
3
4
  ariaLabel?: string;
4
5
  badge?: string;
5
6
  badgeColor?: "green" | "orange" | "red" | "pink";
@@ -9,13 +10,5 @@ export interface PillProps {
9
10
  pillColor?: "blue" | "pink";
10
11
  size?: "sm" | "md" | "lg" | "xl";
11
12
  }
12
- export declare const Pill: ({ ariaLabel, badge, badgeColor, children, className, href, pillColor, size, }: PillProps) => React.DetailedReactHTMLElement<{
13
- "aria-label": string | undefined;
14
- className: string;
15
- "data-badge": string | undefined;
16
- "data-badge-color": "green" | "orange" | "red" | "pink";
17
- "data-pill-color": "pink" | "blue";
18
- "data-size": "sm" | "lg" | "xl" | undefined;
19
- href: string | undefined;
20
- }, HTMLElement>;
13
+ export declare const Pill: ({ ariaLabel, as, badge, badgeColor, children, className, href, pillColor, size, }: PillProps) => React.ReactElement<React.AnchorHTMLAttributes<HTMLElement>, string | React.JSXElementConstructor<any>>;
21
14
  export default Pill;
@@ -1,7 +1,10 @@
1
1
  import React from 'react';
2
2
 
3
- const Pill = ({ ariaLabel, badge, badgeColor = "red", children, className = "", href, pillColor = "blue", size = "md", }) => {
4
- const element = href ? "a" : "span";
3
+ const Pill = ({ ariaLabel, as, badge, badgeColor = "red", children, className = "", href, pillColor = "blue", size = "md", }) => {
4
+ let element = href ? "a" : "span";
5
+ if (as) {
6
+ element = as;
7
+ }
5
8
  const componentProps = {
6
9
  "aria-label": ariaLabel,
7
10
  className: `coop-pill ${className}`,
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@coopdigital/react",
3
3
  "type": "module",
4
- "version": "0.3.0",
4
+ "version": "0.4.1",
5
5
  "main": "dist/index.js",
6
6
  "private": false,
7
7
  "publishConfig": {
@@ -9,10 +9,21 @@
9
9
  },
10
10
  "files": [
11
11
  "dist",
12
- "src"
12
+ "src",
13
+ "!**/__{screenshots,tests}__",
14
+ "!**/*.{stories,test}.*"
15
+ ],
16
+ "keywords": [
17
+ "co-op",
18
+ "design-system",
19
+ "experience-kit",
20
+ "experience-library",
21
+ "components",
22
+ "react"
13
23
  ],
14
24
  "scripts": {
15
25
  "test": "vitest run",
26
+ "test:watch": "vitest",
16
27
  "test:coverage": "vitest run --coverage",
17
28
  "build": "tsx scripts build",
18
29
  "build:storybook": "storybook build --disable-telemetry && npm run storybook:fix-paths",
@@ -28,7 +39,7 @@
28
39
  "devDependencies": {
29
40
  "@axe-core/playwright": "^4.10.1",
30
41
  "@chromatic-com/storybook": "^3.2.5",
31
- "@coopdigital/styles": "^0.5.0",
42
+ "@coopdigital/styles": "^0.5.2",
32
43
  "@playwright/test": "^1.50.1",
33
44
  "@rollup/plugin-node-resolve": "^16.0.0",
34
45
  "@rollup/plugin-typescript": "^12.1.2",
@@ -53,5 +64,5 @@
53
64
  "react": "^19.0.0",
54
65
  "react-dom": "^19.0.0"
55
66
  },
56
- "gitHead": "ccedccf7209f30e95b5312052836bbd745410d0c"
67
+ "gitHead": "827c8ec0c84d2f8034e28acde178e7c3e7ed488b"
57
68
  }
@@ -1,6 +1,8 @@
1
- import React from "react"
1
+ import React, { AnchorHTMLAttributes, ForwardRefExoticComponent } from "react"
2
2
 
3
3
  export interface PillProps {
4
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
5
+ as?: React.FC<AnchorHTMLAttributes<HTMLElement>> | ForwardRefExoticComponent<any> | string
4
6
  ariaLabel?: string
5
7
  badge?: string
6
8
  badgeColor?: "green" | "orange" | "red" | "pink"
@@ -13,6 +15,7 @@ export interface PillProps {
13
15
 
14
16
  export const Pill = ({
15
17
  ariaLabel,
18
+ as,
16
19
  badge,
17
20
  badgeColor = "red",
18
21
  children,
@@ -21,7 +24,11 @@ export const Pill = ({
21
24
  pillColor = "blue",
22
25
  size = "md",
23
26
  }: PillProps) => {
24
- const element = href ? "a" : "span"
27
+ let element: PillProps["as"] = href ? "a" : "span"
28
+
29
+ if (as) {
30
+ element = as
31
+ }
25
32
 
26
33
  const componentProps = {
27
34
  "aria-label": ariaLabel,
@@ -1 +0,0 @@
1
- export {};
@@ -1,4 +0,0 @@
1
- /**
2
- * @vitest-environment jsdom
3
- */
4
- export {};
@@ -1 +0,0 @@
1
- export {};
@@ -1,4 +0,0 @@
1
- /**
2
- * @vitest-environment jsdom
3
- */
4
- export {};
@@ -1,8 +0,0 @@
1
- import { expect, test } from "@playwright/test"
2
-
3
- const server_url = "/iframe.html?viewMode=story&id=components-button--primary&args=&globals="
4
-
5
- test("Add to basket button visibility", async ({ page }) => {
6
- await page.goto(server_url)
7
- await expect(page.getByRole("button", { name: "Add to basket" })).toBeVisible()
8
- })
@@ -1,13 +0,0 @@
1
- import type { Meta, StoryObj } from "@storybook/react"
2
-
3
- import Button from "./Button"
4
-
5
- const meta: Meta<typeof Button> = {
6
- component: Button,
7
- }
8
-
9
- export default meta
10
-
11
- type Story = StoryObj<typeof meta>
12
-
13
- export const Primary: Story = {}
@@ -1,18 +0,0 @@
1
- /**
2
- * @vitest-environment jsdom
3
- */
4
-
5
- import { render } from "@testing-library/react"
6
- import { describe, it, afterEach } from "vitest"
7
- import { Button } from "."
8
- import { cleanup } from "@testing-library/react"
9
- afterEach(() => {
10
- cleanup()
11
- })
12
-
13
- describe("Button", () => {
14
- it("renders button", () => {
15
- render(<Button />)
16
- //screen.debug()
17
- })
18
- })
@@ -1,134 +0,0 @@
1
- import { expect, test } from "@playwright/test"
2
- import AxeBuilder from "@axe-core/playwright"
3
-
4
- const defaultUrl = "/iframe.html?globals=&args=&id=components-pill--default&viewMode=story"
5
- const withoutLinkUrl = "/iframe.html?globals=&args=&id=components-pill--without-link&viewMode=story"
6
- const withBadgeUrl = "/iframe.html?globals=&args=&id=components-pill--with-badge&viewMode=story"
7
-
8
- test.describe("Pill default", () => {
9
- test("Renders", async ({ page }) => {
10
- await page.goto(defaultUrl)
11
- const pill = page.getByRole("link", { name: "Be more Co-op" })
12
- await expect(pill).toBeVisible()
13
- await expect(pill).toHaveAttribute("href", "/be-more-co-op")
14
- })
15
-
16
- test("Does accept additional classes", async ({ page }) => {
17
- await page.goto(defaultUrl + "&args=className:coop--uppercase")
18
- const pill = page.getByRole("link", { name: "Be more Co-op" })
19
- await expect(pill).toHaveClass(/coop-pill/)
20
- await expect(pill).toHaveClass(/coop--uppercase/)
21
- })
22
-
23
- test("Doesn't display new badge", async ({ page }) => {
24
- await page.goto(defaultUrl)
25
- const pill = page.getByRole("link", { name: "Be more Co-op" })
26
- await expect(pill).not.toHaveAttribute("data-badge")
27
- })
28
-
29
- test("Doesn't have any automatically detectable accessibility issues", async ({ page }) => {
30
- await page.goto(defaultUrl)
31
-
32
- await page.locator("#storybook-root").waitFor()
33
-
34
- const accessibilityScanResults = await new AxeBuilder({ page })
35
- .include("#storybook-root")
36
- .analyze()
37
-
38
- expect(accessibilityScanResults.violations).toEqual([])
39
- })
40
-
41
- test("Does match the previous screenshot", async ({ page }) => {
42
- await page.goto(defaultUrl)
43
- await expect(page).toHaveScreenshot()
44
- })
45
- })
46
-
47
- test.describe("Pill with badge", () => {
48
- test("Renders and has the badge", async ({ page }) => {
49
- await page.goto(withBadgeUrl)
50
- const pill = page.getByRole("link", { name: "Your weekly offers" })
51
- await expect(pill).toBeVisible()
52
- await expect(pill).toHaveAttribute("href", "/your-weekly-offers")
53
- await expect(pill).toHaveAttribute("data-badge")
54
- })
55
-
56
- test("Doesn't have any automatically detectable accessibility issues", async ({ page }) => {
57
- await page.goto(withBadgeUrl)
58
-
59
- await page.locator("#storybook-root").waitFor()
60
-
61
- const accessibilityScanResults = await new AxeBuilder({ page })
62
- .include("#storybook-root")
63
- .analyze()
64
-
65
- expect(accessibilityScanResults.violations).toEqual([])
66
- })
67
-
68
- test("Does match the previous screenshot", async ({ page }) => {
69
- await page.goto(withBadgeUrl)
70
- await expect(page).toHaveScreenshot()
71
- })
72
- })
73
-
74
- test.describe("Pill with or without href", () => {
75
- test("Doesn't add a link when href is not provided", async ({ page }) => {
76
- await page.goto(withoutLinkUrl)
77
- const pill = page.locator("span.coop-pill")
78
- await expect(pill).toBeVisible()
79
- })
80
-
81
- test("Renders a link when href is provided", async ({ page }) => {
82
- await page.goto(withoutLinkUrl + "&args=href:test")
83
- const pill = page.locator("a.coop-pill")
84
- await expect(pill).toBeVisible()
85
- })
86
- })
87
-
88
- test.describe("Pill size attribute", () => {
89
- test("Doesn't apply data-size when size is 'md' (default)", async ({ page }) => {
90
- await page.goto(defaultUrl + "&args=size:md")
91
- const pill = page.locator(".coop-pill")
92
- await expect(pill).not.toHaveAttribute("data-size")
93
- })
94
-
95
- test("Doesn't apply data-size when size is undefined", async ({ page }) => {
96
- await page.goto(defaultUrl)
97
- const pill = page.locator(".coop-pill")
98
- await expect(pill).not.toHaveAttribute("data-size")
99
- })
100
-
101
- test("Does NOT apply data-size when size is an empty string", async ({ page }) => {
102
- await page.goto(defaultUrl + "&args=size:")
103
- const pill = page.locator(".coop-pill")
104
- await expect(pill).not.toHaveAttribute("data-size")
105
- })
106
- })
107
-
108
- test.describe("Pill badge color attribute", () => {
109
- test("Applies data-badge-color when badgeColor is 'orange'", async ({ page }) => {
110
- await page.goto(defaultUrl + "&args=badgeColor:orange")
111
- const pill = page.locator(".coop-pill")
112
- await expect(pill).toHaveAttribute("data-badge-color", "orange")
113
- })
114
-
115
- test("Applies default badge color when badgeColor is not provided", async ({ page }) => {
116
- await page.goto(defaultUrl)
117
- const pill = page.locator(".coop-pill")
118
- await expect(pill).toHaveAttribute("data-badge-color", "red")
119
- })
120
- })
121
-
122
- test.describe("Pill color attribute", () => {
123
- test("Applies data-pill-color when pillColor is 'pink'", async ({ page }) => {
124
- await page.goto(defaultUrl + "&args=pillColor:pink")
125
- const pill = page.locator(".coop-pill")
126
- await expect(pill).toHaveAttribute("data-pill-color", "pink")
127
- })
128
-
129
- test("Applies default pill color when pillColor is not provided", async ({ page }) => {
130
- await page.goto(defaultUrl)
131
- const pill = page.locator(".coop-pill")
132
- await expect(pill).toHaveAttribute("data-pill-color", "blue")
133
- })
134
- })
@@ -1,138 +0,0 @@
1
- import type { Meta, StoryObj } from "@storybook/react"
2
- import "../../../../styles/src/components/Pill.scss"
3
-
4
- import Pill from "./Pill"
5
-
6
- const meta: Meta<typeof Pill> = {
7
- component: Pill,
8
- argTypes: {
9
- href: {
10
- description: "Specifies the URL that the Pill component will link to when clicked.",
11
- },
12
- children: {
13
- description:
14
- "Represents the content inside the Pill component. It can be any valid JSX or string.",
15
- control: false,
16
- },
17
- className: {
18
- description: "Receives any optional className to be applied to Pill component.",
19
- },
20
- badge: {
21
- description: "Specifies what text Pill should display on the badge.",
22
- },
23
- size: {
24
- description: "Specifies what should be the Pill size.",
25
- },
26
- },
27
- parameters: {
28
- docs: {
29
- description: {
30
- component:
31
- "The Pill component is a small, rounded label used to link and highlight information such as categories or articles. It can be customised with different content and styles to suit various use cases.",
32
- },
33
- },
34
- },
35
- }
36
-
37
- export default meta
38
-
39
- type Story = StoryObj<typeof meta>
40
-
41
- export const Default: Story = {
42
- args: { href: "/be-more-co-op", children: "Be more Co-op" },
43
- parameters: {
44
- docs: {
45
- description: {
46
- story: "Displays a basic Pill with default settings.",
47
- },
48
- },
49
- },
50
- }
51
-
52
- export const WithoutLink: Story = {
53
- args: { children: "Be more Co-op" },
54
- parameters: {
55
- docs: {
56
- description: {
57
- story: "Displays a basic Pill as basic <span> element.",
58
- },
59
- },
60
- },
61
- }
62
-
63
- export const WithBadge: Story = {
64
- args: {
65
- href: "/your-weekly-offers",
66
- badge: "NEW",
67
- children: "Your weekly offers",
68
- },
69
- parameters: {
70
- docs: {
71
- description: {
72
- story: "Displays a Pill with an additional custom badge to indicate new content.",
73
- },
74
- },
75
- },
76
- }
77
-
78
- export const Sizes: Story = {
79
- args: {
80
- children: "Your weekly offers",
81
- },
82
- parameters: {
83
- docs: {
84
- description: {
85
- story: "Displays all the 4 sizes supported.",
86
- },
87
- },
88
- },
89
- render: (args) => (
90
- <>
91
- <div className="coop-pill-group">
92
- <Pill {...args} size="xl" />
93
- <Pill {...args} size="lg" />
94
- <Pill {...args} size="md" />
95
- <Pill {...args} size="sm" />
96
- </div>
97
- <div className="coop-pill-group">
98
- <Pill {...args} badge="NEW" size="xl" />
99
- <Pill {...args} badge="NEW" size="lg" />
100
- <Pill {...args} badge="NEW" size="md" />
101
- <Pill {...args} badge="NEW" size="sm" />
102
- </div>
103
- </>
104
- ),
105
- }
106
-
107
- export const ColourVariations: Story = {
108
- args: {
109
- children: "Your weekly offers",
110
- },
111
- parameters: {
112
- docs: {
113
- description: {
114
- story: "Displays all the colour variations supported for Badge and Pill.",
115
- },
116
- },
117
- },
118
- render: (args) => (
119
- <>
120
- <div className="coop-pill-group">
121
- <Pill {...args} pillColor="blue" />
122
- <Pill {...args} pillColor="pink" />
123
- </div>
124
- <div className="coop-pill-group">
125
- <Pill {...args} badge="NEW" pillColor="blue" badgeColor="red" />
126
- <Pill {...args} badge="NEW" pillColor="blue" badgeColor="green" />
127
- <Pill {...args} badge="NEW" pillColor="blue" badgeColor="orange" />
128
- <Pill {...args} badge="NEW" pillColor="blue" badgeColor="pink" />
129
- </div>
130
- <div className="coop-pill-group">
131
- <Pill {...args} badge="NEW" pillColor="pink" badgeColor="red" />
132
- <Pill {...args} badge="NEW" pillColor="pink" badgeColor="green" />
133
- <Pill {...args} badge="NEW" pillColor="pink" badgeColor="orange" />
134
- <Pill {...args} badge="NEW" pillColor="pink" badgeColor="pink" />
135
- </div>
136
- </>
137
- ),
138
- }
@@ -1,25 +0,0 @@
1
- /**
2
- * @vitest-environment jsdom
3
- */
4
-
5
- import { render, cleanup } from "@testing-library/react"
6
- import { describe, it, afterEach } from "vitest"
7
- import { Pill } from "."
8
-
9
- afterEach(() => {
10
- cleanup()
11
- })
12
-
13
- describe("Pill", () => {
14
- it("Renders default Pill", () => {
15
- render(<Pill href="test/abc">Add missing receipt</Pill>)
16
- })
17
-
18
- it("Renders Pill with badge", () => {
19
- render(
20
- <Pill href="test/abc" badge="NEW">
21
- <span>Add missing receipt</span>
22
- </Pill>
23
- )
24
- })
25
- })