@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 +8 -0
- package/dist/components/tabs/TabItem.js +63 -35
- package/dist/components/tabs/Tabs.d.ts +12 -0
- package/dist/components/tabs/Tabs.js +58 -60
- package/dist/punkt-react.cjs +1 -1
- package/dist/shared-utils/tabs/keyboard.d.ts +1 -0
- package/dist/shared-utils/tabs/keyboard.js +14 -0
- package/dist/shared-utils/tabs/panels.d.ts +2 -0
- package/dist/shared-utils/tabs/panels.js +7 -0
- package/package.json +3 -3
- package/src/components/tabs/TabItem.tsx +40 -6
- package/src/components/tabs/Tabs.test.tsx +425 -5
- package/src/components/tabs/Tabs.tsx +63 -22
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 {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
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
|
|
9
|
-
let { arrowNav:
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
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
|
-
|
|
42
|
+
f && (e.key === "Enter" || e.key === " " || e.key === "Spacebar") ? (e.preventDefault(), e.stopPropagation()) : O(y, e);
|
|
15
43
|
},
|
|
16
44
|
onClick: (e) => {
|
|
17
|
-
|
|
45
|
+
f ? (e.preventDefault(), e.stopPropagation()) : (k(y), m?.(e));
|
|
18
46
|
},
|
|
19
|
-
tabIndex:
|
|
47
|
+
tabIndex: f ? -1 : j || !x ? void 0 : -1,
|
|
20
48
|
ref: (e) => {
|
|
21
|
-
|
|
49
|
+
P.current = e, D(y, e, f), typeof b == "function" ? b(e) : b && (b.current = e);
|
|
22
50
|
}
|
|
23
|
-
},
|
|
24
|
-
|
|
25
|
-
name:
|
|
26
|
-
className: `pkt-icon--small ${
|
|
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
|
-
|
|
29
|
-
|
|
30
|
-
skin:
|
|
56
|
+
a,
|
|
57
|
+
_ && /* @__PURE__ */ l(r, {
|
|
58
|
+
skin: v,
|
|
31
59
|
size: "small",
|
|
32
|
-
children:
|
|
60
|
+
children: _
|
|
33
61
|
})
|
|
34
62
|
] });
|
|
35
|
-
return
|
|
36
|
-
...
|
|
37
|
-
href:
|
|
38
|
-
"aria-disabled":
|
|
39
|
-
className: `pkt-tabs__link ${
|
|
40
|
-
children:
|
|
41
|
-
}) : /* @__PURE__ */
|
|
42
|
-
...
|
|
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:
|
|
45
|
-
"aria-disabled":
|
|
46
|
-
className: `pkt-tabs__button pkt-link-button ${
|
|
47
|
-
children:
|
|
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
|
-
|
|
51
|
-
export {
|
|
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 {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
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
|
|
9
|
-
let e =
|
|
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
|
-
},
|
|
13
|
-
let
|
|
14
|
-
|
|
15
|
-
|
|
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 >=
|
|
25
|
+
t >= N && delete C.current[t];
|
|
18
26
|
});
|
|
19
|
-
}, [
|
|
20
|
-
let
|
|
21
|
-
if (
|
|
22
|
-
let t =
|
|
23
|
-
t?.action && t.action(e),
|
|
24
|
-
},
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
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:
|
|
52
|
-
src:
|
|
42
|
+
children: m ? /* @__PURE__ */ f("img", {
|
|
43
|
+
src: m,
|
|
53
44
|
alt: "",
|
|
54
45
|
"aria-hidden": "true"
|
|
55
|
-
}) :
|
|
56
|
-
name:
|
|
46
|
+
}) : c ? /* @__PURE__ */ f(t, {
|
|
47
|
+
name: c,
|
|
57
48
|
className: "pkt-tabs__separator-icon"
|
|
58
49
|
}) : null
|
|
59
|
-
},
|
|
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
|
-
},
|
|
70
|
-
return /* @__PURE__ */
|
|
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:
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
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__ */
|
|
78
|
-
className:
|
|
79
|
-
ref:
|
|
80
|
-
children: /* @__PURE__ */
|
|
73
|
+
children: /* @__PURE__ */ f("div", {
|
|
74
|
+
className: H,
|
|
75
|
+
ref: x,
|
|
76
|
+
children: /* @__PURE__ */ f("div", {
|
|
81
77
|
className: "pkt-tabs__list",
|
|
82
|
-
role:
|
|
83
|
-
|
|
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
|
-
|
|
89
|
-
export {
|
|
86
|
+
h.displayName = "PktTabs";
|
|
87
|
+
export { h as PktTabs, m as useTabsContext };
|