@oslokommune/punkt-react 16.3.0 → 16.5.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,6 +1,6 @@
1
1
  {
2
2
  "name": "@oslokommune/punkt-react",
3
- "version": "16.3.0",
3
+ "version": "16.5.0",
4
4
  "description": "React komponentbibliotek til Punkt, et designsystem laget av Oslo Origo",
5
5
  "homepage": "https://punkt.oslo.kommune.no",
6
6
  "author": "Team Designsystem, Oslo Origo",
@@ -39,7 +39,7 @@
39
39
  "dependencies": {
40
40
  "@lit-labs/ssr-dom-shim": "^1.2.1",
41
41
  "@lit/react": "^1.0.7",
42
- "@oslokommune/punkt-elements": "^16.3.0",
42
+ "@oslokommune/punkt-elements": "^16.5.0",
43
43
  "classnames": "^2.5.1",
44
44
  "prettier": "^3.3.3",
45
45
  "react-hook-form": "^7.53.0"
@@ -50,7 +50,7 @@
50
50
  "@eslint/eslintrc": "^3.3.3",
51
51
  "@eslint/js": "^9.37.0",
52
52
  "@oslokommune/punkt-assets": "^16.0.0",
53
- "@oslokommune/punkt-css": "^16.3.0",
53
+ "@oslokommune/punkt-css": "^16.4.0",
54
54
  "@testing-library/jest-dom": "^6.5.0",
55
55
  "@testing-library/react": "^16.0.1",
56
56
  "@testing-library/user-event": "^14.5.2",
@@ -109,5 +109,5 @@
109
109
  "url": "https://github.com/oslokommune/punkt/issues"
110
110
  },
111
111
  "license": "MIT",
112
- "gitHead": "3561a39e6329e3fc85d558d89cf20b58aa410d24"
112
+ "gitHead": "81940eb91344498b82fc74316684b452f9fcc82c"
113
113
  }
@@ -1,45 +1,79 @@
1
- import { fireEvent, render } from '@testing-library/react'
1
+ import '@testing-library/jest-dom'
2
+
3
+ import { cleanup, fireEvent, render } from '@testing-library/react'
2
4
  import { axe, toHaveNoViolations } from 'jest-axe'
5
+ import { createRef } from 'react'
3
6
  import { vi } from 'vitest'
4
7
 
5
8
  import { PktBackLink } from './BackLink'
6
9
 
7
10
  expect.extend(toHaveNoViolations)
8
11
 
