@oslokommune/punkt-react 20.1.7 → 20.2.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.
@@ -49,6 +49,8 @@ export type { TPktCounterPosition, IPktInputWrapperBase, IInputWrapperSpecProps,
49
49
  export type { TMessageboxSkin } from './skin';
50
50
  export { TAG_SKINS, TAG_STATES, TAG_TEXT_STYLES, TAG_DEFAULTS } from './tag';
51
51
  export type { TTagSkin, TTagState, TTagTextStyle, IPktTagBase, ITagSpecProps } from './tag';
52
+ export { TABS_DEFAULTS, TAB_ITEM_DEFAULTS } from './tabs';
53
+ export type { IPktTab, IPktTabTag, IPktTabsBase, IPktTabItemBase, ITabsSpecProps, ITabItemSpecProps, } from './tabs';
52
54
  export { MESSAGEBOX_SKINS, MESSAGEBOX_LIGHT_ONLY_SKINS, MESSAGEBOX_DEFAULTS } from './messagebox';
53
55
  export type { IPktMessageboxBase, IMessageboxSpecProps } from './messagebox';
54
56
  export { CALENDAR_DEFAULTS } from './calendar';
@@ -15,11 +15,12 @@ import { INPUT_WRAPPER_DEFAULTS as g } from "./input-wrapper.js";
15
15
  import { TEXTAREA_DEFAULTS as _ } from "./textarea.js";
16
16
  import { TEXTINPUT_DEFAULTS as v } from "./textinput.js";
17
17
  import { TAG_DEFAULTS as y } from "./tag.js";
18
- import { MESSAGEBOX_DEFAULTS as b, MESSAGEBOX_LIGHT_ONLY_SKINS as x } from "./messagebox.js";
18
+ import { TABS_DEFAULTS as b, TAB_ITEM_DEFAULTS as x } from "./tabs.js";
19
+ import { MESSAGEBOX_DEFAULTS as S, MESSAGEBOX_LIGHT_ONLY_SKINS as C } from "./messagebox.js";
19
20
  import "./calendar.js";
20
- import { DATEPICKER_DEFAULTS as S } from "./datepicker.js";
21
- import { PROGRESSBAR_DEFAULTS as C } from "./progressbar.js";
21
+ import { DATEPICKER_DEFAULTS as w } from "./datepicker.js";
22
+ import { PROGRESSBAR_DEFAULTS as T } from "./progressbar.js";
22
23
  import "./combobox.js";
23
- import { SELECT_DEFAULTS as w } from "./select.js";
24
- import { TABLE_DEFAULTS as T } from "./table.js";
25
- export { p as ACCORDION_LIGHT_ONLY_SKINS, u as ANIMATION_DEFAULT_PATH, m as BUTTON_DEFAULTS, i as CARD_DEFAULTS, a as CARD_LIGHT_ONLY_SKINS, o as CHECKBOX_DEFAULTS, l as CONSENT_DEFAULTS, S as DATEPICKER_DEFAULTS, n as DEFAULT_FOOTER_PERSONVERN_URL, r as DEFAULT_FOOTER_TILGJENGELIGHET_URL, e as DEFAULT_HEADER_FOOTER_URL, t as DEFAULT_HEADER_LOGO_LINK, d as ICON_DEFAULTS, g as INPUT_WRAPPER_DEFAULTS, h as LINKCARD_LIGHT_ONLY_SKINS, f as LOADER_DEFAULTS, b as MESSAGEBOX_DEFAULTS, x as MESSAGEBOX_LIGHT_ONLY_SKINS, c as OPTION_GROUP_DEFAULTS, C as PROGRESSBAR_DEFAULTS, s as RADIOBUTTON_DEFAULTS, w as SELECT_DEFAULTS, T as TABLE_DEFAULTS, y as TAG_DEFAULTS, _ as TEXTAREA_DEFAULTS, v as TEXTINPUT_DEFAULTS };
24
+ import { SELECT_DEFAULTS as E } from "./select.js";
25
+ import { TABLE_DEFAULTS as D } from "./table.js";
26
+ export { p as ACCORDION_LIGHT_ONLY_SKINS, u as ANIMATION_DEFAULT_PATH, m as BUTTON_DEFAULTS, i as CARD_DEFAULTS, a as CARD_LIGHT_ONLY_SKINS, o as CHECKBOX_DEFAULTS, l as CONSENT_DEFAULTS, w as DATEPICKER_DEFAULTS, n as DEFAULT_FOOTER_PERSONVERN_URL, r as DEFAULT_FOOTER_TILGJENGELIGHET_URL, e as DEFAULT_HEADER_FOOTER_URL, t as DEFAULT_HEADER_LOGO_LINK, d as ICON_DEFAULTS, g as INPUT_WRAPPER_DEFAULTS, h as LINKCARD_LIGHT_ONLY_SKINS, f as LOADER_DEFAULTS, S as MESSAGEBOX_DEFAULTS, C as MESSAGEBOX_LIGHT_ONLY_SKINS, c as OPTION_GROUP_DEFAULTS, T as PROGRESSBAR_DEFAULTS, s as RADIOBUTTON_DEFAULTS, E as SELECT_DEFAULTS, D as TABLE_DEFAULTS, b as TABS_DEFAULTS, x as TAB_ITEM_DEFAULTS, y as TAG_DEFAULTS, _ as TEXTAREA_DEFAULTS, v as TEXTINPUT_DEFAULTS };
@@ -0,0 +1,66 @@
1
+ /**
2
+ * tabs.ts
3
+ *
4
+ * Shared types and defaults for pkt-tabs and pkt-tab-item.
5
+ * Used by React, Elements, and typed component specs.
6
+ *
7
+ * @remarks React also takes a `tabs` array (`IPktTab[]`) instead of tab item
8
+ * children. Elements only takes `pkt-tab-item` children. Elements widens
9
+ * `ariaLabel` to `string | null`, like the native `ariaLabel` property.
10
+ */
11
+ import type { TTagSkin } from './tag';
12
+ /** The tag on a tab in React's `tabs` array. */
13
+ export interface IPktTabTag {
14
+ text: string;
15
+ skin?: TTagSkin;
16
+ }
17
+ /** One tab in React's `tabs` array. */
18
+ export interface IPktTab {
19
+ text: string;
20
+ href?: string;
21
+ action?: (index: number) => void;
22
+ disabled?: boolean;
23
+ icon?: string;
24
+ controls?: string;
25
+ tag?: IPktTabTag;
26
+ active?: boolean;
27
+ }
28
+ /** Tabs props shared by React, Elements, and typed specs. */
29
+ export interface IPktTabsBase {
30
+ arrowNav?: boolean;
31
+ disableArrowNav?: boolean;
32
+ separatorIconName?: string;
33
+ separatorIconPath?: string;
34
+ togglePanels?: boolean;
35
+ ariaLabel?: string;
36
+ ariaLabelledBy?: string;
37
+ }
38
+ /** Tab item props shared by React, Elements, and typed specs. */
39
+ export interface IPktTabItemBase {
40
+ active?: boolean;
41
+ disabled?: boolean;
42
+ href?: string;
43
+ icon?: string;
44
+ controls?: string;
45
+ tag?: string;
46
+ tagSkin?: TTagSkin;
47
+ index?: number;
48
+ }
49
+ /** Props documented in the tabs spec. */
50
+ export type ITabsSpecProps = Pick<IPktTabsBase, 'arrowNav' | 'disableArrowNav' | 'separatorIconName' | 'separatorIconPath' | 'togglePanels' | 'ariaLabelledBy' | 'ariaLabel'> & {
51
+ /** React only. Elements takes `pkt-tab-item` children. */
52
+ tabs?: IPktTab[];
53
+ };
54
+ /** Props documented in the tab item spec. */
55
+ export type ITabItemSpecProps = Pick<IPktTabItemBase, 'active' | 'disabled' | 'href' | 'icon' | 'tag' | 'tagSkin' | 'index' | 'controls'>;
56
+ export declare const TABS_DEFAULTS: {
57
+ readonly arrowNav: true;
58
+ readonly disableArrowNav: false;
59
+ readonly togglePanels: false;
60
+ };
61
+ export declare const TAB_ITEM_DEFAULTS: {
62
+ readonly active: false;
63
+ readonly disabled: false;
64
+ readonly tagSkin: 'blue';
65
+ readonly index: 0;
66
+ };
@@ -0,0 +1,12 @@
1
+ // ../../shared-types/tabs.ts
2
+ var e = {
3
+ arrowNav: !0,
4
+ disableArrowNav: !1,
5
+ togglePanels: !1
6
+ }, t = {
7
+ active: !1,
8
+ disabled: !1,
9
+ tagSkin: "blue",
10
+ index: 0
11
+ };
12
+ export { e as TABS_DEFAULTS, t as TAB_ITEM_DEFAULTS };
@@ -0,0 +1 @@
1
+ export declare function tabIndexForKey(key: string, current: number, count: number, isDisabled: (index: number) => boolean): number | null;
@@ -0,0 +1,14 @@
1
+ // ../../shared-utils/tabs/keyboard.ts
2
+ function e(e, t, n, r) {
3
+ let i = Array.from({ length: n }, (e, t) => t).filter((e) => !r(e));
4
+ if (!i.length) return null;
5
+ let a = i[0], o = i[i.length - 1];
6
+ switch (e) {
7
+ case "Home": return a;
8
+ case "End": return o;
9
+ case "ArrowRight": return i.find((e) => e > t) ?? a;
10
+ case "ArrowLeft": return [...i].reverse().find((e) => e < t) ?? o;
11
+ default: return null;
12
+ }
13
+ }
14
+ export { e as tabIndexForKey };
@@ -0,0 +1,2 @@
1
+ export declare const tabIdFor: (controls: string) => string;
2
+ export declare function syncTabPanel(tab: HTMLElement, controls: string, active: boolean, tabSemantics: boolean): void;
@@ -0,0 +1,7 @@
1
+ // ../../shared-utils/tabs/panels.ts
2
+ var e = (e) => `${e}-tab`;
3
+ function t(e, t, n, r) {
4
+ let i = e.getRootNode().getElementById?.(t);
5
+ i && (i.hidden = !n, r && (i.hasAttribute("role") || i.setAttribute("role", "tabpanel"), i.hasAttribute("tabindex") || i.setAttribute("tabindex", "0"), e.id && !i.hasAttribute("aria-labelledby") && !i.hasAttribute("aria-label") && i.setAttribute("aria-labelledby", e.id)));
6
+ }
7
+ export { t as syncTabPanel, e as tabIdFor };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@oslokommune/punkt-react",
3
- "version": "20.1.7",
3
+ "version": "20.2.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",
@@ -42,7 +42,7 @@
42
42
  "@eslint/eslintrc": "^3.3.5",
43
43
  "@eslint/js": "^10.0.1",
44
44
  "@oslokommune/punkt-assets": "^20.0.5",
45
- "@oslokommune/punkt-css": "^20.1.7",
45
+ "@oslokommune/punkt-css": "^20.2.0",
46
46
  "@testing-library/jest-dom": "^7.0.1",
47
47
  "@testing-library/react": "^16.3.3",
48
48
  "@testing-library/user-event": "^14.6.8",
@@ -102,5 +102,5 @@
102
102
  "url": "https://github.com/oslokommune/punkt/issues"
103
103
  },
