@oslokommune/punkt-react 16.1.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.
@@ -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
  )