@oslokommune/punkt-react 19.3.1 → 19.5.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.
@@ -0,0 +1,69 @@
1
+ /**
2
+ * consent.ts
3
+ *
4
+ * Type definitions for pkt-consent.
5
+ * Used by React, Elements, and typed component specs.
6
+ */
7
+ /** Consent trigger variants — shared runtime source for types, specs, and Preview. */
8
+ export declare const CONSENT_TRIGGER_TYPES: readonly ['button', 'link', 'footerlink', 'icon'];
9
+ export type TPktConsentTriggerType = (typeof CONSENT_TRIGGER_TYPES)[number];
10
+ /**
11
+ * Trigger variants offered in Preview/docs. `footerlink` is left out because it is
12
+ * styled for the footer, where PktFooter and PktFooterSimple set it themselves.
13
+ */
14
+ export declare const CONSENT_TRIGGER_TYPE_OPTIONS: readonly ["button", "link", "icon"];
15
+ /** One cookie category in the consent cookie. */
16
+ export interface IConsentItem {
17
+ name: string;
18
+ consent: boolean;
19
+ }
20
+ /** Parsed consent cookie. */
21
+ export interface IConsentValue {
22
+ items: IConsentItem[];
23
+ }
24
+ /** Payload from the cookie banner's `CookieManager.setCookie` event. */
25
+ export interface ICookieConsentEvent {
26
+ value: string | IConsentValue;
27
+ }
28
+ /** Detail of the `toggle-consent` event: cookie category name → consent given. */
29
+ export type TConsentDetailsMap = Record<string, boolean>;
30
+ export type TCookieEventHandler = (consent: ICookieConsentEvent) => void;
31
+ /** `window.__cookieEvents` exposed by the cookie banner script. */
32
+ export interface ICookieEvents {
33
+ on(eventName: string, handler: TCookieEventHandler): void;
34
+ off(eventName: string, handler: TCookieEventHandler): void;
35
+ }
36
+ /** `window.cookieBanner` exposed by the cookie banner script. */
37
+ export interface ICookieBannerApi {
38
+ cookieConsent: {
39
+ validateConsentCookie(): Promise<boolean>;
40
+ getConsentCookie(): string | IConsentValue;
41
+ };
42
+ openCookieModal(): void;
43
+ }
44
+ /** Consent props shared by React and Elements. */
45
+ export interface IPktConsentBase {
46
+ devMode?: boolean;
47
+ cookieDomain?: string | null;
48
+ cookieSecure?: string | null;
49
+ cookieExpiryDays?: string | null;
50
+ hotjarId?: string | null;
51
+ googleAnalyticsId?: string | null;
52
+ i18nLanguage?: string;
53
+ triggerType?: TPktConsentTriggerType | null;
54
+ triggerText?: string | null;
55
+ }
56
+ /**
57
+ * Documented Consent props (subset of `IPktConsentBase`).
58
+ * Used for typed component specs in documentation.
59
+ */
60
+ export type IConsentSpecProps = Pick<IPktConsentBase, 'devMode' | 'cookieDomain' | 'cookieSecure' | 'cookieExpiryDays' | 'hotjarId' | 'googleAnalyticsId' | 'i18nLanguage' | 'triggerType' | 'triggerText'>;
61
+ /**
62
+ * Default values for pkt-consent. Single source of truth for both frameworks.
63
+ * `triggerText` is absent — it falls back to the `consentStrings` text for `i18nLanguage`.
64
+ */
65
+ export declare const CONSENT_DEFAULTS: {
66
+ readonly devMode: false;
67
+ readonly i18nLanguage: 'nb';
68
+ readonly triggerType: 'button';
69
+ };
@@ -0,0 +1,39 @@
1
+ /**
2
+ * icon.ts
3
+ *
4
+ * Type definitions for pkt-icon.
5
+ * Used by React, Elements, and typed component specs.
6
+ */
7
+ import type { Booleanish } from './booleanish';
8
+ /** Default CDN path for icons. Overridden globally by `window.pktIconPath`. */
9
+ export declare const ICON_DEFAULT_PATH = "https://punkt-cdn.oslo.kommune.no/latest/icons/";
10
+ /** Icon size modifier classes — shared runtime source for types, specs, and Preview. */
11
+ export declare const ICON_SIZE_CLASSES: readonly ['pkt-icon--small', 'pkt-icon--medium', 'pkt-icon--large'];
12
+ export type TIconSizeClass = (typeof ICON_SIZE_CLASSES)[number];
13
+ /**
14
+ * Icon props shared by React and Elements.
15
+ *
16
+ * @remarks Elements narrows `name` to `PktIconName` and takes `aria-hidden`/`aria-label`
17
+ * as string attributes.
18
+ */
19
+ export interface IPktIconBase {
20
+ name?: string;
21
+ path?: string;
22
+ ariaHidden?: Booleanish;
23
+ ariaLabel?: string;
24
+ }
25
+ /**
26
+ * Documented Icon props. `className` is not a component prop but the size class
27
+ * (`className` in React, `class` in Elements), documented so Preview can offer it.
28
+ */
29
+ export type IIconSpecProps = Pick<IPktIconBase, 'name' | 'path' | 'ariaHidden' | 'ariaLabel'> & {
30
+ className?: TIconSizeClass;
31
+ };
32
+ /**
33
+ * Default values for pkt-icon. Single source of truth for both frameworks.
34
+ * `path` is only the fallback — `window.pktIconPath` wins when set.
35
+ */
36
+ export declare const ICON_DEFAULTS: {
37
+ readonly name: '';
38
+ readonly path: "https://punkt-cdn.oslo.kommune.no/latest/icons/";
39
+ };
@@ -13,6 +13,10 @@ export { CARD_SKINS, CARD_PADDINGS, CARD_IMAGE_SHAPES, CARD_IMAGE_ASPECTS, CARD_
13
13
  export type { TCardSkin, TCardPadding, TCardImageShape, TCardImageAspect, TCardTagPosition, ICardTag, ICardImage, IPktCardBase, ICardSpecProps, } from './card';
14
14
  export { CHECKBOX_LABEL_POSITIONS, CHECKBOX_DEFAULTS } from './checkbox';
15
15
  export type { TCheckboxLabelPosition, IPktCheckboxBase, ICheckboxSpecProps } from './checkbox';
16
+ export { CONSENT_TRIGGER_TYPES, CONSENT_TRIGGER_TYPE_OPTIONS, CONSENT_DEFAULTS } from './consent';
17
+ export type { TPktConsentTriggerType, IConsentItem, IConsentValue, ICookieConsentEvent, TConsentDetailsMap, TCookieEventHandler, ICookieEvents, ICookieBannerApi, IPktConsentBase, IConsentSpecProps, } from './consent';
18
+ export { ICON_DEFAULT_PATH, ICON_SIZE_CLASSES, ICON_DEFAULTS } from './icon';
19
+ export type { TIconSizeClass, IPktIconBase, IIconSpecProps } from './icon';
16
20
  export { LAYOUT_DIRECTIONS } from './layout';
17
21
  export type { TLayout, TVerticalPosition, THorizontalPosition, TPosition, TPositionWithCenter, } from './layout';
18
22
  export type { THTMLButtonType, TSelectionMode } from './form';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@oslokommune/punkt-react",
3
- "version": "19.3.1",
3
+ "version": "19.5.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",
@@ -101,5 +101,5 @@
101
101
  "url": "https://github.com/oslokommune/punkt/issues"
102
102
  },
