@oslokommune/punkt-react 20.2.1 → 20.2.2

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.
@@ -1,8 +1,19 @@
1
1
  'use client'
2
2
 
3
3
  import classNames from 'classnames'
4
- import { ForwardedRef, forwardRef, useCallback, useEffect, useLayoutEffect, useMemo, useRef, useState } from 'react'
5
- import { type THeaderFooterApi, DEFAULT_HEADER_LOGO_LINK } from 'shared-types'
4
+ import {
5
+ ForwardedRef,
6
+ forwardRef,
7
+ useCallback,
8
+ useContext,
9
+ useEffect,
10
+ useLayoutEffect,
11
+ useMemo,
12
+ useRef,
13
+ useState,
14
+ } from 'react'
15
+ import { resolveOverride } from 'shared-strings'
16
+ import { type THeaderFooterApi, HEADER_GLOBAL_DEFAULTS } from 'shared-types'
6
17
  import { fetchHeaderFooterData, selectLocaleData } from 'shared-utils/header-footer'
7
18
  import { getDeepActiveElement, getFocusableElementsDeep } from 'shared-utils/focus-trap'
8
19
 
@@ -15,6 +26,7 @@ import { PktIcon } from '../icon/Icon'
15
26
  import { PktSearchInput } from '../searchinput/SearchInput'
16
27
  import { IPktHeaderGlobal } from './types'
17
28
  import { usePktStrings } from '../../hooks/usePktStrings'
29
+ import { PktStringsContext } from '../strings/PktStringsProvider'
18
30
 
19
31
  const defaultLogoPath = 'https://punkt-cdn.oslo.kommune.no/latest/logos/'
20
32
 