104
104
  "license": "MIT",
105
- "gitHead": "2cda971b4a2832f62d45d829dfe8d8904da3326a"
105
+ "gitHead": "fc42bfbbb587bfd13f8a5d0e94e042cfc0d19695"
106
106
  }
@@ -25,7 +25,8 @@ export type { IPktRadioButton } from './radio/RadioButton'
25
25
  export type { IPktRadioGroup } from './radiogroup/RadioGroup'
26
26
  export type { IPktStep } from './stepper/Step'
27
27
  export type { IPktStepper } from './stepper/Stepper'
28
- export type { IPktTab } from './tabs/Tabs'
28
+ export type { IPktTab, IPktTabs } from './tabs/Tabs'
29
+ export type { IPktTabItem } from './tabs/TabItem'
29
30
  export type { IPktTag } from './tag/Tag'
30
31
  export type { IPktTextarea } from './textarea/Textarea'
31
32
  export type { IPktTextinput } from './textinput/Textinput'
@@ -1,30 +1,65 @@
1
1
  'use client'
2
2
 
3
- import { forwardRef, type KeyboardEvent, type MouseEvent, type ReactNode } from 'react'
3
+ import { forwardRef, type KeyboardEvent, type MouseEvent, type ReactNode, useEffect, useRef } from 'react'
4
4
 
