@oslokommune/punkt-react 19.14.2 → 19.15.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.
@@ -5925,70 +5925,74 @@ var ca = "is-scroll-locked", la = 0, ua = 0, da = () => typeof document < "u" &&
5925
5925
  !da() || la === 0 || (--la, la === 0 && pa());
5926
5926
  }, ga = () => ({ locked: !1 }), _a = (e, t) => {
5927
5927
  e.locked !== t && (e.locked = t, t ? ma() : ha());
5928
- }, va = i(({ children: e, className: t, open: n = !1, headingText: r = "", hideCloseButton: i = !1, closeOnBackdropClick: a = !1, size: s = "medium", removePadding: l = !1, closeButtonSkin: u = "blue", variant: d = "dialog", drawerPosition: f = "right", transparentBackdrop: m = !1, onClose: h, strings: g, ...y }, b) => {
5929
- let x = ke(["generic"], g), S = p(null), C = p(ga()), w = o((e) => {
5930
- S.current = e, typeof b == "function" ? b(e) : b && (b.current = e);
5931
- }, [b]), T = o(() => {
5932
- S.current?.open && (S.current.close(), _a(C.current, !1), h?.());
5933
- }, [h]);
5928
+ }, va = i(({ children: e, className: t, open: n = !1, headingText: r = "", headingIconName: i, headingIconPath: a, hideCloseButton: s = !1, closeOnBackdropClick: l = !1, size: u = "medium", removePadding: d = !1, closeButtonSkin: f = "blue", variant: m = "dialog", drawerPosition: h = "right", transparentBackdrop: g = !1, onClose: y, strings: b, ...x }, S) => {
5929
+ let C = ke(["generic"], b), w = p(null), T = p(ga()), E = ar(), D = `pkt-modal__headingText-${E}`, O = `pkt-modal__content-${E}`, k = o((e) => {
5930
+ w.current = e, typeof S == "function" ? S(e) : S && (S.current = e);
5931
+ }, [S]), A = o(() => {
5932
+ w.current?.open && (w.current.close(), _a(T.current, !1), y?.());
5933
+ }, [y]);
5934
5934
  c(() => {
5935
- n ? (S.current?.open || S.current?.showModal(), _a(C.current, !0)) : (S.current?.open && S.current.close(), _a(C.current, !1));
5935
+ n ? (w.current?.open || w.current?.showModal(), _a(T.current, !0)) : (w.current?.open && w.current.close(), _a(T.current, !1));
5936
5936
  }, [n]), c(() => {
5937
- let e = C.current;
5937
+ let e = T.current;
5938
5938
  return () => {
5939
5939
  _a(e, !1);
5940
5940
  };
5941
5941
  }, []);
5942
- let E = o((e) => {
5943
- a && e.target === S.current && T();
5944
- }, [a, T]), D = o(() => {
5945
- _a(C.current, !1), h?.();
5946
- }, [h]), O = u === "blue", k = (0, z.default)({
5942
+ let j = o((e) => {
5943
+ l && e.target === w.current && A();
5944
+ }, [l, A]), M = o(() => {
5945
+ _a(T.current, !1), y?.();
5946
+ }, [y]), N = f === "blue", P = (0, z.default)({
5947
5947
  "pkt-modal": !0,
5948
- "pkt-modal--removePadding": l,
5948
+ "pkt-modal--removePadding": d,
5949
5949
  "pkt-modal--noHeadingText": !r,
5950
- "pkt-modal--noShadow": u === "yellow-filled",
5951
- "pkt-modal--transparentBackdrop": m,
5952
- [`pkt-modal--${s}`]: s,
5953
- [`pkt-modal__${d}`]: d,
5954
- [`pkt-modal__drawer--${f}`]: d === "drawer"
5950
+ "pkt-modal--noShadow": f === "yellow-filled",
5951
+ "pkt-modal--transparentBackdrop": g,
5952
+ [`pkt-modal--${u}`]: u,
5953
+ [`pkt-modal__${m}`]: m,
5954
+ [`pkt-modal__drawer--${h}`]: m === "drawer"
5955
5955
  }, t);
5956
5956
  return /* @__PURE__ */ _("dialog", {
5957
- ...y,
5958
- className: k,
5959
- ref: w,
5960
- "aria-labelledby": r ? "pkt-modal__headingText" : void 0,
5961
- "aria-describedby": "pkt-modal__content",
5962
- onClick: E,
5963
- onClose: D,
5957
+ ...x,
5958
+ className: P,
5959
+ ref: k,
5960
+ "aria-labelledby": r ? D : void 0,
5961
+ "aria-describedby": O,
5962
+ onClick: j,
5963
+ onClose: M,
5964
5964
  children: /* @__PURE__ */ v("div", {
5965
5965
  className: "pkt-modal__wrapper",
5966
- children: [(r || !i) && /* @__PURE__ */ v("div", {
5966
+ children: [(r || !s) && /* @__PURE__ */ v("div", {
5967
5967
  className: "pkt-modal__header",
5968
5968
  children: [
5969
5969
  /* @__PURE__ */ _("div", { className: "pkt-modal__header-background" }),
5970
- r ? /* @__PURE__ */ _("h1", {
5971
- id: "pkt-modal__headingText",
5972
- className: "pkt-modal__headingText pkt-txt-24",
5973
- children: r
5970
+ r ? /* @__PURE__ */ v("h1", {
5971
+ id: D,
5972
+ className: (0, z.default)("pkt-modal__headingText", "pkt-txt-24", { "pkt-modal__headingText--hasIcon": i }),
5973
+ children: [i && /* @__PURE__ */ _(J, {
5974
+ className: "pkt-modal__headingIcon",
5975
+ name: i,
5976
+ ...a && { path: a }
5977
+ }), r]
5974
5978
  }) : /* @__PURE__ */ _("div", { className: "pkt-modal__headingText" }),
5975
- i ? /* @__PURE__ */ _("div", { className: "pkt-modal__noCloseButton" }) : /* @__PURE__ */ _("div", {
5976
- className: (0, z.default)("pkt-modal__closeButton", { [`pkt-modal__closeButton--${u}`]: !0 }),
5979
+ s ? /* @__PURE__ */ _("div", { className: "pkt-modal__noCloseButton" }) : /* @__PURE__ */ _("div", {
5980
+ className: (0, z.default)("pkt-modal__closeButton", { [`pkt-modal__closeButton--${f}`]: !0 }),
5977
5981
  children: /* @__PURE__ */ _(Ee, {
5978
- onClick: T,
5979
- "aria-label": x.generic.close,
5982
+ onClick: A,
5983
+ "aria-label": C.generic.close,
5980
5984
  iconName: "close",
5981
5985
  variant: "icon-only",
5982
5986
  size: "medium",
5983
- skin: O ? "tertiary" : "primary",
5984
- children: x.generic.close
5987
+ skin: N ? "tertiary" : "primary",
5988
+ children: C.generic.close
5985
5989
  })
5986
5990
  })
5987
5991
  ]
5988
5992
  }), /* @__PURE__ */ _("div", {
5989
5993
  className: "pkt-modal__container",
5990
5994
  children: /* @__PURE__ */ _("div", {
5991
- id: "pkt-modal__content",
5995
+ id: O,
5992
5996
  className: "pkt-modal__content pkt-txt-18-light",
5993
5997
  children: e
5994
5998
  })
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@oslokommune/punkt-react",
3
- "version": "19.14.2",
3
+ "version": "19.15.1",
4
4
  "description": "React komponentbibliotek til Punkt, designsystem for Oslo kommune",
5
5
  "homepage": "https://punkt.oslo.kommune.no",
6
6
  "author": "Team Designsystem, Oslo kommune",
@@ -41,7 +41,7 @@
41
41
  "@eslint/eslintrc": "^3.3.5",
42
42
  "@eslint/js": "^10.0.1",
43
43
  "@oslokommune/punkt-assets": "^19.14.2",
44
- "@oslokommune/punkt-css": "^19.14.2",
44
+ "@oslokommune/punkt-css": "^19.15.1",
45
45
  "@testing-library/jest-dom": "^6.9.1",
46
46
  "@testing-library/react": "^16.3.2",
47
47
  "@testing-library/user-event": "^14.6.1",
@@ -101,5 +101,5 @@
101
101
  "url": "https://github.com/oslokommune/punkt/issues"
102
102
  },
103
103
  "license": "MIT",
104
- "gitHead": "6d0937b0b2b5c9257079680b17248ad702419c53"
104
+ "gitHead": "c3e2cb3ef59a925134568e0b9ef3b44d60541500"
105
105
  }
@@ -47,4 +47,89 @@ describe('PktModal', () => {
47
47
  expect(dialog).toHaveClass('pkt-modal--medium')
48
48
  })
49
49
 
50
+ describe('heading icon', () => {
51
+ test('renders no icon by default', () => {
52
+ const { container } = render(<PktModal headingText="Historikk">innhold</PktModal>)
53
+ const heading = container.querySelector('.pkt-modal__headingText')
54
+ expect(heading).not.toHaveClass('pkt-modal__headingText--hasIcon')
55
+ expect(heading?.querySelector('.pkt-icon')).toBeNull()
56
+ })
57
+
58
+ test('renders the icon before the heading text', () => {
59
+ const { container } = render(
60
+ <PktModal headingText="Historikk" headingIconName="clock-history">
61
+ innhold
62
+ </PktModal>,
63
+ )
64
+ const heading = container.querySelector('.pkt-modal__headingText')
65
+ expect(heading).toHaveClass('pkt-modal__headingText--hasIcon')
66
+ const icon = heading?.firstElementChild
67
+ expect(icon).toHaveClass('pkt-icon', 'pkt-modal__headingIcon')
68
+ expect(icon).toHaveAttribute('name', 'clock-history')
69
+ expect(heading).toHaveTextContent('Historikk')
70
+ })
71
+
72
+ test('passes headingIconPath to the icon', () => {
73
+ const customPath = 'https://custom-cdn.example.com/icons/'
74
+ const { container } = render(
75
+ <PktModal headingText="Historikk" headingIconName="clock-history" headingIconPath={customPath}>
76
+ innhold
77
+ </PktModal>,
78
+ )
79
+ const icon = container.querySelector('.pkt-modal__headingIcon')
80
+ expect(icon).toHaveAttribute('data-path', customPath)
81
+ })
82
+
83
+ test('does not render the icon without headingText', () => {
84
+ const { container } = render(<PktModal headingIconName="clock-history">innhold</PktModal>)
85
+ expect(container.querySelector('.pkt-modal__headingIcon')).toBeNull()
86
+ })
87
+
88
+ test('renders with no wcag errors with axe', async () => {
89
+ const { container } = render(<PktModal headingText="Historikk" headingIconName="clock-history"></PktModal>)
90
+ const results = await axe(container)
91
+ expect(results).toHaveNoViolations()
92
+ })
93
+ })
94
+
95
+ describe('ids', () => {
96
+ test('labels and describes the dialog with its own heading and content', () => {
97
+ const { container } = render(<PktModal headingText="Historikk">innhold</PktModal>)
98
+ const dialog = container.querySelector('dialog')!
99
+ const heading = container.querySelector('h1.pkt-modal__headingText')!
100
+ const content = container.querySelector('.pkt-modal__content')!
101
+ expect(heading.id).toMatch(/^pkt-modal__headingText-/)
102
+ expect(content.id).toMatch(/^pkt-modal__content-/)
103
+ expect(dialog).toHaveAttribute('aria-labelledby', heading.id)
104
+ expect(dialog).toHaveAttribute('aria-describedby', content.id)
105
+ })
106
+
107
+ test('gives each modal on the page unique ids', () => {
108
+ const { container } = render(
109
+ <>
110
+ <PktModal headingText="Første">innhold</PktModal>
111
+ <PktModal headingText="Andre">innhold</PktModal>
112
+ </>,
113
+ )
114
+ const [first, second] = Array.from(container.querySelectorAll('dialog'))
115
+ const firstHeading = first.querySelector('h1')!
116
+ const secondHeading = second.querySelector('h1')!
117
+ expect(firstHeading.id).not.toBe(secondHeading.id)
118
+ expect(first).toHaveAttribute('aria-labelledby', firstHeading.id)
119
+ expect(second).toHaveAttribute('aria-labelledby', secondHeading.id)
120
+ expect(first.getAttribute('aria-describedby')).not.toBe(second.getAttribute('aria-describedby'))
121
+ })
122
+
123
+ test('keeps the ids stable across re-renders', () => {
124
+ const { container, rerender } = render(<PktModal headingText="Historikk">innhold</PktModal>)
125
+ const before = container.querySelector('h1')!.id
126
+ rerender(<PktModal headingText="Endret">innhold</PktModal>)
127
+ expect(container.querySelector('h1')!.id).toBe(before)
128
+ })
129
+
130
+ test('omits aria-labelledby without headingText', () => {
131
+ const { container } = render(<PktModal>innhold</PktModal>)
132
+ expect(container.querySelector('dialog')).not.toHaveAttribute('aria-labelledby')
133
+ })
134
+ })
50
135
  })
@@ -16,13 +16,17 @@ import {
16
16
  import type { IPktStringsProps } from 'shared-strings'
17
17
  import { createScrollLockToken, setScrollLock } from 'shared-utils/scroll-lock'
18
18
 
19
+ import { useGeneratedId } from '../../hooks/useGeneratedId'
19
20
  import { usePktStrings } from '../../hooks/usePktStrings'
20
21
  import { PktButton } from '../button/Button'
22
+ import { PktIcon } from '../icon/Icon'
21
23
 
22
24
  export interface IPktModal
23
25
  extends Omit<HTMLAttributes<HTMLDialogElement>, 'onClose' | 'open'>, IPktStringsProps<'generic'> {
24
26
  open?: boolean
25
27
  headingText?: string
28
+ headingIconName?: string
29
+ headingIconPath?: string
26
30
  hideCloseButton?: boolean
27
31
  closeOnBackdropClick?: boolean
28
32
  size?: 'small' | 'medium' | 'large' | 'fit-content'
@@ -43,6 +47,8 @@ export const PktModal = forwardRef(
43
47
  className,
44
48
  open = false,
45
49
  headingText = '',
50
+ headingIconName,
51
+ headingIconPath,
46
52
  hideCloseButton = false,
47
53
  closeOnBackdropClick = false,
48
54
  size = 'medium',
@@ -60,6 +66,9 @@ export const PktModal = forwardRef(
60
66
  const s = usePktStrings(['generic'], strings)
61
67
  const internalRef = useRef<HTMLDialogElement | null>(null)
62
68
  const scrollLockTokenRef = useRef(createScrollLockToken())
69
+ const idSuffix = useGeneratedId()
70
+ const headingId = `pkt-modal__headingText-${idSuffix}`
71
+ const contentId = `pkt-modal__content-${idSuffix}`
63
72
 
64
73
  // Merge consumer ref + internal ref so we always have a working DOM handle
65
74
  // for our own logic, regardless of whether the consumer passes a callback ref
@@ -138,8 +147,8 @@ export const PktModal = forwardRef(
138
147
  {...props}
139
148
  className={classes}
140
149
  ref={setDialogRef}
141
- aria-labelledby={headingText ? 'pkt-modal__headingText' : undefined}
142
- aria-describedby="pkt-modal__content"
150
+ aria-labelledby={headingText ? headingId : undefined}
151
+ aria-describedby={contentId}
143
152
  onClick={handleBackdropClick}
144
153
  onClose={handleNativeClose}
145
154
  >
@@ -148,7 +157,19 @@ export const PktModal = forwardRef(
148
157
  <div className="pkt-modal__header">
149
158
  <div className="pkt-modal__header-background" />
150
159
  {headingText ? (
151
- <h1 id="pkt-modal__headingText" className="pkt-modal__headingText pkt-txt-24">
160
+ <h1
161
+ id={headingId}
162
+ className={classNames('pkt-modal__headingText', 'pkt-txt-24', {
163
+ 'pkt-modal__headingText--hasIcon': headingIconName,
164
+ })}
165
+ >
166
+ {headingIconName && (
167
+ <PktIcon
168
+ className="pkt-modal__headingIcon"
169
+ name={headingIconName}
170
+ {...(headingIconPath && { path: headingIconPath })}
171
+ />
172
+ )}
152
173
  {headingText}
153
174
  </h1>
154
175
  ) : (
@@ -177,7 +198,7 @@ export const PktModal = forwardRef(
177
198
  </div>
178
199
  )}
179
200
  <div className="pkt-modal__container">
180
- <div id="pkt-modal__content" className="pkt-modal__content pkt-txt-18-light">
201
+ <div id={contentId} className="pkt-modal__content pkt-txt-18-light">
181
202
  {children}
182
203
  </div>
183
204
  </div>