@oslokommune/punkt-react 20.1.7 → 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,14 @@ 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
+
8
16
  ## [20.1.7](https://github.com/oslokommune/punkt/compare/20.1.6...20.1.7) (2026-11-14)
9
17
 
10
18
  ---
@@ -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 };