@oslokommune/punkt-react 20.1.6 → 20.2.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/CHANGELOG.md CHANGED
@@ -5,6 +5,19 @@ og skriver commits ca etter [Conventional Commits](https://conventionalcommits.o
5
5
 
6
6
  ---
7
7
 
8
+ ## [20.2.0](https://github.com/oslokommune/punkt/compare/20.1.7...20.2.0) (2026-11-17)
9
+
10
+ ### Features
11
+ * Tabs bytter panel selv og forbedret etter ARIA-standarder (#3830).
12
+
13
+ ---
14
+
15
+
16
+ ## [20.1.7](https://github.com/oslokommune/punkt/compare/20.1.6...20.1.7) (2026-11-14)
17
+
18
+ ---
19
+
20
+
8
21
  ## [20.1.6](https://github.com/oslokommune/punkt/compare/20.1.5...20.1.6) (2026-11-12)
9
22
 
10
23
  ### Bug Fixes
@@ -1,5 +1,5 @@
1
1
  import { ReactNode } from 'react';
2
- import { IPktAccordionBase, TAccordionSkin } from '../../shared-types';
2
+ import { IPktAccordionBase, TAccordionIndicator, TAccordionSkin } from '../../shared-types';
3
3
  export type TPktAccordionSkin = TAccordionSkin;
4
4
  export interface IPktAccordion extends IPktAccordionBase {
5
5
  children?: ReactNode;
@@ -8,5 +8,6 @@ export interface IPktAccordion extends IPktAccordionBase {
8
8
  export declare const useAccordionContext: () => {
9
9
  name?: string;
10
10
  skin?: TPktAccordionSkin;
11
+ indicator?: TAccordionIndicator;
11
12
  };
12
13
  export declare const PktAccordion: import('react').ForwardRefExoticComponent<IPktAccordion & import('react').RefAttributes<HTMLDivElement>>;
@@ -2,24 +2,25 @@
2
2
  import { createContext as e, forwardRef as t, useContext as n } from "react";
3
3
  import { jsx as r } from "react/jsx-runtime";
4
4
  // src/components/accordion/Accordion.tsx
5
- var i = e({}), a = () => n(i), o = t(({ compact: e = !1, skin: t = "borderless", ariaLabelledBy: n, children: a, name: o, className: s }, c) => {
6
- let l = [
5
+ var i = e({}), a = () => n(i), o = t(({ compact: e = !1, skin: t = "borderless", indicator: n, ariaLabelledBy: a, children: o, name: s, className: c }, l) => {
6
+ let u = [
7
7
  "pkt-accordion",
8
8
  e && "pkt-accordion--compact",
9
9
  t && `pkt-accordion--${t}`,
10
- s
10
+ c
11
11
  ].filter(Boolean).join(" ");
12
12
  return /* @__PURE__ */ r(i.Provider, {
13
13
  value: {
14
- name: o,
15
- skin: t
14
+ name: s,
15
+ skin: t,
16
+ indicator: n
16
17
  },
17
18
  children: /* @__PURE__ */ r("div", {
18
- ref: c,
19
- className: l,
19
+ ref: l,
20
+ className: u,
20
21
  "data-testid": "pkt-accordion",
21
- "aria-labelledby": n || void 0,
22
- children: a
22
+ "aria-labelledby": a || void 0,
23
+ children: o
23
24
  })
24
25
  });
25
26
  });
@@ -6,33 +6,33 @@ import { useAccordionContext as n } from "./Accordion.js";
6
6
  import { forwardRef as r, useState as i } from "react";
7
7
  import { Fragment as a, jsx as o, jsxs as s } from "react/jsx-runtime";
8
8
  // src/components/accordion/AccordionItem.tsx
9
- var c = r(({ defaultOpen: r = !1, id: c, title: l, skin: u, compact: d = !1, isOpen: f, children: p, name: m, className: h, onClick: g, onToggle: _ }, v) => {
10
- let [y, b] = i(r), { name: x, skin: S } = n(), C = m || x, w = u || S, T = f === void 0 ? y : f, E = (e) => {
9
+ var c = r(({ defaultOpen: r = !1, id: c, title: l, skin: u, compact: d = !1, indicator: f, isOpen: p, children: m, name: h, className: g, onClick: _, onToggle: v }, y) => {
10
+ let [b, x] = i(r), { name: S, skin: C, indicator: w } = n(), T = h || S, E = u || C, D = E === "plus-minus" || (f || w) === "plus-minus", O = p === void 0 ? b : p, k = (e) => {
11
11
  let t = e.currentTarget.open;
12
- t !== T && (f === void 0 && b(t), _?.(e));
13
- }, D = (e) => {
12
+ t !== O && (p === void 0 && x(t), v?.(e));
13
+ }, A = (e) => {
14
14
  setTimeout(() => {
15
- g?.(e);
15
+ _?.(e);
16
16
  }, 0);
17
- }, O = [
17
+ }, j = [
18
18
  "pkt-accordion-item",
19
19
  d && "pkt-accordion-item--compact",
20
20
  u && `pkt-accordion-item--${u}`,
21
- h
21
+ g
22
22
  ].filter(Boolean).join(" ");
23
23
  return /* @__PURE__ */ s("details", {
24
- ref: v,
25
- className: O,
24
+ ref: y,
25
+ className: j,
26
26
  id: c,
27
- open: T,
28
- onClick: D,
29
- name: C,
30
- onToggle: E,
31
- "data-mode": w && e.includes(w) ? "light" : void 0,
27
+ open: O,
28
+ onClick: A,
29
+ name: T,
30
+ onToggle: k,
31
+ "data-mode": E && e.includes(E) ? "light" : void 0,
32
32
  children: [/* @__PURE__ */ s("summary", {
33
33
  className: "pkt-accordion-item__title",
34
34
  id: `pkt-accordion-item-summary-${c}`,
35
- children: [l, w === "plus-minus" ? /* @__PURE__ */ s(a, { children: [/* @__PURE__ */ o(t, {
35
+ children: [l, D ? /* @__PURE__ */ s(a, { children: [/* @__PURE__ */ o(t, {
36
36
  name: "plus-sign",
37
37
  className: "pkt-accordion-item__icon pkt-accordion-item__icon--plus",
38
38
  "aria-hidden": "true"
@@ -51,7 +51,7 @@ var c = r(({ defaultOpen: r = !1, id: c, title: l, skin: u, compact: d = !1, isO
51
51
  role: "region",
52
52
  children: /* @__PURE__ */ o("div", {
53
53
  className: "pkt-accordion-item__content-inner",
54
- children: p
54
+ children: m
55
55
  })
56
56
  })]
57
57
  });
@@ -1,51 +1,79 @@
1
1
  "use client";
2
2
  import { PktIcon as e } from "../icon/Icon.js";
3
- import { PktTag as t } from "../tag/Tag.js";
4
- import { useTabsContext as n } from "./Tabs.js";
5
- import { forwardRef as r } from "react";
6
- import { Fragment as i, jsx as a, jsxs as o } from "react/jsx-runtime";
3
+ import { syncTabPanel as t, tabIdFor as n } from "../../shared-utils/tabs/panels.js";
4
+ import { PktTag as r } from "../tag/Tag.js";
5
+ import { useTabsContext as i } from "./Tabs.js";
6
+ import { forwardRef as a, useEffect as o, useRef as s } from "react";
7
+ import { Fragment as c, jsx as l, jsxs as u } from "react/jsx-runtime";
7
8
  // src/components/tabs/TabItem.tsx
8
- var s = r(({ children: r, active: s, disabled: c = !1, href: l, onClick: u, icon: d, controls: f, tag: p, tagSkin: m, index: h = 0 }, g) => {
9
- let { arrowNav: _, registerTabRef: v, handleKeyPress: y, selectTab: b } = n(), x = !!s && !c, S = {
10
- "aria-selected": _ ? x : void 0,
11
- "aria-controls": f,
12
- role: _ ? "tab" : void 0,
9
+ var d = a(({ children: a, active: d, disabled: f = !1, href: p, onClick: m, icon: h, controls: g, tag: _, tagSkin: v = "blue", index: y = 0 }, b) => {
10
+ let { arrowNav: x, togglePanels: S, selectedIndex: C, setSelectedIndex: w, fallbackFocusIndex: T, reportTab: E, registerTabRef: D, handleKeyPress: O, selectTab: k } = i(), A = !f && (S && C !== null ? C === y : !!d), j = A || T === y, M = S && g ? n(g) : void 0, N = !x && A ? p ? "page" : "true" : void 0, P = s(null);
11
+ o(() => {
12
+ S && d && !f && w(y);
13
+ }, [
14
+ S,
15
+ d,
16
+ f,
17
+ y,
18
+ w
19
+ ]), o(() => (E(y, {
20
+ active: A,
21
+ disabled: f
22
+ }), () => E(y, null)), [
23
+ y,
24
+ A,
25
+ f,
26
+ E
27
+ ]), o(() => {
28
+ S && g && P.current && t(P.current, g, A, x);
29
+ }, [
30
+ S,
31
+ g,
32
+ A,
33
+ x
34
+ ]);
35
+ let F = {
36
+ id: M,
37
+ "aria-selected": x ? A : void 0,
38
+ "aria-current": N,
39
+ "aria-controls": g,
40
+ role: x ? "tab" : void 0,
13
41
  onKeyDown: (e) => {
14
- c && (e.key === "Enter" || e.key === " " || e.key === "Spacebar") ? (e.preventDefault(), e.stopPropagation()) : y(h, e);
42
+ f && (e.key === "Enter" || e.key === " " || e.key === "Spacebar") ? (e.preventDefault(), e.stopPropagation()) : O(y, e);
15
43
  },
16
44
  onClick: (e) => {
17
- c ? (e.preventDefault(), e.stopPropagation()) : (b(h), u?.(e));
45
+ f ? (e.preventDefault(), e.stopPropagation()) : (k(y), m?.(e));
18
46
  },
19
- tabIndex: c ? -1 : x || !_ ? void 0 : -1,
47
+ tabIndex: f ? -1 : j || !x ? void 0 : -1,
20
48
  ref: (e) => {
21
- v(h, e, c), typeof g == "function" ? g(e) : g && (g.current = e);
49
+ P.current = e, D(y, e, f), typeof b == "function" ? b(e) : b && (b.current = e);
22
50
  }
23
- }, C = /* @__PURE__ */ o(i, { children: [
24
- d && /* @__PURE__ */ a(e, {
25
- name: d,
26
- className: `pkt-icon--small ${d === "check" ? "pkt-tabs__status-icon" : ""}`
51
+ }, I = /* @__PURE__ */ u(c, { children: [
52
+ h && /* @__PURE__ */ l(e, {
53
+ name: h,
54
+ className: `pkt-icon--small ${h === "check" ? "pkt-tabs__status-icon" : ""}`
27
55
  }),
28
- r,
29
- p && /* @__PURE__ */ a(t, {
30
- skin: m,
56
+ a,
57
+ _ && /* @__PURE__ */ l(r, {
58
+ skin: v,
31
59
  size: "small",
32
- children: p
60
+ children: _
33
61
  })
34
62
  ] });
35
- return l ? /* @__PURE__ */ a("a", {
36
- ...S,
37
- href: c ? void 0 : l,
38
- "aria-disabled": c || void 0,
39
- className: `pkt-tabs__link ${x ? "active" : ""} ${c ? "pkt-tabs__item--disabled" : ""}`,
40
- children: C
41
- }) : /* @__PURE__ */ a("button", {
42
- ...S,
63
+ return p ? /* @__PURE__ */ l("a", {
64
+ ...F,
65
+ href: f ? void 0 : p,
66
+ "aria-disabled": f || void 0,
67
+ className: `pkt-tabs__link ${A ? "active" : ""} ${f ? "pkt-tabs__item--disabled" : ""}`,
68
+ children: I
69
+ }) : /* @__PURE__ */ l("button", {
70
+ ...F,
43
71
  type: "button",
44
- disabled: c,
45
- "aria-disabled": c || void 0,
46
- className: `pkt-tabs__button pkt-link-button ${x ? "active" : ""} ${c ? "pkt-tabs__item--disabled" : ""}`,
47
- children: C
72
+ disabled: f,
73
+ "aria-disabled": f || void 0,
74
+ className: `pkt-tabs__button pkt-link-button ${A ? "active" : ""} ${f ? "pkt-tabs__item--disabled" : ""}`,
75
+ children: I
48
76
  });
49
77
  });
50
- s.displayName = "PktTabItem";
51
- export { s as PktTabItem };
78
+ d.displayName = "PktTabItem";
79
+ export { d as PktTabItem };
@@ -1,7 +1,16 @@
1
1
  import { KeyboardEvent, ReactNode } from 'react';
2
2
  import { TTagSkin } from '../../shared-types';
3
+ type TTabState = {
4
+ active: boolean;
5
+ disabled: boolean;
6
+ };
3
7
  interface ITabsContext {
4
8
  arrowNav: boolean;
9
+ togglePanels: boolean;
10
+ selectedIndex: number | null;
11
+ setSelectedIndex: (index: number) => void;
12
+ fallbackFocusIndex: number | null;
13
+ reportTab: (index: number, state: TTabState | null) => void;
5
14
  registerTabRef: (index: number, el: HTMLAnchorElement | HTMLButtonElement | null, disabled?: boolean) => void;
6
15
  handleKeyPress: (index: number, event: KeyboardEvent<HTMLAnchorElement | HTMLButtonElement>) => void;
7
16
  selectTab: (index: number) => void;
@@ -25,6 +34,9 @@ export interface IPktTabs {
25
34
  disableArrowNav?: boolean;
26
35
  separatorIconName?: string;
27
36
  separatorIconPath?: string;
37
+ togglePanels?: boolean;
38
+ ariaLabel?: string;
39
+ ariaLabelledBy?: string;
28
40
  tabs?: IPktTab[];
29
41
  onTabSelected?: (index: number) => void;
30
42
  children?: ReactNode;
@@ -1,62 +1,53 @@
1
1
  "use client";
2
- import { PktIcon as e } from "../icon/Icon.js";
3
- import { PktTabItem as t } from "./TabItem.js";
4
- import { withStableKeys as n } from "../../utils/stableKeys.js";
5
- import { Children as r, createContext as i, forwardRef as a, useContext as o, useEffect as s, useRef as c } from "react";
6
- import { jsx as l } from "react/jsx-runtime";
2
+ import { tabIndexForKey as e } from "../../shared-utils/tabs/keyboard.js";
3
+ import { PktIcon as t } from "../icon/Icon.js";
4
+ import { PktTabItem as n } from "./TabItem.js";
5
+ import { withStableKeys as r } from "../../utils/stableKeys.js";
6
+ import { Children as i, createContext as a, forwardRef as o, useCallback as s, useContext as c, useEffect as l, useRef as u, useState as d } from "react";
7
+ import { jsx as f } from "react/jsx-runtime";
7
8
  // src/components/tabs/Tabs.tsx
8
- var u = i(null), d = () => {
9
- let e = o(u);
9
+ var p = a(null), m = () => {
10
+ let e = c(p);
10
11
  if (!e) throw Error("TabItem must be used within a Tabs component");
11
12
  return e;
12
- }, f = a(({ arrowNav: i = !0, disableArrowNav: a = !1, separatorIconName: o, separatorIconPath: d, tabs: f, onTabSelected: p, children: m }, h) => {
13
- let g = c([]), _ = c({}), v = i && !a, y = r.toArray(m), b = y.length > 0, x = b ? y.length : f?.length || 0, S = !!(o || d);
14
- s(() => {
15
- g.current = g.current.slice(0, x), Object.keys(_.current).forEach((e) => {
13
+ }, h = o(({ arrowNav: a = !0, disableArrowNav: o = !1, separatorIconName: c, separatorIconPath: m, togglePanels: h = !1, ariaLabel: g, ariaLabelledBy: _, tabs: v, onTabSelected: y, children: b }, x) => {
14
+ let S = u([]), C = u({}), [w, T] = d(null), [E, D] = d(null), O = s((e, t) => {
15
+ D((n) => {
16
+ let r = n?.[e];
17
+ if (t ? r?.active === t.active && r?.disabled === t.disabled : !r) return n;
18
+ let i = { ...n };
19
+ return t ? i[e] = t : delete i[e], i;
20
+ });
21
+ }, []), k = E && !Object.values(E).some((e) => e.active) ? Object.keys(E).map(Number).filter((e) => !E[e].disabled).sort((e, t) => e - t)[0] ?? null : null, A = a && !o, j = i.toArray(b), M = j.length > 0, N = M ? j.length : v?.length || 0, P = !!(c || m);
22
+ l(() => {
23
+ S.current = S.current.slice(0, N), Object.keys(C.current).forEach((e) => {
16
24
  let t = Number(e);
17
- t >= x && delete _.current[t];
25
+ t >= N && delete C.current[t];
18
26
  });
19
- }, [x]);
20
- let C = (e) => f ? !!f[e]?.disabled : !!_.current[e], w = (e) => {
21
- if (C(e)) return;
22
- let t = f?.[e];
23
- t?.action && t.action(e), p && p(e);
24
- }, T = (e, t) => {
25
- let n = e + t;
26
- for (; n >= 0 && n < x;) {
27
- if (!C(n)) return n;
28
- n += t;
29
- }
30
- return null;
31
- }, E = (e, t) => {
32
- if (v) {
33
- if (t.key === "ArrowLeft") {
34
- t.preventDefault();
35
- let n = T(e, -1);
36
- n !== null && g.current[n]?.focus();
37
- }
38
- if (t.key === "ArrowRight") {
39
- t.preventDefault();
40
- let n = T(e, 1);
41
- n !== null && g.current[n]?.focus();
42
- }
43
- (t.key === "Enter" || t.key === " " || t.key === "Spacebar" || t.key === "ArrowDown") && (t.preventDefault(), C(e) || w(e));
44
- }
45
- }, D = (e, t, n = !1) => {
46
- g.current[e] = t, _.current[e] = n;
47
- }, O = (t) => /* @__PURE__ */ l("span", {
27
+ }, [N]);
28
+ let F = (e) => v ? !!v[e]?.disabled : !!C.current[e], I = (e) => {
29
+ if (F(e)) return;
30
+ let t = v?.[e];
31
+ t?.action && t.action(e), y && y(e), h && T(e);
32
+ }, L = (t, n) => {
33
+ if (!A) return;
34
+ let r = e(n.key, t, N, F);
35
+ r === null ? (n.key === "Enter" || n.key === " " || n.key === "Spacebar" || n.key === "ArrowDown") && (n.preventDefault(), F(t) || I(t)) : (n.preventDefault(), S.current[r]?.focus(), h && r !== t && I(r));
36
+ }, R = (e, t, n = !1) => {
37
+ S.current[e] = t, C.current[e] = n;
38
+ }, z = (e) => /* @__PURE__ */ f("span", {
48
39
  className: "pkt-tabs__separator",
49
40
  "aria-hidden": "true",
50
41
  role: "presentation",
51
- children: d ? /* @__PURE__ */ l("img", {
52
- src: d,
42
+ children: m ? /* @__PURE__ */ f("img", {
43
+ src: m,
53
44
  alt: "",
54
45
  "aria-hidden": "true"
55
- }) : o ? /* @__PURE__ */ l(e, {
56
- name: o,
46
+ }) : c ? /* @__PURE__ */ f(t, {
47
+ name: c,
57
48
  className: "pkt-tabs__separator-icon"
58
49
  }) : null
59
- }, t), k = (e) => !S || e.length <= 1 ? e : e.flatMap((t, n) => n < e.length - 1 ? [t, O(`separator-${n}`)] : [t]), A = n(f ?? [], (e) => `${e.href ?? ""}|${e.text}`).map(({ item: e, key: n }, r) => /* @__PURE__ */ l(t, {
50
+ }, e), B = (e) => !P || e.length <= 1 ? e : e.flatMap((t, n) => n < e.length - 1 ? [t, z(`separator-${n}`)] : [t]), V = r(v ?? [], (e) => `${e.href ?? ""}|${e.text}`).map(({ item: e, key: t }, r) => /* @__PURE__ */ f(n, {
60
51
  active: !e.disabled && e.active,
61
52
  disabled: e.disabled,
62
53
  href: e.href,
@@ -66,24 +57,31 @@ var u = i(null), d = () => {
66
57
  tagSkin: e.tag?.skin,
67
58
  index: r,
68
59
  children: e.text
69
- }, n)), j = ["pkt-tabs", S && "pkt-tabs--with-separator"].filter(Boolean).join(" "), M = k(b ? y : A || []);
70
- return /* @__PURE__ */ l(u.Provider, {
60
+ }, t)), H = ["pkt-tabs", P && "pkt-tabs--with-separator"].filter(Boolean).join(" "), U = B(M ? j : V || []);
61
+ return /* @__PURE__ */ f(p.Provider, {
71
62
  value: {
72
- arrowNav: v,
73
- registerTabRef: D,
74
- handleKeyPress: E,
75
- selectTab: w
63
+ arrowNav: A,
64
+ togglePanels: h,
65
+ selectedIndex: w,
66
+ setSelectedIndex: T,
67
+ fallbackFocusIndex: k,
68
+ reportTab: O,
69
+ registerTabRef: R,
70
+ handleKeyPress: L,
71
+ selectTab: I
76
72
  },
77
- children: /* @__PURE__ */ l("div", {
78
- className: j,
79
- ref: h,
80
- children: /* @__PURE__ */ l("div", {
73
+ children: /* @__PURE__ */ f("div", {
74
+ className: H,
75
+ ref: x,
76
+ children: /* @__PURE__ */ f("div", {
81
77
  className: "pkt-tabs__list",
82
- role: v ? "tablist" : "navigation",
83
- children: M
78
+ role: A ? "tablist" : "navigation",
79
+ "aria-label": g || void 0,
80
+ "aria-labelledby": _ || void 0,
81
+ children: U
84
82
  })
85
83
  })
86
84
  });
87
85
  });
88
- f.displayName = "PktTabs";
89
- export { f as PktTabs, d as useTabsContext };
86
+ h.displayName = "PktTabs";
87
+ export { h as PktTabs, m as useTabsContext };