5
5
  import { PktIcon } from '../icon/Icon'
6
- import type { TTagSkin } from 'shared-types'
6
+ import { TAB_ITEM_DEFAULTS, type IPktTabItemBase } from 'shared-types'
7
+ import { syncTabPanel, tabIdFor } from 'shared-utils/tabs/panels'
7
8
 
8
9
  import { PktTag } from '../tag/Tag'
9
10
  import { useTabsContext } from './Tabs'
10
11
 
11
- export interface IPktTabItem {
12
+ export interface IPktTabItem extends IPktTabItemBase {
12
13
  children: ReactNode
13
- active?: boolean
14
- disabled?: boolean
15
- href?: string
16
14
  onClick?: (event: MouseEvent) => void
17
- icon?: string
18
- controls?: string
19
- tag?: string
20
- tagSkin?: TTagSkin
21
- index?: number
22
15
  }
23
16
 
24
17
  export const PktTabItem = forwardRef<HTMLAnchorElement | HTMLButtonElement, IPktTabItem>(
25
- ({ children, active, disabled = false, href, onClick, icon, controls, tag, tagSkin, index = 0 }, ref) => {
26
- const { arrowNav, registerTabRef, handleKeyPress, selectTab } = useTabsContext()
27
- const isActive = !!active && !disabled
18
+ (
19
+ {
20
+ children,
21
+ active = TAB_ITEM_DEFAULTS.active,
22
+ disabled = TAB_ITEM_DEFAULTS.disabled,
23
+ href,
24
+ onClick,
25
+ icon,
26
+ controls,
27
+ tag,
28
+ tagSkin = TAB_ITEM_DEFAULTS.tagSkin,
29
+ index = TAB_ITEM_DEFAULTS.index,
30
+ },
31
+ ref,
32
+ ) => {
33
+ const {
34
+ arrowNav,
35
+ togglePanels,
36
+ selectedIndex,
37
+ setSelectedIndex,
38
+ fallbackFocusIndex,
39
+ reportTab,
40
+ registerTabRef,
41
+ handleKeyPress,
42
+ selectTab,
43
+ } = useTabsContext()
44
+ const isActive = !disabled && (togglePanels && selectedIndex !== null ? selectedIndex === index : !!active)
45
+ const isFocusable = isActive || fallbackFocusIndex === index
46
+ const tabId = togglePanels && controls ? tabIdFor(controls) : undefined
47
+ const ariaCurrent: 'page' | 'true' | undefined = !arrowNav && isActive ? (href ? 'page' : 'true') : undefined
48
+ const elementRef = useRef<HTMLAnchorElement | HTMLButtonElement | null>(null)
49
+
50
+ useEffect(() => {
51
+ if (togglePanels && active && !disabled) setSelectedIndex(index)
52
+ }, [togglePanels, active, disabled, index, setSelectedIndex])
53
+
54
+ useEffect(() => {
55
+ reportTab(index, { active: isActive, disabled })
56
+ return () => reportTab(index, null)
57
+ }, [index, isActive, disabled, reportTab])
58
+
59
+ useEffect(() => {
60
+ if (!togglePanels || !controls || !elementRef.current) return
61
+ syncTabPanel(elementRef.current, controls, isActive, arrowNav)
62
+ }, [togglePanels, controls, isActive, arrowNav])
28
63
 
29
64
  const handleClick = (event: MouseEvent<HTMLAnchorElement | HTMLButtonElement>) => {
30
65
  if (disabled) {
@@ -47,14 +82,17 @@ export const PktTabItem = forwardRef<HTMLAnchorElement | HTMLButtonElement, IPkt
47
82
  }
48
83
 
49
84
  const commonProps = {
85
+ id: tabId,
50
86
  'aria-selected': arrowNav ? isActive : undefined,
87
+ 'aria-current': ariaCurrent,
51
88
  'aria-controls': controls,
52
89
  role: arrowNav ? 'tab' : undefined,
53
90
  onKeyDown: handleKeyDown,
54
91
  onClick: handleClick,
55
- tabIndex: disabled ? -1 : isActive || !arrowNav ? undefined : -1,
92
+ tabIndex: disabled ? -1 : isFocusable || !arrowNav ? undefined : -1,
56
93
  ref: (el: HTMLAnchorElement | HTMLButtonElement | null) => {
57
- registerTabRef(index, el, disabled)
94
+ elementRef.current = el
95
+ registerTabRef(index, el)
58
96
  if (typeof ref === 'function') {
59
97
  ref(el)
60
98
  } else if (ref) {
@@ -65,7 +103,9 @@ export const PktTabItem = forwardRef<HTMLAnchorElement | HTMLButtonElement, IPkt
65
103
 
66
104
  const content = (
67
105
  <>
68
- {icon && <PktIcon name={icon} className={`pkt-icon--small ${icon === 'check' ? 'pkt-tabs__status-icon' : ''}`} />}
106
+ {icon && (
107
+ <PktIcon name={icon} className={`pkt-icon--small ${icon === 'check' ? 'pkt-tabs__status-icon' : ''}`} />
108
+ )}
69
109
  {children}
70
110
  {tag && (
71
111
  <PktTag skin={tagSkin} size="small">