@oslokommune/punkt-react 16.2.0 → 16.3.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.3.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.3.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.3.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": "3561a39e6329e3fc85d558d89cf20b58aa410d24"
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
  }
@@ -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
  )