@@ -36,25 +48,25 @@ export const PktHeaderGlobal = forwardRef(
36
48
  'data-mode': dataMode,
37
49
  dataUrl,
38
50
  data,
39
- locale = 'nb-NO',
40
- logoLink = DEFAULT_HEADER_LOGO_LINK,
51
+ locale = HEADER_GLOBAL_DEFAULTS.locale,
52
+ logoLink = HEADER_GLOBAL_DEFAULTS.logoLink,
41
53
  logoClick,
42
54
  logoPath = typeof window !== 'undefined' ? window.pktLogoPath : defaultLogoPath,
43
- showSearch = true,
55
+ showSearch = HEADER_GLOBAL_DEFAULTS.showSearch,
44
56
  searchPlaceholder,
45
57
  onSearch,
46
- showContact = true,
58
+ showContact = HEADER_GLOBAL_DEFAULTS.showContact,
47
59
  user,
48
60
  userMenu,
49
61
  representing,
50
- canChangeRepresentation = false,
62
+ canChangeRepresentation = HEADER_GLOBAL_DEFAULTS.canChangeRepresentation,
51
63
  changeRepresentation,
52
- logOutButtonPlacement = 'none',
64
+ logOutButtonPlacement = HEADER_GLOBAL_DEFAULTS.logOutButtonPlacement,
53
65
  logOut,
54
- position = 'fixed',
55
- scrollBehavior = 'hide',
56
- mobileBreakpoint = 768,
57
- tabletBreakpoint = 1024,
66
+ position = HEADER_GLOBAL_DEFAULTS.position,
67
+ scrollBehavior = HEADER_GLOBAL_DEFAULTS.scrollBehavior,
68
+ mobileBreakpoint = HEADER_GLOBAL_DEFAULTS.mobileBreakpoint,
69
+ tabletBreakpoint = HEADER_GLOBAL_DEFAULTS.tabletBreakpoint,
58
70
  onDataLoaded,
59
71
  onDataError,
60
72
  strings,
@@ -254,10 +266,17 @@ export const PktHeaderGlobal = forwardRef(
254
266
  const showLogoutInUserMenu = logOutButtonPlacement === 'userMenu' || logOutButtonPlacement === 'both'
255
267
 
256
268
  const headerStrings = usePktStrings(['header'], strings).header
269
+ const stringsFromProvider = useContext(PktStringsContext)
270
+ // The payload's placeholder is the default, but an explicit override wins
271
+ const searchPlaceholderOverride =
272
+ searchPlaceholder ?? resolveOverride('header', 'search', stringsFromProvider, strings)
257
273
  const search = localeData?.search
258
274
  const contact = localeData?.header?.contact
259
- const menuText = localeData?.i18n?.menu || headerStrings.menu
260
- const searchText = localeData?.i18n?.search || headerStrings.search
275
+ // Same priority for button texts and labels: override, then payload, then catalogue
276
+ const headerText = (key: 'menu' | 'search', fromPayload?: string) =>
277
+ resolveOverride('header', key, stringsFromProvider, strings) ?? (fromPayload || headerStrings[key])
278
+ const menuText = headerText('menu', localeData?.i18n?.menu)
279
+ const searchText = headerText('search', localeData?.i18n?.search)
261
280
  const isMenuOpen = openMenu === 'menu'
262
281
  const isUserOpen = openMenu === 'user'
263
282
  const isSearchOpen = openMenu === 'search'
@@ -305,7 +324,7 @@ export const PktHeaderGlobal = forwardRef(
305
324
  id={searchId}
306
325
  name={search.input.name}
307
326
  aria-label={searchText}
308
- placeholder={searchPlaceholder ?? search.input.placeholder}
327
+ placeholder={searchPlaceholderOverride ?? search.input.placeholder}
309
328
  action={search.endpoint}
310
329
  method="get"
311
330
  onSearch={onSearch}
@@ -441,7 +460,7 @@ export const PktHeaderGlobal = forwardRef(
441
460
  id={searchId}
442
461
  name={search.input.name}
443
462
  aria-label={searchText}
444
- placeholder={searchPlaceholder ?? search.input.placeholder}
463
+ placeholder={searchPlaceholderOverride ?? search.input.placeholder}
445
464
  action={search.endpoint}
446
465
  method="get"
447
466
  onSearch={onSearch}
@@ -23,7 +23,7 @@ import { PktHeaderUserMenu } from '../headerUserMenu/UserMenu'
23
23
  import { PktIcon } from '../icon/Icon'
24
24
  import { PktLink } from '../link/Link'
25
25
  import { PktTextinput } from '../textinput/Textinput'
26
- import { DEFAULT_HEADER_LOGO_LINK } from 'shared-types'
26
+ import { HEADER_SERVICE_DEFAULTS } from 'shared-types'
27
27
 
28
28
  import { IPktHeader, Representing, THeaderMenu, User, UserMenuFooterItem, UserMenuItem } from './types'
29
29
  import { usePktStrings } from '../../hooks/usePktStrings'
@@ -46,16 +46,16 @@ if (typeof window !== 'undefined') {
46
46
  export const PktHeaderService = forwardRef(
47
47
  (
48
48
  {
49
- mobileBreakpoint = 768,
50
- tabletBreakpoint = 1280,
49
+ mobileBreakpoint = HEADER_SERVICE_DEFAULTS.mobileBreakpoint,
50
+ tabletBreakpoint = HEADER_SERVICE_DEFAULTS.tabletBreakpoint,
51
51
  children,
52
52
  className,
53
- compact = false,
53
+ compact = HEADER_SERVICE_DEFAULTS.compact,
54
54
  'data-mode': dataMode,
55
- logOutButtonPlacement = 'none',
55
+ logOutButtonPlacement = HEADER_SERVICE_DEFAULTS.logOutButtonPlacement,
56
56
  logOut,
57
57
  openedMenu,
58
- showSearch = false,
58
+ showSearch = HEADER_SERVICE_DEFAULTS.showSearch,
59
59
  onSearch,
60
60
  onSearchChange,
61
61
  searchValue,
@@ -68,15 +68,15 @@ export const PktHeaderService = forwardRef(
68
68
  userMenu,
69
69
  userMenuFooter,
70
70
  userOptions,
71
- canChangeRepresentation = false,
71
+ canChangeRepresentation = HEADER_SERVICE_DEFAULTS.canChangeRepresentation,
72
72
  changeRepresentation,
73
- hideLogo = false,
74
- logoLink = DEFAULT_HEADER_LOGO_LINK,
73
+ hideLogo = HEADER_SERVICE_DEFAULTS.hideLogo,
74
+ logoLink = HEADER_SERVICE_DEFAULTS.logoLink,
75
75
  logoClick,
76
76
  logoPath = typeof window !== 'undefined' ? window.pktLogoPath : defaultLogoPath,
77
- position = 'fixed',
78
- scrollBehavior = 'hide',
79
- slotMenuVariant = 'icon-only',
77
+ position = HEADER_SERVICE_DEFAULTS.position,
78
+ scrollBehavior = HEADER_SERVICE_DEFAULTS.scrollBehavior,
79
+ slotMenuVariant = HEADER_SERVICE_DEFAULTS.slotMenuVariant,
80
80
  slotMenuText,
81
81
  strings,
82
82
  }: IPktHeaderService,
@@ -3,143 +3,83 @@ import type {
3
3
  THeaderMenu,
4
4
  THeaderPosition,
5
5
  THeaderScrollBehavior,
6
+ THeaderType,
6
7
  TLogOutButtonPlacement,
7
8
  TSlotMenuVariant,
8
9
  THeaderFooterApi,
9
10
  THeaderMenuLocale,
10
- IPktHeaderGlobal as ISharedPktHeaderGlobal,
11
+ User,
12
+ Representing,
13
+ UserMenuItem,
14
+ UserMenuFooterItem,
15
+ IPktHeaderServiceBase,
16
+ IPktHeaderGlobalBase,
11
17
  } from 'shared-types'
12
- import type { IPktStringsProps } from 'shared-strings'
13
18
 
14
19
  export type {
15
20
  THeaderMenu,
16
21
  THeaderPosition,
17
22
  THeaderScrollBehavior,
23
+ THeaderType,
18
24
  TLogOutButtonPlacement,
19
25
  TSlotMenuVariant,
20
26
  THeaderFooterApi,
21
27
  THeaderMenuLocale,
28
+ User,
29
+ Representing,
30
+ UserMenuItem,
31
+ UserMenuFooterItem,
22
32
  }
23
33
 
24
- /** Which header variant `<PktHeader>` renders. */
25
- export type THeaderType = 'service' | 'global'
26
-
27
- /** User object for header components */
28
- export interface User {
29
- name: string
30
- /** @deprecated shortname is deprecated and will be removed in a future version */
31
- shortname?: string
32
- lastLoggedIn?: string | Date
33
- }
34
-
35
- /** Representing object for header components */
36
- export interface Representing {
37
- name: string
38
- /** @deprecated shortname is deprecated and will be removed in a future version */
39
- shortname?: string
40
- orgNumber?: string | number
41
- }
42
-
43
- /** User menu item for header components */
44
- export interface UserMenuItem {
45
- iconName?: string
46
- title: string
47
- target: string | (() => void)
48
- }
49
-
50
- /** @deprecated Use UserMenuItem instead */
51
- export interface UserMenuFooterItem {
52
- title: string
53
- target: string | (() => void)
54
- }
55
-
56
- export interface IPktHeader extends IPktStringsProps<'header'> {
57
- children?: ReactNode
34
+ /**
35
+ * React-specific props both header variants share, on top of
36
+ * {@link IPktHeaderCommonBase} (in shared-types).
37
+ */
38
+ interface IPktHeaderReactCommon {
58
39
  /** Additional CSS class name(s) to apply to the header */
59
40
  className?: string
60
41
  /** Set dark mode on the header */
61
42
  'data-mode'?: 'dark'
62
- /** Hide the Oslo logo. Default: false (logo is shown) */
63
- hideLogo?: boolean
64
- /** Link URL for the logo. Pass an empty string to render the logo without a link */
65
- logoLink?: string
66
- /** Override CDN path for the logo */
67
- logoPath?: string
68
- /** Callback when logo is clicked */
43
+ /** Callback when the logo is clicked. Call `event.preventDefault()` to cancel navigation */
69
44
  logoClick?: (event: MouseEvent) => void
70
- /** Name of the service displayed in the header */
71
- serviceName?: string
72
- /** Link URL for the service name */
73
- serviceLink?: string
45
+ /** Callback when the user clicks "Endre organisasjon" */
46
+ changeRepresentation?: () => void
47
+ /** Callback when the user clicks logout. The logout button is only shown when this is set */
48
+ logOut?: () => void
49
+ }
50
+
51
+ /**
52
+ * Props for the service Header component.
53
+ *
54
+ * Extends the framework-agnostic {@link IPktHeaderServiceBase} base
55
+ * (in shared-types) with React-specific callbacks, DOM props and
56
+ * deprecated props.
57
+ */
58
+ export interface IPktHeader extends IPktHeaderServiceBase, IPktHeaderReactCommon {
59
+ /** Which header variant to render. Use `IPktHeaderGlobal` for the global header. Narrowed to `'service'` in the next major */
60
+ type?: THeaderType
61
+ children?: ReactNode
74
62
  /** Callback when service name is clicked */
75
63
  serviceClick?: (event: MouseEvent) => void
76
- /** Use compact header height */
77
- compact?: boolean
78
- /** User object with name, shortname, and lastLoggedIn */
79
- user?: User
80
- /** User menu items displayed in the dropdown */
81
- userMenu?: UserMenuItem[]
82
- /** Representing object with name, shortname, and orgNumber */
83
- representing?: Representing
84
- /** Whether the user can change representation. Shows "Endre organisasjon" button */
85
- canChangeRepresentation?: boolean
86
- /** Callback when user clicks "Endre organisasjon" */
87
- changeRepresentation?: () => void
88
64
  /** @deprecated Use userMenu instead. Will show console.warn if used. */
89
65
  userMenuFooter?: UserMenuFooterItem[]
90
66
  /** @deprecated userOptions is no longer available. Use userMenu instead. */
91
67
  userOptions?: UserMenuItem[]
92
- /** Where to show the logout button: 'userMenu', 'header', 'both', or 'none' */
93
- logOutButtonPlacement?: TLogOutButtonPlacement
94
- /** Callback when user clicks logout */
95
- logOut?: () => void
96
- /** Show search input in the header */
97
- showSearch?: boolean
98
68
  /** Callback when user submits search (presses Enter) */
99
69
  onSearch?: (query: string) => void
100
70
  /** Callback when search input value changes */
101
71
  onSearchChange?: (query: string) => void
102
- /** Controlled value for the search input */
103
- searchValue?: string
104
- /** Placeholder text for the search input. Default: "Søk" */
105
- searchPlaceholder?: string
106
- /** Custom breakpoint for responsive behavior (grid layout) in pixels. Default: 1024 */
107
- mobileBreakpoint?: number
108
- /** Custom breakpoint for tablet responsive behavior (interaction pattern) in pixels. Default: 1280 */
109
- tabletBreakpoint?: number
110
- /** Which menu is initially open */
111
- openedMenu?: THeaderMenu
112
- /** Header position. 'fixed' fixes to top of viewport, 'relative' follows document flow. Default: 'fixed' */
113
- position?: THeaderPosition
114
- /** Scroll behavior. 'hide' hides header on scroll down, 'none' keeps it visible. Default: 'hide' */
115
- scrollBehavior?: THeaderScrollBehavior
116
- /** Variant for the slot menu button in tablet/mobile mode. Default: 'icon-only' */
117
- slotMenuVariant?: TSlotMenuVariant
118
- /** Text for the slot menu button in tablet/mobile mode. Default: 'Meny' */
119
- slotMenuText?: string
120
- /** Which header variant to render. Default: 'service' */
121
- type?: THeaderType
122
72
  }
123
73
 
124
74
  /**
125
75
  * Props for the global (kommune-wide) Header component.
126
76
  *
127
- * Extends the framework-agnostic {@link ISharedPktHeaderGlobal} base
77
+ * Extends the framework-agnostic {@link IPktHeaderGlobalBase} base
128
78
  * (in shared-types) with React-specific callbacks and DOM props.
129
79
  */
130
- export interface IPktHeaderGlobal extends ISharedPktHeaderGlobal, IPktStringsProps<'header'> {
131
- /** Additional CSS class name(s) to apply to the header */
132
- className?: string
133
- /** Set dark mode on the header */
134
- 'data-mode'?: 'dark'
135
- /** Callback when the logo is clicked */
136
- logoClick?: (event: MouseEvent) => void
80
+ export interface IPktHeaderGlobal extends IPktHeaderGlobalBase, IPktHeaderReactCommon {
137
81
  /** Callback when the user submits search. When set, navigation is left to the consumer */
138
82
  onSearch?: (query: string) => void
139
- /** Callback when the user clicks "Endre organisasjon" */
140
- changeRepresentation?: () => void
141
- /** Callback when the user clicks logout */
142
- logOut?: () => void
143
83
  /** Fired when the payload has been fetched (or `data` was supplied) */
144
84
  onDataLoaded?: (data: THeaderFooterApi) => void
145
85
  /** Fired when the fetch fails */
@@ -101,6 +101,20 @@ describe('PktHeaderMenu rendering', () => {
101
101
  expect(container.querySelector('nav')?.getAttribute('aria-label')).toBe('Oslo kommune hovedmeny')
102
102
  })
103
103
 
104
+ test('strings override the nav aria-label from the payload', () => {
105
+ const { container } = render(
106
+ <PktHeaderMenu data={LOCAL_DATA} open strings={{ header: { navAriaLabel: 'Egen meny' } }} />,
107
+ )
108
+ expect(container.querySelector('nav')?.getAttribute('aria-label')).toBe('Egen meny')
109
+ })
110
+
111
+ test('falls back to the string catalogue when the payload has no nav aria-label', () => {
112
+ const nb = LOCAL_DATA['nb-NO']!
113
+ const data = { 'nb-NO': { ...nb, i18n: { ...nb.i18n, navAriaLabel: '' } } } as THeaderFooterApi
114
+ const { container } = render(<PktHeaderMenu data={data} open />)
115
+ expect(container.querySelector('nav')?.getAttribute('aria-label')).toBe('Hovedmeny')
116
+ })
117
+
104
118
  test('switches content when locale changes', () => {
105
119
  const { container, rerender } = render(<PktHeaderMenu data={LOCAL_DATA} locale="nb-NO" mobileBreakpoint={0} open />)
106
120
  expect(container.querySelector('.pkt-header-menu__services-title')?.textContent).toContain('Tjenester og tilbud')
@@ -5,6 +5,7 @@ import {
5
5
  type MutableRefObject,
6
6
  type Ref,
7
7
  useCallback,
8
+ useContext,
8
9
  useEffect,
9
10
  useMemo,
10
11
  useRef,
@@ -20,7 +21,7 @@ import {
20
21
  type THeaderFooterApi,
21
22
  type THeaderMenuLocale,
22
23
  } from 'shared-types'
23
- import type { IPktStringsProps } from 'shared-strings'
24
+ import { resolveOverride, type IPktStringsProps } from 'shared-strings'
24
25
  import { deriveSocialIcon, fetchHeaderFooterData, mapOdsIcon, selectLocaleData } from 'shared-utils/header-footer'
25
26
 
26
27
  import { useElementWidth } from '../../hooks/useElementWidth'
@@ -28,6 +29,7 @@ import { usePktStrings } from '../../hooks/usePktStrings'
28
29
  import { PktAccordion } from '../accordion/Accordion'
29
30
  import { PktAccordionItem } from '../accordion/AccordionItem'
30
31
  import { PktIcon } from '../icon/Icon'
32
+ import { PktStringsContext } from '../strings/PktStringsProvider'
31
33
 
32
34
  export type {
33
35
  THeaderFooterApi,
@@ -76,6 +78,7 @@ export const PktHeaderMenu = ({
76
78
  ...rest
77
79
  }: IPktHeaderMenu) => {
78
80
  const headerStrings = usePktStrings(['header'], strings).header
81
+ const stringsFromProvider = useContext(PktStringsContext)
79
82
  const [fetchState, setFetchState] = useState<LoadState>('loading')
80
83
  const [fetchedData, setFetchedData] = useState<THeaderFooterApi | undefined>(undefined)
81
84
  const loadState: LoadState = data ? 'ready' : fetchState
@@ -148,7 +151,9 @@ export const PktHeaderMenu = ({
148
151
  }
149
152
 
150
153
  const { megamenu, i18n } = localeData
151
- const navAriaLabel = i18n?.navAriaLabel || 'Hovedmeny'
154
+ const navAriaLabel =
155
+ resolveOverride('header', 'navAriaLabel', stringsFromProvider, strings) ??
156
+ (i18n?.navAriaLabel || headerStrings.navAriaLabel)
152
157
 
153
158
  return (
154
159
  <nav {...rest} ref={setRootRef} className={hostClasses} aria-label={navAriaLabel}>
@@ -12,7 +12,7 @@ export type { IPktDatepicker } from './datepicker/Datepicker'
12
12
  export type { IPktFileUpload } from './fileupload/FileUpload'
13
13
  export type { IPktFooter } from './footer/Footer'
14
14
  export type { IPktFooterSimple } from './footerSimple/FooterSimple'
15
- export type { IPktHeader } from './header/Header'
15
+ export type { IPktHeader, IPktHeaderGlobal, IPktHeaderProps } from './header/Header'
16
16
  export type { IPktHeaderMenu } from './header-menu/HeaderMenu'
17
17
  export type { IPktHeaderService } from './header/HeaderService'
18
18
  export type { IPktInputWrapper } from './inputwrapper/InputWrapper'