@oslokommune/punkt-react 19.19.1 → 19.20.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.
@@ -80,6 +80,9 @@ export declare const nb: {
80
80
  tag: {
81
81
  remove: string;
82
82
  };
83
+ progressbar: {
84
+ fraction: string;
85
+ };
83
86
  header: {
84
87
  search: string;
85
88
  menu: string;
@@ -109,6 +109,7 @@ var e = {
109
109
  tagsLabelOther: "merkelapper"
110
110
  },
111
111
  tag: { remove: "Klikk for å fjerne {label}" },
112
+ progressbar: { fraction: "{current} av {total}" },
112
113
  header: {
113
114
  search: "Søk",
114
115
  menu: "Meny",
@@ -55,7 +55,8 @@ export type { IDatepickerStrings } from './datepicker';
55
55
  export { defaultSingleStrings, defaultRangeStrings } from './datepicker';
56
56
  export type { ITimepickerStrings } from './timepicker';
57
57
  export { defaultTimepickerStrings } from './timepicker';
58
- export type { TProgressbarRole, TProgressbarSkin, TProgressbarStatusPlacement, TProgressbarStatusType, TProgressbarTitlePosition, } from './progressbar';
58
+ export { PROGRESSBAR_DEFAULTS, PROGRESSBAR_ROLES, PROGRESSBAR_SKINS, PROGRESSBAR_STATUS_PLACEMENTS, PROGRESSBAR_STATUS_TYPES, PROGRESSBAR_TITLE_POSITIONS, } from './progressbar';
59
+ export type { IPktProgressbarBase, IProgressbarSpecProps, TProgressbarRole, TProgressbarSkin, TProgressbarStatusPlacement, TProgressbarStatusType, TProgressbarTitlePosition, } from './progressbar';
59
60
  export { COMBOBOX_TAG_SKINS, COMBOBOX_DISPLAY_VALUES, COMBOBOX_TAG_PLACEMENTS, COMBOBOX_DEFAULTS, } from './combobox';
60
61
  export type { IPktComboboxOption, TPktComboboxTagSkin, TPktComboboxDisplayValue, TPktComboboxTagPlacement, IPktComboboxBase, IComboboxSpecProps, } from './combobox';
61
62
  export { SELECT_DEFAULTS } from './select';
@@ -14,7 +14,8 @@ import { TEXTAREA_DEFAULTS as m } from "./textarea.js";
14
14
  import { TEXTINPUT_DEFAULTS as h } from "./textinput.js";
15
15
  import { MESSAGEBOX_DEFAULTS as g } from "./messagebox.js";
16
16
  import "./calendar.js";
17
+ import { PROGRESSBAR_DEFAULTS as _ } from "./progressbar.js";
17
18
  import "./combobox.js";
18
- import { SELECT_DEFAULTS as _ } from "./select.js";
19
- import { TABLE_DEFAULTS as v } from "./table.js";
20
- export { f as ACCORDION_LIGHT_ONLY_SKINS, l as ANIMATION_DEFAULT_PATH, i as CARD_DEFAULTS, a as CHECKBOX_DEFAULTS, c as CONSENT_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, u as ICON_DEFAULTS, p as INPUT_WRAPPER_DEFAULTS, d as LOADER_DEFAULTS, g as MESSAGEBOX_DEFAULTS, s as OPTION_GROUP_DEFAULTS, o as RADIOBUTTON_DEFAULTS, _ as SELECT_DEFAULTS, v as TABLE_DEFAULTS, m as TEXTAREA_DEFAULTS, h as TEXTINPUT_DEFAULTS };
19
+ import { SELECT_DEFAULTS as v } from "./select.js";
20
+ import { TABLE_DEFAULTS as y } from "./table.js";
21
+ export { f as ACCORDION_LIGHT_ONLY_SKINS, l as ANIMATION_DEFAULT_PATH, i as CARD_DEFAULTS, a as CHECKBOX_DEFAULTS, c as CONSENT_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, u as ICON_DEFAULTS, p as INPUT_WRAPPER_DEFAULTS, d as LOADER_DEFAULTS, g as MESSAGEBOX_DEFAULTS, s as OPTION_GROUP_DEFAULTS, _ as PROGRESSBAR_DEFAULTS, o as RADIOBUTTON_DEFAULTS, v as SELECT_DEFAULTS, y as TABLE_DEFAULTS, m as TEXTAREA_DEFAULTS, h as TEXTINPUT_DEFAULTS };
@@ -1,5 +1,82 @@
1
- export type TProgressbarRole = 'progressbar' | 'meter';
2
- export type TProgressbarSkin = 'dark-blue' | 'light-blue' | 'green' | 'red';
3
- export type TProgressbarStatusPlacement = 'center' | 'left' | 'following';
4
- export type TProgressbarStatusType = 'none' | 'percentage' | 'fraction';
5
- export type TProgressbarTitlePosition = 'left' | 'center';
1
+ /**
2
+ * progressbar.ts
3
+ *
4
+ * Shared types and defaults for Progressbar, used by both React and Elements.
5
+ *
6
+ * The value lists are `as const` arrays rather than plain union types. That way
7
+ * the values also exist at runtime, so the component spec and Preview can use
8
+ * them directly (`type: [...PROGRESSBAR_SKINS]`) instead of repeating them.
9
+ * The types are derived from the arrays, so there is a single source.
10
+ */
11
+ import type { IPktStringsProps } from '../shared-strings';
12
+ import type { TAriaLive } from './aria';
13
+ /** Element role: `progressbar` for progress, `meter` for measuring a value. */
14
+ export declare const PROGRESSBAR_ROLES: readonly ['progressbar', 'meter'];
15
+ export type TProgressbarRole = (typeof PROGRESSBAR_ROLES)[number];
16
+ /** Color of the progress bar. */
17
+ export declare const PROGRESSBAR_SKINS: readonly ['dark-blue', 'light-blue', 'green', 'red'];
18
+ export type TProgressbarSkin = (typeof PROGRESSBAR_SKINS)[number];
19
+ /** Where the status text is placed relative to the bar. */
20
+ export declare const PROGRESSBAR_STATUS_PLACEMENTS: readonly ['left', 'following', 'center'];
21
+ export type TProgressbarStatusPlacement = (typeof PROGRESSBAR_STATUS_PLACEMENTS)[number];
22
+ /** How status is shown: none, percentage ("40%") or fraction ("4 av 10"). */
23
+ export declare const PROGRESSBAR_STATUS_TYPES: readonly ['none', 'percentage', 'fraction'];
24
+ export type TProgressbarStatusType = (typeof PROGRESSBAR_STATUS_TYPES)[number];
25
+ /** Alignment of the title above the bar. */
26
+ export declare const PROGRESSBAR_TITLE_POSITIONS: readonly ['left', 'center'];
27
+ export type TProgressbarTitlePosition = (typeof PROGRESSBAR_TITLE_POSITIONS)[number];
28
+ /**
29
+ * Framework-agnostic props for Progressbar.
30
+ *
31
+ * Contains no React or Lit types, so both packages can build on it:
32
+ * React extends it with `HTMLAttributes` and `ref`, Elements uses it as is.
33
+ */
34
+ export interface IPktProgressbarBase extends IPktStringsProps<'progressbar'> {
35
+ /** Accessible name. When set, `aria-labelledby` is not used. */
36
+ ariaLabel?: string | null;
37
+ /** Id of the element that labels the bar. Falls back to the title. */
38
+ ariaLabelledby?: string | null;
39
+ /** How assertively screen readers announce changes. */
40
+ ariaLive?: TAriaLive;
41
+ /** Custom text value. With `statusType="fraction"`, "X av Y" is generated when empty. */
42
+ ariaValueText?: string | null;
43
+ id?: string;
44
+ role?: TProgressbarRole;
45
+ skin?: TProgressbarSkin;
46
+ statusPlacement?: TProgressbarStatusPlacement;
47
+ statusType?: TProgressbarStatusType;
48
+ title?: string | null;
49
+ titlePosition?: TProgressbarTitlePosition;
50
+ /** Current value. Required, since the bar is meaningless without it. */
51
+ valueCurrent: number;
52
+ valueMax?: number;
53
+ valueMin?: number;
54
+ }
55
+ /**
56
+ * Props documented in the component spec (docs and Preview).
57
+ * The spec file is type-checked against this, so a prop that doesn't exist on
58
+ * `IPktProgressbarBase` is a compile error.
59
+ */
60
+ export type IProgressbarSpecProps = Pick<IPktProgressbarBase, 'skin' | 'title' | 'valueMin' | 'valueMax' | 'ariaLabel' | 'ariaLabelledby' | 'ariaValueText' | 'valueCurrent' | 'statusType' | 'statusPlacement' | 'id' | 'titlePosition' | 'role' | 'ariaLive'>;
61
+ /**
62
+ * Default values, shared by React and Elements.
63
+ *
64
+ * These used to be hardcoded in each package and in the JSON spec, and they had
65
+ * drifted apart (the spec said `statusType: 'percentage'`, the components `'none'`).
66
+ * Both components and the spec now read from here.
67
+ *
68
+ * `title` and `id` are left out on purpose: React and Elements handle them
69
+ * differently (Elements generates the id in the property initializer, React
70
+ * via `useGeneratedId`).
71
+ */
72
+ export declare const PROGRESSBAR_DEFAULTS: {
73
+ readonly ariaLive: 'polite';
74
+ readonly role: 'progressbar';
75
+ readonly skin: 'dark-blue';
76
+ readonly statusPlacement: 'following';
77
+ readonly statusType: 'none';
78
+ readonly titlePosition: 'left';
79
+ readonly valueCurrent: 0;
80
+ readonly valueMax: 100;
81
+ readonly valueMin: 0;
82
+ };
@@ -0,0 +1,14 @@
1
+ //#region ../../shared-types/progressbar.ts
2
+ var e = {
3
+ ariaLive: "polite",
4
+ role: "progressbar",
5
+ skin: "dark-blue",
6
+ statusPlacement: "following",
7
+ statusType: "none",
8
+ titlePosition: "left",
9
+ valueCurrent: 0,
10
+ valueMax: 100,
11
+ valueMin: 0
12
+ };
13
+ //#endregion
14
+ export { e as PROGRESSBAR_DEFAULTS };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@oslokommune/punkt-react",
3
- "version": "19.19.1",
3
+ "version": "19.20.0",
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",
@@ -103,5 +103,5 @@
103
103
  "url": "https://github.com/oslokommune/punkt/issues"
104
104
  },
105
105
  "license": "MIT",
106
- "gitHead": "8373ea76c93221f22496eb298b2382b41bef53ab"
106
+ "gitHead": "0d41efc82367542a5b09c49d9f5894b096c669d0"
107
107
  }
@@ -69,6 +69,31 @@ describe('PktProgressbar', () => {
69
69
  expect(status).toHaveTextContent('50 av 200')
70
70
  })
71
71
 
72
+ test('renders fraction relative to valueMin', () => {
73
+ const { container } = render(
74
+ <PktProgressbar id="progress-min" valueCurrent={25} valueMin={20} valueMax={30} statusType="fraction" />,
75
+ )
76
+ const bar = container.querySelector('.pkt-progressbar')
77
+ const barContainer = container.querySelector<HTMLElement>('.pkt-progressbar__container')
78
+ expect(container.querySelector('.pkt-progressbar__status')).toHaveTextContent('5 av 10')
79
+ expect(bar).toHaveAttribute('aria-valuetext', '5 av 10')
80
+ expect(barContainer?.style.getPropertyValue('--pkt-progress-width')).toBe('50%')
81
+ })
82
+
83
+ test('uses the same bar width for fraction and percentage', () => {
84
+ const props = { valueCurrent: 10, valueMin: 1, valueMax: 10 }
85
+ const { container } = render(
86
+ <>
87
+ <PktProgressbar id="progress-fraction" statusType="fraction" {...props} />
88
+ <PktProgressbar id="progress-percentage" statusType="percentage" {...props} />
89
+ </>,
90
+ )
91
+ const widths = Array.from(container.querySelectorAll<HTMLElement>('.pkt-progressbar__container'), (el) =>
92
+ el.style.getPropertyValue('--pkt-progress-width'),
93
+ )
94
+ expect(widths).toEqual(['100%', '100%'])
95
+ })
96
+
72
97
  test('renders status with center placement', () => {
73
98
  const { container } = render(
74
99
  <PktProgressbar id="progress7" valueCurrent={50} statusType="percentage" statusPlacement="center" />,
@@ -2,15 +2,18 @@
2
2
 
3
3
  import classNames from 'classnames'
4
4
  import { forwardRef, HTMLAttributes, Ref, useEffect, useRef, useState } from 'react'
5
- import type {
6
- TAriaLive,
7
- TProgressbarRole,
8
- TProgressbarSkin,
9
- TProgressbarStatusPlacement,
10
- TProgressbarStatusType,
11
- TProgressbarTitlePosition,
5
+ import {
6
+ PROGRESSBAR_DEFAULTS,
7
+ type IPktProgressbarBase,
8
+ type TProgressbarRole,
9
+ type TProgressbarSkin,
10
+ type TProgressbarStatusPlacement,
11
+ type TProgressbarStatusType,
12
+ type TProgressbarTitlePosition,
12
13
  } from 'shared-types'
14
+ import { formatString } from 'shared-strings'
13
15
  import { useGeneratedId } from '../../hooks/useGeneratedId'
16
+ import { usePktStrings } from '../../hooks/usePktStrings'
14
17
 
15
18
  export type {
16
19
  TProgressbarRole,
@@ -20,21 +23,7 @@ export type {
20
23
  TProgressbarTitlePosition,
21
24
  }
22
25
 
23
- export interface IPktProgressbar extends Omit<HTMLAttributes<HTMLDivElement>, 'role' | 'title'> {
24
- ariaLabel?: string | null
25
- ariaLabelledby?: string | null
26
- ariaLive?: TAriaLive
27
- ariaValueText?: string | null
28
- id?: string
29
- role?: TProgressbarRole
30
- skin?: TProgressbarSkin
31
- statusPlacement?: TProgressbarStatusPlacement
32
- statusType?: TProgressbarStatusType
33
- title?: string | null
34
- titlePosition?: TProgressbarTitlePosition
35
- valueCurrent: number
36
- valueMax?: number
37
- valueMin?: number
26
+ export interface IPktProgressbar extends IPktProgressbarBase, Omit<HTMLAttributes<HTMLDivElement>, 'role' | 'title'> {
38
27
  ref?: Ref<HTMLDivElement>
39
28
  }
40
29
 
@@ -43,24 +32,26 @@ export const PktProgressbar = forwardRef<HTMLDivElement, IPktProgressbar>(
43
32
  {
44
33
  ariaLabel,
45
34
  ariaLabelledby,
46
- ariaLive = 'polite',
35
+ ariaLive = PROGRESSBAR_DEFAULTS.ariaLive,
47
36
  ariaValueText,
48
37
  id,
49
- role = 'progressbar',
50
- skin = 'dark-blue',
51
- statusPlacement = 'following',
52
- statusType = 'none',
38
+ role = PROGRESSBAR_DEFAULTS.role,
39
+ skin = PROGRESSBAR_DEFAULTS.skin,
40
+ statusPlacement = PROGRESSBAR_DEFAULTS.statusPlacement,
41
+ statusType = PROGRESSBAR_DEFAULTS.statusType,
53
42
  title,
54
- titlePosition = 'left',
55
- valueCurrent = 0,
56
- valueMax = 100,
57
- valueMin = 0,
43
+ titlePosition = PROGRESSBAR_DEFAULTS.titlePosition,
44
+ valueCurrent = PROGRESSBAR_DEFAULTS.valueCurrent,
45
+ valueMax = PROGRESSBAR_DEFAULTS.valueMax,
46
+ valueMin = PROGRESSBAR_DEFAULTS.valueMin,
47
+ strings,
58
48
  className,
59
49
  ...props
60
50
  },
61
51
  ref,
62
52
  ) => {
63
53
  const progressbarId = useGeneratedId(id)
54
+ const s = usePktStrings(['progressbar'], strings)
64
55
 
65
56
  const labelRef = useRef<HTMLSpanElement>(null)
66
57
  const [labelWidth, setLabelWidth] = useState(0)
@@ -71,18 +62,17 @@ export const PktProgressbar = forwardRef<HTMLDivElement, IPktProgressbar>(
71
62
  }
72
63
  }, [valueCurrent])
73
64
 
74
- const totalSteps = valueMax - valueMin
75
- const currentPercentage =
76
- statusType === 'fraction'
77
- ? Math.round((valueCurrent / totalSteps) * 100)
78
- : Math.round(((valueCurrent - valueMin) / totalSteps) * 100)
65
+ const totalSteps: number = valueMax - valueMin
66
+ const currentStep = valueCurrent - valueMin
67
+ const currentPercentage = Math.round((currentStep / totalSteps) * 100)
79
68
 
80
- const formattedTitle = `${valueCurrent} av ${totalSteps}`
69
+ const formattedTitle = formatString(s.progressbar.fraction, {
70
+ current: currentStep,
71
+ total: totalSteps,
72
+ })
81
73
 
82
74
  const computedAriaValueText =
83
- statusType === 'fraction' && !ariaValueText
84
- ? `${valueCurrent} av ${valueMax - valueMin}`
85
- : ariaValueText || undefined
75
+ statusType === 'fraction' && !ariaValueText ? formattedTitle : ariaValueText || undefined
86
76
 
87
77
  const computedAriaLabelledby = !ariaLabel ? ariaLabelledby || `${progressbarId}-title` : undefined
88
78