@oslokommune/punkt-react 19.3.1 → 19.4.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 +5 -0
- package/dist/components/consent/Consent.d.ts +6 -38
- package/dist/punkt-react.cjs +1 -1
- package/dist/punkt-react.js +552 -548
- package/dist/shared-types/consent.d.ts +69 -0
- package/dist/shared-types/index.d.ts +2 -0
- package/package.json +2 -2
- package/src/components/consent/Consent.test.tsx +6 -0
- package/src/components/consent/Consent.tsx +26 -54
|
@@ -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
|
+
};
|
|
@@ -13,6 +13,8 @@ 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';
|
|
16
18
|
export { LAYOUT_DIRECTIONS } from './layout';
|
|
17
19
|
export type { TLayout, TVerticalPosition, THorizontalPosition, TPosition, TPositionWithCenter, } from './layout';
|
|
18
20
|
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
|
+
"version": "19.4.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": "
|
|
104
|
+
"gitHead": "74057ff529f28056e96199ce2b2def5aee6ef9bf"
|
|
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
|
-
|
|
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?:
|
|
47
|
-
__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:
|
|
89
|
-
const parsed = parseJsonOrPassThrough<
|
|
90
|
-
return parsed.items.reduce<
|
|
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
|
|
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 =
|
|
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 =
|
|
120
|
-
triggerType =
|
|
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 = (
|
|
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:
|
|
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:
|
|
140
|
+
const emitConsents = (consent: ICookieConsentEvent) => {
|
|
169
141
|
const handler = onToggleRef.current
|
|
170
142
|
if (!handler) return
|
|
171
143
|
handler(
|
|
172
|
-
new CustomEvent<
|
|
144
|
+
new CustomEvent<TConsentDetailsMap>('toggle-consent', {
|
|
173
145
|
detail: toConsentDetails(consent),
|
|
174
146
|
bubbles: true,
|
|
175
147
|
cancelable: false,
|