103
103
  "license": "MIT",
104
- "gitHead": "40680531212b05400461eaf3ce7eddc3f14edd85"
104
+ "gitHead": "b3cd6677098953e95888488fa523fcfed4dc714b"
105
105
  }
@@ -74,6 +74,12 @@ describe('PktConsent', () => {
74
74
  expect(screen.getByRole('button', { name: 'Go to settings' })).toBeInTheDocument()
75
75
  })
76
76
 
77
+ test.each(['sv', 'constructor'])('falls back to bokmål when i18nLanguage is "%s"', (language) => {
78
+ render(<PktConsent i18nLanguage={language} />)
79
+
80
+ expect(screen.getByRole('button', { name: 'Innstillinger for informasjonskapsler' })).toBeInTheDocument()
81
+ })
82
+
77
83
  test('renders a custom triggerText when provided', () => {
78
84
  render(<PktConsent triggerText="Endre samtykke" />)
79
85
 
@@ -1,39 +1,22 @@
1
1
  'use client'
2
2
 
3
3
  import { forwardRef, HTMLAttributes, MouseEvent as ReactMouseEvent, Ref, useEffect, useRef } from 'react'
4
+ import {
5
+ CONSENT_DEFAULTS,
6
+ type IConsentValue,
7
+ type ICookieBannerApi,
8
+ type ICookieConsentEvent,
9
+ type ICookieEvents,
10
+ type IPktConsentBase,
11
+ type TConsentDetailsMap,
12
+ type TCookieEventHandler,
13
+ type TPktConsentTriggerType,
14
+ } from 'shared-types'
4
15
 
5
16
  import { PktButton } from '../button/Button'
6
17
  import { consentStrings } from './strings'
7
18
 
8
- interface ConsentItem {
9
- name: string
10
- consent: boolean
11
- }
12
-
13
- interface ConsentValue {
14
- items: ConsentItem[]
15
- }
16
-
17
- interface CookieConsentEvent {
18
- value: string | ConsentValue
19
- }
20
-
21
- type ConsentDetailsMap = Record<string, boolean>
22
-
23
- type CookieEventHandler = (consent: CookieConsentEvent) => void
24
-
25
- interface CookieEvents {
26
- on(eventName: string, handler: CookieEventHandler): void
27
- off(eventName: string, handler: CookieEventHandler): void
28
- }
29
-
30
- interface CookieBannerApi {
31
- cookieConsent: {
32
- validateConsentCookie(): Promise<boolean>
33
- getConsentCookie(): string | ConsentValue
34
- }
35
- openCookieModal(): void
36
- }
19
+ export type { TPktConsentTriggerType }
37
20
 
38
21
  declare global {
39
22
  interface Window {
@@ -43,8 +26,8 @@ declare global {
43
26
  cookieBanner_cookieExpiryDays?: string | null
44
27
  cookieBanner_googleAnalyticsId?: string | null
45
28
  cookieBanner_hotjarId?: string | null
46
- cookieBanner?: CookieBannerApi
47
- __cookieEvents?: CookieEvents
29
+ cookieBanner?: ICookieBannerApi
30
+ __cookieEvents?: ICookieEvents
48
31
  }
49
32
  }
50
33
 
@@ -85,39 +68,28 @@ const parseJsonOrPassThrough = <T,>(value: T | string): T => {
85
68
  }
86
69
  }
87
70
 
88
- const toConsentDetails = (consent: CookieConsentEvent): ConsentDetailsMap => {
89
- const parsed = parseJsonOrPassThrough<ConsentValue>(consent.value)
90
- return parsed.items.reduce<ConsentDetailsMap>((acc, item) => {
71
+ const toConsentDetails = (consent: ICookieConsentEvent): TConsentDetailsMap => {
72
+ const parsed = parseJsonOrPassThrough<IConsentValue>(consent.value)
73
+ return parsed.items.reduce<TConsentDetailsMap>((acc, item) => {
91
74
  acc[item.name] = item.consent
92
75
  return acc
93
76
  }, {})
94
77
  }
95
78
 
96
- export type TPktConsentTriggerType = 'button' | 'link' | 'footerlink' | 'icon'
97
-
98
- export interface IPktConsent extends HTMLAttributes<HTMLElement> {
99
- devMode?: boolean
100
- cookieDomain?: string | null
101
- cookieSecure?: string | null
102
- cookieExpiryDays?: string | null
103
- hotjarId?: string | null
104
- googleAnalyticsId?: string | null
105
- i18nLanguage?: string
106
- triggerType?: TPktConsentTriggerType | null
107
- triggerText?: string | null
108
- onToggleConsent?: (e: CustomEvent<ConsentDetailsMap>) => void
79
+ export interface IPktConsent extends IPktConsentBase, HTMLAttributes<HTMLElement> {
80
+ onToggleConsent?: (e: CustomEvent<TConsentDetailsMap>) => void
109
81
  }
110
82
 
111
83
  export const PktConsent = forwardRef<HTMLElement, IPktConsent>(function PktConsent(
112
84
  {
113
- devMode = false,
85
+ devMode = CONSENT_DEFAULTS.devMode,
114
86
  cookieDomain = null,
115
87
  cookieSecure = null,
116
88
  cookieExpiryDays = null,
117
89
  hotjarId = null,
118
90
  googleAnalyticsId = null,
119
- i18nLanguage = 'nb',
120
- triggerType = 'button',
91
+ i18nLanguage = CONSENT_DEFAULTS.i18nLanguage,
92
+ triggerType = CONSENT_DEFAULTS.triggerType,
121
93
  triggerText,
122
94
  onToggleConsent,
123
95
  ...rest
@@ -146,12 +118,12 @@ export const PktConsent = forwardRef<HTMLElement, IPktConsent>(function PktConse
146
118
 
147
119
  const triggerInitRef = useRef<() => void>(() => {})
148
120
 
149
- const language = (i18nLanguage in consentStrings.i18n ? i18nLanguage : 'nb') as keyof typeof consentStrings.i18n
121
+ const language = (Object.hasOwn(consentStrings.i18n, i18nLanguage) ? i18nLanguage : 'nb') as keyof typeof consentStrings.i18n
150
122
  const resolvedTriggerText = triggerText ?? consentStrings.i18n[language].contentPresentation.buttons.settings
151
123
 
152
124
  useEffect(() => {
153
125
  let cancelled = false
154
- let cookieEventHandler: CookieEventHandler | undefined
126
+ let cookieEventHandler: TCookieEventHandler | undefined
155
127
  let emitTimeout: number | undefined
156
128
 
157
129
  const config = configRef.current
@@ -165,11 +137,11 @@ export const PktConsent = forwardRef<HTMLElement, IPktConsent>(function PktConse
165
137
  if (config.devMode) window.cookieBanner_devMode = config.devMode
166
138
  /* eslint-enable camelcase */
167
139
 
168
- const emitConsents = (consent: CookieConsentEvent) => {
140
+ const emitConsents = (consent: ICookieConsentEvent) => {
169
141
  const handler = onToggleRef.current
170
142
  if (!handler) return
171
143
  handler(
172
- new CustomEvent<ConsentDetailsMap>('toggle-consent', {
144
+ new CustomEvent<TConsentDetailsMap>('toggle-consent', {
173
145
  detail: toConsentDetails(consent),
174
146
  bubbles: true,
175
147
  cancelable: false,
@@ -2,13 +2,12 @@
2
2
 
3
3
  import classNames from 'classnames'
4
4
  import { forwardRef, HTMLAttributes, MutableRefObject, useEffect, useLayoutEffect, useRef } from 'react'
5
- import type { Booleanish } from 'shared-types'
6
-
7
- const defaultPath = 'https://punkt-cdn.oslo.kommune.no/latest/icons/'
5
+ import type { PktIconName } from '@oslokommune/punkt-assets/dist/icons/icon'
6
+ import { ICON_DEFAULTS, type Booleanish, type IPktIconBase } from 'shared-types'
8
7
 
9
8
  if (typeof window !== 'undefined') {
10
9
  window.pktFetch = window.pktFetch === undefined ? (fetch as unknown as typeof window.pktFetch) : window.pktFetch
11
- window.pktIconPath = window.pktIconPath || defaultPath
10
+ window.pktIconPath = window.pktIconPath || ICON_DEFAULTS.path
12
11
  }
13
12
 
14
13
  const errorSvg = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 32 32"></svg>'
@@ -72,11 +71,8 @@ const downloadIconOrGetFromCache = async (name: string, path: string | undefined
72
71
  return errorSvg
73
72
  }
74
73
 
75
- interface IPktIcon extends HTMLAttributes<HTMLElement> {
76
- name?: string
77
- path?: string
78
- ariaHidden?: Booleanish
79
- ariaLabel?: string
74
+ interface IPktIcon extends Omit<IPktIconBase, 'name'>, HTMLAttributes<HTMLElement> {
75
+ name?: PktIconName
80
76
  }
81
77
 
82
78
  const toBoolean = (v: Booleanish | undefined): boolean | undefined => {
@@ -86,7 +82,7 @@ const toBoolean = (v: Booleanish | undefined): boolean | undefined => {
86
82
 
87
83
  export const PktIcon = forwardRef<HTMLSpanElement, IPktIcon>(function PktIcon(
88
84
  {
89
- name = '',
85
+ name = ICON_DEFAULTS.name,
90
86
  path,
91
87
  className,
92
88
  ariaHidden,
@@ -97,7 +93,7 @@ export const PktIcon = forwardRef<HTMLSpanElement, IPktIcon>(function PktIcon(
97
93
  },
98
94
  forwardedRef,
99
95
  ) {
100
- const resolvedPath = path ?? (typeof window !== 'undefined' ? window.pktIconPath : defaultPath)
96
+ const resolvedPath = path ?? (typeof window !== 'undefined' ? window.pktIconPath : ICON_DEFAULTS.path)
101
97
  const elementRef = useRef<HTMLSpanElement | null>(null)
102
98
 
103
99
  const explicitAriaHidden = toBoolean(ariaHidden) ?? toBoolean(ariaHiddenAttr)