@oslokommune/punkt-react 16.2.0 → 16.4.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.2.0",
3
+ "version": "16.4.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.2.0",
42
+ "@oslokommune/punkt-elements": "^16.4.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.2.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": "554167272be133d1486fb9b041b3afe0980c5069"
112
+ "gitHead": "7ea157177224bb319ade35eb479b90e10940fdc6"
113
113
  }
@@ -58,6 +58,9 @@ export const PktAccordionItem = forwardRef<HTMLDetailsElement, IPktAccordionItem
58
58
  const detailsElement = e.currentTarget
59
59
  const newOpenState = detailsElement.open
60
60
 
61
+ // Ignorer toggle-events som skyldes React-synkronisering av open-attributtet
62
+ if (newOpenState === isOpen) return
63
+
61
64
  if (controlledIsOpen === undefined) {
62
65
  setInternalIsOpen(newOpenState)
63
66
  }
@@ -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()}
@@ -11,35 +11,40 @@ expect.extend(toHaveNoViolations)
11
11
  afterEach(cleanup)
12
12
 
13
13
  describe('PktModal', () => {
14
- test('ref works correctly', async () => {
15
- const ref = createRef<HTMLElement>()
14
+ test('ref works correctly', () => {
15
+ const ref = createRef<HTMLDialogElement>()
16
16
  const { unmount } = render(
17
17
  <PktModal ref={ref} headingText="Modal Title">
18
18
  modal content
19
19
  </PktModal>,
20
20
  )
21
- await window.customElements.whenDefined('pkt-modal')
22
- expect(ref.current).toBeInstanceOf(HTMLElement)
21
+ expect(ref.current).toBeInstanceOf(HTMLDialogElement)
23
22
  unmount()
24
23
  expect(ref.current).toBeNull()
25
24
  })
26
25
 
27
- test('renders with the specified size', async () => {
26
+ test('renders with the specified size', () => {
28
27
  const { container } = render(
29
28
  <PktModal headingText="Modal Title" size="small">
30
29
  modal content
31
30
  </PktModal>,
32
31
  )
33
- await window.customElements.whenDefined('pkt-modal')
34
32
  const dialogElement = container.querySelector('dialog.pkt-modal')
35
33
  expect(dialogElement).toHaveClass('pkt-modal--small')
36
34
  })
37
35
 
38
36
  test('renders with no wcag errors with axe', async () => {
39
37
  const { container } = render(<PktModal headingText="Modal Title"></PktModal>)
40
- await window.customElements.whenDefined('pkt-modal')
41
38
  const results = await axe(container)
42
39
 
43
40
  expect(results).toHaveNoViolations()
44
41
  })
42
+
43
+ test('renders with default props', () => {
44
+ const { container } = render(<PktModal>innhold</PktModal>)
45
+ const dialog = container.querySelector('dialog.pkt-modal')
46
+ expect(dialog).toBeInTheDocument()
47
+ expect(dialog).toHaveClass('pkt-modal--medium')
48
+ })
49
+
45
50
  })
@@ -1,13 +1,22 @@
1
1
  'use client'
2
2
 
3
- import { createComponent } from '@lit/react'
4
- import { PktModal as PktElModal } from '@oslokommune/punkt-elements'
5
- // eslint-disable-next-line no-restricted-syntax -- React is required for createComponent
6
- import React, { FC, ForwardedRef, forwardRef, type ReactElement } from 'react'
3
+ import classNames from 'classnames'
4
+ import {
5
+ ForwardedRef,
6
+ forwardRef,
7
+ HTMLAttributes,
8
+ ReactNode,
9
+ Ref,
10
+ useCallback,
11
+ useEffect,
12
+ useRef,
13
+ } from 'react'
7
14
 
8
- import type { PktElConstructor,PktElType } from '@/interfaces/IPktElements'
15
+ import { PktButton } from '../button/Button'
9
16
 
10
- export interface IPktModal extends PktElType {
17
+ export interface IPktModal
18
+ extends Omit<HTMLAttributes<HTMLDialogElement>, 'onClose' | 'open'> {
19
+ open?: boolean
11
20
  headingText?: string
12
21
  hideCloseButton?: boolean
13
22
  closeOnBackdropClick?: boolean
@@ -17,21 +26,148 @@ export interface IPktModal extends PktElType {
17
26
  variant?: 'dialog' | 'drawer'
18
27
  drawerPosition?: 'left' | 'right'
19
28
  transparentBackdrop?: boolean
29
+ onClose?: () => void
30
+ children?: ReactNode
31
+ ref?: Ref<HTMLDialogElement>
20
32
  }
21
- const LitComponent: FC<IPktModal> = createComponent({
22
- tagName: 'pkt-modal',
23
- elementClass: PktElModal as PktElConstructor<HTMLElement>,
24
- react: React,
25
- displayName: 'PktModal',
26
- events: {},
27
- })
28
-
29
- export const PktModal: FC<IPktModal> = forwardRef(
30
- ({ children, ...props }: IPktModal, ref: ForwardedRef<HTMLElement>): ReactElement => {
33
+
34
+ export const PktModal = forwardRef(
35
+ (
36
+ {
37
+ children,
38
+ className,
39
+ open = false,
40
+ headingText = '',
41
+ hideCloseButton = false,
42
+ closeOnBackdropClick = false,
43
+ size = 'medium',
44
+ removePadding = false,
45
+ closeButtonSkin = 'blue',
46
+ variant = 'dialog',
47
+ drawerPosition = 'right',
48
+ transparentBackdrop = false,
49
+ onClose,
50
+ ...props
51
+ }: IPktModal,
52
+ ref: ForwardedRef<HTMLDialogElement>,
53
+ ) => {
54
+ const internalRef = useRef<HTMLDialogElement>(null)
55
+ const dialogRef = (ref as React.RefObject<HTMLDialogElement>) || internalRef
56
+
57
+ const close = useCallback(() => {
58
+ if (!dialogRef.current?.open) return
59
+ dialogRef.current?.close()
60
+ onClose?.()
61
+ }, [dialogRef, onClose])
62
+
63
+ // Synkroniser scroll-lock med dialogens faktiske åpen/lukket-tilstand
64
+ useEffect(() => {
65
+ const dialog = dialogRef.current
66
+ if (!dialog) return
67
+
68
+ const observer = new MutationObserver(() => {
69
+ if (dialog.open) {
70
+ document.body.classList.add('pkt-modal--open')
71
+ } else {
72
+ document.body.classList.remove('pkt-modal--open')
73
+ }
74
+ })
75
+ observer.observe(dialog, { attributes: true, attributeFilter: ['open'] })
76
+ return () => observer.disconnect()
77
+ }, [dialogRef])
78
+
79
+ // Deklarativ åpne/lukke via open-prop
80
+ useEffect(() => {
81
+ if (open) {
82
+ if (!dialogRef.current?.open) {
83
+ dialogRef.current?.showModal()
84
+ }
85
+ } else {
86
+ if (dialogRef.current?.open) {
87
+ dialogRef.current?.close()
88
+ }
89
+ }
90
+ }, [open, dialogRef])
91
+
92
+ const handleBackdropClick = useCallback(
93
+ (event: React.MouseEvent<HTMLDialogElement>) => {
94
+ if (closeOnBackdropClick && event.target === dialogRef.current) {
95
+ close()
96
+ }
97
+ },
98
+ [closeOnBackdropClick, dialogRef, close],
99
+ )
100
+
101
+ const handleNativeClose = useCallback(() => {
102
+ onClose?.()
103
+ }, [onClose])
104
+
105
+ const isCloseButtonSkinDefault = closeButtonSkin === 'blue'
106
+
107
+ const classes = classNames(
108
+ {
109
+ 'pkt-modal': true,
110
+ 'pkt-modal--removePadding': removePadding,
111
+ 'pkt-modal--noHeadingText': !headingText,
112
+ 'pkt-modal--noShadow': closeButtonSkin === 'yellow-filled',
113
+ 'pkt-modal--transparentBackdrop': transparentBackdrop,
114
+ [`pkt-modal--${size}`]: size,
115
+ [`pkt-modal__${variant}`]: variant,
116
+ [`pkt-modal__drawer--${drawerPosition}`]: variant === 'drawer',
117
+ },
118
+ className,
119
+ )
120
+
31
121
  return (
32
- <LitComponent {...props} ref={ref}>
33
- <div className="pkt-contents">{children}</div>
34
- </LitComponent>
122
+ <dialog
123
+ {...props}
124
+ className={classes}
125
+ ref={dialogRef}
126
+ aria-labelledby={headingText ? 'pkt-modal__headingText' : undefined}
127
+ aria-describedby="pkt-modal__content"
128
+ onClick={handleBackdropClick}
129
+ onClose={handleNativeClose}
130
+ >
131
+ <div className="pkt-modal__wrapper">
132
+ {(headingText || !hideCloseButton) && (
133
+ <div className="pkt-modal__header">
134
+ <div className="pkt-modal__header-background" />
135
+ {headingText ? (
136
+ <h1 id="pkt-modal__headingText" className="pkt-modal__headingText pkt-txt-24">
137
+ {headingText}
138
+ </h1>
139
+ ) : (
140
+ <div className="pkt-modal__headingText" />
141
+ )}
142
+ {!hideCloseButton ? (
143
+ <div
144
+ className={classNames('pkt-modal__closeButton', {
145
+ [`pkt-modal__closeButton--${closeButtonSkin}`]: true,
146
+ })}
147
+ >
148
+ <PktButton
149
+ onClick={close}
150
+ aria-label="close"
151
+ iconName="close"
152
+ variant="icon-only"
153
+ size="medium"
154
+ skin={isCloseButtonSkinDefault ? 'tertiary' : 'primary'}
155
+ >
156
+ Lukk
157
+ </PktButton>
158
+ </div>
159
+ ) : (
160
+ <div className="pkt-modal__noCloseButton" />
161
+ )}
162
+ </div>
163
+ )}
164
+ <div className="pkt-modal__container">
165
+ <div id="pkt-modal__content" className="pkt-modal__content pkt-txt-18-light">
166
+ {children}
167
+ </div>
168
+ </div>
169
+ </div>
170
+ </dialog>
35
171
  )
36
172
  },
37
173
  )