12
+ afterEach(cleanup)
13
+
9
14
  describe('PktBackLink', () => {
10
- it('renders with default text and href', async () => {
15
+ test('renders with default text and href', () => {
11
16
  const { getByRole } = render(<PktBackLink />)
12
- await window.customElements.whenDefined('pkt-backlink')
13
17
  const link = getByRole('link')
14
- await expect(link.textContent).toBe('Forsiden')
15
- await expect(link.getAttribute('href')).toBe('/')
18
+ expect(link).toHaveTextContent('Forsiden')
19
+ expect(link).toHaveAttribute('href', '/')
16
20
  })
17
21
 
18
- it('renders with custom text and href', async () => {
22
+ test('renders with custom text and href', () => {
19
23
  const { getByRole } = render(<PktBackLink href="/custom-url" text="Custom Text" />)
20
- await window.customElements.whenDefined('pkt-backlink')
21
24
  const link = getByRole('link')
22
- await expect(link.textContent).toBe('Custom Text')
23
- await expect(link.getAttribute('href')).toBe('/custom-url')
25
+ expect(link).toHaveTextContent('Custom Text')
26
+ expect(link).toHaveAttribute('href', '/custom-url')
24
27
  })
25
28
 
26
- it('calls onClick when clicked', async () => {
29
+ test('calls onClick when clicked', () => {
27
30
  const onClickMock = vi.fn()
28
31
  const { getByText } = render(<PktBackLink text="Back" onClick={onClickMock} />)
29
- await window.customElements.whenDefined('pkt-backlink')
30
- const link = getByText('Back')
31
- await fireEvent.click(link)
32
+ fireEvent.click(getByText('Back'))
33
+ expect(onClickMock).toHaveBeenCalledTimes(1)
34
+ })
32
35
 
33
- await expect(onClickMock).toHaveBeenCalledTimes(1)
36
+ test('renders with default aria-label on nav', () => {
37
+ const { container } = render(<PktBackLink />)
38
+ const nav = container.querySelector('nav')
39
+ expect(nav).toHaveAttribute('aria-label', 'Gå tilbake til forrige side')
40
+ })
41
+
42
+ test('renders with custom aria-label on nav', () => {
43
+ const { container } = render(<PktBackLink ariaLabel="Tilbake til oversikt" />)
44
+ const nav = container.querySelector('nav')
45
+ expect(nav).toHaveAttribute('aria-label', 'Tilbake til oversikt')
46
+ })
47
+
48
+ test('forwards ref to nav element', () => {
49
+ const ref = createRef<HTMLElement>()
50
+ render(<PktBackLink ref={ref} />)
51
+ expect(ref.current).toBeInstanceOf(HTMLElement)
52
+ expect(ref.current?.tagName).toBe('NAV')
53
+ })
54
+
55
+ test('supports custom renderLink', () => {
56
+ const { getByRole } = render(
57
+ <PktBackLink
58
+ href="/test"
59
+ text="Tilbake"
60
+ renderLink={({ href, className, children }) => (
61
+ <button data-href={href} className={className}>
62
+ {children}
63
+ </button>
64
+ )}
65
+ />,
66
+ )
67
+ const button = getByRole('button')
68
+ expect(button).toHaveAttribute('data-href', '/test')
69
+ expect(button).toHaveTextContent('Tilbake')
34
70
  })
35
71
 
36
72
  describe('accessibility', () => {
37
- it('renders with no wcag errors with axe', async () => {
73
+ test('renders with no wcag errors with axe', async () => {
38
74
  const { container } = render(<PktBackLink text="Tilbake" href="/" />)
39
-
40
75
  const results = await axe(container)
41
-
42
- await expect(results).toHaveNoViolations()
76
+ expect(results).toHaveNoViolations()
43
77
  })
44
78
  })
45
79
  })
@@ -1,24 +1,50 @@
1
1
  'use client'
2
2
 
3
- import { createComponent } from '@lit/react'
4
- import { type IPktBackLink as IPktElBackLink, PktBackLink as PktElBackLink } from '@oslokommune/punkt-elements'
5
- // eslint-disable-next-line no-restricted-syntax -- React is required for createComponent
6
- import React, { AnchorHTMLAttributes, ForwardRefExoticComponent, LegacyRef, MouseEventHandler } from 'react'
3
+ import { AnchorHTMLAttributes, forwardRef, ReactNode, Ref } from 'react'
7
4
 
8
- type ExtendedBackLink = Omit<IPktElBackLink, 'text'> & AnchorHTMLAttributes<HTMLAnchorElement>
5
+ import { PktIcon } from '../icon/Icon'
9
6
 
10
- export interface IPktBackLink extends ExtendedBackLink {
7
+ export interface IPktBackLink extends Omit<AnchorHTMLAttributes<HTMLAnchorElement>, 'href'> {
8
+ href?: string
11
9
  text?: string
12
- ref?: LegacyRef<HTMLAnchorElement>
13
10
  ariaLabel?: string
14
- onClick?: MouseEventHandler<HTMLAnchorElement>
11
+ renderLink?: (args: {
12
+ href: string
13
+ className: string
14
+ children: ReactNode
15
+ props: AnchorHTMLAttributes<HTMLAnchorElement>
16
+ }) => ReactNode
17
+ ref?: Ref<HTMLElement>
15
18
  }
16
19
 
17
- export const PktBackLink = createComponent({
18
- tagName: 'pkt-backlink',
19
- elementClass: PktElBackLink,
20
- react: React,
21
- displayName: 'PktBackLink',
22
- }) as ForwardRefExoticComponent<IPktBackLink>
20
+ export const PktBackLink = forwardRef<HTMLElement, IPktBackLink>(
21
+ ({ href = '/', text = 'Forsiden', ariaLabel, renderLink, ...props }, ref) => {
22
+ const linkChildren = (
23
+ <>
24
+ <PktIcon className="pkt-back-link__icon pkt-icon pkt-link__icon" name="chevron-thin-left" aria-hidden="true" />
25
+ <span className="pkt-back-link__text">{text}</span>
26
+ </>
27
+ )
28
+
29
+ const linkRenderer =
30
+ renderLink ||
31
+ (({ href, className, children, props }) => (
32
+ <a href={href} className={className} {...props}>
33
+ {children}
34
+ </a>
35
+ ))
36
+
37
+ return (
38
+ <nav className="pkt-back-link" aria-label={ariaLabel || 'Gå tilbake til forrige side'} ref={ref}>
39
+ {linkRenderer({
40
+ href,
41
+ className: 'pkt-link pkt-link--icon-left',
42
+ children: linkChildren,
43
+ props,
44
+ })}
45
+ </nav>
46
+ )
47
+ },
48
+ )
23
49
 
24
50
  PktBackLink.displayName = 'PktBackLink'
@@ -4,7 +4,6 @@ import classNames from 'classnames'
4
4
  import { forwardRef, HTMLAttributes, ReactNode, Ref } from 'react'
5
5
  import type { TCardSkin, TLayout } from 'shared-types'
6
6
 
7
- import { PktIcon } from '../icon/Icon'
8
7
  import { PktTag } from '../tag/Tag'
9
8
 
10
9
  export type { TCardSkin, TLayout }
@@ -76,8 +75,7 @@ export const PktCard = forwardRef<HTMLDivElement, IPktCard>(
76
75
  'pkt-card--border-on-hover': borderOnHover,
77
76
  })
78
77
 
79
- const ariaLabelLenke =
80
- ariaLabel?.trim() || (heading ? `${heading} lenkekort` : 'lenkekort')
78
+ const ariaLabelLenke = ariaLabel?.trim() || (heading ? `${heading} lenkekort` : 'lenkekort')
81
79
  const ariaLabelVanlig = ariaLabel?.trim() || (heading ? heading : 'kort')
82
80
 
83
81
  const HeadingTag = `h${headingLevel}` as keyof JSX.IntrinsicElements
@@ -168,11 +166,7 @@ export const PktCard = forwardRef<HTMLDivElement, IPktCard>(
168
166
 
169
167
  return (
170
168
  <div ref={ref} className={classNames('pkt-card-root', className)} style={{ display: 'block', width: '100%' }}>
171
- <article
172
- {...props}
173
- className={classes}
174
- aria-label={clickCardLink ? ariaLabelLenke : ariaLabelVanlig}
175
- >
169
+ <article {...props} className={classes} aria-label={clickCardLink ? ariaLabelLenke : ariaLabelVanlig}>
176
170
  {renderImage()}
177
171
  <div className="pkt-card__wrapper">
178
172
  {tagPosition === 'top' && renderTags()}